-
Notifications
You must be signed in to change notification settings - Fork 10
/
init.lua
312 lines (281 loc) · 9.8 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
local S = minetest.get_translator("crafting_bench")
local F = minetest.formspec_escape
local C = minetest.colorize
minetest.register_alias("castle:workbench", "crafting_bench:workbench")
local has_mcl = minetest.get_modpath("mcl_formspec")
local has_default = minetest.get_modpath("default")
local usage_help = S("The inventory on the left is for raw materials, the inventory on the right holds finished products. The crafting grid in the center defines what recipe this workbench will make use of; place raw materials into it in the crafting pattern corresponding to what you want to build.")
if ( minetest.get_modpath("hopper") and hopper ~= nil and hopper.add_container ~= nil ) or has_mcl then
usage_help = usage_help .. "\n\n" .. S("This workbench is compatible with hoppers. Hoppers will insert into the raw material inventory and remove items from the finished goods inventory.")
end
local crafting_rate = tonumber(minetest.settings:get("crafting_bench_crafting_rate")) or 5
if not has_default and not has_mcl then
error("The crafting bench mod needs either the default mod (minetest game) or mineclonia/mineclone2 to work")
end
local invsize_src = 2 * 4
local invsize_dst = 1 * 4
if has_mcl then
invsize_src = 2 * 3
invsize_dst = 1 * 3
end
local groups, mcl_hardness, mcl_blast_res, sounds
local steel_ingot, wood, tree
local formspec
if has_default then
formspec = 'size[10,10;]' ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
'label[1,0;'..S('Source Material')..']' ..
'list[context;src;1,1;2,4;]' ..
'label[4,0;'..S('Recipe to Use')..']' ..
'list[context;rec;4,1;3,3;]' ..
'label[7.5,0;'..S('Craft Output')..']' ..
'list[context;dst;8,1;1,4;]' ..
'list[current_player;main;1,6;8,4;]' ..
'listring[current_name;dst]'..
'listring[current_player;main]'..
'listring[current_name;src]'..
'listring[current_player;main]'
groups = {choppy=2, oddly_breakable_by_hand=2, flammable=2}
sounds = default.node_sound_wood_defaults()
elseif has_mcl then
formspec ='formspec_version[4]'..
'size[11.75,10.425]'..
mcl_formspec.get_itemslot_bg_v4(1, 0.75, 2, 3)..
mcl_formspec.get_itemslot_bg_v4(5, 0.75, 3, 3)..
mcl_formspec.get_itemslot_bg_v4(10, 0.75, 1, 3)..
'label[1,0.375;'..S('Source Material')..']' ..
'list[context;src;1,0.75;2,4;]' ..
'label[5,0.375;'..S('Recipe to Use')..']' ..
'list[context;rec;5,0.75;3,3;]' ..
'label[9.5,0.375;'..S('Craft Output')..']' ..
'list[context;dst;10,0.75;1,4;]' ..
'label[0.375,4.7;' .. F(C(mcl_formspec.label_color, S('Inventory'))) .. ']'..
mcl_formspec.get_itemslot_bg_v4(0.375, 5.1, 9, 3)..
'list[current_player;main;0.375,5.1;9,3;9]'..
mcl_formspec.get_itemslot_bg_v4(0.375, 9.05, 9, 1)..
'list[current_player;main;0.375,9.05;9,1;]'..
'listring[current_name;dst]'..
'listring[current_player;main]'..
'listring[current_name;src]'..
'listring[current_player;main]'
groups = {axey=2, handy=1, flammable=-1, container = 4}
sounds = mcl_sounds.node_sound_wood_defaults()
mcl_hardness = 3
mcl_blast_res = 3
end
minetest.register_node("crafting_bench:workbench",{
description = S("Workbench"),
_doc_items_longdesc = S("A workbench that does work for you. Set a crafting recipe and provide raw materials and items will magically craft themselves once every @1 seconds.", crafting_rate),
_doc_items_usagehelp = usage_help,
tiles = {
"crafting_bench_workbench_top.png",
"crafting_bench_workbench_bottom.png",
"crafting_bench_workbench_side.png",
"crafting_bench_workbench_side.png",
"crafting_bench_workbench_back.png",
"crafting_bench_workbench_front.png"
},
paramtype2 = "facedir",
paramtype = "light",
groups = groups,
sounds = sounds,
drawtype = "normal",
_mcl_hardness = mcl_hardness,
_mcl_blast_resistance = mcl_blast_res,
on_construct = function ( pos )
local meta = minetest.get_meta( pos )
meta:set_string( 'formspec', formspec)
meta:set_string( 'infotext', S('Workbench'))
local inv = meta:get_inventory()
inv:set_size( 'src', invsize_src )
inv:set_size( 'rec', 3 * 3 )
inv:set_size( 'dst', invsize_dst )
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("src") and inv:is_empty("rec") and inv:is_empty("dst")
end,
on_blast = function(pos) end,
on_metadata_inventory_put = function(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name().." put "..stack:to_string().." in workbench at "..minetest.pos_to_string(pos))
end,
on_metadata_inventory_take = function(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name().." takes "..stack:to_string().." from workbench at "..minetest.pos_to_string(pos))
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
if not minetest.is_player(player) or minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if to_list == "dst" then
-- Only allow to take from the output
return 0
elseif to_list == "rec" then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index)
stack:set_count(1)
inv:set_stack(to_list, to_index, stack)
if from_list == "rec" then
-- For convenience: emulate movement instead of duplication
inv:set_stack(from_list, from_index, "")
end
return 0
elseif from_list == "rec" then
-- Remove recipe stack
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_stack(from_list, from_index, "")
return 0
end
return count
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if not minetest.is_player(player) or minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if listname == "rec" then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
stack:set_count(1)
inv:set_stack("rec", index, stack)
return 0
elseif listname == "dst" then
return 0
end
return stack:get_count()
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if not minetest.is_player(player) or minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if listname == "rec" then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_stack("rec", index, "")
return 0
end
return stack:get_count()
end,
})
local get_recipe = function ( inv )
local output, needed, decremented_input
needed = inv:get_list( 'rec' )
output, decremented_input = minetest.get_craft_result( {
method = 'normal',
width = 3,
items = needed
})
local totalneed = {}
if output.item:is_empty() then
output = nil
else
output = output.item
for _, item in ipairs( needed ) do
if item ~= nil and not item:is_empty() and not inv:contains_item( 'src', item ) then
output = nil
break
end
if item ~= nil and not item:is_empty() then
if totalneed[item:get_name()] == nil then
totalneed[item:get_name()] = 1
else
totalneed[item:get_name()] = totalneed[item:get_name()] + 1
end
end
end
for name, number in pairs( totalneed ) do
local totallist = inv:get_list( 'src' )
for i, srcitem in pairs( totallist ) do
if srcitem:get_name() == name then
local taken = srcitem:take_item( number )
number = number - taken:get_count()
totallist[i] = srcitem
end
if number <= 0 then
break
end
end
if number > 0 then
output = nil
break
end
end
end
return needed, decremented_input, output
end
minetest.register_abm( {
nodenames = { 'crafting_bench:workbench' },
interval = crafting_rate,
chance = 1,
action = function ( pos, node )
local meta = minetest.get_meta( pos )
local inv = meta:get_inventory()
local output, decremented_input, needed
if not inv:is_empty( 'src' ) then
-- Check for a valid recipe and sufficient resources to craft it
needed, decremented_input, output = get_recipe( inv )
if output ~= nil and inv:room_for_item( 'dst', output) then
inv:add_item( 'dst', output)
for _, item in pairs( needed ) do
if item ~= nil and item ~= '' then
inv:remove_item( 'src', ItemStack( item ) )
end
end
for i = 1, 9 do
inv:add_item( 'dst', decremented_input.items[i] )
end
end
end
end
} )
-- Make sure that all stacks in the 'recipe' inv count 1 item.
-- Bigger stacks result in item duplication, see issue#14
minetest.register_lbm({
name = "crafting_bench:cleanup_rec_inv",
label = "remove multiple-item-stacks from recipe inv",
nodenames = {"crafting_bench:workbench"},
run_at_every_load = false,
action = function(pos, node)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
-- `get_list` may return `nil` when the node was placed without calling `on_construct`
for i, item in ipairs(inv:get_list("rec") or {}) do
-- Limit to stack size 1 (or keep empty)
inv:set_stack("rec", i, item:peek_item())
local leftover = inv:add_item("dst", item)
if not leftover:is_empty() then
minetest.log("action", "[crafting_bench] deleting leftover " ..
item:to_string() .. " from recipe inv at " .. minetest.pos_to_string(pos))
end
end
end
})
-- Crafting recipe compatibility.
if has_default then
steel_ingot = "default:steel_ingot"
wood = "default:wood"
tree = "default:tree"
elseif has_mcl then
steel_ingot = "mcl_core:iron_ingot"
wood = "mcl_core:wood"
tree = "mcl_core:tree"
end
minetest.register_craft({
output = "crafting_bench:workbench",
recipe = {
{steel_ingot, steel_ingot, steel_ingot},
{wood, wood, steel_ingot},
{tree, tree, steel_ingot},
}
})
-- Hopper compatibility
if minetest.get_modpath("hopper") and hopper ~= nil and hopper.add_container ~= nil then
hopper:add_container({
{"top", "crafting_bench:workbench", "dst"},
{"side", "crafting_bench:workbench", "src"},
{"bottom", "crafting_bench:workbench", "src"},
})
end