Merge pull request 'Correct bucket behavior' (#2007) from NO11/MineClone2:buckets+ into master

Reviewed-on: MineClone2/MineClone2#2007
Reviewed-by: cora <cora@noreply.git.minetest.land>
This commit is contained in:
cora 2022-02-18 00:17:50 +00:00
commit 6493f2885b
3 changed files with 186 additions and 138 deletions

View File

@ -571,3 +571,16 @@ function mcl_util.replace_mob(obj, mob)
obj:set_yaw(rot) obj:set_yaw(rot)
return obj return obj
end end
function mcl_util.get_pointed_thing(player, liquid)
local pos = vector.offset(player:get_pos(), 0, player:get_properties().eye_height, 0)
local look_dir = vector.multiply(player:get_look_dir(), 5)
local pos2 = vector.add(pos, look_dir)
local ray = minetest.raycast(pos, pos2, false, liquid)
if ray then
for pointed_thing in ray do
return pointed_thing
end
end
end

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,87 @@ 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 def = mcl_buckets.buckets[itemname]
if itemname == "mcl_buckets:bucket_empty" then
local pointed_thing = mcl_util.get_pointed_thing(player, true)
if not pointed_thing then
return
end
wielded_item = on_place_bucket_empty(wielded_item, player, pointed_thing)
elseif def then
local pointed_thing = mcl_util.get_pointed_thing(player, false)
if not pointed_thing then
return
end
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

View File

@ -1,6 +1,6 @@
name = mcl_buckets name = mcl_buckets
author = Kahrl author = Kahrl
description = description =
depends = mcl_worlds, mcl_util depends = mcl_worlds, mcl_util, controls
optional_depends = mcl_core, mclx_core, doc optional_depends = mcl_core, mclx_core, doc