craftitems.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. -- mods/default/craftitems.lua
  2. -- support for MT game translation.
  3. local S = default.get_translator
  4. local esc = minetest.formspec_escape
  5. local formspec_size = "size[8,8]"
  6. local function formspec_core(tab)
  7. if tab == nil then tab = 1 else tab = tostring(tab) end
  8. return "tabheader[0,0;book_header;" ..
  9. esc(S("Write")) .. "," ..
  10. esc(S("Read")) .. ";" ..
  11. tab .. ";false;false]"
  12. end
  13. local function formspec_write(title, text)
  14. return "field[0.5,1;7.5,0;title;" .. esc(S("Title:")) .. ";" ..
  15. esc(title) .. "]" ..
  16. "textarea[0.5,1.5;7.5,7;text;" .. esc(S("Contents:")) .. ";" ..
  17. esc(text) .. "]" ..
  18. "button_exit[2.5,7.5;3,1;save;" .. esc(S("Save")) .. "]"
  19. end
  20. local function formspec_read(owner, title, string, text, page, page_max)
  21. return "label[0.5,0.5;" .. esc(S("by @1", owner)) .. "]" ..
  22. "tablecolumns[color;text]" ..
  23. "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
  24. "table[0.4,0;7,0.5;title;#FFFF00," .. esc(title) .. "]" ..
  25. "textarea[0.5,1.5;7.5,7;;" ..
  26. esc(string ~= "" and string or text) .. ";]" ..
  27. "button[2.4,7.6;0.8,0.8;book_prev;<]" ..
  28. "label[3.2,7.7;" .. esc(S("Page @1 of @2", page, page_max)) .. "]" ..
  29. "button[4.9,7.6;0.8,0.8;book_next;>]"
  30. end
  31. local function formspec_string(lpp, page, lines, string)
  32. for i = ((lpp * page) - lpp) + 1, lpp * page do
  33. if not lines[i] then break end
  34. string = string .. lines[i] .. "\n"
  35. end
  36. return string
  37. end
  38. local book_writers = {}
  39. minetest.register_on_leaveplayer(function(player)
  40. book_writers[player:get_player_name()] = nil
  41. end)
  42. local tab_number
  43. local lpp = 14 -- Lines per book's page
  44. local function book_on_use(itemstack, user)
  45. local player_name = user:get_player_name()
  46. local meta = itemstack:get_meta()
  47. local title, text, owner = "", "", player_name
  48. local page, page_max, lines, string = 1, 1, {}, ""
  49. -- Backwards compatibility
  50. local old_data = minetest.deserialize(itemstack:get_meta():get_string(""))
  51. if old_data then
  52. meta:from_table({ fields = old_data })
  53. end
  54. local data = meta:to_table().fields
  55. if data.owner then
  56. title = data.title or ""
  57. text = data.text or ""
  58. owner = data.owner
  59. for str in (text .. "\n"):gmatch("([^\n]*)[\n]") do
  60. lines[#lines+1] = str
  61. end
  62. if data.page then
  63. page = data.page
  64. page_max = data.page_max
  65. string = formspec_string(lpp, page, lines, string)
  66. end
  67. end
  68. local formspec
  69. if title == "" and text == "" then
  70. formspec = formspec_write(title, text)
  71. elseif owner == player_name then
  72. local tab = tab_number or 1
  73. if tab == 2 then
  74. formspec = formspec_core(tab) ..
  75. formspec_read(owner, title, string, text, page, page_max)
  76. else
  77. formspec = formspec_core(tab) .. formspec_write(title, text)
  78. end
  79. else
  80. formspec = formspec_read(owner, title, string, text, page, page_max)
  81. end
  82. minetest.show_formspec(player_name, "default:book", formspec_size .. formspec)
  83. -- Store the wield index in case the user accidentally switches before the formspec is shown
  84. book_writers[player_name] = {wield_index = user:get_wield_index()}
  85. return itemstack
  86. end
  87. local max_text_size = 10000
  88. local max_title_size = 80
  89. local short_title_size = 35
  90. minetest.register_on_player_receive_fields(function(player, formname, fields)
  91. if formname ~= "default:book" then
  92. return
  93. end
  94. local player_name = player:get_player_name()
  95. local inv = player:get_inventory()
  96. if not book_writers[player_name] then
  97. return
  98. end
  99. local wield_index = book_writers[player_name].wield_index
  100. local wield_list = player:get_wield_list()
  101. local stack = inv:get_stack(wield_list, wield_index)
  102. local written = stack:get_name() == "default:book_written"
  103. if stack:get_name() ~= "default:book" and not written then
  104. -- No book in the wield slot, abort & inform the player
  105. minetest.chat_send_player(player_name, S("The book you were writing to mysteriously disappeared."))
  106. return
  107. end
  108. local data = stack:get_meta():to_table().fields
  109. local title = data.title or ""
  110. local text = data.text or ""
  111. if fields.book_header ~= nil and data.owner == player_name then
  112. local contents
  113. local tab = tonumber(fields.book_header)
  114. if tab == 1 then
  115. contents = formspec_core(tab) ..
  116. formspec_write(title, text)
  117. elseif tab == 2 then
  118. local lines, string = {}, ""
  119. for str in (text .. "\n"):gmatch("([^\n]*)[\n]") do
  120. lines[#lines+1] = str
  121. end
  122. string = formspec_string(lpp, data.page, lines, string)
  123. contents = formspec_read(player_name, title, string,
  124. text, data.page, data.page_max)
  125. else
  126. return -- malicious data
  127. end
  128. tab_number = tab
  129. local formspec = formspec_size .. formspec_core(tab) .. contents
  130. minetest.show_formspec(player_name, "default:book", formspec)
  131. return
  132. end
  133. if fields.quit then
  134. book_writers[player_name] = nil
  135. end
  136. if fields.save and fields.title and fields.text then
  137. local new_stack
  138. if not written then
  139. local count = stack:get_count()
  140. if count == 1 then
  141. stack:set_name("default:book_written")
  142. else
  143. stack:set_count(count - 1)
  144. new_stack = ItemStack("default:book_written")
  145. end
  146. end
  147. if data.owner ~= player_name and title ~= "" and text ~= "" then
  148. return
  149. end
  150. if not data then data = {} end
  151. data.title = fields.title:sub(1, max_title_size)
  152. data.owner = player:get_player_name()
  153. local short_title = data.title
  154. -- Don't bother triming the title if the trailing dots would make it longer
  155. if #short_title > short_title_size + 3 then
  156. short_title = short_title:sub(1, short_title_size) .. "..."
  157. end
  158. data.description = S("\"@1\" by @2", short_title, data.owner)
  159. data.text = fields.text:sub(1, max_text_size)
  160. data.text = data.text:gsub("\r\n", "\n"):gsub("\r", "\n")
  161. data.text = data.text:gsub("[%z\1-\8\11-\31\127]", "") -- strip naughty control characters (keeps \t and \n)
  162. data.page = 1
  163. data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
  164. if new_stack then
  165. new_stack:get_meta():from_table({ fields = data })
  166. if inv:room_for_item("main", new_stack) then
  167. inv:add_item("main", new_stack)
  168. else
  169. minetest.add_item(player:get_pos(), new_stack)
  170. end
  171. else
  172. stack:get_meta():from_table({ fields = data })
  173. end
  174. elseif fields.book_next or fields.book_prev then
  175. if not data.page then
  176. return
  177. end
  178. data.page = tonumber(data.page)
  179. data.page_max = tonumber(data.page_max)
  180. if fields.book_next then
  181. data.page = data.page + 1
  182. if data.page > data.page_max then
  183. data.page = 1
  184. end
  185. else
  186. data.page = data.page - 1
  187. if data.page == 0 then
  188. data.page = data.page_max
  189. end
  190. end
  191. stack:get_meta():from_table({fields = data})
  192. stack = book_on_use(stack, player)
  193. end
  194. -- Update stack
  195. inv:set_stack(wield_list, wield_index, stack)
  196. end)
  197. --
  198. -- Craftitem registry
  199. --
  200. minetest.register_craftitem("default:blueberries", {
  201. description = S("Blueberries"),
  202. inventory_image = "default_blueberries.png",
  203. groups = {food_blueberries = 1, food_berry = 1},
  204. on_use = minetest.item_eat(2),
  205. })
  206. minetest.register_craftitem("default:book", {
  207. description = S("Book"),
  208. inventory_image = "default_book.png",
  209. groups = {book = 1, flammable = 3},
  210. on_use = book_on_use,
  211. })
  212. minetest.register_craftitem("default:book_written", {
  213. description = S("Book with Text"),
  214. inventory_image = "default_book_written.png",
  215. groups = {book = 1, not_in_creative_inventory = 1, flammable = 3},
  216. stack_max = 1,
  217. on_use = book_on_use,
  218. })
  219. minetest.register_craftitem("default:bronze_ingot", {
  220. description = S("Bronze Ingot"),
  221. inventory_image = "default_bronze_ingot.png"
  222. })
  223. minetest.register_craftitem("default:clay_brick", {
  224. description = S("Clay Brick"),
  225. inventory_image = "default_clay_brick.png",
  226. })
  227. minetest.register_craftitem("default:clay_lump", {
  228. description = S("Clay Lump"),
  229. inventory_image = "default_clay_lump.png",
  230. })
  231. minetest.register_craftitem("default:coal_lump", {
  232. description = S("Coal Lump"),
  233. inventory_image = "default_coal_lump.png",
  234. groups = {coal = 1, flammable = 1}
  235. })
  236. minetest.register_craftitem("default:copper_ingot", {
  237. description = S("Copper Ingot"),
  238. inventory_image = "default_copper_ingot.png"
  239. })
  240. minetest.register_craftitem("default:copper_lump", {
  241. description = S("Copper Lump"),
  242. inventory_image = "default_copper_lump.png"
  243. })
  244. minetest.register_craftitem("default:diamond", {
  245. description = S("Diamond"),
  246. inventory_image = "default_diamond.png",
  247. })
  248. minetest.register_craftitem("default:flint", {
  249. description = S("Flint"),
  250. inventory_image = "default_flint.png"
  251. })
  252. minetest.register_craftitem("default:gold_ingot", {
  253. description = S("Gold Ingot"),
  254. inventory_image = "default_gold_ingot.png"
  255. })
  256. minetest.register_craftitem("default:gold_lump", {
  257. description = S("Gold Lump"),
  258. inventory_image = "default_gold_lump.png"
  259. })
  260. minetest.register_craftitem("default:iron_lump", {
  261. description = S("Iron Lump"),
  262. inventory_image = "default_iron_lump.png"
  263. })
  264. minetest.register_craftitem("default:mese_crystal", {
  265. description = S("Mese Crystal"),
  266. inventory_image = "default_mese_crystal.png",
  267. })
  268. minetest.register_craftitem("default:mese_crystal_fragment", {
  269. description = S("Mese Crystal Fragment"),
  270. inventory_image = "default_mese_crystal_fragment.png",
  271. })
  272. minetest.register_craftitem("default:obsidian_shard", {
  273. description = S("Obsidian Shard"),
  274. inventory_image = "default_obsidian_shard.png",
  275. })
  276. minetest.register_craftitem("default:paper", {
  277. description = S("Paper"),
  278. inventory_image = "default_paper.png",
  279. groups = {flammable = 3},
  280. })
  281. minetest.register_craftitem("default:steel_ingot", {
  282. description = S("Steel Ingot"),
  283. inventory_image = "default_steel_ingot.png"
  284. })
  285. minetest.register_craftitem("default:stick", {
  286. description = S("Stick"),
  287. inventory_image = "default_stick.png",
  288. groups = {stick = 1, flammable = 2},
  289. })
  290. minetest.register_craftitem("default:tin_ingot", {
  291. description = S("Tin Ingot"),
  292. inventory_image = "default_tin_ingot.png"
  293. })
  294. minetest.register_craftitem("default:tin_lump", {
  295. description = S("Tin Lump"),
  296. inventory_image = "default_tin_lump.png"
  297. })
  298. --
  299. -- Crafting recipes
  300. --
  301. minetest.register_craft({
  302. output = "default:book",
  303. recipe = {
  304. {"default:paper"},
  305. {"default:paper"},
  306. {"default:paper"},
  307. }
  308. })
  309. default.register_craft_metadata_copy("default:book", "default:book_written")
  310. minetest.register_craft({
  311. output = "default:bronze_ingot 9",
  312. recipe = {
  313. {"default:copper_ingot", "default:copper_ingot", "default:copper_ingot"},
  314. {"default:copper_ingot", "default:tin_ingot", "default:copper_ingot"},
  315. {"default:copper_ingot", "default:copper_ingot", "default:copper_ingot"},
  316. }
  317. })
  318. minetest.register_craft({
  319. output = "default:clay_brick 4",
  320. recipe = {
  321. {"default:brick"},
  322. }
  323. })
  324. minetest.register_craft({
  325. output = "default:clay_lump 4",
  326. recipe = {
  327. {"default:clay"},
  328. }
  329. })
  330. minetest.register_craft({
  331. output = "default:coal_lump 9",
  332. recipe = {
  333. {"default:coalblock"},
  334. }
  335. })
  336. minetest.register_craft({
  337. output = "default:copper_ingot 9",
  338. recipe = {
  339. {"default:copperblock"},
  340. }
  341. })
  342. minetest.register_craft({
  343. output = "default:diamond 9",
  344. recipe = {
  345. {"default:diamondblock"},
  346. }
  347. })
  348. minetest.register_craft({
  349. output = "default:gold_ingot 9",
  350. recipe = {
  351. {"default:goldblock"},
  352. }
  353. })
  354. minetest.register_craft({
  355. output = "default:mese_crystal",
  356. recipe = {
  357. {"default:mese_crystal_fragment", "default:mese_crystal_fragment", "default:mese_crystal_fragment"},
  358. {"default:mese_crystal_fragment", "default:mese_crystal_fragment", "default:mese_crystal_fragment"},
  359. {"default:mese_crystal_fragment", "default:mese_crystal_fragment", "default:mese_crystal_fragment"},
  360. }
  361. })
  362. minetest.register_craft({
  363. output = "default:mese_crystal 9",
  364. recipe = {
  365. {"default:mese"},
  366. }
  367. })
  368. minetest.register_craft({
  369. output = "default:mese_crystal_fragment 9",
  370. recipe = {
  371. {"default:mese_crystal"},
  372. }
  373. })
  374. minetest.register_craft({
  375. output = "default:obsidian_shard 9",
  376. recipe = {
  377. {"default:obsidian"}
  378. }
  379. })
  380. minetest.register_craft({
  381. output = "default:paper",
  382. recipe = {
  383. {"default:papyrus", "default:papyrus", "default:papyrus"},
  384. }
  385. })
  386. minetest.register_craft({
  387. output = "default:steel_ingot 9",
  388. recipe = {
  389. {"default:steelblock"},
  390. }
  391. })
  392. minetest.register_craft({
  393. output = "default:stick 4",
  394. recipe = {
  395. {"group:wood"},
  396. }
  397. })
  398. minetest.register_craft({
  399. output = "default:tin_ingot 9",
  400. recipe = {
  401. {"default:tinblock"},
  402. }
  403. })
  404. --
  405. -- Cooking recipes
  406. --
  407. minetest.register_craft({
  408. type = "cooking",
  409. output = "default:clay_brick",
  410. recipe = "default:clay_lump",
  411. })
  412. minetest.register_craft({
  413. type = "cooking",
  414. output = "default:copper_ingot",
  415. recipe = "default:copper_lump",
  416. })
  417. minetest.register_craft({
  418. type = "cooking",
  419. output = "default:gold_ingot",
  420. recipe = "default:gold_lump",
  421. })
  422. minetest.register_craft({
  423. type = "cooking",
  424. output = "default:steel_ingot",
  425. recipe = "default:iron_lump",
  426. })
  427. minetest.register_craft({
  428. type = "cooking",
  429. output = "default:tin_ingot",
  430. recipe = "default:tin_lump",
  431. })
  432. --
  433. -- Fuels
  434. --
  435. minetest.register_craft({
  436. type = "fuel",
  437. recipe = "default:book",
  438. burntime = 3,
  439. })
  440. minetest.register_craft({
  441. type = "fuel",
  442. recipe = "default:book_written",
  443. burntime = 3,
  444. })
  445. minetest.register_craft({
  446. type = "fuel",
  447. recipe = "default:coal_lump",
  448. burntime = 40,
  449. })
  450. minetest.register_craft({
  451. type = "fuel",
  452. recipe = "default:paper",
  453. burntime = 1,
  454. })
  455. minetest.register_craft({
  456. type = "fuel",
  457. recipe = "group:stick",
  458. burntime = 1,
  459. })