From 584a75df76649e4935248f1326d8efb432442aa4 Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 20 Feb 2022 17:36:23 +0100 Subject: [PATCH 1/8] Fix filling cauldrons with water. When using a bucket of water on a cauldron, this would not fill the cauldron as expected, but deposit a water source block on top of the cauldron instead. Applied patch from mineclone5 #38 (commit # 698c29733f06a7fcb7e755bf26ee46b33b00699b) from mineclone5 that fixes this problem. --- mods/ITEMS/mcl_buckets/init.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mods/ITEMS/mcl_buckets/init.lua b/mods/ITEMS/mcl_buckets/init.lua index 5d372ef1f..d3cabc853 100644 --- a/mods/ITEMS/mcl_buckets/init.lua +++ b/mods/ITEMS/mcl_buckets/init.lua @@ -162,8 +162,11 @@ local function on_place_bucket(itemstack, user, pointed_thing, def) local undernode = get_node(pointed_thing.under) 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 + local name1, name2 = undernode.name, abovenode.name + local regnode1, regnode2 = minetest.registered_nodes[name1], minetest.registered_nodes[name2] + local buildable1 = regnode1 and (regnode1.buildable_to or regnode1.groups.cauldron == 1) + local buildable2 = regnode2 and (regnode2.buildable_to or regnode2.groups.cauldron == 1) + if not buildable1 and not buildable2 then return itemstack end --if both nodes aren't buildable_to, skip if buildable1 then From 315f251584fb55e6a76b3397f8b96f4d17476d47 Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 20 Feb 2022 17:49:24 +0100 Subject: [PATCH 2/8] Add local to speedup global function access. The minetest.registered_nodes function is called from multiple places. Define a local alias registered_nodes and substitute it in all calls. --- mods/ITEMS/mcl_buckets/init.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mods/ITEMS/mcl_buckets/init.lua b/mods/ITEMS/mcl_buckets/init.lua index d3cabc853..506ce6476 100644 --- a/mods/ITEMS/mcl_buckets/init.lua +++ b/mods/ITEMS/mcl_buckets/init.lua @@ -22,6 +22,8 @@ local get_node = minetest.get_node local add_node = minetest.add_node local add_item = minetest.add_item +local registered_nodes = minetest.registered_nodes + if mod_mcl_core then minetest.register_craft({ @@ -40,21 +42,21 @@ mcl_buckets = { -- Sound helper functions for placing and taking liquids 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 minetest.sound_play(def.sounds.place, {gain=1.0, pos = pos, pitch = 1 + math.random(-10, 10)*0.005}, true) end end 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 minetest.sound_play(def.sounds.dug, {gain=1.0, pos = pos, pitch = 1 + math.random(-10, 10)*0.005}, true) end end 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) minetest.add_node(pos, {name=itemstring, param2=fullness}) end @@ -163,7 +165,7 @@ local function on_place_bucket(itemstack, user, pointed_thing, def) local undernode = get_node(pointed_thing.under) local abovenode = get_node(pointed_thing.above) local name1, name2 = undernode.name, abovenode.name - local regnode1, regnode2 = minetest.registered_nodes[name1], minetest.registered_nodes[name2] + local regnode1, regnode2 = registered_nodes[name1], registered_nodes[name2] local buildable1 = regnode1 and (regnode1.buildable_to or regnode1.groups.cauldron == 1) local buildable2 = regnode2 and (regnode2.buildable_to or regnode2.groups.cauldron == 1) @@ -347,7 +349,7 @@ function mcl_buckets.register_liquid(def) 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" + local buildable = 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 From 13baa68b677d1ed4e981ed9fea53c960585596ad Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 20 Feb 2022 17:58:17 +0100 Subject: [PATCH 3/8] Fixed missed alias uses and added set_node alias. Two instances of add_node and add_item still used the full minetest call, substitute the alias. Added a set_node alias for several minetest.set_node calls. --- mods/ITEMS/mcl_buckets/init.lua | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mods/ITEMS/mcl_buckets/init.lua b/mods/ITEMS/mcl_buckets/init.lua index 506ce6476..1d37f2e0d 100644 --- a/mods/ITEMS/mcl_buckets/init.lua +++ b/mods/ITEMS/mcl_buckets/init.lua @@ -19,6 +19,7 @@ local string = string local raycast = minetest.raycast local get_node = minetest.get_node +local set_node = minetest.set_node local add_node = minetest.add_node local add_item = minetest.add_item @@ -58,7 +59,7 @@ end local function place_liquid(pos, itemstring) local fullness = registered_nodes[itemstring].liquid_range sound_place(itemstring, pos) - minetest.add_node(pos, {name=itemstring, param2=fullness}) + add_node(pos, {name=itemstring, param2=fullness}) end local function give_bucket(new_bucket, itemstack, user) @@ -272,14 +273,14 @@ local function on_place_bucket_empty(itemstack, user, pointed_thing) -- FIXME: replace this ugly code by cauldrons API if nn == "mcl_cauldrons:cauldron_3" then -- 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 new_bucket = ItemStack("mcl_buckets:bucket_water") end sound_take("mcl_core:water_source", pointed_thing.under) elseif nn == "mcl_cauldrons:cauldron_3r" then -- 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 new_bucket = ItemStack("mcl_buckets:bucket_river_water") end @@ -384,13 +385,13 @@ minetest.register_craftitem("mcl_buckets:bucket_empty", { collect_liquid = true end if collect_liquid then - minetest.set_node(droppos, {name="air"}) + set_node(droppos, {name="air"}) -- Fill bucket with liquid stack = new_bucket else -- No liquid found: Drop empty bucket - minetest.add_item(droppos, stack) + add_item(droppos, stack) stack:take_item() end return stack From b3aed9d6b829547f74a466c73438f98176c2c533 Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 20 Feb 2022 18:06:04 +0100 Subject: [PATCH 4/8] Use proper get_item_group accessor. Instead of directly accessing the group table in node definitions, use the proper minetest.get_item_group accessor to test for "cauldron" group. Also adds local alias get_item_group for the global minetest call. --- mods/ITEMS/mcl_buckets/init.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mods/ITEMS/mcl_buckets/init.lua b/mods/ITEMS/mcl_buckets/init.lua index 1d37f2e0d..3bbae5487 100644 --- a/mods/ITEMS/mcl_buckets/init.lua +++ b/mods/ITEMS/mcl_buckets/init.lua @@ -24,7 +24,7 @@ local add_node = minetest.add_node local add_item = minetest.add_item local registered_nodes = minetest.registered_nodes - +local get_item_group = minetest.get_item_group if mod_mcl_core then minetest.register_craft({ @@ -167,8 +167,8 @@ local function on_place_bucket(itemstack, user, pointed_thing, def) local abovenode = get_node(pointed_thing.above) local name1, name2 = undernode.name, abovenode.name local regnode1, regnode2 = registered_nodes[name1], registered_nodes[name2] - local buildable1 = regnode1 and (regnode1.buildable_to or regnode1.groups.cauldron == 1) - local buildable2 = regnode2 and (regnode2.buildable_to or regnode2.groups.cauldron == 1) + local buildable1 = regnode1 and (regnode1.buildable_to or get_item_group(name1, "cauldron") == 1) + local buildable2 = regnode2 and (regnode2.buildable_to or get_item_group(name2, "cauldron") == 1) if not buildable1 and not buildable2 then return itemstack end --if both nodes aren't buildable_to, skip From 88ce1e36629a51e3d8bd04a52fdb0e22b55c45d9 Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 20 Feb 2022 18:15:56 +0100 Subject: [PATCH 5/8] Remove redundant logic. The function on_place_bucket contains the following logic: if not a and not b then return x end if a then foo(a) elseif b then foo(b) else return x end The "if not a and not b then .." is removed because the case is handled by the else case later on. This will allow some further simplifications. --- mods/ITEMS/mcl_buckets/init.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/mods/ITEMS/mcl_buckets/init.lua b/mods/ITEMS/mcl_buckets/init.lua index 3bbae5487..ba2ece4f8 100644 --- a/mods/ITEMS/mcl_buckets/init.lua +++ b/mods/ITEMS/mcl_buckets/init.lua @@ -170,8 +170,6 @@ local function on_place_bucket(itemstack, user, pointed_thing, def) local buildable1 = regnode1 and (regnode1.buildable_to or get_item_group(name1, "cauldron") == 1) local buildable2 = regnode2 and (regnode2.buildable_to or get_item_group(name2, "cauldron") == 1) - if not buildable1 and not buildable2 then return itemstack end --if both nodes aren't buildable_to, skip - if buildable1 then local result, take_bucket = get_extra_check(def.extra_check, pointed_thing.under, user) if result then From d2261426c3457fcd7348eeae48c8d95bc920352c Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 20 Feb 2022 18:40:57 +0100 Subject: [PATCH 6/8] Remove redundant variables in on_place_bucket. The function on_place_bucket defined a couple of variables that were used only once or in a redundant check. After removal of the redundant check in a previous commit, all use-once variables can now be substituted with their assignment expressions. --- mods/ITEMS/mcl_buckets/init.lua | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/mods/ITEMS/mcl_buckets/init.lua b/mods/ITEMS/mcl_buckets/init.lua index ba2ece4f8..663d1ca70 100644 --- a/mods/ITEMS/mcl_buckets/init.lua +++ b/mods/ITEMS/mcl_buckets/init.lua @@ -165,12 +165,8 @@ local function on_place_bucket(itemstack, user, pointed_thing, def) local undernode = get_node(pointed_thing.under) local abovenode = get_node(pointed_thing.above) - local name1, name2 = undernode.name, abovenode.name - local regnode1, regnode2 = registered_nodes[name1], registered_nodes[name2] - local buildable1 = regnode1 and (regnode1.buildable_to or get_item_group(name1, "cauldron") == 1) - local buildable2 = regnode2 and (regnode2.buildable_to or get_item_group(name2, "cauldron") == 1) - 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) if result then local node_place = get_node_place(def.source_place, pointed_thing.under) @@ -191,7 +187,7 @@ local function on_place_bucket(itemstack, user, pointed_thing, def) end end 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) if result then local node_place = get_node_place(def.source_place, pointed_thing.above) From 639bf936c586ab96910770e804d4b691cb498fe5 Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 20 Feb 2022 19:52:05 +0100 Subject: [PATCH 7/8] Use alias for minetest.* namespace function call. * Fix one missed minetest.get_node use in earlier commit. --- mods/ITEMS/mcl_buckets/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/ITEMS/mcl_buckets/init.lua b/mods/ITEMS/mcl_buckets/init.lua index 663d1ca70..11e1d1c4d 100644 --- a/mods/ITEMS/mcl_buckets/init.lua +++ b/mods/ITEMS/mcl_buckets/init.lua @@ -226,7 +226,7 @@ local function on_place_bucket_empty(itemstack, user, pointed_thing) return new_stack end - local node = minetest.get_node(pointed_thing.under) + local node = get_node(pointed_thing.under) local nn = node.name local new_bucket From e54502edd65549cd6ab7765200228fb9416ef5c1 Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 20 Feb 2022 20:24:55 +0100 Subject: [PATCH 8/8] Add more local aliases for minetest.* namespace functions * Add locals for - minetest.is_creative_enabled - minetest.is_protected - minetest.record_protection_violation --- mods/ITEMS/mcl_buckets/init.lua | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/mods/ITEMS/mcl_buckets/init.lua b/mods/ITEMS/mcl_buckets/init.lua index 11e1d1c4d..e253a342a 100644 --- a/mods/ITEMS/mcl_buckets/init.lua +++ b/mods/ITEMS/mcl_buckets/init.lua @@ -25,6 +25,9 @@ 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 minetest.register_craft({ @@ -64,7 +67,7 @@ end local function give_bucket(new_bucket, itemstack, user) 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? return itemstack else @@ -131,7 +134,7 @@ end local function get_bucket_drop(itemstack, user, take_bucket) -- 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. -- Drop empty bucket otherwise. local new_bucket = ItemStack("mcl_buckets:bucket_empty") @@ -173,8 +176,8 @@ local function on_place_bucket(itemstack, user, pointed_thing, def) local pns = user:get_player_name() -- Check protection - if minetest.is_protected(pointed_thing.under, pns) then - minetest.record_protection_violation(pointed_thing.under, pns) + if is_protected(pointed_thing.under, pns) then + record_protection_violation(pointed_thing.under, pns) return itemstack end @@ -194,8 +197,8 @@ local function on_place_bucket(itemstack, user, pointed_thing, def) local pns = user:get_player_name() -- Check protection - if minetest.is_protected(pointed_thing.above, pns) then - minetest.record_protection_violation(pointed_thing.above, pns) + if is_protected(pointed_thing.above, pns) then + record_protection_violation(pointed_thing.above, pns) return itemstack end @@ -232,8 +235,8 @@ local function on_place_bucket_empty(itemstack, user, pointed_thing) local new_bucket local liquid_node = bucket_raycast(user) if liquid_node then - if minetest.is_protected(liquid_node.above, user:get_player_name()) then - minetest.record_protection_violation(liquid_node.above, user:get_player_name()) + if is_protected(liquid_node.above, user:get_player_name()) then + record_protection_violation(liquid_node.above, user:get_player_name()) end local liquid_name = get_node(liquid_node.above).name if liquid_name then @@ -242,7 +245,7 @@ local function on_place_bucket_empty(itemstack, user, pointed_thing) --minetest.chat_send_all("test") -- Fill bucket, but not in Creative Mode -- 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 new_bucket = ItemStack({name = liquid_def.bucketname}) if liquid_def.on_take then @@ -268,14 +271,14 @@ local function on_place_bucket_empty(itemstack, user, pointed_thing) if nn == "mcl_cauldrons:cauldron_3" then -- Take water out of full 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") end sound_take("mcl_core:water_source", pointed_thing.under) elseif nn == "mcl_cauldrons:cauldron_3r" then -- Take river water out of full 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") end sound_take("mclx_core:river_water_source", pointed_thing.under)