2 screwdriver = screwdriver or {}
4 -- Nodes allowed to be cut.
5 -- Only the regular, solid blocks without formspec or explosivity can be cut.
6 function workbench:nodes(def)
7 return (def.drawtype == "normal" or def.drawtype:find("glass")) and
8 (def.groups.cracky or def.groups.choppy) and not
9 def.on_construct and not def.after_place_node and not
10 def.after_place_node and not def.on_rightclick and not
11 def.on_blast and not def.allow_metadata_inventory_take and not
12 (def.groups.not_in_creative_inventory == 1) and not
13 def.groups.wool and not def.description:find("Ore") and
14 def.description and def.description ~= "" and def.light_source == 0
17 -- Nodeboxes definitions.
19 -- Name Yield X Y Z W H L
20 {"nanoslab", 16, { 0, 0, 0, 8, 1, 8 }},
21 {"micropanel", 16, { 0, 0, 0, 16, 1, 8 }},
22 {"microslab", 8, { 0, 0, 0, 16, 1, 16 }},
23 {"thinstair", 8, { 0, 7, 0, 16, 1, 8 },
24 { 0, 15, 8, 16, 1, 8 }},
25 {"cube", 4, { 0, 0, 0, 8, 8, 8 }},
26 {"panel", 4, { 0, 0, 0, 16, 8, 8 }},
28 {"doublepanel", 2, { 0, 0, 0, 16, 8, 8 },
29 { 0, 8, 8, 16, 8, 8 }},
30 {"halfstair", 2, { 0, 0, 0, 8, 8, 16 },
31 { 0, 8, 8, 8, 8, 8 }},
32 {"outerstair", 1, { 0, 0, 0, 16, 8, 16 },
33 { 0, 8, 8, 8, 8, 8 }},
35 {"innerstair", 1, { 0, 0, 0, 16, 8, 16 },
36 { 0, 8, 8, 16, 8, 8 },
40 -- Tools allowed to be repaired.
41 function workbench:repairable(stack)
42 local tools = {"pick", "axe", "shovel", "sword", "hoe", "armor", "shield"}
43 for _, t in pairs(tools) do
44 if stack:find(t) then return true end
49 function workbench:get_output(inv, input, name)
50 if inv:is_empty("input") then
51 inv:set_list("forms", {}) return
55 for _, n in pairs(self.defs) do
56 local count = math.min(n[2] * input:get_count(), input:get_stack_max())
57 local item = name.."_"..n[1]
58 if not n[3] then item = "stairs:"..n[1].."_"..name:match(":(.*)") end
59 output[#output+1] = item.." "..count
61 inv:set_list("forms", output)
64 function workbench:formspecs(meta, id)
67 [[ label[0.9,1.23;Cut]
68 label[0.9,2.23;Repair]
69 box[-0.05,1;2.05,0.9;#555555]
70 box[-0.05,2;2.05,0.9;#555555]
71 button[0,0;2,1;craft;Crafting]
72 button[2,0;2,1;storage;Storage]
73 image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
74 image[0,1;1,1;worktable_saw.png]
75 image[0,2;1,1;worktable_anvil.png]
76 image[3,2;1,1;hammer_layout.png]
77 list[context;input;2,1;1,1;]
78 list[context;tool;2,2;1,1;]
79 list[context;hammer;3,2;1,1;]
80 list[context;forms;4,0;4,3;] ]],
82 [[ image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
83 button[0,0;1.5,1;back;< Back]
84 list[current_player;craft;2,0;3,3;]
85 list[current_player;craftpreview;6,1;1,1;]
86 listring[current_player;main]
87 listring[current_player;craft] ]],
89 [[ list[context;storage;0,1;8,2;]
90 button[0,0;1.5,1;back;< Back]
91 listring[context;storage]
92 listring[current_player;main] ]]
95 meta:set_string("formspec", "size[8,7;]list[current_player;main;0,3.25;8,4;]"..
96 formspecs[id]..xbg..default.get_hotbar_bg(0,3.25))
99 function workbench.construct(pos)
100 local meta = minetest.get_meta(pos)
101 local inv = meta:get_inventory()
103 inv:set_size("tool", 1)
104 inv:set_size("input", 1)
105 inv:set_size("hammer", 1)
106 inv:set_size("forms", 4*3)
107 inv:set_size("storage", 8*2)
109 meta:set_string("infotext", "Work Bench")
110 workbench:formspecs(meta, 1)
113 function workbench.fields(pos, _, fields)
114 local meta = minetest.get_meta(pos)
115 if fields.back then workbench:formspecs(meta, 1)
116 elseif fields.craft then workbench:formspecs(meta, 2)
117 elseif fields.storage then workbench:formspecs(meta, 3)
118 elseif fields.backcraft then workbench:formspecs(meta, 1) end -- Legacy code for older formspecs.
121 function workbench.dig(pos)
122 local inv = minetest.get_meta(pos):get_inventory()
123 return inv:is_empty("input") and inv:is_empty("hammer") and
124 inv:is_empty("tool") and inv:is_empty("storage")
127 function workbench.timer(pos)
128 local timer = minetest.get_node_timer(pos)
129 local inv = minetest.get_meta(pos):get_inventory()
130 local tool = inv:get_stack("tool", 1)
131 local hammer = inv:get_stack("hammer", 1)
133 if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
137 -- Tool's wearing range: 0-65535 | 0 = new condition.
141 inv:set_stack("tool", 1, tool)
142 inv:set_stack("hammer", 1, hammer)
146 function workbench.put(_, listname, _, stack)
147 local stackname = stack:get_name()
148 if (listname == "tool" and stack:get_wear() > 0 and workbench:repairable(stackname)) or
149 (listname == "input" and minetest.registered_nodes[stackname.."_cube"]) or
150 (listname == "hammer" and stackname == "xdecor:hammer") or
151 listname == "storage" then
152 return stack:get_count()
157 function workbench.take(_, listname, _, stack, player)
158 if listname == "forms" then
159 local inv = player:get_inventory()
160 if inv:room_for_item("main", stack:get_name()) then return -1 end
163 return stack:get_count()
166 function workbench.move(_, _, _, to_list, _, count)
167 if to_list == "storage" then return count end
171 function workbench.on_put(pos, listname, _, stack)
172 local inv = minetest.get_meta(pos):get_inventory()
173 if listname == "input" then
174 local input = inv:get_stack("input", 1)
175 workbench:get_output(inv, input, stack:get_name())
176 elseif listname == "tool" or listname == "hammer" then
177 local timer = minetest.get_node_timer(pos)
182 function workbench.on_take(pos, listname, index, stack)
183 local inv = minetest.get_meta(pos):get_inventory()
184 local input = inv:get_stack("input", 1)
186 if listname == "input" then
187 if stack:get_name() == input:get_name() then
188 workbench:get_output(inv, input, stack:get_name())
190 inv:set_list("forms", {})
192 elseif listname == "forms" then
193 input:take_item(math.ceil(stack:get_count() / workbench.defs[index][2]))
194 inv:set_stack("input", 1, input)
195 workbench:get_output(inv, input, input:get_name())
199 xdecor.register("workbench", {
200 description = "Work Bench",
201 groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
202 sounds = default.node_sound_wood_defaults(),
203 tiles = {"xdecor_workbench_top.png", "xdecor_workbench_top.png",
204 "xdecor_workbench_sides.png", "xdecor_workbench_sides.png",
205 "xdecor_workbench_front.png", "xdecor_workbench_front.png"},
206 on_rotate = screwdriver.rotate_simple,
207 can_dig = workbench.dig,
208 on_timer = workbench.timer,
209 on_construct = workbench.construct,
210 on_receive_fields = workbench.fields,
211 on_metadata_inventory_put = workbench.on_put,
212 on_metadata_inventory_take = workbench.on_take,
213 allow_metadata_inventory_put = workbench.put,
214 allow_metadata_inventory_take = workbench.take,
215 allow_metadata_inventory_move = workbench.move
218 for _, d in pairs(workbench.defs) do
219 for node in pairs(minetest.registered_nodes) do
220 local def = minetest.registered_nodes[node]
221 if workbench:nodes(def) and d[3] then
222 local groups, tiles = {}, {}
223 groups.not_in_creative_inventory = 1
225 for k, v in pairs(def.groups) do
226 if k ~= "wood" and k ~= "stone" and k ~= "level" then
232 if #def.tiles > 1 and not (def.drawtype:sub(1,5) == "glass") then
235 tiles = {def.tiles[1]}
238 tiles = {def.tile_images[1]}
241 if not minetest.registered_nodes["stairs:slab_"..node:match(":(.*)")] then
242 stairs.register_stair_and_slab(node:match(":(.*)"), node, groups, tiles,
243 def.description.." Stair", def.description.." Slab", def.sounds)
246 minetest.register_node(":"..node.."_"..d[1], {
247 description = def.description.." "..d[1]:gsub("^%l", string.upper),
249 paramtype2 = "facedir",
250 drawtype = "nodebox",
254 -- `unpack` has been changed to `table.unpack` in newest Lua versions.
255 node_box = xdecor.pixelbox(16, {unpack(d, 3)}),
256 sunlight_propagates = true,
257 on_place = minetest.rotate_node