From 0adb8dadff37a37877b30204c4c6689d409ac39a Mon Sep 17 00:00:00 2001 From: Sven Volkmann Date: Wed, 12 Aug 2020 20:15:26 +0200 Subject: [PATCH] fixing bug issue #50 --- techpack_warehouse/box_copper.lua | 5 ++-- techpack_warehouse/box_gold.lua | 5 ++-- techpack_warehouse/box_steel.lua | 5 ++-- techpack_warehouse/common.lua | 6 ++--- tubelib/defect.lua | 1 - tubelib/distributor.lua | 7 +++-- tubelib/node_states.lua | 45 ++++++++++++++++++++++++------- tubelib/pusher.lua | 7 +++-- tubelib_addons1/autocrafter.lua | 5 ++-- tubelib_addons1/fermenter.lua | 5 ++-- tubelib_addons1/grinder.lua | 6 ++--- tubelib_addons1/harvester.lua | 5 ++-- tubelib_addons1/liquidsampler.lua | 5 ++-- tubelib_addons1/pusher_fast.lua | 5 ++-- tubelib_addons1/quarry.lua | 5 ++-- tubelib_addons1/reformer.lua | 5 ++-- tubelib_addons3/distributor.lua | 7 +++-- tubelib_addons3/pusher.lua | 5 ++-- tubelib_addons3/pushing_chest.lua | 15 ++--------- 19 files changed, 74 insertions(+), 75 deletions(-) diff --git a/techpack_warehouse/box_copper.lua b/techpack_warehouse/box_copper.lua index dc541c3..2683b1b 100644 --- a/techpack_warehouse/box_copper.lua +++ b/techpack_warehouse/box_copper.lua @@ -47,8 +47,8 @@ minetest.register_node(NODE_NAME, { can_dig = function(pos) return wh.can_dig(Box, pos) end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) - wh.after_dig_node(Box, pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + wh.on_dig_node(Box, pos, node, player) end, allow_metadata_inventory_put = function(pos, listname, index, stack, player) return wh.allow_metadata_inventory_put(Box, pos, listname, index, stack, player) @@ -58,7 +58,6 @@ minetest.register_node(NODE_NAME, { allow_metadata_inventory_move = wh.allow_metadata_inventory_move, on_rotate = screwdriver.disallow, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/techpack_warehouse/box_gold.lua b/techpack_warehouse/box_gold.lua index 5f80712..b6745c2 100644 --- a/techpack_warehouse/box_gold.lua +++ b/techpack_warehouse/box_gold.lua @@ -47,8 +47,8 @@ minetest.register_node(NODE_NAME, { can_dig = function(pos) return wh.can_dig(Box, pos) end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) - wh.after_dig_node(Box, pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + wh.on_dig_node(Box, pos, node, player) end, allow_metadata_inventory_put = function(pos, listname, index, stack, player) return wh.allow_metadata_inventory_put(Box, pos, listname, index, stack, player) @@ -58,7 +58,6 @@ minetest.register_node(NODE_NAME, { allow_metadata_inventory_move = wh.allow_metadata_inventory_move, on_rotate = screwdriver.disallow, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/techpack_warehouse/box_steel.lua b/techpack_warehouse/box_steel.lua index 9e26082..cf6f85a 100644 --- a/techpack_warehouse/box_steel.lua +++ b/techpack_warehouse/box_steel.lua @@ -47,8 +47,8 @@ minetest.register_node(NODE_NAME, { can_dig = function(pos) return wh.can_dig(Box, pos) end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) - wh.after_dig_node(Box, pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + wh.on_dig_node(Box, pos, node, player) end, allow_metadata_inventory_put = function(pos, listname, index, stack, player) return wh.allow_metadata_inventory_put(Box, pos, listname, index, stack, player) @@ -58,7 +58,6 @@ minetest.register_node(NODE_NAME, { allow_metadata_inventory_move = wh.allow_metadata_inventory_move, on_rotate = screwdriver.disallow, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/techpack_warehouse/common.lua b/techpack_warehouse/common.lua index 7f6d9f9..cfba34a 100644 --- a/techpack_warehouse/common.lua +++ b/techpack_warehouse/common.lua @@ -323,10 +323,10 @@ function techpack_warehouse.can_dig(self, pos) return inv:is_empty("main") and inv:is_empty("shift") end -function techpack_warehouse.after_dig_node(self, pos, oldnode, oldmetadata, digger) +function techpack_warehouse.on_dig_node(self, pos, node, digger) tubelib.remove_node(pos) - if oldnode.name == self.node_name then -- not for defect nodes - self.State:after_dig_node(pos, oldnode, oldmetadata, digger) + if node.name == self.node_name then -- not for defect nodes + self.State:on_dig_node(pos, node, digger) end end diff --git a/tubelib/defect.lua b/tubelib/defect.lua index 2c789d1..40ede92 100644 --- a/tubelib/defect.lua +++ b/tubelib/defect.lua @@ -8,7 +8,6 @@ minetest.register_node("tubelib:defect_dummy", { "tubelib_front.png^tubelib_defect.png", "tubelib_front.png^tubelib_defect.png", }, - drop = "", groups = {cracky=3, crumbly=3, choppy=3, not_in_creative_inventory=1}, is_ground_content = false, }) diff --git a/tubelib/distributor.lua b/tubelib/distributor.lua index 1ca0607..5df4209 100644 --- a/tubelib/distributor.lua +++ b/tubelib/distributor.lua @@ -391,9 +391,9 @@ minetest.register_node("tubelib:distributor", { return inv:is_empty("src") end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) - tubelib.remove_node(pos) -- <<=== tubelib - State:after_dig_node(pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + State:on_dig_node(pos, node, player) + tubelib.remove_node(pos) end, allow_metadata_inventory_put = allow_metadata_inventory_put, @@ -403,7 +403,6 @@ minetest.register_node("tubelib:distributor", { on_timer = keep_running, on_rotate = screwdriver.disallow, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/tubelib/node_states.lua b/tubelib/node_states.lua index 3f89e7a..573ff6c 100644 --- a/tubelib/node_states.lua +++ b/tubelib/node_states.lua @@ -302,6 +302,7 @@ function NodeStates:keep_running(pos, meta, val, num_items) self:start(pos, meta, true) meta:set_int("tubelib_countdown", val) meta:set_int("tubelib_item_meter", meta:get_int("tubelib_item_meter") + (num_items or 1)) + if self.aging_level1 then local cnt = meta:get_int("tubelib_aging") + num_items meta:set_int("tubelib_aging", cnt) @@ -427,15 +428,39 @@ function NodeStates:on_node_repair(pos) return false end --- Return working or defect machine, depending on machine lifetime -function NodeStates:after_dig_node(pos, oldnode, oldmetadata, digger) - local inv = minetest.get_inventory({type="player", name=digger:get_player_name()}) - local cnt = oldmetadata.fields.tubelib_aging and tonumber(oldmetadata.fields.tubelib_aging) - if not cnt or cnt < 1 then cnt = 1 end - local is_defect = cnt > self.aging_level1 and math.random(math.max(1, math.floor(self.aging_level2 / cnt))) == 1 - if self.node_name_defect and is_defect then - inv:add_item("main", ItemStack(self.node_name_defect)) - else - inv:add_item("main", ItemStack(self.node_name_passive)) + +--[[ +Callback after digging a node but before removing the node. + +The tubelib node becomes defect after digging it: + - always if the aging counter "tubelib_aging" is greater than self.aging_level2 + - with a certain probability if the aging counter "tubelib_aging" is greater than self.aging_level1 + but smaller than self.aging_level2 + +Info: If a tubelib machine has been running quite some time but is dropped as a non-defect machine and then placed back again, the +tubelib machine will be reset to new (digging will reset the aging counter). So this code tries to prevent this exploit + +]]-- +function NodeStates:on_dig_node(pos, node, player) + local meta = M(pos) + local cnt = tonumber(meta:get_string("tubelib_aging")) + if (not cnt or cnt < 1) then + cnt = 1 end + + local is_defect = (cnt > self.aging_level1) and ( math.random(math.max(1, math.floor(self.aging_level2 / cnt))) == 1 ) + + if is_defect then + self:defect(pos, meta) -- replace node with defect one + node = minetest.get_node(pos) + end + + + minetest.node_dig(pos, node, player) -- default behaviour (this function is called automatically if on_dig() callback isn't set) + end + + + + + diff --git a/tubelib/pusher.lua b/tubelib/pusher.lua index 8e6e878..87fc30c 100644 --- a/tubelib/pusher.lua +++ b/tubelib/pusher.lua @@ -99,15 +99,14 @@ minetest.register_node("tubelib:pusher", { end end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) - tubelib.remove_node(pos) -- <<=== tubelib - State:after_dig_node(pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + State:on_dig_node(pos, node, player) + tubelib.remove_node(pos) end, on_timer = keep_running, on_rotate = screwdriver.disallow, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/tubelib_addons1/autocrafter.lua b/tubelib_addons1/autocrafter.lua index 4c9aa3d..8437903 100644 --- a/tubelib_addons1/autocrafter.lua +++ b/tubelib_addons1/autocrafter.lua @@ -312,10 +312,10 @@ minetest.register_node("tubelib_addons1:autocrafter", { return inv:is_empty("dst") and inv:is_empty("src") end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) autocrafterCache[minetest.hash_node_position(pos)] = nil + State:on_dig_node(pos, node, player) tubelib.remove_node(pos) - State:after_dig_node(pos, oldnode, oldmetadata, digger) end, on_rotate = screwdriver.disallow, @@ -326,7 +326,6 @@ minetest.register_node("tubelib_addons1:autocrafter", { allow_metadata_inventory_take = allow_metadata_inventory_take, allow_metadata_inventory_move = allow_metadata_inventory_move, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/tubelib_addons1/fermenter.lua b/tubelib_addons1/fermenter.lua index 44aec81..15628b7 100644 --- a/tubelib_addons1/fermenter.lua +++ b/tubelib_addons1/fermenter.lua @@ -207,9 +207,9 @@ minetest.register_node("tubelib_addons1:fermenter", { end end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + State:on_dig_node(pos, node, player) tubelib.remove_node(pos) - State:after_dig_node(pos, oldnode, oldmetadata, digger) end, on_rotate = screwdriver.disallow, @@ -219,7 +219,6 @@ minetest.register_node("tubelib_addons1:fermenter", { allow_metadata_inventory_move = allow_metadata_inventory_move, allow_metadata_inventory_take = allow_metadata_inventory_take, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/tubelib_addons1/grinder.lua b/tubelib_addons1/grinder.lua index 693c402..23124ad 100644 --- a/tubelib_addons1/grinder.lua +++ b/tubelib_addons1/grinder.lua @@ -147,11 +147,12 @@ minetest.register_node("tubelib_addons1:grinder", { return inv:is_empty("dst") and inv:is_empty("src") end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) - State:after_dig_node(pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + State:on_dig_node(pos, node, player) tubelib.remove_node(pos) end, + on_rotate = screwdriver.disallow, on_timer = keep_running, on_receive_fields = on_receive_fields, @@ -159,7 +160,6 @@ minetest.register_node("tubelib_addons1:grinder", { allow_metadata_inventory_move = allow_metadata_inventory_move, allow_metadata_inventory_take = allow_metadata_inventory_take, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/tubelib_addons1/harvester.lua b/tubelib_addons1/harvester.lua index 8213d28..37b05f6 100644 --- a/tubelib_addons1/harvester.lua +++ b/tubelib_addons1/harvester.lua @@ -336,8 +336,8 @@ minetest.register_node("tubelib_addons1:harvester_base", { return inv:is_empty("main") end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) - State:after_dig_node(pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + State:on_dig_node(pos, node, player) tubelib.remove_node(pos) end, @@ -347,7 +347,6 @@ minetest.register_node("tubelib_addons1:harvester_base", { allow_metadata_inventory_put = allow_metadata_inventory_put, allow_metadata_inventory_take = allow_metadata_inventory_take, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/tubelib_addons1/liquidsampler.lua b/tubelib_addons1/liquidsampler.lua index 484192a..3e51938 100644 --- a/tubelib_addons1/liquidsampler.lua +++ b/tubelib_addons1/liquidsampler.lua @@ -159,8 +159,8 @@ minetest.register_node("tubelib_addons1:liquidsampler", { return inv:is_empty("dst") and inv:is_empty("src") end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) - State:after_dig_node(pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + State:on_dig_node(pos, node, player) tubelib.remove_node(pos) end, @@ -171,7 +171,6 @@ minetest.register_node("tubelib_addons1:liquidsampler", { allow_metadata_inventory_move = allow_metadata_inventory_move, allow_metadata_inventory_take = allow_metadata_inventory_take, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/tubelib_addons1/pusher_fast.lua b/tubelib_addons1/pusher_fast.lua index 8789028..4baad5c 100644 --- a/tubelib_addons1/pusher_fast.lua +++ b/tubelib_addons1/pusher_fast.lua @@ -99,15 +99,14 @@ minetest.register_node("tubelib_addons1:pusher_fast", { end end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + State:on_dig_node(pos, node, player) tubelib.remove_node(pos) - State:after_dig_node(pos, oldnode, oldmetadata, digger) end, on_timer = keep_running, on_rotate = screwdriver.disallow, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/tubelib_addons1/quarry.lua b/tubelib_addons1/quarry.lua index cd5a3dc..ea294b6 100644 --- a/tubelib_addons1/quarry.lua +++ b/tubelib_addons1/quarry.lua @@ -316,9 +316,9 @@ minetest.register_node("tubelib_addons1:quarry", { return inv:is_empty("main") end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + State:on_dig_node(pos, node, player) tubelib.remove_node(pos) - State:after_dig_node(pos, oldnode, oldmetadata, digger) end, on_rotate = screwdriver.disallow, @@ -327,7 +327,6 @@ minetest.register_node("tubelib_addons1:quarry", { allow_metadata_inventory_put = allow_metadata_inventory_put, allow_metadata_inventory_take = allow_metadata_inventory_take, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/tubelib_addons1/reformer.lua b/tubelib_addons1/reformer.lua index b157087..4b66d5e 100644 --- a/tubelib_addons1/reformer.lua +++ b/tubelib_addons1/reformer.lua @@ -203,9 +203,9 @@ minetest.register_node("tubelib_addons1:reformer", { end end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + State:on_dig_node(pos, node, player) tubelib.remove_node(pos) - State:after_dig_node(pos, oldnode, oldmetadata, digger) end, on_rotate = screwdriver.disallow, @@ -215,7 +215,6 @@ minetest.register_node("tubelib_addons1:reformer", { allow_metadata_inventory_move = allow_metadata_inventory_move, allow_metadata_inventory_take = allow_metadata_inventory_take, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/tubelib_addons3/distributor.lua b/tubelib_addons3/distributor.lua index e41b156..86d621e 100644 --- a/tubelib_addons3/distributor.lua +++ b/tubelib_addons3/distributor.lua @@ -359,9 +359,9 @@ minetest.register_node("tubelib_addons3:distributor", { local inv = M(pos):get_inventory() return inv:is_empty("src") end, - - after_dig_node = function(pos, oldnode, oldmetadata, digger) - State:after_dig_node(pos, oldnode, oldmetadata, digger) + + on_dig = function(pos, node, player) + State:on_dig_node(pos, node, player) tubelib.remove_node(pos) end, @@ -372,7 +372,6 @@ minetest.register_node("tubelib_addons3:distributor", { on_timer = keep_running, on_rotate = screwdriver.disallow, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/tubelib_addons3/pusher.lua b/tubelib_addons3/pusher.lua index 0074d85..1207377 100644 --- a/tubelib_addons3/pusher.lua +++ b/tubelib_addons3/pusher.lua @@ -84,15 +84,14 @@ minetest.register_node("tubelib_addons3:pusher", { end end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + State:on_dig_node(pos, node, player) tubelib.remove_node(pos) - State:after_dig_node(pos, oldnode, oldmetadata, digger) end, on_timer = keep_running, on_rotate = screwdriver.disallow, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir", diff --git a/tubelib_addons3/pushing_chest.lua b/tubelib_addons3/pushing_chest.lua index a5d5d32..d8ee7fd 100644 --- a/tubelib_addons3/pushing_chest.lua +++ b/tubelib_addons3/pushing_chest.lua @@ -75,16 +75,6 @@ local function aging(pos, meta) end end -local function after_dig_node(pos, oldnode, oldmetadata, digger) - local inv = minetest.get_inventory({type="player", name=digger:get_player_name()}) - local cnt = oldmetadata.fields.tubelib_aging and tonumber(oldmetadata.fields.tubelib_aging) or 0 - local is_defect = cnt > AGING_LEVEL1 and math.random(AGING_LEVEL2 / cnt) == 1 - if is_defect then - inv:add_item("main", ItemStack("tubelib_addons3:pushing_chest_defect")) - else - inv:add_item("main", ItemStack("tubelib_addons3:pushing_chest")) - end -end local function set_state(meta, state) local number = meta:get_string("number") @@ -200,8 +190,8 @@ minetest.register_node("tubelib_addons3:pushing_chest", { return inv:is_empty("main") and inv:is_empty("shift") end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) - after_dig_node(pos, oldnode, oldmetadata, digger) + on_dig = function(pos, node, player) + State:on_dig_node(pos, node, player) tubelib.remove_node(pos) end, @@ -212,7 +202,6 @@ minetest.register_node("tubelib_addons3:pushing_chest", { on_timer = shift_items, on_rotate = screwdriver.disallow, - drop = "", paramtype = "light", sunlight_propagates = true, paramtype2 = "facedir",