1 local function log(level, message, ...)
2 minetest.log(level, '[xdecor] ' .. message:format(...))
7 screwdriver = screwdriver or {}
8 local min, ceil = math.min, math.ceil
9 local registered_nodes = minetest.registered_nodes
11 -- Nodes allowed to be cut
12 -- Only the regular, solid blocks without metas or explosivity can be cut
14 for node, def in pairs(registered_nodes) do
15 if xdecor.stairs_valid_def(def) then
16 nodes[#nodes + 1] = node
20 -- Optionally, you can register custom cuttable nodes in the workbench
21 WB.custom_nodes_register = {
26 __concat = function(t1, t2)
35 nodes = nodes .. WB.custom_nodes_register
37 -- Nodeboxes definitions
39 -- Name YieldX YZ WH L
40 {"nanoslab", 16, { 0, 0, 0, 8, 1, 8 }},
41 {"micropanel", 16, { 0, 0, 0, 16, 1, 8 }},
42 {"microslab", 8, { 0, 0, 0, 16, 1, 16 }},
43 {"thinstair", 8, { 0, 7, 0, 16, 1, 8 },
44 { 0, 15, 8, 16, 1, 8 }},
45 {"cube", 4, { 0, 0, 0, 8, 8, 8 }},
46 {"panel", 4, { 0, 0, 0, 16, 8, 8 }},
48 {"doublepanel", 2, { 0, 0, 0, 16, 8, 8 },
49 { 0, 8, 8, 16, 8, 8 }},
50 {"halfstair", 2, { 0, 0, 0, 8, 8, 16 },
51 { 0, 8, 8, 8, 8, 8 }},
52 {"stair_outer", 1, nil },
54 {"stair_inner", 1, nil }
57 local repairable_tools = {"pick", "axe", "shovel", "sword", "hoe", "armor", "shield"}
59 -- Tools allowed to be repaired
60 function workbench:repairable(stack)
61 for _, t in ipairs(repairable_tools) do
68 function workbench:get_output(inv, input, name)
70 for i = 1, #self.defs do
71 local nbox = self.defs[i]
72 local count = min(nbox[2] * input:get_count(), input:get_stack_max())
73 local item = name .. "_" .. nbox[1]
75 item = nbox[3] and item or "stairs:" .. nbox[1] .. "_" .. name:match(":(.*)")
76 output[i] = item .. " " .. count
79 inv:set_list("forms", output)
84 label[0.9,2.23;Repair]
85 box[-0.05,1;2.05,0.9;#555555]
86 box[-0.05,2;2.05,0.9;#555555]
87 button[0,0;2,1;craft;Crafting]
88 button[2,0;2,1;storage;Storage]
89 image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
90 image[0,1;1,1;worktable_saw.png]
91 image[0,2;1,1;worktable_anvil.png]
92 image[3,2;1,1;hammer_layout.png]
93 list[context;input;2,1;1,1;]
94 list[context;tool;2,2;1,1;]
95 list[context;hammer;3,2;1,1;]
96 list[context;forms;4,0;4,3;]
97 listring[current_player;main]
98 listring[context;tool]
99 listring[current_player;main]
100 listring[context;hammer]
101 listring[current_player;main]
102 listring[context;forms]
103 listring[current_player;main]
104 listring[context;input]
107 local crafting_fs = [[
108 image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
109 button[0,0;1.5,1;back;< Back]
110 list[current_player;craft;2,0;3,3;]
111 list[current_player;craftpreview;6,1;1,1;]
112 listring[current_player;main]
113 listring[current_player;craft]
116 local storage_fs = [[
117 list[context;storage;0,1;8,2;]
118 button[0,0;1.5,1;back;< Back]
119 listring[context;storage]
120 listring[current_player;main]
134 function workbench:set_formspec(meta, id)
135 meta:set_string("formspec",
136 "size[8,7;]list[current_player;main;0,3.25;8,4;]" ..
137 formspecs[id] .. xbg .. default.get_hotbar_bg(0,3.25))
140 function workbench.construct(pos)
141 local meta = minetest.get_meta(pos)
142 local inv = meta:get_inventory()
144 inv:set_size("tool", 1)
145 inv:set_size("input", 1)
146 inv:set_size("hammer", 1)
147 inv:set_size("forms", 4*3)
148 inv:set_size("storage", 8*2)
150 meta:set_string("infotext", "Work Bench")
151 workbench:set_formspec(meta, 1)
154 function workbench.fields(pos, _, fields)
155 if fields.quit then return end
157 local meta = minetest.get_meta(pos)
158 local id = fields.back and 1 or fields.craft and 2 or fields.storage and 3
159 if not id then return end
161 workbench:set_formspec(meta, id)
164 function workbench.dig(pos)
165 local inv = minetest.get_meta(pos):get_inventory()
166 return inv:is_empty("input") and inv:is_empty("hammer") and
167 inv:is_empty("tool") and inv:is_empty("storage")
170 function workbench.timer(pos)
171 local timer = minetest.get_node_timer(pos)
172 local inv = minetest.get_meta(pos):get_inventory()
173 local tool = inv:get_stack("tool", 1)
174 local hammer = inv:get_stack("hammer", 1)
176 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
181 -- Tool's wearing range: 0-65535; 0 = new condition
185 inv:set_stack("tool", 1, tool)
186 inv:set_stack("hammer", 1, hammer)
191 function workbench.allow_put(pos, listname, index, stack, player)
192 local player_name = player:get_player_name()
193 if minetest.is_protected(pos, player_name) then
194 minetest.record_protection_violation(pos, player_name)
198 local stackname = stack:get_name()
199 if (listname == "tool" and stack:get_wear() > 0 and
200 workbench:repairable(stackname)) or
201 (listname == "input" and registered_nodes[stackname .. "_cube"]) or
202 (listname == "hammer" and stackname == "xdecor:hammer") or
203 listname == "storage" then
204 return stack:get_count()
210 function workbench.on_put(pos, listname, index, stack, player)
212 '%s put %s in workbench at %s',
213 player:get_player_name(),
215 minetest.pos_to_string(pos))
217 local inv = minetest.get_meta(pos):get_inventory()
218 if listname == "input" then
219 local input = inv:get_stack("input", 1)
220 workbench:get_output(inv, input, stack:get_name())
221 elseif listname == "tool" or listname == "hammer" then
222 local timer = minetest.get_node_timer(pos)
227 function workbench.allow_move(pos, from_list, from_index, to_list, to_index, count, player)
228 local player_name = player:get_player_name()
229 if minetest.is_protected(pos, player_name) then
230 minetest.record_protection_violation(pos, player_name)
234 return (to_list == "storage" and from_list ~= "forms") and count or 0
237 function workbench.on_move(pos, from_list, from_index, to_list, to_index, count, player)
238 local meta = minetest.get_meta(pos)
239 local inv = meta:get_inventory()
240 local from_stack = inv:get_stack(from_list, from_index)
241 local to_stack = inv:get_stack(to_list, to_index)
244 '%s moved %s in workbench at %s',
245 player:get_player_name(),
247 minetest.pos_to_string(pos))
249 workbench.on_take(pos, from_list, from_index, from_stack, player)
250 workbench.on_put(pos, to_list, to_index, to_stack, player)
253 function workbench.allow_take(pos, listname, index, stack, player)
254 local player_name = player:get_player_name()
255 if minetest.is_protected(pos, player_name) then
256 minetest.record_protection_violation(pos, player_name)
260 return stack:get_count()
263 function workbench.on_take(pos, listname, index, stack, player)
265 '%s took %s from workbench at %s',
266 player:get_player_name(),
268 minetest.pos_to_string(pos))
270 local inv = minetest.get_meta(pos):get_inventory()
271 local input = inv:get_stack("input", 1)
272 local inputname = input:get_name()
273 local stackname = stack:get_name()
275 if listname == "input" then
276 if stackname == inputname and registered_nodes[inputname .. "_cube"] then
277 workbench:get_output(inv, input, stackname)
279 inv:set_list("forms", {})
281 elseif listname == "forms" then
282 local fromstack = inv:get_stack(listname, index)
283 if not fromstack:is_empty() and fromstack:get_name() ~= stackname then
284 local player_inv = player:get_inventory()
285 if player_inv:room_for_item("main", fromstack) then
286 player_inv:add_item("main", fromstack)
290 input:take_item(ceil(stack:get_count() / workbench.defs[index][2]))
291 inv:set_stack("input", 1, input)
292 workbench:get_output(inv, input, inputname)
296 xdecor.register("workbench", {
297 description = "Work Bench",
298 groups = {cracky = 2, choppy = 2, oddly_breakable_by_hand = 1},
299 sounds = default.node_sound_wood_defaults(),
301 "xdecor_workbench_top.png","xdecor_workbench_top.png",
302 "xdecor_workbench_sides.png", "xdecor_workbench_sides.png",
303 "xdecor_workbench_front.png", "xdecor_workbench_front.png"
305 on_rotate = screwdriver.rotate_simple,
306 can_dig = workbench.dig,
307 on_timer = workbench.timer,
308 on_construct = workbench.construct,
309 on_receive_fields = workbench.fields,
310 on_metadata_inventory_put = workbench.on_put,
311 on_metadata_inventory_take = workbench.on_take,
312 on_metadata_inventory_move = workbench.on_move,
313 allow_metadata_inventory_put = workbench.allow_put,
314 allow_metadata_inventory_take = workbench.allow_take,
315 allow_metadata_inventory_move = workbench.allow_move
318 for _, d in ipairs(workbench.defs) do
320 local node = nodes[i]
321 local mod_name, item_name = node:match("^(.-):(.*)")
322 local def = registered_nodes[node]
324 if item_name and d[3] then
327 groups.not_in_creative_inventory = 1
329 for k, v in pairs(def.groups) do
330 if k ~= "wood" and k ~= "stone" and k ~= "level" then
336 if #def.tiles > 1 and (def.drawtype:sub(1,5) ~= "glass") then
339 tiles = {def.tiles[1]}
342 tiles = {def.tile_images[1]}
345 if not registered_nodes["stairs:slab_" .. item_name] then
346 stairs.register_stair_and_slab(item_name, node,
347 groups, tiles, def.description .. " Stair",
348 def.description .. " Slab", def.sounds)
351 minetest.register_node(":" .. node .. "_" .. d[1], {
352 description = def.description .. " " .. d[1]:gsub("^%l", string.upper),
354 paramtype2 = "facedir",
355 drawtype = "nodebox",
359 -- `unpack` has been changed to `table.unpack` in newest Lua versions
360 node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
361 sunlight_propagates = true,
362 on_place = minetest.rotate_node
365 elseif item_name and mod_name then
366 minetest.register_alias_force(
367 ("%s:%s_innerstair"):format(mod_name, item_name),
368 ("stairs:stair_inner_%s"):format(item_name)
370 minetest.register_alias_force(
371 ("%s:%s_outerstair"):format(mod_name, item_name),
372 ("stairs:stair_outer_%s"):format(item_name)
380 minetest.register_tool("xdecor:hammer", {
381 description = "Hammer",
382 inventory_image = "xdecor_hammer.png",
383 wield_image = "xdecor_hammer.png",
384 on_use = function() do
391 minetest.register_craft({
392 output = "xdecor:hammer",
394 {"default:steel_ingot", "group:stick", "default:steel_ingot"},
395 {"", "group:stick", ""}
399 minetest.register_craft({
400 output = "xdecor:workbench",
402 {"group:wood", "group:wood"},
403 {"group:wood", "group:wood"}