RealBadAngel
2013-08-04 3b32bf7907c20e226698a2a3af2807a7b1359661
commit | author | age
d5c554 1 --[[
M 2     Wrench mod
3         Adds a wrench that allows the player to pickup nodes that contain an inventory with items or metadata that needs perserving.
4         The wrench has the same tool capability as the normal hand.
5         To pickup a node simply right click on it. If the node contains a formspec, you will need to shift+right click instead.
6     supported_nodes
7         This table stores all nodes that are compatible with the wrench mod.
8         Syntax:
9             [<node name>] = {
10                 name = "wrench:<temporary node name>",
11                 lists = {"<inventory list name>"},
12                 metas = {{string="<meta name>"},{int="<meta name>"},{float="<meta name>"}},
13                 owner_protection[optional] = 1,
14                 store_meta_always[optional] = 1,
15             }
16             <temporary node name> - can be anything as long as it is unique
17             [optional] - parameters do not have to be included
18             owner_protection - nodes that are protected by owner requirements (Ex. locked chests)
19             store_meta_always - when nodes are broken this ensures metadata and inventory is always stored (Ex. active state for machines)
20 --]]
21c96a 21 local supported_nodes = {
d5c554 22 ["default:chest"] = {
M 23     name="wrench:default_chest",
24     lists={"main"},
25     metas={},
26 },
27 ["default:chest_locked"] = {
28     name="wrench:default_chest_locked",
29     lists={"main"},
30     metas={{string="owner"},{string="infotext"}},
31     owner_protection=1,
32 },
33 ["default:furnace"] = {
34     name="wrench:default_furnace",
35     lists={"fuel", "src", "dst"},
36     metas={{string="infotext"},{float="fuel_totaltime"},{float="fuel_time"},{float="src_totaltime"},{float="src_time"}},
37 },
38 ["default:furnace_active"] = {
39     name="wrench:default_furnace",
40     lists={"fuel", "src", "dst"},
41     metas={{string="infotext"},{float="fuel_totaltime"},{float="fuel_time"},{float="src_totaltime"},{float="src_time"}},
42     store_meta_always=1,
43 },
44 ["default:sign_wall"] = {
45     name="wrench:default_sing_wall",
46     lists={},
47     metas={{string="infotext"},{string="text"}},
48 },
49 ["technic:iron_chest"] = {
50     name="wrench:technic_iron_chest",
51     lists={"main"},
52     metas={},
53 },
54 ["technic:iron_locked_chest"] = {
55     name="wrench:technic_iron_locked_chest",
56     lists={"main"},
57     metas={{string="infotext"},{string="owner"}},
58     owner_protection=1,
59 },
60 ["technic:copper_chest"] = {
61     name="wrench:technic_copper_chest",
62     lists={"main"},
63     metas={},
64 },
65 ["technic:copper_locked_chest"] = {
66     name="wrench:technic_copper_locked_chest",
67     lists={"main"},
68     metas={{string="infotext"},{string="owner"}},
69     owner_protection=1,
70 },
71 ["technic:silver_chest"] = {
72     name="wrench:technic_silver_chest",
73     lists={"main"},
74     metas={{string="infotext"},{string="formspec"}},
75 },
76 ["technic:silver_locked_chest"] = {
77     name="wrench:technic_silver_locked_chest",
78     lists={"main"},
79     metas={{string="infotext"},{string="owner"},{string="formspec"}},
80     owner_protection=1,
81     },
82 ["technic:gold_chest"] = {
83     name="wrench:technic_gold_chest",
84     lists={"main"},
85     metas={{string="infotext"},{string="formspec"}},
86 },
87 ["technic:gold_locked_chest"] = {
88     name="wrench:technic_gold_locked_chest",
89     lists={"main"},
90     metas={{string="infotext"},{string="owner"},{string="formspec"}},
91     owner_protection=1,
92 },
93 ["technic:mithril_chest"] = {
94     name="wrench:technic_mithril_chest",
95     lists={"main"},
96     metas={{string="infotext"},{string="formspec"}},
97 },
98 ["technic:mithril_locked_chest"] = {
99     name="wrench:technic_mithril_locked_chest",
100     lists={"main"},
101     metas={{string="infotext"},{string="owner"},{string="formspec"}},
102     owner_protection=1,
103 },
104 ["technic:electric_furnace"] = {
105     name="wrench:technic_electric_furnace",
106     lists={"src", "dst"},
107     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"},{int="src_time"}},
108 },
109 ["technic:electric_furnace_active"] = {
110     name="wrench:technic_electric_furnace_active",
111     lists={"src", "dst"},
112     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"},{int="src_time"}},
113     store_meta_always=1,
114 },
115 ["technic:mv_electric_furnace"] = {
116     name="wrench:technic_mv_electric_furnace",
117     lists={"src", "dst", "upgrade1", "upgrade2"},
118     metas={{string="infotext"},{string="formspec"},{int="state"},{int="MV_EU_demand"},{int="MV_EU_input"},{int="tube_time"},{int="src_time"}},
119 },
120 ["technic:mv_electric_furnace_active"] = {
121     name="wrench:technic_mv_electric_furnace_active",
122     lists={"src", "dst", "upgrade1", "upgrade2"},
123     metas={{string="infotext"},{string="formspec"},{int="state"},{int="MV_EU_demand"},{int="MV_EU_input"},{int="tube_time"},{int="src_time"}},
124     store_meta_always=1,
125 },
126 ["technic:coal_alloy_furnace"] = {
127     name="wrench:technic_coal_alloy_furnace",
128     lists={"fuel", "src", "src2", "dst"},
129     metas={{string="infotext"},{float="fuel_totaltime"},{float="fuel_time"},{float="src_totaltime"},{float="src_time"}},
130 },
131 ["technic:coal_alloy_furnace_active"] = {
132     name="wrench:technic_coal_alloy_furnace_active",
133     lists={"fuel", "src", "src2", "dst"},
134     metas={{string="infotext"},{float="fuel_totaltime"},{float="fuel_time"},{float="src_totaltime"},{float="src_time"}},
135     store_meta_always=1,
136 },
137 ["technic:alloy_furnace"] = {
138     name="wrench:technic_alloy_furnace",
139     lists={"src", "src2", "dst"},
140     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"},{int="tube_time"},{int="src_time"}},
141 },
142 ["technic:alloy_furnace_active"] = {
143     name="wrench:technic_alloy_furnace_active",
144     lists={"src", "src2", "dst"},
145     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"},{int="tube_time"},{int="src_time"}},
146     store_meta_always=1,
147 },
148 ["technic:mv_alloy_furnace"] = {
149     name="wrench:technic_mv_alloy_furnace",
150     lists={"src", "src2", "dst", "upgrade1", "upgrade2"},
151     metas={{string="infotext"},{string="formspec"},{int="state"},{int="MV_EU_demand"},{int="MV_EU_input"},{int="tube_time"},{int="src_time"}},
152 },
153 ["technic:mv_alloy_furnace_active"] = {
154     name="wrench:technic_mv_alloy_furnace_active",
155     lists={"src", "src2", "dst", "upgrade1", "upgrade2"},
156     metas={{string="infotext"},{string="formspec"},{int="state"},{int="MV_EU_demand"},{int="MV_EU_input"},{int="tube_time"},{int="src_time"}},
157     store_meta_always=1,
158 },
159 ["technic:tool_workshop"] = {
160     name="wrench:technic_tool_workshop",
161     lists={"src"},
162     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"}},
163 },
164 ["technic:grinder"] = {
165     name="wrench:technic_grinder",
166     lists={"src", "dst"},
167     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"},{int="src_time"}},
168 },
169 ["technic:grinder_active"] = {
170     name="wrench:technic_grinder_active",
171     lists={"src", "dst"},
172     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"},{int="src_time"}},
173     store_meta_always=1,
174 },
175 ["technic:mv_grinder"] = {
176     name="wrench:technic_mv_grinder",
177     lists={"src", "dst", "upgrade1", "upgrade2"},
178     metas={{string="infotext"},{string="formspec"},{int="state"},{int="MV_EU_demand"},{int="MV_EU_input"},{int="tube_time"},{int="src_time"}},
179 },
180 ["technic:mv_grinder_active"] = {
181     name="wrench:technic_mv_grinder_active",
182     lists={"src", "dst", "upgrade1", "upgrade2"},
183     metas={{string="infotext"},{string="formspec"},{int="state"},{int="MV_EU_demand"},{int="MV_EU_input"},{int="tube_time"},{int="src_time"}},
184     store_meta_always=1,
185 },
186 ["technic:extractor"] = {
187     name="wrench:technic_extractor",
188     lists={"src", "dst"},
189     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"},{int="src_time"}},
190 },
191 ["technic:extractor_active"] = {
192     name="wrench:technic_extractor_active",
193     lists={"src", "dst"},
194     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"},{int="src_time"}},
195     store_meta_always=1,
196 },
197 ["technic:compressor"] = {
198     name="wrench:technic_compressor",
199     lists={"src", "dst"},
200     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"},{int="src_time"}},
201 },
202 ["technic:compressor_active"] = {
203     name="wrench:technic_compressor_active",
204     lists={"src", "dst"},
205     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"},{int="src_time"}},
206     store_meta_always=1,
207 },
208 ["technic:cnc"] = {
209     name="wrench:technic_cnc",
210     lists={"src", "dst"},
211     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"},{int="src_time"},{string="cnc_product"}},
212 },
213 ["technic:cnc_active"] = {
214     name="wrench:technic_cnc_active",
215     lists={"src", "dst"},
216     metas={{string="infotext"},{string="formspec"},{int="state"},{int="LV_EU_demand"},{int="LV_EU_input"},{int="src_time"},{string="cnc_product"}},
217     store_meta_always=1,
218 },
21c96a 219 }
M 220 local chest_mark_colors = {
221     {'_black','Black'},
222     {'_blue','Blue'}, 
223     {'_brown','Brown'},
224     {'_cyan','Cyan'},
225     {'_dark_green','Dark Green'},
226     {'_dark_grey','Dark Grey'},
227     {'_green','Green'},
228     {'_grey','Grey'},
229     {'_magenta','Magenta'},
230     {'_orange','Orange'},
231     {'_pink','Pink'},
232     {'_red','Red'},
233     {'_violet','Violet'},
234     {'_white','White'},
235     {'_yellow','Yellow'},
236     {'','None'}
237 }
238 for i=1,15,1 do
d5c554 239     supported_nodes["technic:gold_chest"..chest_mark_colors[i][1]] = {
M 240         name="wrench:technic_gold_chest"..chest_mark_colors[i][1],
241         lists={"main"},
242         metas={{string="infotext"},{string="formspec"}},
243     }
244     supported_nodes["technic:gold_locked_chest"..chest_mark_colors[i][1]] = {
245         name="wrench:technic_gold_locked_chest"..chest_mark_colors[i][1],
246         lists={"main"},
247         metas={{string="infotext"},{string="owner"},{string="formspec"}},
248         owner_protection=1,
249     }
250 end
251 for i=0,8,1 do
252     if i==0 then i="" end
253     supported_nodes["technic:battery_box"..i] = {
254         name="wrench:technic_battery_box"..i,
255         lists={"src", "dst"},
256         metas={{string="infotext"},{string="formspec"},{int="LV_EU_demand"},{int="LV_EU_supply"},{int="LV_EU_input"},{int="internal_EU_charge"},{float="last_side_shown"}},
257         store_meta_always=1,
258     }
259     supported_nodes["technic:mv_battery_box"..i] = {
260         name="wrench:technic_mv_battery_box"..i,
261         lists={"src", "dst"},
262         metas={{string="infotext"},{string="formspec"},{int="MV_EU_demand"},{int="MV_EU_supply"},{int="MV_EU_input"},{int="internal_EU_charge"},{float="last_side_shown"}},
263         store_meta_always=1,
264     }
265     supported_nodes["technic:hv_battery_box"..i] = {
266         name="wrench:technic_hv_battery_box"..i,
267         lists={"src", "dst"},
268         metas={{string="infotext"},{string="formspec"},{int="HV_EU_demand"},{int="HV_EU_supply"},{int="HV_EU_input"},{int="internal_EU_charge"},{float="last_side_shown"}},
269         store_meta_always=1,
270     }
21c96a 271 end
M 272
273 local function convert_to_original_name(name)
274     for key,value in pairs(supported_nodes) do
275         if name == value.name then return key end
276     end
277 end
278
3b7695 279 for name,info in pairs(supported_nodes) do
21c96a 280     local olddef = minetest.registered_nodes[name]
M 281     if olddef ~= nil then
282         local newdef = {}
283         for key,value in pairs(olddef) do
284             newdef[key] = value
285         end
286         newdef.stack_max = 1
287         newdef.description = newdef.description.." with items"
3b7695 288         newdef.groups = {}
21c96a 289         newdef.groups.not_in_creative_inventory = 1
M 290         newdef.on_construct = nil
291         newdef.on_destruct = nil
292         newdef.after_place_node = function(pos, placer, itemstack)
d5c554 293             minetest.set_node(pos, {name = convert_to_original_name(itemstack:get_name()),
M 294                                                 param2 = minetest.get_node(pos).param2})
3b7695 295                 local meta = minetest.get_meta(pos)
M 296                 local inv = meta:get_inventory()
3b32bf 297                 local item_meta =itemstack:to_table()
R 298                 local data = minetest.deserialize(item_meta["metadata"])
3b7695 299                 local lists = data.lists
M 300                 for listname,list in pairs(lists) do
301                     inv:set_list(listname, list)
302                 end
303                 local metas = data.metas
d5c554 304                 local temp = nil
3b7695 305                 for i=1,#metas,1 do
d5c554 306                     temp = metas[i]
3b7695 307                     if temp.string ~= nil then
M 308                         meta:set_string(temp.string, temp.value)
309                     end
310                     if temp.int ~= nil then
311                         meta:set_int(temp.int, temp.value)
312                     end
313                     if temp.float ~= nil then
314                         meta:set_float(temp.float, temp.value)
315                     end
316                 end
21c96a 317         end
3b7695 318         minetest.register_node(info.name, newdef)
21c96a 319     end
M 320 end
321
322 minetest.register_tool("wrench:wrench", {
323     description = "Wrench",
324     inventory_image = "technic_wrench.png",
325     tool_capabilities = {
326         full_punch_interval = 0.9,
327         max_drop_level = 0,
328         groupcaps = {
329             crumbly = {times={[2]=3.00, [3]=0.70}, uses=0, maxlevel=1},
330             snappy = {times={[3]=0.40}, uses=0, maxlevel=1},
331             oddly_breakable_by_hand = {times={[1]=7.00,[2]=4.00,[3]=1.40}, uses=0, maxlevel=3}
332         },
333         damage_groups = {fleshy=1},
334     },
335     on_place = function(itemstack, placer, pointed_thing)
336         if not placer:is_player() then return end
337         local pos = pointed_thing.under
338         if pos == nil then return end
339         local name = minetest.get_node(pos).name
340         local support = supported_nodes[name]
341         if support == nil then return end
3b7695 342         local meta = minetest.get_meta(pos)
d5c554 343         if support.owner_protection ~= nil then
M 344             local owner = meta:get_string("owner")
345             if owner ~= nil then
346                 if owner ~= placer:get_player_name() then
3b7695 347                     minetest.log("action", placer:get_player_name()..
M 348                     " tried to destroy a locked chest belonging to "..
d5c554 349                     owner.." at "..
3b7695 350                     minetest.pos_to_string(pos))
M 351                     return
352                 end
21c96a 353             end
M 354         end
3b7695 355         
21c96a 356         local lists = support.lists
3b7695 357         local inv = meta:get_inventory()
21c96a 358         local empty = true
3b7695 359         local metadata_str = {}
21c96a 360         local list_str = {}
M 361         for i=1,#lists,1 do
362             if not inv:is_empty(lists[i]) then empty = false end
363             local list = inv:get_list(lists[i])
364             for j=1,#list,1 do
365                 list[j] = list[j]:to_string()
366             end
367             list_str[lists[i]] = list
368         end
3b7695 369         metadata_str.lists = list_str
M 370         
371         local metas = support.metas
372         local meta_str = {}
373         for i=1,#metas,1 do
374             local temp = metas[i]
375             if temp.string ~= nil then
376                 meta_str[i] = {string = temp.string, value = meta:get_string(temp.string)}
377             end
378             if temp.int ~= nil then
379                 meta_str[i] = {int = temp.int, value = meta:get_int(temp.int)}
380             end
381             if temp.float ~= nil then
382                 meta_str[i] = {float = temp.float, value = meta:get_float(temp.float)}
383             end
384         end
385         metadata_str.metas = meta_str
386         
21c96a 387         inv = placer:get_inventory()
3b7695 388         local stack = {name = name}
21c96a 389         if inv:room_for_item("main", stack) then
M 390             minetest.remove_node(pos)
391             itemstack:add_wear(65535/20)
d5c554 392             if empty and #lists > 0 and support.store_meta_always == nil then
21c96a 393                 inv:add_item("main", stack)
M 394             else
395                 stack.name = supported_nodes[name].name
3b7695 396                 stack.metadata = minetest.serialize(metadata_str)
21c96a 397                 inv:add_item("main", stack)
M 398             end
399         end
400         return itemstack
401     end,
402 })
403
404 minetest.register_craft({
405     output = "wrench:wrench",
406     recipe = {
d5c554 407     {"default:steel_ingot","","default:steel_ingot"},
M 408     {"","default:steel_ingot",""},
409     {"","default:steel_ingot",""},
21c96a 410     },
3b32bf 411 })