]> git.lizzy.rs Git - Crafter.git/blob - mods/armor/init.lua
Add in prototype of armor
[Crafter.git] / mods / armor / init.lua
1 function recalculate_armor(player)
2     if not player or (player and not player:is_player()) then return end
3     local inv = player:get_inventory()
4     local meta = player:get_meta()
5     local player_skin = meta:get_string("skin")
6
7     local stack = inv:get_stack("armor_head",1):get_name()
8     stack = stack:gsub("_item.png","")
9     stack = stack:gsub("armor:","")
10     if stack ~= "" then
11         player_skin = player_skin.."^"..stack..".png"
12     end
13
14     stack = inv:get_stack("armor_torso",1):get_name()
15     stack = stack:gsub("_item.png","")
16     stack = stack:gsub("armor:","")
17     if stack ~= "" then
18         player_skin = player_skin.."^"..stack..".png"
19     end
20
21     stack = inv:get_stack("armor_legs",1):get_name()
22     stack = stack:gsub("_item.png","")
23     stack = stack:gsub("armor:","")
24     if stack ~= "" then
25         player_skin = player_skin.."^"..stack..".png"
26     end
27
28     stack = inv:get_stack("armor_feet",1):get_name()
29     stack = stack:gsub("_item.png","")
30     stack = stack:gsub("armor:","")
31     if stack ~= "" then
32         player_skin = player_skin.."^"..stack..".png"
33     end
34     player:set_properties({textures = {player_skin}})
35 end
36
37
38 minetest.register_on_joinplayer(function(player)
39     local inv = player:get_inventory()
40
41     inv:set_size("armor_head" ,1)
42     inv:set_size("armor_torso",1)
43     inv:set_size("armor_legs" ,1)
44     inv:set_size("armor_feet" ,1)
45
46     minetest.after(0.1,function()
47         recalculate_armor(player)
48     end)
49 end)
50
51 minetest.register_on_player_inventory_action(function(player, action, inventory, inventory_info)
52     if inventory_info.from_list == "armor_head" or inventory_info.from_list == "armor_torso" or inventory_info.from_list == "armor_legs" or inventory_info.from_list == "armor_feet" or
53        inventory_info.to_list   == "armor_head" or inventory_info.to_list   == "armor_torso" or inventory_info.to_list   == "armor_legs" or inventory_info.to_list   == "armor_feet" then
54         minetest.after(0,function()
55             recalculate_armor(player)
56         end)
57     end
58 end)
59
60 --only allow players to put armor in the right slots to stop exploiting chestplates
61 minetest.register_allow_player_inventory_action(function(player, action, inventory, inventory_info)
62     if inventory_info.to_list == "armor_head" then
63         local stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
64         local item = stack:get_name()
65         if minetest.get_item_group(item, "helmet") == 0 then
66             return(0)
67         end
68     elseif inventory_info.to_list == "armor_torso" then
69         local stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
70         local item = stack:get_name()
71         if minetest.get_item_group(item, "chestplate") == 0 then
72             return(0)
73         end
74     elseif inventory_info.to_list == "armor_legs" then
75         local stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
76         local item = stack:get_name()
77         if minetest.get_item_group(item, "leggings") == 0 then
78             return(0)
79         end
80     elseif inventory_info.to_list == "armor_feet" then
81         local stack = inventory:get_stack(inventory_info.from_list,inventory_info.from_index)
82         local item = stack:get_name()
83         if minetest.get_item_group(item, "boots") == 0 then
84             return(0)
85         end
86     end
87 end)
88
89 local armor_type = {["helmet"]=2,["chestplate"]=5,["leggings"]=3,["boots"]=2}
90 local materials = {["iron"]=2,["chain"]=4,["gold"]=3,["diamond"]=7}
91
92 local function bool_int(state)
93     if state == true then return(1) end
94     if state == false or not state then return(0) end
95 end
96
97 for material_id,material in pairs(materials) do
98     for armor_id,armor in pairs(armor_type) do
99         print(material_id,material,"|",armor_id,armor)
100         minetest.register_tool("armor:"..material_id.."_"..armor_id,{
101             description = material_id:gsub("^%l", string.upper).." "..armor_id:gsub("^%l", string.upper),
102     
103             groups = {
104                 armor         = 1,
105                 armor_level   = material,
106                 armor_defense = armor,
107                 helmet        = bool_int(armor_id == "helmet"),
108                 chestplate    = bool_int(armor_id == "chestplate"),
109                 leggings      = bool_int(armor_id == "leggings"),
110                 boots         = bool_int(armor_id == "boots"),
111             },
112             inventory_image = material_id.."_"..armor_id.."_item.png",
113             stack_max = 1,
114             tool_capabilities = {
115                 full_punch_interval = 0,
116                 max_drop_level = 0,
117                 groupcaps = {
118                 },
119                 damage_groups = {
120
121                 },
122                 punch_attack_uses = 0,
123             }
124         })
125
126         if armor_id == "helmet" then
127             minetest.register_craft({
128                 output = "armor:"..material_id.."_"..armor_id,
129                 recipe = {
130                     {"main:"..material_id, "main:"..material_id, "main:"..material_id},
131                     {"main:"..material_id, ""                  , "main:"..material_id},
132                     {""                  , ""                  , ""                  }
133                 }
134             })
135         elseif armor_id == "chestplate" then
136             minetest.register_craft({
137                 output = "armor:"..material_id.."_"..armor_id,
138                 recipe = {
139                     {"main:"..material_id, ""                  , "main:"..material_id},
140                     {"main:"..material_id, "main:"..material_id, "main:"..material_id},
141                     {"main:"..material_id, "main:"..material_id, "main:"..material_id}
142                 }
143             })
144         elseif armor_id == "leggings" then
145             minetest.register_craft({
146                 output = "armor:"..material_id.."_"..armor_id,
147                 recipe = {
148                     {"main:"..material_id, "main:"..material_id, "main:"..material_id},
149                     {"main:"..material_id, ""                  , "main:"..material_id},
150                     {"main:"..material_id, ""                  , "main:"..material_id}
151                 }
152             })
153         elseif armor_id == "boots" then
154             minetest.register_craft({
155                 output = "armor:"..material_id.."_"..armor_id,
156                 recipe = {
157                     {""                  , "", ""                  },
158                     {"main:"..material_id, "", "main:"..material_id},
159                     {"main:"..material_id, "", "main:"..material_id}
160                 }
161             })
162         end
163         
164     end
165 end