From d7bb017dcaf5182fb95e36115d998f1b2aef3afd Mon Sep 17 00:00:00 2001 From: teknomunk Date: Thu, 2 Jan 2025 06:28:34 -0600 Subject: [PATCH 1/3] Fix sweetberry bonemealing crash, fix sweetberry growth, fix double bonemeal usage --- mods/CORE/mcl_util/init.lua | 15 +++++++++++++++ mods/ITEMS/mcl_bone_meal/init.lua | 13 +++---------- mods/ITEMS/mcl_farming/mod.conf | 2 +- mods/ITEMS/mcl_farming/sweet_berry.lua | 20 +++++++------------- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index 56a4aaa3d..c43731dbc 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -343,6 +343,21 @@ function mcl_util.call_on_rightclick(itemstack, player, pointed_thing) end end end +function mcl_util.handle_node_rightclick(itemstack, player, pointed_thing) + -- Call on_rightclick if the pointed node defines it + if pointed_thing and pointed_thing.type == "node" then + local pos = pointed_thing.under + local node = minetest.get_node(pos) + if player and not player:get_player_control().sneak then + local nodedef = minetest.registered_nodes[node.name] + local on_rightclick = nodedef and nodedef.on_rightclick + if on_rightclick then + return on_rightclick(pos, node, player, itemstack, pointed_thing) or itemstack, true + end + end + end + return itemstack, false +end function mcl_util.calculate_durability(itemstack) local unbreaking_level = mcl_enchanting.get_enchantment(itemstack, "unbreaking") diff --git a/mods/ITEMS/mcl_bone_meal/init.lua b/mods/ITEMS/mcl_bone_meal/init.lua index e899ca765..6ce1554e8 100644 --- a/mods/ITEMS/mcl_bone_meal/init.lua +++ b/mods/ITEMS/mcl_bone_meal/init.lua @@ -123,17 +123,10 @@ minetest.register_craftitem("mcl_bone_meal:bone_meal", { inventory_image = "mcl_bone_meal.png", groups = {craftitem=1}, on_place = function(itemstack, placer, pointed_thing) - local pos = pointed_thing.under - local node = minetest.get_node(pos) - local ndef = minetest.registered_nodes[node.name] - -- Use pointed node's on_rightclick function first, if present. - if placer and not placer:get_player_control().sneak then - if ndef and ndef.on_rightclick then - local new_stack = mcl_util.call_on_rightclick(itemstack, placer, pointed_thing) - if new_stack and new_stack ~= itemstack then return new_stack end - end - end + local called + itemstack, called = mcl_util.handle_node_rightclick(itemstack, placer, pointed_thing) + if called then return itemstack end return mcl_bone_meal.use_bone_meal(itemstack, placer, pointed_thing) end, diff --git a/mods/ITEMS/mcl_farming/mod.conf b/mods/ITEMS/mcl_farming/mod.conf index fe4bc1564..c833c031f 100644 --- a/mods/ITEMS/mcl_farming/mod.conf +++ b/mods/ITEMS/mcl_farming/mod.conf @@ -1,3 +1,3 @@ name = mcl_farming -depends = mcl_core, mcl_sounds, mcl_wool, mcl_torches, mcl_weather, mobs_mc, mcl_colors, mcl_init +depends = mcl_core, mcl_sounds, mcl_wool, mcl_torches, mcl_weather, mobs_mc, mcl_colors, mcl_init, mcl_bone_meal optional_depends = mcl_armor, doc diff --git a/mods/ITEMS/mcl_farming/sweet_berry.lua b/mods/ITEMS/mcl_farming/sweet_berry.lua index 769e7b6cf..76d4d939a 100644 --- a/mods/ITEMS/mcl_farming/sweet_berry.lua +++ b/mods/ITEMS/mcl_farming/sweet_berry.lua @@ -14,19 +14,21 @@ for i=0, 3 do local on_bonemealing = nil local function do_berry_drop(pos) - for j=1, berries_to_drop[math.random(2)] do + if not berries_to_drop then return end + + for _=1, berries_to_drop[math.random(2)] do minetest.add_item(pos, "mcl_farming:sweet_berry") end minetest.swap_node(pos, {name = "mcl_farming:sweet_berry_bush_1"}) end if i ~= 3 then - on_bonemealing = function(itemstack, placer, pointed_thing) + on_bonemealing = function(_, _, pointed_thing) local pos = pointed_thing.under local node = minetest.get_node(pos) - return mcl_farming:grow_plant("plant_sweet_berry_bush", pos, node, 0, true) + return mcl_farming:grow_plant("plant_sweet_berry_bush", pos, node, 1, true) end else - on_bonemealing = function(itemstack, placer, pointed_thing) + on_bonemealing = function(_, _, pointed_thing) do_berry_drop(pointed_thing.under) end end @@ -65,20 +67,12 @@ for i=0, 3 do _mcl_blast_resistance = 0, _mcl_hardness = 0, _on_bone_meal = on_bonemealing, - on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) + on_rightclick = function(pos, _, clicker, itemstack, pointed_thing) local pn = clicker:get_player_name() if clicker:is_player() and minetest.is_protected(pos, pn) then minetest.record_protection_violation(pos, pn) return itemstack end - if 3 ~= i and mcl_dye and - clicker:get_wielded_item():get_name() == "mcl_bone_meal:bone_meal" then - mcl_dye.apply_bone_meal({under=pos, above=vector.offset(pos,0,1,0)},clicker) - if not minetest.is_creative_enabled(pn) then - itemstack:take_item() - end - return - end if i >= 2 then do_berry_drop(pos) From e82496c8120c896dd67bbd6bda7fea4b15334e80 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Thu, 2 Jan 2025 18:05:03 -0600 Subject: [PATCH 2/3] Refactor to remove drop_berries that was used exactly twice and had same function as another variable, refactor do_berry_drop to return boolean on whether berries were dropped, refactor right-click handler to make it more clear that the bonemealing check only happens if berries were not dropped --- mods/ITEMS/mcl_farming/sweet_berry.lua | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/mods/ITEMS/mcl_farming/sweet_berry.lua b/mods/ITEMS/mcl_farming/sweet_berry.lua index 76d4d939a..53f5d958b 100644 --- a/mods/ITEMS/mcl_farming/sweet_berry.lua +++ b/mods/ITEMS/mcl_farming/sweet_berry.lua @@ -9,18 +9,18 @@ for i=0, 3 do if i > 0 then groups.sweet_berry_thorny = 1 end - local drop_berries = (i >= 2) - local berries_to_drop = drop_berries and {i - 1, i} or nil - - local on_bonemealing = nil + local berries_to_drop = (i >= 2) and {i - 1, i} or nil local function do_berry_drop(pos) - if not berries_to_drop then return end + if not berries_to_drop then return false end for _=1, berries_to_drop[math.random(2)] do minetest.add_item(pos, "mcl_farming:sweet_berry") end minetest.swap_node(pos, {name = "mcl_farming:sweet_berry_bush_1"}) + return true end + + local on_bonemealing = nil if i ~= 3 then on_bonemealing = function(_, _, pointed_thing) local pos = pointed_thing.under @@ -49,13 +49,13 @@ for i=0, 3 do liquid_range = 0, walkable = false, -- Dont even create a table if no berries are dropped. - drop = not drop_berries and "" or { + drop = berries_to_drop and { max_items = 1, items = { { items = {"mcl_farming:sweet_berry " .. berries_to_drop[1] }, rarity = 2 }, { items = {"mcl_farming:sweet_berry " .. berries_to_drop[2] } } } - }, + } or "", selection_box = { type = "fixed", fixed = {-6 / 16, -0.5, -6 / 16, 6 / 16, (-0.30 + (i*0.25)), 6 / 16}, @@ -74,13 +74,11 @@ for i=0, 3 do return itemstack end - if i >= 2 then - do_berry_drop(pos) - else - -- Use bonemeal - if mcl_bone_meal and clicker:get_wielded_item():get_name() == "mcl_bone_meal:bone_meal" then - return mcl_bone_meal.use_bone_meal(itemstack, clicker, pointed_thing) - end + if do_berry_drop(pos) then return itemstack end + + -- Use bonemeal + if mcl_bone_meal and clicker:get_wielded_item():get_name() == "mcl_bone_meal:bone_meal" then + return mcl_bone_meal.use_bone_meal(itemstack, clicker, pointed_thing) end return itemstack end, From ab7d0544da6c7f3fd73532159d572932f3396ff8 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Thu, 2 Jan 2025 18:07:12 -0600 Subject: [PATCH 3/3] Add TODO for future planned work --- mods/CORE/mcl_util/init.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index c43731dbc..552c135a1 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -343,6 +343,8 @@ function mcl_util.call_on_rightclick(itemstack, player, pointed_thing) end end end + +--- TODO: replace with global right-click handler patched in with core.on_register_mods_loaded() function mcl_util.handle_node_rightclick(itemstack, player, pointed_thing) -- Call on_rightclick if the pointed node defines it if pointed_thing and pointed_thing.type == "node" then