From 74fac5ff9a959320f00a0872ff33da08b77e079f Mon Sep 17 00:00:00 2001 From: tenplus1 Date: Sat, 25 Jan 2025 14:45:20 +0000 Subject: [PATCH] separate chest and doors code --- chest.lua | 253 ++++++++++++++++++++++++++++++++++ doors_chest.lua => doors.lua | 257 ++--------------------------------- init.lua | 3 +- pvp.lua | 2 - 4 files changed, 263 insertions(+), 252 deletions(-) create mode 100644 chest.lua rename doors_chest.lua => doors.lua (65%) diff --git a/chest.lua b/chest.lua new file mode 100644 index 0000000..d59ee55 --- /dev/null +++ b/chest.lua @@ -0,0 +1,253 @@ + +-- translation + +local S = minetest.get_translator("protector") +local F = minetest.formspec_escape + +-- MineClone support + +local mcl = minetest.get_modpath("mcl_core") +local mcf = minetest.get_modpath("mcl_formspec") + +-- Are crafts enabled? + +local protector_crafts = minetest.settings:get_bool("protector_crafts") ~= false + +-- Protected Chest + +local chest_size = mcl and (9 * 3) or (8 * 4) + +minetest.register_node("protector:chest", { + description = S("Protected Chest"), + tiles = { + "default_chest_top.png", "default_chest_top.png", + "default_chest_side.png", "default_chest_side.png", + "default_chest_side.png", "default_chest_front.png^protector_logo.png" + }, + paramtype2 = "facedir", + groups = {dig_immediate = 2, unbreakable = 1}, + legacy_facedir_simple = true, + is_ground_content = false, + sounds = default.node_sound_wood_defaults(), + + on_construct = function(pos) + + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + + meta:set_string("infotext", S("Protected Chest")) + meta:set_string("name", S("Protected Chest")) + inv:set_size("main", chest_size) + end, + + can_dig = function(pos,player) + + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + + if inv:is_empty("main") then + + if not minetest.is_protected(pos, player:get_player_name()) then + return true + end + end + end, + + on_metadata_inventory_put = function(pos, listname, index, stack, player) + + minetest.log("action", player:get_player_name() + .. " moves stuff to protected chest at " .. minetest.pos_to_string(pos)) + end, + + on_metadata_inventory_take = function(pos, listname, index, stack, player) + + minetest.log("action", player:get_player_name() + .. " takes stuff from protected chest at " .. minetest.pos_to_string(pos)) + end, + + on_metadata_inventory_move = function( + pos, from_list, from_index, to_list, to_index, count, player) + + minetest.log("action", player:get_player_name() + .. " moves stuff inside protected chest at " .. minetest.pos_to_string(pos)) + end, + + allow_metadata_inventory_put = function(pos, listname, index, stack, player) + + if minetest.is_protected(pos, player:get_player_name()) then + return 0 + end + + return stack:get_count() + end, + + allow_metadata_inventory_take = function(pos, listname, index, stack, player) + + if minetest.is_protected(pos, player:get_player_name()) then + return 0 + end + + return stack:get_count() + end, + + allow_metadata_inventory_move = function( + pos, from_list, from_index, to_list, to_index, count, player) + + if minetest.is_protected(pos, player:get_player_name()) then + return 0 + end + + return count + end, + + on_rightclick = function(pos, node, clicker) + + if minetest.is_protected(pos, clicker:get_player_name()) then return end + + local meta = minetest.get_meta(pos) ; if not meta then return end + + local spos = pos.x .. "," .. pos.y .. "," ..pos.z + local formspec + + -- mineclone support + if mcl and mcf then + + formspec = "size[9,8.75]" + .. "label[0,0;" .. minetest.formspec_escape( + minetest.colorize("#313131", "Protected Chest")) .. "]" + .. "list[nodemeta:" .. spos .. ";main;0,0.5;9,3;]" + .. mcl_formspec.get_itemslot_bg(0,0.5,9,3) + .. "image_button[3.0,3.5;1.05,0.8;protector_up_icon.png;protect_up;]" + .. "image_button[4.0,3.5;1.05,0.8;protector_down_icon.png;protect_down;]" + .. "label[0,4.0;" .. minetest.formspec_escape( + minetest.colorize("#313131", "Inventory")) .. "]" + .. "list[current_player;main;0,4.5;9,3;9]" + .. mcl_formspec.get_itemslot_bg(0,4.5,9,3) + .. "list[current_player;main;0,7.74;9,1;]" + .. mcl_formspec.get_itemslot_bg(0,7.74,9,1) + .. "listring[nodemeta:" .. spos .. ";main]" + .. "listring[current_player;main]" + + else -- default formspec + + formspec = "size[8,9]" + .. "list[nodemeta:".. spos .. ";main;0,0.3;8,4;]" + + .. "image_button[-0.01,4.26;1.05,0.8;protector_up_icon.png;protect_up;]" + .. "image_button[0.98,4.26;1.05,0.8;protector_down_icon.png;protect_down;]" + .. "tooltip[protect_up;" .. S("To Chest") .. "]" + .. "tooltip[protect_down;" .. S("To Inventory") .. "]" + + .. "field[2.3,4.8;4,0.25;protect_name;;" + .. meta:get_string("name") .. "]" + .. "button[5.99,4.5;2.05,0.25;protect_rename;" .. S("Rename") .. "]" + + .. "list[current_player;main;0,5;8,1;]" + .. "list[current_player;main;0,6.08;8,3;8]" + .. "listring[nodemeta:" .. spos .. ";main]" + .. "listring[current_player;main]" + end + + minetest.show_formspec(clicker:get_player_name(), + "protector:chest_" .. minetest.pos_to_string(pos), formspec) + end, + + on_blast = function() end +}) + +-- Container transfer helper + +local function to_from(src, dst) + + local stack, item, leftover + local size = dst:get_size("main") + + for i = 1, size do + + stack = src:get_stack("main", i) + item = stack:get_name() + + if item ~= "" and dst:room_for_item("main", item) then + + leftover = dst:add_item("main", stack) + + if leftover and not leftover:is_empty() then + src:set_stack("main", i, leftover) + else + src:set_stack("main", i, nil) + end + end + end +end + +-- Protected Chest formspec buttons + +minetest.register_on_player_receive_fields(function(player, formname, fields) + + if string.sub(formname, 0, 16) ~= "protector:chest_" then return end + + local pos_s = string.sub(formname, 17) + local pos = minetest.string_to_pos(pos_s) + + if minetest.is_protected(pos, player:get_player_name()) then return end + + local meta = minetest.get_meta(pos) ; if not meta then return end + local chest_inv = meta:get_inventory() ; if not chest_inv then return end + local player_inv = player:get_inventory() + + -- copy contents of player inventory to chest + if fields.protect_up then + + to_from(player_inv, chest_inv) + + -- copy contents of chest to player inventory + elseif fields.protect_down then + + to_from(chest_inv, player_inv) + + elseif fields.protect_name or fields.protect_rename then + + -- change chest infotext to display name + if fields.protect_name ~= "" then + + if fields.protect_name ~= string.match(fields.protect_name, "[%w%s_-]+") + or fields.protect_name:len() > 35 then + return + end + + meta:set_string("name", fields.protect_name) + meta:set_string("infotext", fields.protect_name) + else + meta:set_string("name", S("Protected Chest")) + meta:set_string("infotext", S("Protected Chest")) + end + + end +end) + +-- Protected Chest recipes + +if protector_crafts then + + if mcl then + + minetest.register_craft({ + output = "protector:chest", + recipe = { {"mcl_chests:chest", "mcl_core:gold_ingot"} } + }) + else + minetest.register_craft({ + output = "protector:chest", + recipe = { + {"group:wood", "group:wood", "group:wood"}, + {"group:wood", "default:copper_ingot", "group:wood"}, + {"group:wood", "group:wood", "group:wood"} + } + }) + + minetest.register_craft({ + output = "protector:chest", + recipe = { {"default:chest", "default:copper_ingot"} } + }) + end +end diff --git a/doors_chest.lua b/doors.lua similarity index 65% rename from doors_chest.lua rename to doors.lua index f07dc70..06330e7 100644 --- a/doors_chest.lua +++ b/doors.lua @@ -2,12 +2,10 @@ -- doors code from an old client re-used local S = minetest.get_translator("protector") -local F = minetest.formspec_escape -- MineClone support local mcl = minetest.get_modpath("mcl_core") -local mcf = minetest.get_modpath("mcl_formspec") -- Are crafts enabled? @@ -163,8 +161,8 @@ local function register_door(name, def) node_dig_prediction = "", drop = name, drawtype = "nodebox", - node_box = { type = "fixed", fixed = def.node_box_bottom }, - selection_box = { type = "fixed", fixed = def.selection_box_bottom }, + node_box = {type = "fixed", fixed = def.node_box_bottom}, + selection_box = {type = "fixed", fixed = def.selection_box_bottom}, groups = def.groups, _mcl_hardness = 0.8, _mcl_blast_resistance = 1, @@ -199,8 +197,8 @@ local function register_door(name, def) node_dig_prediction = "", drop = "", drawtype = "nodebox", - node_box = { type = "fixed", fixed = def.node_box_top }, - selection_box = { type = "fixed", fixed = def.selection_box_top }, + node_box = {type = "fixed", fixed = def.node_box_top}, + selection_box = {type = "fixed", fixed = def.selection_box_top}, groups = def.groups, _mcl_hardness = 0.8, _mcl_blast_resistance = 1, @@ -234,8 +232,8 @@ local function register_door(name, def) node_dig_prediction = "", drop = name, drawtype = "nodebox", - node_box = { type = "fixed", fixed = def.node_box_bottom }, - selection_box = { type = "fixed", fixed = def.selection_box_bottom }, + node_box = {type = "fixed", fixed = def.node_box_bottom}, + selection_box = {type = "fixed", fixed = def.selection_box_bottom}, groups = def.groups, _mcl_hardness = 0.8, _mcl_blast_resistance = 1, @@ -269,8 +267,8 @@ local function register_door(name, def) node_dig_prediction = "", drop = "", drawtype = "nodebox", - node_box = { type = "fixed", fixed = def.node_box_top }, - selection_box = { type = "fixed", fixed = def.selection_box_top }, + node_box = {type = "fixed", fixed = def.node_box_top}, + selection_box = {type = "fixed", fixed = def.selection_box_top}, groups = def.groups, _mcl_hardness = 0.8, _mcl_blast_resistance = 1, @@ -513,242 +511,3 @@ if protector_crafts then }) end end - --- Protected Chest - -local chest_size = mcl and (9 * 3) or (8 * 4) - -minetest.register_node("protector:chest", { - description = S("Protected Chest"), - tiles = { - "default_chest_top.png", "default_chest_top.png", - "default_chest_side.png", "default_chest_side.png", - "default_chest_side.png", "default_chest_front.png^protector_logo.png" - }, - paramtype2 = "facedir", - groups = {dig_immediate = 2, unbreakable = 1}, - legacy_facedir_simple = true, - is_ground_content = false, - sounds = default.node_sound_wood_defaults(), - - on_construct = function(pos) - - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - - meta:set_string("infotext", S("Protected Chest")) - meta:set_string("name", S("Protected Chest")) - inv:set_size("main", chest_size) - end, - - can_dig = function(pos,player) - - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - - if inv:is_empty("main") then - - if not minetest.is_protected(pos, player:get_player_name()) then - return true - end - end - end, - - on_metadata_inventory_put = function(pos, listname, index, stack, player) - - minetest.log("action", player:get_player_name() - .. " moves stuff to protected chest at " .. minetest.pos_to_string(pos)) - end, - - on_metadata_inventory_take = function(pos, listname, index, stack, player) - - minetest.log("action", player:get_player_name() - .. " takes stuff from protected chest at " .. minetest.pos_to_string(pos)) - end, - - on_metadata_inventory_move = function( - pos, from_list, from_index, to_list, to_index, count, player) - - minetest.log("action", player:get_player_name() - .. " moves stuff inside protected chest at " .. minetest.pos_to_string(pos)) - end, - - allow_metadata_inventory_put = function(pos, listname, index, stack, player) - - if minetest.is_protected(pos, player:get_player_name()) then - return 0 - end - - return stack:get_count() - end, - - allow_metadata_inventory_take = function(pos, listname, index, stack, player) - - if minetest.is_protected(pos, player:get_player_name()) then - return 0 - end - - return stack:get_count() - end, - - allow_metadata_inventory_move = function( - pos, from_list, from_index, to_list, to_index, count, player) - - if minetest.is_protected(pos, player:get_player_name()) then - return 0 - end - - return count - end, - - on_rightclick = function(pos, node, clicker) - - if minetest.is_protected(pos, clicker:get_player_name()) then return end - - local meta = minetest.get_meta(pos) ; if not meta then return end - - local spos = pos.x .. "," .. pos.y .. "," ..pos.z - local formspec - - -- mineclone support - if mcl and mcf then - - formspec = "size[9,8.75]" - .. "label[0,0;" .. minetest.formspec_escape( - minetest.colorize("#313131", "Protected Chest")) .. "]" - .. "list[nodemeta:" .. spos .. ";main;0,0.5;9,3;]" - .. mcl_formspec.get_itemslot_bg(0,0.5,9,3) - .. "image_button[3.0,3.5;1.05,0.8;protector_up_icon.png;protect_up;]" - .. "image_button[4.0,3.5;1.05,0.8;protector_down_icon.png;protect_down;]" - .. "label[0,4.0;" .. minetest.formspec_escape( - minetest.colorize("#313131", "Inventory")) .. "]" - .. "list[current_player;main;0,4.5;9,3;9]" - .. mcl_formspec.get_itemslot_bg(0,4.5,9,3) - .. "list[current_player;main;0,7.74;9,1;]" - .. mcl_formspec.get_itemslot_bg(0,7.74,9,1) - .. "listring[nodemeta:" .. spos .. ";main]" - .. "listring[current_player;main]" - - else -- default formspec - - formspec = "size[8,9]" - .. "list[nodemeta:".. spos .. ";main;0,0.3;8,4;]" - - .. "image_button[-0.01,4.26;1.05,0.8;protector_up_icon.png;protect_up;]" - .. "image_button[0.98,4.26;1.05,0.8;protector_down_icon.png;protect_down;]" - .. "tooltip[protect_up;" .. S("To Chest") .. "]" - .. "tooltip[protect_down;" .. S("To Inventory") .. "]" - - .. "field[2.3,4.8;4,0.25;protect_name;;" - .. meta:get_string("name") .. "]" - .. "button[5.99,4.5;2.05,0.25;protect_rename;" .. S("Rename") .. "]" - - .. "list[current_player;main;0,5;8,1;]" - .. "list[current_player;main;0,6.08;8,3;8]" - .. "listring[nodemeta:" .. spos .. ";main]" - .. "listring[current_player;main]" - end - - minetest.show_formspec(clicker:get_player_name(), - "protector:chest_" .. minetest.pos_to_string(pos), formspec) - end, - - on_blast = function() end -}) - --- Container transfer helper - -local function to_from(src, dst) - - local stack, item, leftover - local size = dst:get_size("main") - - for i = 1, size do - - stack = src:get_stack("main", i) - item = stack:get_name() - - if item ~= "" and dst:room_for_item("main", item) then - - leftover = dst:add_item("main", stack) - - if leftover and not leftover:is_empty() then - src:set_stack("main", i, leftover) - else - src:set_stack("main", i, nil) - end - end - end -end - --- Protected Chest formspec buttons - -minetest.register_on_player_receive_fields(function(player, formname, fields) - - if string.sub(formname, 0, 16) ~= "protector:chest_" then return end - - local pos_s = string.sub(formname, 17) - local pos = minetest.string_to_pos(pos_s) - - if minetest.is_protected(pos, player:get_player_name()) then return end - - local meta = minetest.get_meta(pos) ; if not meta then return end - local chest_inv = meta:get_inventory() ; if not chest_inv then return end - local player_inv = player:get_inventory() - - -- copy contents of player inventory to chest - if fields.protect_up then - - to_from(player_inv, chest_inv) - - -- copy contents of chest to player inventory - elseif fields.protect_down then - - to_from(chest_inv, player_inv) - - elseif fields.protect_name or fields.protect_rename then - - -- change chest infotext to display name - if fields.protect_name ~= "" then - - if fields.protect_name ~= string.match(fields.protect_name, "[%w%s_-]+") - or fields.protect_name:len() > 35 then - return - end - - meta:set_string("name", fields.protect_name) - meta:set_string("infotext", fields.protect_name) - else - meta:set_string("name", S("Protected Chest")) - meta:set_string("infotext", S("Protected Chest")) - end - - end -end) - --- Protected Chest recipes - -if protector_crafts then - - if mcl then - - minetest.register_craft({ - output = "protector:chest", - recipe = { {"mcl_chests:chest", "mcl_core:gold_ingot"} } - }) - else - minetest.register_craft({ - output = "protector:chest", - recipe = { - {"group:wood", "group:wood", "group:wood"}, - {"group:wood", "default:copper_ingot", "group:wood"}, - {"group:wood", "group:wood", "group:wood"} - } - }) - - minetest.register_craft({ - output = "protector:chest", - recipe = { {"default:chest", "default:copper_ingot"} } - }) - end -end diff --git a/init.lua b/init.lua index 077f11c..808342d 100644 --- a/init.lua +++ b/init.lua @@ -754,7 +754,8 @@ minetest.register_node("protector:display_node", { -- load mod sections -dofile(MP .. "/doors_chest.lua") +dofile(MP .. "/doors.lua") +dofile(MP .. "/chest.lua") dofile(MP .. "/pvp.lua") dofile(MP .. "/admin.lua") dofile(MP .. "/tool.lua") diff --git a/pvp.lua b/pvp.lua index 6860467..2af4bd7 100644 --- a/pvp.lua +++ b/pvp.lua @@ -61,11 +61,9 @@ and minetest.settings:get_bool("protector_pvp") then end return false - end) else print("[MOD] Protector - pvp_protect not active, update your version of Minetest") - end else print("[MOD] Protector - pvp_protect is disabled")