-
Notifications
You must be signed in to change notification settings - Fork 7
/
duplicator.lua
256 lines (226 loc) · 6.79 KB
/
duplicator.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
--[[
Signs Bot
=========
Copyright (C) 2019-2021 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
Signs duplicator
]]--
-- for lazy programmers
local M = minetest.get_meta
-- Load support for I18n.
local S = signs_bot.S
local lib = signs_bot.lib
local formspec = "size[8,7.3]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"label[0.1,0.2;"..S("Template:").."]"..
"list[context;temp;2,0;1,1;]"..
"label[0.1,1.2;"..S("Input:").."]"..
"list[context;inp;2,1;1,1;]"..
"label[0.1,2.2;"..S("Output:").."]"..
"list[context;outp;2,2;1,1;]"..
"label[3,0.2;"..S("1. Place one 'cmnd' sign").."]"..
"label[3,1.2;"..S("2. Add 'blank signs'").."]"..
"label[3,2.2;"..S("3. Take the copies").."]"..
"list[current_player;main;0,3.5;8,4;]"..
"listring[context;inp]"..
"listring[current_player;main]"..
"listring[current_player;main]"..
"listring[context;outp]"
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if listname == "outp" then
return 0
end
if minetest.get_item_group(stack:get_name(), "sign_bot_sign") ~= 1
and stack:get_name() ~= "default:book_written" then
return 0
end
if listname == "temp" then
return 1
end
return stack:get_count()
end
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
return stack:get_count()
end
local function get_template_data(stack)
local name = stack:get_name()
local data = stack:get_meta():to_table().fields
if name == "default:sign_user" or name == "signs_bot:sign_cmnd" then
return data.description, data.cmnd
end
if name == "default:book_written" then
return data.title, data.text
end
end
local function get_dest_item(stack)
local name = stack:get_name()
if name == "signs_bot:sign_blank" or name == "signs_bot:sign_user" then
return ItemStack("signs_bot:sign_user")
end
if name == "signs_bot:sign_cmnd" then
return ItemStack("signs_bot:sign_cmnd")
end
end
local function move_to_output(pos)
local inv = M(pos):get_inventory()
local inp_stack = inv:get_stack("inp", 1)
local temp_stack = inv:get_stack("temp", 1)
local outp_stack = inv:get_stack("outp", 1)
local dest_item = get_dest_item(inp_stack)
local descr, cmnd = get_template_data(temp_stack)
if dest_item and descr then
dest_item:set_count(inp_stack:get_count())
local meta = dest_item:get_meta()
meta:set_string("description", descr)
meta:set_string("cmnd", cmnd)
inp_stack:clear()
inv:set_stack("inp", 1, inp_stack)
inv:set_stack("outp", 1, dest_item)
end
end
local function on_metadata_inventory_put(pos, listname, index, stack, player)
if listname == "inp" then
minetest.after(0.5, move_to_output, pos)
end
end
minetest.register_node("signs_bot:duplicator", {
description = S("Signs Duplicator"),
stack_max = 1,
tiles = {
-- up, down, right, left, back, front
'signs_bot_base_top.png',
'signs_bot_base_top.png',
'signs_bot_duplicator.png',
'signs_bot_duplicator.png',
'signs_bot_duplicator.png',
'signs_bot_duplicator.png',
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size('inp', 1)
inv:set_size('temp', 1)
inv:set_size('outp', 1)
meta:set_string("formspec", formspec)
end,
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_take = allow_metadata_inventory_take,
on_metadata_inventory_put = on_metadata_inventory_put,
can_dig = function(pos, player)
if minetest.is_protected(pos, player:get_player_name()) then
return false
end
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:is_empty("inp") and inv:is_empty("temp") and inv:is_empty("outp")
end,
paramtype2 = "facedir",
is_ground_content = false,
groups = {cracky = 1},
sounds = default.node_sound_metal_defaults(),
})
minetest.register_craft({
output = "signs_bot:duplicator",
recipe = {
{"default:steel_ingot", "group:wood", "default:steel_ingot"},
{"", "basic_materials:gear_steel", ""},
{"default:tin_ingot", "", "default:tin_ingot"}
}
})
local function formspec_user(cmnd)
cmnd = minetest.formspec_escape(cmnd)
return "size[6,5]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"label[0.2,0;"..cmnd.."]"
end
minetest.register_node("signs_bot:sign_user", {
description = S('Sign "user"'),
drawtype = "nodebox",
inventory_image = "signs_bot_sign_user.png",
node_box = {
type = "fixed",
fixed = {
{ -1/16, -8/16, -1/16, 1/16, 4/16, 1/16},
{ -6/16, -5/16, -2/16, 6/16, 3/16, -1/16},
},
},
paramtype2 = "facedir",
tiles = {
"default_wood.png",
"default_wood.png",
"default_wood.png",
"default_wood.png",
"default_wood.png",
"default_wood.png^signs_bot_sign_user.png",
},
after_place_node = function(pos, placer, itemstack)
local imeta = itemstack:get_meta()
local nmeta = minetest.get_meta(pos)
if imeta:get_string("description") ~= "" then
nmeta:set_string("signs_bot_cmnd", imeta:get_string("cmnd"))
nmeta:set_string("sign_name", imeta:get_string("description"))
end
nmeta:set_string("infotext", nmeta:get_string("sign_name"))
local text = nmeta:get_string("sign_name").."\n"..imeta:get_string("cmnd")
nmeta:set_string("formspec", formspec_user(text))
end,
after_dig_node = lib.after_dig_sign_node,
drop = "",
on_rotate = screwdriver.disallow,
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2, sign_bot_sign = 1, not_in_creative_inventory = 1},
sounds = default.node_sound_wood_defaults(),
})
signs_bot.register_sign({
name = "sign_blank",
description = S('Sign "blank"'),
commands = "",
image = "signs_bot_sign_blank.png",
})
minetest.register_craft({
output = "signs_bot:sign_blank 6",
recipe = {
{"group:wood", "default:stick", "group:wood"},
{"dye:yellow", "default:stick", "dye:yellow"},
{"", "", ""}
}
})
if minetest.get_modpath("doc") then
doc.add_entry("signs_bot", "duplicator", {
name = S("Signs Duplicator"),
data = {
item = "signs_bot:duplicator",
text = table.concat({
S("The Duplicator can be used to make copies of signs."),
S("1. Put one 'cmnd' sign to be used as template into the 'Template' inventory"),
S("2. Add one or several 'blank signs' to the 'Input' inventory."),
S("3. Take the copies from the 'Output' inventory."),
"",
S("Written books [default:book_written] can alternatively be used as template"),
S("Already written signs can be used as input, too."),
}, "\n")
},
})
end
if minetest.get_modpath("doc") then
doc.add_entry("signs_bot", "sign_blank", {
name = S('Sign "blank"'),
data = {
item = "signs_bot:sign_blank",
text = S("Needed as input for the Duplicator.")
},
})
end