metalstache
2013-08-04 d5c55415014e53ba0eaeb48729ec7a2b27ff8a3b
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             minetest.after(0.5, function(pos, placer, itemstack)
M 296                 local meta = minetest.get_meta(pos)
297                 local inv = meta:get_inventory()
298                 local data = minetest.deserialize(itemstack:get_metadata())
299                 local lists = data.lists
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
317             end, pos, placer, itemstack)
21c96a 318         end
3b7695 319         minetest.register_node(info.name, newdef)
21c96a 320     end
M 321 end
322
323 minetest.register_tool("wrench:wrench", {
324     description = "Wrench",
325     inventory_image = "technic_wrench.png",
326     tool_capabilities = {
327         full_punch_interval = 0.9,
328         max_drop_level = 0,
329         groupcaps = {
330             crumbly = {times={[2]=3.00, [3]=0.70}, uses=0, maxlevel=1},
331             snappy = {times={[3]=0.40}, uses=0, maxlevel=1},
332             oddly_breakable_by_hand = {times={[1]=7.00,[2]=4.00,[3]=1.40}, uses=0, maxlevel=3}
333         },
334         damage_groups = {fleshy=1},
335     },
336     on_place = function(itemstack, placer, pointed_thing)
337         if not placer:is_player() then return end
338         local pos = pointed_thing.under
339         if pos == nil then return end
340         local name = minetest.get_node(pos).name
341         local support = supported_nodes[name]
342         if support == nil then return end
3b7695 343         local meta = minetest.get_meta(pos)
d5c554 344         if support.owner_protection ~= nil then
M 345             local owner = meta:get_string("owner")
346             if owner ~= nil then
347                 if owner ~= placer:get_player_name() then
3b7695 348                     minetest.log("action", placer:get_player_name()..
M 349                     " tried to destroy a locked chest belonging to "..
d5c554 350                     owner.." at "..
3b7695 351                     minetest.pos_to_string(pos))
M 352                     return
353                 end
21c96a 354             end
M 355         end
3b7695 356         
21c96a 357         local lists = support.lists
3b7695 358         local inv = meta:get_inventory()
21c96a 359         local empty = true
3b7695 360         local metadata_str = {}
21c96a 361         local list_str = {}
M 362         for i=1,#lists,1 do
363             if not inv:is_empty(lists[i]) then empty = false end
364             local list = inv:get_list(lists[i])
365             for j=1,#list,1 do
366                 list[j] = list[j]:to_string()
367             end
368             list_str[lists[i]] = list
369         end
3b7695 370         metadata_str.lists = list_str
M 371         
372         local metas = support.metas
373         local meta_str = {}
374         for i=1,#metas,1 do
375             local temp = metas[i]
376             if temp.string ~= nil then
377                 meta_str[i] = {string = temp.string, value = meta:get_string(temp.string)}
378             end
379             if temp.int ~= nil then
380                 meta_str[i] = {int = temp.int, value = meta:get_int(temp.int)}
381             end
382             if temp.float ~= nil then
383                 meta_str[i] = {float = temp.float, value = meta:get_float(temp.float)}
384             end
385         end
386         metadata_str.metas = meta_str
387         
21c96a 388         inv = placer:get_inventory()
3b7695 389         local stack = {name = name}
21c96a 390         if inv:room_for_item("main", stack) then
M 391             minetest.remove_node(pos)
392             itemstack:add_wear(65535/20)
d5c554 393             if empty and #lists > 0 and support.store_meta_always == nil then
21c96a 394                 inv:add_item("main", stack)
M 395             else
396                 stack.name = supported_nodes[name].name
3b7695 397                 stack.metadata = minetest.serialize(metadata_str)
21c96a 398                 inv:add_item("main", stack)
M 399             end
400         end
401         return itemstack
402     end,
403 })
404
405 minetest.register_craft({
406     output = "wrench:wrench",
407     recipe = {
d5c554 408     {"default:steel_ingot","","default:steel_ingot"},
M 409     {"","default:steel_ingot",""},
410     {"","default:steel_ingot",""},
21c96a 411     },
M 412 })