Added chest management buttons and ability to name chest
authortenplus1 <kinsellaja@yahoo.com>
Fri, 13 Feb 2015 19:10:29 +0000 (19:10 +0000)
committertenplus1 <kinsellaja@yahoo.com>
Fri, 13 Feb 2015 19:10:29 +0000 (19:10 +0000)
README.md
init.lua

index 21566cd..b731cf8 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,9 +1,15 @@
-minetest-protect
-================
+Protector Redo mod [protect]
 
-Protector mod for minetest
-based on glomie's mod, remade by Zeg9 and reworked by TenPlus1 to support minetest 0.4.9 and include protected doors and chests
+Protector redo mod for minetest is based on glomie's mod, remade by Zeg9 and reworked by TenPlus1 to support minetest 0.4.9 and include protected doors and chests
 
 https://forum.minetest.net/viewtopic.php?f=11&t=9376
 
-Released under WTFPL
\ No newline at end of file
+Released under WTFPL
+
+0.1 - Initial release
+0.2 - Texture update
+0.3 - Added Protection Logo to blend in with player builds
+0.4 - Code tweak for 0.4.10+
+0.5 - Added protector.radius variable in init.lua (default: 5)
+0.6 - Added Protected Doors (wood and steel) and Protected Chest
+0.7 - Protected Chests now have "To Chest" and "To Inventory" buttons to copy contents across, also chests can be named
index d180f6f..8876546 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -44,18 +44,17 @@ end
 -- Protector Interface
 
 protector.generate_formspec = function(meta)
-       if meta:get_int("page") == nil then meta:set_int("page",0) end
-       local formspec = "size[8,7]"..default.gui_bg..default.gui_bg_img..default.gui_slots -- Added new formspec defaults
+
+       local formspec = "size[8,7]"..default.gui_bg..default.gui_bg_img..default.gui_slots
                .."label[2.5,0;-- Protector interface --]"
-               .."label[0,1;Punch node to show protected area]"
-               .."label[0,2;Members: (type nick, press Enter to add)]"
+               .."label[0,1;PUNCH node to show protected area or USE for area check]"
+               .."label[0,2;Members: (type player name then press Enter to add)]"
+
        local members = protector.get_member_list(meta)
-       
        local npp = 12
-       local s = 0
        local i = 0
+
        for _, member in ipairs(members) do
-               if s < meta:get_int("page")*15 then s = s +1 else
                        if i < npp then
                                formspec = formspec .. "button["..(i%4*2)..","
                                ..math.floor(i/4+3)..";1.5,.5;protector_member;"..member.."]"
@@ -63,14 +62,15 @@ protector.generate_formspec = function(meta)
                                ..math.floor(i/4+3)..";.75,.5;protector_del_member_"..member..";X]"
                        end
                        i = i +1
-               end
        end
-       local add_i = i
-       if add_i < npp then
+       
+       if i < npp then
                formspec = formspec
-               .."field["..(add_i%4*2+1/3)..","..(math.floor(add_i/4+3)+1/3)..";1.433,.5;protector_add_member;;]"
+               .."field["..(i%4*2+1/3)..","..(math.floor(i/4+3)+1/3)..";1.433,.5;protector_add_member;;]"
        end
-                               formspec = formspec.."button_exit[1,6.2;2,0.5;close_me;<< Back]"
+
+       formspec = formspec.."button_exit[2.5,6.2;3,0.5;close_me;Close]"
+
        return formspec
 end
 
@@ -189,8 +189,7 @@ minetest.register_node("protector:protect", {
        after_place_node = function(pos, placer)
                local meta = minetest.env:get_meta(pos)
                meta:set_string("owner", placer:get_player_name() or "")
-               meta:set_string("infotext", "Protection (owned by "..
-               meta:get_string("owner")..")")
+               meta:set_string("infotext", "Protection (owned by "..meta:get_string("owner")..")")
                meta:set_string("members", "")
        end,
 
@@ -206,8 +205,7 @@ minetest.register_node("protector:protect", {
                local meta = minetest.env:get_meta(pos)
                if protector.can_dig(1,pos,clicker:get_player_name(),true,1) then
                        minetest.show_formspec(clicker:get_player_name(), 
-                       "protector_"..minetest.pos_to_string(pos), protector.generate_formspec(meta)
-                       )
+                       "protector:node_"..minetest.pos_to_string(pos), protector.generate_formspec(meta))
                end
        end,
 
@@ -265,8 +263,7 @@ minetest.register_node("protector:protect2", {
        after_place_node = function(pos, placer)
                local meta = minetest.env:get_meta(pos)
                meta:set_string("owner", placer:get_player_name() or "")
-               meta:set_string("infotext", "Protection (owned by "..
-               meta:get_string("owner")..")")
+               meta:set_string("infotext", "Protection (owned by "..meta:get_string("owner")..")")
                meta:set_string("members", "")
        end,
 
@@ -282,8 +279,7 @@ minetest.register_node("protector:protect2", {
                local meta = minetest.env:get_meta(pos)
                if protector.can_dig(1,pos,clicker:get_player_name(),true,1) then
                        minetest.show_formspec(clicker:get_player_name(), 
-                       "protector_"..minetest.pos_to_string(pos), protector.generate_formspec(meta)
-                       )
+                       "protector:node_"..minetest.pos_to_string(pos), protector.generate_formspec(meta))
                end
        end,
 
@@ -315,16 +311,15 @@ minetest.register_craft({
        }
 })
 
--- If name entered into protector formspec
-
+-- If name entered or button press
 minetest.register_on_player_receive_fields(function(player,formname,fields)
-       if string.sub(formname,0,string.len("protector_")) == "protector_" then
-               local pos_s = string.sub(formname,string.len("protector_")+1)
+
+       if string.sub(formname,0,string.len("protector:node_")) == "protector:node_" then
+
+               local pos_s = string.sub(formname,string.len("protector:node_")+1)
                local pos = minetest.string_to_pos(pos_s)
                local meta = minetest.env:get_meta(pos)
 
-               if meta:get_int("page") == nil then meta:set_int("page",0) end
-
                if not protector.can_dig(1,pos,player:get_player_name(),true,1) then
                        return
                end
@@ -340,12 +335,13 @@ minetest.register_on_player_receive_fields(function(player,formname,fields)
                                protector.del_member(meta, string.sub(field,string.len("protector_del_member_")+1))
                        end
                end
-
-               if fields.close_me then
-                       meta:set_int("page",meta:get_int("page"))
-                       else minetest.show_formspec(player:get_player_name(), formname, protector.generate_formspec(meta))
+               
+               if not fields.close_me then
+                       minetest.show_formspec(player:get_player_name(), formname, protector.generate_formspec(meta))
                end
+
        end
+
 end)
 
 minetest.register_entity("protector:display", {
@@ -555,13 +551,17 @@ local function get_locked_chest_formspec(pos)
                default.gui_bg_img..
                default.gui_slots..
                "list[nodemeta:".. spos .. ";main;0,0.3;8,4;]"..
-               "list[current_player;main;0,4.85;8,1;]"..
+               "button[0,4.5;2,0.25;toup;To Chest]"..
+               "field[2.3,4.8;4,0.25;chestname;;]"..
+               "button[6,4.5;2,0.25;todn;To Inventory]"..
+               "list[current_player;main;0,5;8,1;]"..
                "list[current_player;main;0,6.08;8,3;8]"..
-               default.get_hotbar_bg(0,4.85)
+               default.get_hotbar_bg(0,5)
  return formspec
 end
 
 -- Protected Chest
+
 minetest.register_node("protector:chest", {
        description = "Protected Chest",
        tiles = {"default_chest_top.png", "default_chest_top.png", "default_chest_side.png",
@@ -608,13 +608,68 @@ minetest.register_node("protector:chest", {
                if not minetest.is_protected(pos, clicker:get_player_name()) then
                        minetest.show_formspec(
                                clicker:get_player_name(),
-                               "default:chest_locked",
+                               "protector:chest_"..minetest.pos_to_string(pos),
                                get_locked_chest_formspec(pos)
                        )
                end
        end,
 })
 
+-- Proteted Chest formspec buttons
+
+minetest.register_on_player_receive_fields(function(player,formname,fields)
+
+       if string.sub(formname,0,string.len("protector:chest_")) == "protector:chest_" then
+
+               local pos_s = string.sub(formname,string.len("protector:chest_")+1)
+               local pos = minetest.string_to_pos(pos_s)
+               local meta = minetest.env:get_meta(pos)
+
+               local chest_inv = meta:get_inventory()
+               local player_inv = player:get_inventory()
+
+               if fields.toup then
+
+                       -- copy contents of players inventory to chest
+                       for i,v in ipairs( player_inv:get_list( "main" ) or {}) do
+                               if( chest_inv and chest_inv:room_for_item('main', v)) then
+                                       local leftover = chest_inv:add_item( 'main', v )
+                                       player_inv:remove_item( "main", v )
+                                       if( leftover and not( leftover:is_empty() )) then
+                                               player_inv:add_item( "main", v )
+                                       end
+                               end
+                       end
+       
+               elseif fields.todn then
+
+                       -- copy contents of chest to players inventory
+                       for i,v in ipairs( chest_inv:get_list( 'main' ) or {}) do
+                               if( player_inv:room_for_item( "main", v)) then
+                                       local leftover = player_inv:add_item( "main", v )
+                                       chest_inv:remove_item( 'main', v )
+                                       if( leftover and not( leftover:is_empty() )) then
+                                               chest_inv:add_item( 'main', v )
+                                       end
+                               end
+                       end
+
+               elseif fields.chestname then
+
+                       -- change chest infotext to display name
+                       if fields.chestname ~= "" then
+                               meta:set_string("infotext", "Protected Chest ("..fields.chestname..")")
+                       else
+                               meta:set_string("infotext", "Protected Chest")
+                       end
+
+               end
+       end
+
+end)
+
+-- Protected Chest recipe
+
 minetest.register_craft({
        output = 'protector:chest',
        recipe = {
@@ -623,3 +678,10 @@ minetest.register_craft({
                {'group:wood', 'group:wood', 'group:wood'},
        }
 })
+
+minetest.register_craft({
+       output = 'protector:chest',
+       recipe = {
+               {'default:chest', 'default:copper_ingot', ''},
+       }
+})