Merge pull request 'fix filling cauldrons with water buckets and some minor refactoring.' (#2011) from kabou/MineClone2:fix-buckets-cauldrons into master

Reviewed-on: MineClone2/MineClone2#2011
Reviewed-by: cora <cora@noreply.git.minetest.land>
This commit is contained in:
cora 2022-02-21 15:18:02 +00:00
commit a0b5e4dd0b
1 changed files with 29 additions and 26 deletions

View File

@ -19,9 +19,15 @@ local string = string
local raycast = minetest.raycast local raycast = minetest.raycast
local get_node = minetest.get_node local get_node = minetest.get_node
local set_node = minetest.set_node
local add_node = minetest.add_node local add_node = minetest.add_node
local add_item = minetest.add_item local add_item = minetest.add_item
local registered_nodes = minetest.registered_nodes
local get_item_group = minetest.get_item_group
local is_creative_enabled = minetest.is_creative_enabled
local is_protected = minetest.is_protected
local record_protection_violation = minetest.record_protection_violation
if mod_mcl_core then if mod_mcl_core then
minetest.register_craft({ minetest.register_craft({
@ -40,28 +46,28 @@ mcl_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)
local def = minetest.registered_nodes[itemname] local def = registered_nodes[itemname]
if def and def.sounds and def.sounds.place then if def and def.sounds and def.sounds.place then
minetest.sound_play(def.sounds.place, {gain=1.0, pos = pos, pitch = 1 + math.random(-10, 10)*0.005}, true) minetest.sound_play(def.sounds.place, {gain=1.0, pos = pos, pitch = 1 + math.random(-10, 10)*0.005}, true)
end end
end end
local function sound_take(itemname, pos) local function sound_take(itemname, pos)
local def = minetest.registered_nodes[itemname] local def = registered_nodes[itemname]
if def and def.sounds and def.sounds.dug then if def and def.sounds and def.sounds.dug then
minetest.sound_play(def.sounds.dug, {gain=1.0, pos = pos, pitch = 1 + math.random(-10, 10)*0.005}, true) minetest.sound_play(def.sounds.dug, {gain=1.0, pos = pos, pitch = 1 + math.random(-10, 10)*0.005}, true)
end end
end end
local function place_liquid(pos, itemstring) local function place_liquid(pos, itemstring)
local fullness = minetest.registered_nodes[itemstring].liquid_range local fullness = registered_nodes[itemstring].liquid_range
sound_place(itemstring, pos) sound_place(itemstring, pos)
minetest.add_node(pos, {name=itemstring, param2=fullness}) add_node(pos, {name=itemstring, param2=fullness})
end end
local function give_bucket(new_bucket, itemstack, user) local function give_bucket(new_bucket, itemstack, user)
local inv = user:get_inventory() local inv = user:get_inventory()
if minetest.is_creative_enabled(user:get_player_name()) then if is_creative_enabled(user:get_player_name()) then
--TODO: is a full bucket added if inv doesn't contain one? --TODO: is a full bucket added if inv doesn't contain one?
return itemstack return itemstack
else else
@ -128,7 +134,7 @@ end
local function get_bucket_drop(itemstack, user, take_bucket) local function get_bucket_drop(itemstack, user, take_bucket)
-- Handle bucket item and inventory stuff -- Handle bucket item and inventory stuff
if take_bucket and not minetest.is_creative_enabled(user:get_player_name()) then if take_bucket and not is_creative_enabled(user:get_player_name()) then
-- Add empty bucket and put it into inventory, if possible. -- Add empty bucket and put it into inventory, if possible.
-- Drop empty bucket otherwise. -- Drop empty bucket otherwise.
local new_bucket = ItemStack("mcl_buckets:bucket_empty") local new_bucket = ItemStack("mcl_buckets:bucket_empty")
@ -162,19 +168,16 @@ local function on_place_bucket(itemstack, user, pointed_thing, def)
local undernode = get_node(pointed_thing.under) local undernode = get_node(pointed_thing.under)
local abovenode = get_node(pointed_thing.above) local abovenode = get_node(pointed_thing.above)
local buildable1 = minetest.registered_nodes[undernode.name] and minetest.registered_nodes[undernode.name].buildable_to
local buildable2 = minetest.registered_nodes[abovenode.name] and minetest.registered_nodes[abovenode.name].buildable_to
if not buildable1 and not buildable2 then return itemstack end --if both nodes aren't buildable_to, skip
if buildable1 then if registered_nodes[undernode.name].buildable_to or get_item_group(undernode.name, "cauldron") == 1 then
local result, take_bucket = get_extra_check(def.extra_check, pointed_thing.under, user) local result, take_bucket = get_extra_check(def.extra_check, pointed_thing.under, user)
if result then if result then
local node_place = get_node_place(def.source_place, pointed_thing.under) local node_place = get_node_place(def.source_place, pointed_thing.under)
local pns = user:get_player_name() local pns = user:get_player_name()
-- Check protection -- Check protection
if minetest.is_protected(pointed_thing.under, pns) then if is_protected(pointed_thing.under, pns) then
minetest.record_protection_violation(pointed_thing.under, pns) record_protection_violation(pointed_thing.under, pns)
return itemstack return itemstack
end end
@ -187,15 +190,15 @@ local function on_place_bucket(itemstack, user, pointed_thing, def)
end end
end end
return get_bucket_drop(itemstack, user, take_bucket) return get_bucket_drop(itemstack, user, take_bucket)
elseif buildable2 then elseif registered_nodes[abovenode.name].buildable_to or get_item_group(abovenode.name, "cauldron") == 1 then
local result, take_bucket = get_extra_check(def.extra_check, pointed_thing.above, user) local result, take_bucket = get_extra_check(def.extra_check, pointed_thing.above, user)
if result then if result then
local node_place = get_node_place(def.source_place, pointed_thing.above) local node_place = get_node_place(def.source_place, pointed_thing.above)
local pns = user:get_player_name() local pns = user:get_player_name()
-- Check protection -- Check protection
if minetest.is_protected(pointed_thing.above, pns) then if is_protected(pointed_thing.above, pns) then
minetest.record_protection_violation(pointed_thing.above, pns) record_protection_violation(pointed_thing.above, pns)
return itemstack return itemstack
end end
@ -226,14 +229,14 @@ local function on_place_bucket_empty(itemstack, user, pointed_thing)
return new_stack return new_stack
end end
local node = minetest.get_node(pointed_thing.under) local node = get_node(pointed_thing.under)
local nn = node.name local nn = node.name
local new_bucket local new_bucket
local liquid_node = bucket_raycast(user) local liquid_node = bucket_raycast(user)
if liquid_node then if liquid_node then
if minetest.is_protected(liquid_node.above, user:get_player_name()) then if is_protected(liquid_node.above, user:get_player_name()) then
minetest.record_protection_violation(liquid_node.above, user:get_player_name()) record_protection_violation(liquid_node.above, user:get_player_name())
end end
local liquid_name = get_node(liquid_node.above).name local liquid_name = get_node(liquid_node.above).name
if liquid_name then if liquid_name then
@ -242,7 +245,7 @@ local function on_place_bucket_empty(itemstack, user, pointed_thing)
--minetest.chat_send_all("test") --minetest.chat_send_all("test")
-- Fill bucket, but not in Creative Mode -- Fill bucket, but not in Creative Mode
-- FIXME: remove this line -- FIXME: remove this line
--if not minetest.is_creative_enabled(user:get_player_name()) then --if not is_creative_enabled(user:get_player_name()) then
if not false then if not false then
new_bucket = ItemStack({name = liquid_def.bucketname}) new_bucket = ItemStack({name = liquid_def.bucketname})
if liquid_def.on_take then if liquid_def.on_take then
@ -267,15 +270,15 @@ local function on_place_bucket_empty(itemstack, user, pointed_thing)
-- FIXME: replace this ugly code by cauldrons API -- FIXME: replace this ugly code by cauldrons API
if nn == "mcl_cauldrons:cauldron_3" then if nn == "mcl_cauldrons:cauldron_3" then
-- Take water out of full cauldron -- Take water out of full cauldron
minetest.set_node(pointed_thing.under, {name="mcl_cauldrons:cauldron"}) set_node(pointed_thing.under, {name="mcl_cauldrons:cauldron"})
if not minetest.is_creative_enabled(user:get_player_name()) then if not is_creative_enabled(user:get_player_name()) then
new_bucket = ItemStack("mcl_buckets:bucket_water") new_bucket = ItemStack("mcl_buckets:bucket_water")
end end
sound_take("mcl_core:water_source", pointed_thing.under) sound_take("mcl_core:water_source", pointed_thing.under)
elseif nn == "mcl_cauldrons:cauldron_3r" then elseif nn == "mcl_cauldrons:cauldron_3r" then
-- Take river water out of full cauldron -- Take river water out of full cauldron
minetest.set_node(pointed_thing.under, {name="mcl_cauldrons:cauldron"}) set_node(pointed_thing.under, {name="mcl_cauldrons:cauldron"})
if not minetest.is_creative_enabled(user:get_player_name()) then if not is_creative_enabled(user:get_player_name()) then
new_bucket = ItemStack("mcl_buckets:bucket_river_water") new_bucket = ItemStack("mcl_buckets:bucket_river_water")
end end
sound_take("mclx_core:river_water_source", pointed_thing.under) sound_take("mclx_core:river_water_source", pointed_thing.under)
@ -344,7 +347,7 @@ function mcl_buckets.register_liquid(def)
stack_max = 1, stack_max = 1,
groups = def.groups, groups = def.groups,
_on_dispense = function(stack, pos, droppos, dropnode, dropdir) _on_dispense = function(stack, pos, droppos, dropnode, dropdir)
local buildable = minetest.registered_nodes[dropnode.name].buildable_to or dropnode.name == "mcl_portals:portal" local buildable = registered_nodes[dropnode.name].buildable_to or dropnode.name == "mcl_portals:portal"
if not buildable then return stack end if not buildable then return stack end
local result, take_bucket = get_extra_check(def.extra_check, droppos, nil) local result, take_bucket = get_extra_check(def.extra_check, droppos, nil)
if result then -- Fail placement of liquid if result is false if result then -- Fail placement of liquid if result is false
@ -379,13 +382,13 @@ minetest.register_craftitem("mcl_buckets:bucket_empty", {
collect_liquid = true collect_liquid = true
end end
if collect_liquid then if collect_liquid then
minetest.set_node(droppos, {name="air"}) set_node(droppos, {name="air"})
-- Fill bucket with liquid -- Fill bucket with liquid
stack = new_bucket stack = new_bucket
else else
-- No liquid found: Drop empty bucket -- No liquid found: Drop empty bucket
minetest.add_item(droppos, stack) add_item(droppos, stack)
stack:take_item() stack:take_item()
end end
return stack return stack