From bfd952b51a61278c7108cd32e75677f93ebcf14d Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Mon, 4 Nov 2019 20:00:43 +0100 Subject: [PATCH] Delayer: Combine shared definitions, add protection (#490) --- .gitignore | 2 + mesecons_delayer/init.lua | 131 +++++++++++++++++--------------------- 2 files changed, 62 insertions(+), 71 deletions(-) diff --git a/.gitignore b/.gitignore index b25c15b..41756b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ *~ +*.patch +*.diff diff --git a/mesecons_delayer/init.lua b/mesecons_delayer/init.lua index 94450db..66665ad 100644 --- a/mesecons_delayer/init.lua +++ b/mesecons_delayer/init.lua @@ -33,19 +33,9 @@ end -- Register the 2 (states) x 4 (delay times) delayers -for i = 1, 4 do -local groups = {} -if i == 1 then - groups = {bendy=2,snappy=1,dig_immediate=2} -else - groups = {bendy=2,snappy=1,dig_immediate=2, not_in_creative_inventory=1} -end +local delaytime = { 0.1, 0.3, 0.5, 1.0 } -local delaytime -if i == 1 then delaytime = 0.1 -elseif i == 2 then delaytime = 0.3 -elseif i == 3 then delaytime = 0.5 -elseif i == 4 then delaytime = 1.0 end +for i = 1, 4 do local boxes = { { -6/16, -8/16, -6/16, 6/16, -7/16, 6/16 }, -- the main slab @@ -61,19 +51,9 @@ local boxes = { { 6/16, -8/16, -1/16, 8/16, -7/16, 1/16 } } -minetest.register_node("mesecons_delayer:delayer_off_"..tostring(i), { - description = "Delayer", +-- Delayer definition defaults +local def = { drawtype = "nodebox", - tiles = { - "mesecons_delayer_off_"..tostring(i)..".png", - "mesecons_delayer_bottom.png", - "mesecons_delayer_ends_off.png", - "mesecons_delayer_ends_off.png", - "mesecons_delayer_sides_off.png", - "mesecons_delayer_sides_off.png" - }, - inventory_image = "mesecons_delayer_off_1.png", - wield_image = "mesecons_delayer_off_1.png", walkable = true, selection_box = { type = "fixed", @@ -83,26 +63,46 @@ minetest.register_node("mesecons_delayer:delayer_off_"..tostring(i), { type = "fixed", fixed = boxes }, - groups = groups, paramtype = "light", paramtype2 = "facedir", sunlight_propagates = true, is_ground_content = false, - drop = 'mesecons_delayer:delayer_off_1', - on_punch = function (pos, node) - if node.name=="mesecons_delayer:delayer_off_1" then - minetest.swap_node(pos, {name = "mesecons_delayer:delayer_off_2", param2=node.param2}) - elseif node.name=="mesecons_delayer:delayer_off_2" then - minetest.swap_node(pos, {name = "mesecons_delayer:delayer_off_3", param2=node.param2}) - elseif node.name=="mesecons_delayer:delayer_off_3" then - minetest.swap_node(pos, {name = "mesecons_delayer:delayer_off_4", param2=node.param2}) - elseif node.name=="mesecons_delayer:delayer_off_4" then - minetest.swap_node(pos, {name = "mesecons_delayer:delayer_off_1", param2=node.param2}) - end - end, - delayer_time = delaytime, - delayer_onstate = "mesecons_delayer:delayer_on_"..tostring(i), + delayer_time = delaytime[i], sounds = default.node_sound_stone_defaults(), + on_blast = mesecon.on_blastnode, + drop = "mesecons_delayer:delayer_off_1", +} + +-- Deactivated delayer definition defaults +local off_groups = {bendy=2,snappy=1,dig_immediate=2} +if i > 1 then + off_groups.not_in_creative_inventory = 1 +end + +local off_state = { + description = "Delayer", + tiles = { + "mesecons_delayer_off_"..tostring(i)..".png", + "mesecons_delayer_bottom.png", + "mesecons_delayer_ends_off.png", + "mesecons_delayer_ends_off.png", + "mesecons_delayer_sides_off.png", + "mesecons_delayer_sides_off.png" + }, + inventory_image = "mesecons_delayer_off_1.png", + wield_image = "mesecons_delayer_off_1.png", + groups = off_groups, + on_punch = function(pos, node, puncher) + if minetest.is_protected(pos, puncher and puncher:get_player_name()) then + return + end + + minetest.swap_node(pos, { + name = "mesecons_delayer:delayer_off_"..tostring(i % 4 + 1), + param2 = node.param2 + }) + end, + delayer_onstate = "mesecons_delayer:delayer_on_"..tostring(i), mesecons = { receptor = { @@ -115,13 +115,15 @@ minetest.register_node("mesecons_delayer:delayer_off_"..tostring(i), { action_on = delayer_activate } }, - on_blast = mesecon.on_blastnode, -}) +} +for k, v in pairs(def) do + off_state[k] = off_state[k] or v +end +minetest.register_node("mesecons_delayer:delayer_off_"..tostring(i), off_state) - -minetest.register_node("mesecons_delayer:delayer_on_"..tostring(i), { +-- Activated delayer definition defaults +local on_state = { description = "You hacker you", - drawtype = "nodebox", tiles = { "mesecons_delayer_on_"..tostring(i)..".png", "mesecons_delayer_bottom.png", @@ -129,36 +131,19 @@ minetest.register_node("mesecons_delayer:delayer_on_"..tostring(i), { "mesecons_delayer_ends_on.png", "mesecons_delayer_sides_on.png", "mesecons_delayer_sides_on.png" - }, - walkable = true, - selection_box = { - type = "fixed", - fixed = { -8/16, -8/16, -8/16, 8/16, -6/16, 8/16 }, - }, - node_box = { - type = "fixed", - fixed = boxes }, groups = {bendy = 2, snappy = 1, dig_immediate = 2, not_in_creative_inventory = 1}, - paramtype = "light", - paramtype2 = "facedir", - sunlight_propagates = true, - is_ground_content = false, - drop = 'mesecons_delayer:delayer_off_1', - on_punch = function (pos, node) - if node.name=="mesecons_delayer:delayer_on_1" then - minetest.swap_node(pos, {name = "mesecons_delayer:delayer_on_2", param2=node.param2}) - elseif node.name=="mesecons_delayer:delayer_on_2" then - minetest.swap_node(pos, {name = "mesecons_delayer:delayer_on_3", param2=node.param2}) - elseif node.name=="mesecons_delayer:delayer_on_3" then - minetest.swap_node(pos, {name = "mesecons_delayer:delayer_on_4", param2=node.param2}) - elseif node.name=="mesecons_delayer:delayer_on_4" then - minetest.swap_node(pos, {name = "mesecons_delayer:delayer_on_1", param2=node.param2}) + on_punch = function(pos, node, puncher) + if minetest.is_protected(pos, puncher and puncher:get_player_name()) then + return end + + minetest.swap_node(pos, { + name = "mesecons_delayer:delayer_on_"..tostring(i % 4 + 1), + param2 = node.param2 + }) end, - delayer_time = delaytime, delayer_offstate = "mesecons_delayer:delayer_off_"..tostring(i), - sounds = default.node_sound_stone_defaults(), mesecons = { receptor = { @@ -171,8 +156,12 @@ minetest.register_node("mesecons_delayer:delayer_on_"..tostring(i), { action_off = delayer_deactivate } }, - on_blast = mesecon.on_blastnode, -}) +} +for k, v in pairs(def) do + on_state[k] = on_state[k] or v +end +minetest.register_node("mesecons_delayer:delayer_on_"..tostring(i), on_state) + end minetest.register_craft({