-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcraftitems.lua
543 lines (459 loc) · 13 KB
/
craftitems.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
-- mods/default/craftitems.lua
-- support for MT game translation.
local S = default.get_translator
local esc = minetest.formspec_escape
local formspec_size = "size[8,8]"
local function formspec_core(tab)
if tab == nil then tab = 1 else tab = tostring(tab) end
return "tabheader[0,0;book_header;" ..
esc(S("Write")) .. "," ..
esc(S("Read")) .. ";" ..
tab .. ";false;false]"
end
local function formspec_write(title, text)
return "field[0.5,1;7.5,0;title;" .. esc(S("Title:")) .. ";" ..
esc(title) .. "]" ..
"textarea[0.5,1.5;7.5,7;text;" .. esc(S("Contents:")) .. ";" ..
esc(text) .. "]" ..
"button_exit[2.5,7.5;3,1;save;" .. esc(S("Save")) .. "]"
end
local function formspec_read(owner, title, string, text, page, page_max)
return "label[0.5,0.5;" .. esc(S("by @1", owner)) .. "]" ..
"tablecolumns[color;text]" ..
"tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
"table[0.4,0;7,0.5;title;#FFFF00," .. esc(title) .. "]" ..
"textarea[0.5,1.5;7.5,7;;" ..
esc(string ~= "" and string or text) .. ";]" ..
"button[2.4,7.6;0.8,0.8;book_prev;<]" ..
"label[3.2,7.7;" .. esc(S("Page @1 of @2", page, page_max)) .. "]" ..
"button[4.9,7.6;0.8,0.8;book_next;>]"
end
local function formspec_string(lpp, page, lines, string)
for i = ((lpp * page) - lpp) + 1, lpp * page do
if not lines[i] then break end
string = string .. lines[i] .. "\n"
end
return string
end
local book_writers = {}
minetest.register_on_leaveplayer(function(player)
book_writers[player:get_player_name()] = nil
end)
local tab_number
local lpp = 14 -- Lines per book's page
local function book_on_use(itemstack, user)
local player_name = user:get_player_name()
local meta = itemstack:get_meta()
local title, text, owner = "", "", player_name
local page, page_max, lines, string = 1, 1, {}, ""
-- Backwards compatibility
local old_data = minetest.deserialize(itemstack:get_meta():get_string(""))
if old_data then
meta:from_table({ fields = old_data })
end
local data = meta:to_table().fields
if data.owner then
title = data.title or ""
text = data.text or ""
owner = data.owner
for str in (text .. "\n"):gmatch("([^\n]*)[\n]") do
lines[#lines+1] = str
end
if data.page then
page = data.page
page_max = data.page_max
string = formspec_string(lpp, page, lines, string)
end
end
local formspec
if title == "" and text == "" then
formspec = formspec_write(title, text)
elseif owner == player_name then
local tab = tab_number or 1
if tab == 2 then
formspec = formspec_core(tab) ..
formspec_read(owner, title, string, text, page, page_max)
else
formspec = formspec_core(tab) .. formspec_write(title, text)
end
else
formspec = formspec_read(owner, title, string, text, page, page_max)
end
minetest.show_formspec(player_name, "default:book", formspec_size .. formspec)
-- Store the wield index in case the user accidentally switches before the formspec is shown
book_writers[player_name] = {wield_index = user:get_wield_index()}
return itemstack
end
local max_text_size = 10000
local max_title_size = 80
local short_title_size = 35
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "default:book" then
return
end
local player_name = player:get_player_name()
local inv = player:get_inventory()
if not book_writers[player_name] then
return
end
local wield_index = book_writers[player_name].wield_index
local wield_list = player:get_wield_list()
local stack = inv:get_stack(wield_list, wield_index)
local written = stack:get_name() == "default:book_written"
if stack:get_name() ~= "default:book" and not written then
-- No book in the wield slot, abort & inform the player
minetest.chat_send_player(player_name, S("The book you were writing to mysteriously disappeared."))
return
end
local data = stack:get_meta():to_table().fields
local title = data.title or ""
local text = data.text or ""
if fields.book_header ~= nil and data.owner == player_name then
local contents
local tab = tonumber(fields.book_header)
if tab == 1 then
contents = formspec_core(tab) ..
formspec_write(title, text)
elseif tab == 2 then
local lines, string = {}, ""
for str in (text .. "\n"):gmatch("([^\n]*)[\n]") do
lines[#lines+1] = str
end
string = formspec_string(lpp, data.page, lines, string)
contents = formspec_read(player_name, title, string,
text, data.page, data.page_max)
else
return -- malicious data
end
tab_number = tab
local formspec = formspec_size .. formspec_core(tab) .. contents
minetest.show_formspec(player_name, "default:book", formspec)
return
end
if fields.quit then
book_writers[player_name] = nil
end
if fields.save and fields.title and fields.text then
local new_stack
if not written then
local count = stack:get_count()
if count == 1 then
stack:set_name("default:book_written")
else
stack:set_count(count - 1)
new_stack = ItemStack("default:book_written")
end
end
if data.owner ~= player_name and title ~= "" and text ~= "" then
return
end
if not data then data = {} end
data.title = fields.title:sub(1, max_title_size)
data.owner = player:get_player_name()
local short_title = data.title
-- Don't bother triming the title if the trailing dots would make it longer
if #short_title > short_title_size + 3 then
short_title = short_title:sub(1, short_title_size) .. "..."
end
data.description = S("\"@1\" by @2", short_title, data.owner)
data.text = fields.text:sub(1, max_text_size)
data.text = data.text:gsub("\r\n", "\n"):gsub("\r", "\n")
data.text = data.text:gsub("[%z\1-\8\11-\31\127]", "") -- strip naughty control characters (keeps \t and \n)
data.page = 1
data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
if new_stack then
new_stack:get_meta():from_table({ fields = data })
if inv:room_for_item("main", new_stack) then
inv:add_item("main", new_stack)
else
minetest.add_item(player:get_pos(), new_stack)
end
else
stack:get_meta():from_table({ fields = data })
end
elseif fields.book_next or fields.book_prev then
if not data.page then
return
end
data.page = tonumber(data.page)
data.page_max = tonumber(data.page_max)
if fields.book_next then
data.page = data.page + 1
if data.page > data.page_max then
data.page = 1
end
else
data.page = data.page - 1
if data.page == 0 then
data.page = data.page_max
end
end
stack:get_meta():from_table({fields = data})
stack = book_on_use(stack, player)
end
-- Update stack
inv:set_stack(wield_list, wield_index, stack)
end)
--
-- Craftitem registry
--
minetest.register_craftitem("default:blueberries", {
description = S("Blueberries"),
inventory_image = "default_blueberries.png",
groups = {food_blueberries = 1, food_berry = 1},
on_use = minetest.item_eat(2),
})
minetest.register_craftitem("default:book", {
description = S("Book"),
inventory_image = "default_book.png",
groups = {book = 1, flammable = 3},
on_use = book_on_use,
})
minetest.register_craftitem("default:book_written", {
description = S("Book with Text"),
inventory_image = "default_book_written.png",
groups = {book = 1, not_in_creative_inventory = 1, flammable = 3},
stack_max = 1,
on_use = book_on_use,
})
minetest.register_craftitem("default:bronze_ingot", {
description = S("Bronze Ingot"),
inventory_image = "default_bronze_ingot.png"
})
minetest.register_craftitem("default:clay_brick", {
description = S("Clay Brick"),
inventory_image = "default_clay_brick.png",
})
minetest.register_craftitem("default:clay_lump", {
description = S("Clay Lump"),
inventory_image = "default_clay_lump.png",
})
minetest.register_craftitem("default:coal_lump", {
description = S("Coal Lump"),
inventory_image = "default_coal_lump.png",
groups = {coal = 1, flammable = 1}
})
minetest.register_craftitem("default:copper_ingot", {
description = S("Copper Ingot"),
inventory_image = "default_copper_ingot.png"
})
minetest.register_craftitem("default:copper_lump", {
description = S("Copper Lump"),
inventory_image = "default_copper_lump.png"
})
minetest.register_craftitem("default:diamond", {
description = S("Diamond"),
inventory_image = "default_diamond.png",
})
minetest.register_craftitem("default:flint", {
description = S("Flint"),
inventory_image = "default_flint.png"
})
minetest.register_craftitem("default:gold_ingot", {
description = S("Gold Ingot"),
inventory_image = "default_gold_ingot.png"
})
minetest.register_craftitem("default:gold_lump", {
description = S("Gold Lump"),
inventory_image = "default_gold_lump.png"
})
minetest.register_craftitem("default:iron_lump", {
description = S("Iron Lump"),
inventory_image = "default_iron_lump.png"
})
minetest.register_craftitem("default:mese_crystal", {
description = S("Mese Crystal"),
inventory_image = "default_mese_crystal.png",
})
minetest.register_craftitem("default:mese_crystal_fragment", {
description = S("Mese Crystal Fragment"),
inventory_image = "default_mese_crystal_fragment.png",
})
minetest.register_craftitem("default:obsidian_shard", {
description = S("Obsidian Shard"),
inventory_image = "default_obsidian_shard.png",
})
minetest.register_craftitem("default:paper", {
description = S("Paper"),
inventory_image = "default_paper.png",
groups = {flammable = 3},
})
minetest.register_craftitem("default:steel_ingot", {
description = S("Steel Ingot"),
inventory_image = "default_steel_ingot.png"
})
minetest.register_craftitem("default:stick", {
description = S("Stick"),
inventory_image = "default_stick.png",
groups = {stick = 1, flammable = 2},
})
minetest.register_craftitem("default:tin_ingot", {
description = S("Tin Ingot"),
inventory_image = "default_tin_ingot.png"
})
minetest.register_craftitem("default:tin_lump", {
description = S("Tin Lump"),
inventory_image = "default_tin_lump.png"
})
--
-- Crafting recipes
--
minetest.register_craft({
output = "default:book",
recipe = {
{"default:paper"},
{"default:paper"},
{"default:paper"},
}
})
default.register_craft_metadata_copy("default:book", "default:book_written")
minetest.register_craft({
output = "default:bronze_ingot 9",
recipe = {
{"default:copper_ingot", "default:copper_ingot", "default:copper_ingot"},
{"default:copper_ingot", "default:tin_ingot", "default:copper_ingot"},
{"default:copper_ingot", "default:copper_ingot", "default:copper_ingot"},
}
})
minetest.register_craft({
output = "default:clay_brick 4",
recipe = {
{"default:brick"},
}
})
minetest.register_craft({
output = "default:clay_lump 4",
recipe = {
{"default:clay"},
}
})
minetest.register_craft({
output = "default:coal_lump 9",
recipe = {
{"default:coalblock"},
}
})
minetest.register_craft({
output = "default:copper_ingot 9",
recipe = {
{"default:copperblock"},
}
})
minetest.register_craft({
output = "default:diamond 9",
recipe = {
{"default:diamondblock"},
}
})
minetest.register_craft({
output = "default:gold_ingot 9",
recipe = {
{"default:goldblock"},
}
})
minetest.register_craft({
output = "default:mese_crystal",
recipe = {
{"default:mese_crystal_fragment", "default:mese_crystal_fragment", "default:mese_crystal_fragment"},
{"default:mese_crystal_fragment", "default:mese_crystal_fragment", "default:mese_crystal_fragment"},
{"default:mese_crystal_fragment", "default:mese_crystal_fragment", "default:mese_crystal_fragment"},
}
})
minetest.register_craft({
output = "default:mese_crystal 9",
recipe = {
{"default:mese"},
}
})
minetest.register_craft({
output = "default:mese_crystal_fragment 9",
recipe = {
{"default:mese_crystal"},
}
})
minetest.register_craft({
output = "default:obsidian_shard 9",
recipe = {
{"default:obsidian"}
}
})
minetest.register_craft({
output = "default:paper",
recipe = {
{"default:papyrus", "default:papyrus", "default:papyrus"},
}
})
minetest.register_craft({
output = "default:steel_ingot 9",
recipe = {
{"default:steelblock"},
}
})
minetest.register_craft({
output = "default:stick 4",
recipe = {
{"group:wood"},
}
})
minetest.register_craft({
output = "default:tin_ingot 9",
recipe = {
{"default:tinblock"},
}
})
--
-- Cooking recipes
--
minetest.register_craft({
type = "cooking",
output = "default:clay_brick",
recipe = "default:clay_lump",
})
minetest.register_craft({
type = "cooking",
output = "default:copper_ingot",
recipe = "default:copper_lump",
})
minetest.register_craft({
type = "cooking",
output = "default:gold_ingot",
recipe = "default:gold_lump",
})
minetest.register_craft({
type = "cooking",
output = "default:steel_ingot",
recipe = "default:iron_lump",
})
minetest.register_craft({
type = "cooking",
output = "default:tin_ingot",
recipe = "default:tin_lump",
})
--
-- Fuels
--
minetest.register_craft({
type = "fuel",
recipe = "default:book",
burntime = 3,
})
minetest.register_craft({
type = "fuel",
recipe = "default:book_written",
burntime = 3,
})
minetest.register_craft({
type = "fuel",
recipe = "default:coal_lump",
burntime = 40,
})
minetest.register_craft({
type = "fuel",
recipe = "default:paper",
burntime = 1,
})
minetest.register_craft({
type = "fuel",
recipe = "group:stick",
burntime = 1,
})