Correct bucket behavior

This commit is contained in:
NO11 2022-02-17 14:36:48 +00:00
parent ad99746d99
commit 4707cd4526
1 changed files with 168 additions and 137 deletions

View File

@ -33,8 +33,10 @@ if mod_mcl_core then
}) })
end end
mcl_buckets = {} mcl_buckets = {
mcl_buckets.liquids = {} liquids = {},
buckets = {},
}
-- Sound helper functions for placing and taking liquids -- Sound helper functions for placing and taking liquids
local function sound_place(itemname, pos) local function sound_place(itemname, pos)
@ -147,33 +149,7 @@ local function get_bucket_drop(itemstack, user, take_bucket)
end end
end end
function mcl_buckets.register_liquid(def) local function on_place_bucket(itemstack, user, pointed_thing, def)
for _,source in ipairs(def.source_take) do
mcl_buckets.liquids[source] = {
source_place = def.source_place,
source_take = source,
on_take = def.on_take,
bucketname = def.bucketname,
}
pointable_sources[source] = true
if type(def.source_place) == "string" then
mcl_buckets.liquids[def.source_place] = mcl_buckets.liquids[source]
end
end
if def.bucketname == nil or def.bucketname == "" then
error(string.format("[mcl_bucket] Invalid itemname then registering [%s]!", def.name))
end
minetest.register_craftitem(def.bucketname, {
description = def.name,
_doc_items_longdesc = def.longdesc,
_doc_items_usagehelp = def.usagehelp,
_tt_help = def.tt_help,
inventory_image = def.inventory_image,
stack_max = 1,
groups = def.groups,
on_place = function(itemstack, user, pointed_thing)
-- Must be pointing to node -- Must be pointing to node
if pointed_thing.type ~= "node" then if pointed_thing.type ~= "node" then
return return
@ -235,31 +211,10 @@ function mcl_buckets.register_liquid(def)
else else
return itemstack return itemstack
end end
end,
_on_dispense = function(stack, pos, droppos, dropnode, dropdir)
local buildable = minetest.registered_nodes[dropnode.name].buildable_to or dropnode.name == "mcl_portals:portal"
if not buildable then return stack end
local result, take_bucket = get_extra_check(def.extra_check, droppos, nil)
if result then -- Fail placement of liquid if result is false
place_liquid(droppos, get_node_place(def.source_place, droppos))
end
if take_bucket then
stack:set_name("mcl_buckets:bucket_empty")
end
return stack
end,
})
end end
minetest.register_craftitem("mcl_buckets:bucket_empty", {
description = S("Empty Bucket"), local function on_place_bucket_empty(itemstack, user, pointed_thing)
_doc_items_longdesc = S("A bucket can be used to collect and release liquids."),
_doc_items_usagehelp = S("Punch a liquid source to collect it. You can then use the filled bucket to place the liquid somewhere else."),
_tt_help = S("Collects liquids"),
--liquids_pointable = true,
inventory_image = "bucket.png",
stack_max = 16,
on_place = function(itemstack, user, pointed_thing)
-- Must be pointing to node -- Must be pointing to node
if pointed_thing.type ~= "node" then if pointed_thing.type ~= "node" then
return itemstack return itemstack
@ -330,7 +285,83 @@ minetest.register_craftitem("mcl_buckets:bucket_empty", {
end end
end end
return itemstack return itemstack
end
controls.register_on_press(function(player, key)
if key ~= "RMB" then
return
end
local wielded_item = player:get_wielded_item()
local itemname = wielded_item:get_name()
local pointed_thing = mcl_util.get_pointed_thing(player)
if not pointed_thing then
return
end
local def = mcl_buckets.buckets[itemname]
if itemname == "mcl_buckets:bucket_empty" then
wielded_item = on_place_bucket_empty(wielded_item, player, pointed_thing)
elseif def then
wielded_item = on_place_bucket(wielded_item, player, pointed_thing, def)
end
player:set_wielded_item(wielded_item)
end)
function mcl_buckets.register_liquid(def)
for _,source in ipairs(def.source_take) do
mcl_buckets.liquids[source] = {
source_place = def.source_place,
source_take = source,
on_take = def.on_take,
bucketname = def.bucketname,
}
pointable_sources[source] = true
if type(def.source_place) == "string" then
mcl_buckets.liquids[def.source_place] = mcl_buckets.liquids[source]
end
end
mcl_buckets.buckets[def.bucketname] = def
if def.bucketname == nil or def.bucketname == "" then
error(string.format("[mcl_bucket] Invalid itemname then registering [%s]!", def.name))
end
minetest.register_craftitem(def.bucketname, {
description = def.name,
_doc_items_longdesc = def.longdesc,
_doc_items_usagehelp = def.usagehelp,
_tt_help = def.tt_help,
inventory_image = def.inventory_image,
stack_max = 1,
groups = def.groups,
_on_dispense = function(stack, pos, droppos, dropnode, dropdir)
local buildable = minetest.registered_nodes[dropnode.name].buildable_to or dropnode.name == "mcl_portals:portal"
if not buildable then return stack end
local result, take_bucket = get_extra_check(def.extra_check, droppos, nil)
if result then -- Fail placement of liquid if result is false
place_liquid(droppos, get_node_place(def.source_place, droppos))
end
if take_bucket then
stack:set_name("mcl_buckets:bucket_empty")
end
return stack
end, end,
})
end
minetest.register_craftitem("mcl_buckets:bucket_empty", {
description = S("Empty Bucket"),
_doc_items_longdesc = S("A bucket can be used to collect and release liquids."),
_doc_items_usagehelp = S("Punch a liquid source to collect it. You can then use the filled bucket to place the liquid somewhere else."),
_tt_help = S("Collects liquids"),
--liquids_pointable = true,
inventory_image = "bucket.png",
stack_max = 16,
_on_dispense = function(stack, pos, droppos, dropnode, dropdir) _on_dispense = function(stack, pos, droppos, dropnode, dropdir)
-- Fill empty bucket with liquid or drop bucket if no liquid -- Fill empty bucket with liquid or drop bucket if no liquid
local collect_liquid = false local collect_liquid = false