craftitems.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. end
  126. tab_number = tab
  127. local formspec = formspec_size .. formspec_core(tab) .. contents
  128. minetest.show_formspec(player_name, "default:book", formspec)
  129. return
  130. end
  131. if fields.quit then
  132. book_writers[player_name] = nil
  133. end
  134. if fields.save and fields.title and fields.text then
  135. local new_stack
  136. if not written then
  137. local count = stack:get_count()
  138. if count == 1 then
  139. stack:set_name("default:book_written")
  140. else
  141. stack:set_count(count - 1)
  142. new_stack = ItemStack("default:book_written")
  143. end
  144. end
  145. if data.owner ~= player_name and title ~= "" and text ~= "" then
  146. return
  147. end
  148. if not data then data = {} end
  149. data.title = fields.title:sub(1, max_title_size)
  150. data.owner = player:get_player_name()
  151. local short_title = data.title
  152. -- Don't bother triming the title if the trailing dots would make it longer
  153. if #short_title > short_title_size + 3 then
  154. short_title = short_title:sub(1, short_title_size) .. "..."
  155. end
  156. data.description = S("\"@1\" by @2", short_title, data.owner)
  157. data.text = fields.text:sub(1, max_text_size)
  158. data.text = data.text:gsub("\r\n", "\n"):gsub("\r", "\n")
  159. data.text = data.text:gsub("[%z\1-\8\11-\31\127]", "") -- strip naughty control characters (keeps \t and \n)
  160. data.page = 1
  161. data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
  162. if new_stack then
  163. new_stack:get_meta():from_table({ fields = data })
  164. if inv:room_for_item("main", new_stack) then
  165. inv:add_item("main", new_stack)
  166. else
  167. minetest.add_item(player:get_pos(), new_stack)
  168. end
  169. else
  170. stack:get_meta():from_table({ fields = data })
  171. end
  172. elseif fields.book_next or fields.book_prev then
  173. if not data.page then
  174. return
  175. end
  176. data.page = tonumber(data.page)
  177. data.page_max = tonumber(data.page_max)
  178. if fields.book_next then
  179. data.page = data.page + 1
  180. if data.page > data.page_max then
  181. data.page = 1
  182. end
  183. else
  184. data.page = data.page - 1
  185. if data.page == 0 then
  186. data.page = data.page_max
  187. end
  188. end
  189. stack:get_meta():from_table({fields = data})
  190. stack = book_on_use(stack, player)
  191. end
  192. -- Update stack
  193. inv:set_stack(wield_list, wield_index, stack)
  194. end)
  195. --
  196. -- Craftitem registry
  197. --
  198. minetest.register_craftitem("default:blueberries", {
  199. description = S("Blueberries"),
  200. inventory_image = "default_blueberries.png",
  201. groups = {food_blueberries = 1, food_berry = 1},
  202. on_use = minetest.item_eat(2),
  203. })
  204. minetest.register_craftitem("default:book", {
  205. description = S("Book"),
  206. inventory_image = "default_book.png",
  207. groups = {book = 1, flammable = 3},
  208. on_use = book_on_use,
  209. })
  210. minetest.register_craftitem("default:book_written", {
  211. description = S("Book with Text"),
  212. inventory_image = "default_book_written.png",
  213. groups = {book = 1, not_in_creative_inventory = 1, flammable = 3},
  214. stack_max = 1,
  215. on_use = book_on_use,
  216. })
  217. minetest.register_craftitem("default:bronze_ingot", {
  218. description = S("Bronze Ingot"),
  219. inventory_image = "default_bronze_ingot.png"
  220. })
  221. minetest.register_craftitem("default:clay_brick", {
  222. description = S("Clay Brick"),
  223. inventory_image = "default_clay_brick.png",
  224. })
  225. minetest.register_craftitem("default:clay_lump", {
  226. description = S("Clay Lump"),
  227. inventory_image = "default_clay_lump.png",
  228. })
  229. minetest.register_craftitem("default:coal_lump", {
  230. description = S("Coal Lump"),
  231. inventory_image = "default_coal_lump.png",
  232. groups = {coal = 1, flammable = 1}
  233. })
  234. minetest.register_craftitem("default:copper_ingot", {
  235. description = S("Copper Ingot"),
  236. inventory_image = "default_copper_ingot.png"
  237. })
  238. minetest.register_craftitem("default:copper_lump", {
  239. description = S("Copper Lump"),
  240. inventory_image = "default_copper_lump.png"
  241. })
  242. minetest.register_craftitem("default:diamond", {
  243. description = S("Diamond"),
  244. inventory_image = "default_diamond.png",
  245. })
  246. minetest.register_craftitem("default:flint", {
  247. description = S("Flint"),
  248. inventory_image = "default_flint.png"
  249. })
  250. minetest.register_craftitem("default:gold_ingot", {
  251. description = S("Gold Ingot"),
  252. inventory_image = "default_gold_ingot.png"
  253. })
  254. minetest.register_craftitem("default:gold_lump", {
  255. description = S("Gold Lump"),
  256. inventory_image = "default_gold_lump.png"
  257. })
  258. minetest.register_craftitem("default:iron_lump", {
  259. description = S("Iron Lump"),
  260. inventory_image = "default_iron_lump.png"
  261. })
  262. minetest.register_craftitem("default:mese_crystal", {
  263. description = S("Mese Crystal"),
  264. inventory_image = "default_mese_crystal.png",
  265. })
  266. minetest.register_craftitem("default:mese_crystal_fragment", {
  267. description = S("Mese Crystal Fragment"),
  268. inventory_image = "default_mese_crystal_fragment.png",
  269. })
  270. minetest.register_craftitem("default:obsidian_shard", {
  271. description = S("Obsidian Shard"),
  272. inventory_image = "default_obsidian_shard.png",
  273. })
  274. minetest.register_craftitem("default:paper", {
  275. description = S("Paper"),
  276. inventory_image = "default_paper.png",
  277. groups = {flammable = 3},
  278. })
  279. minetest.register_craftitem("default:steel_ingot", {
  280. description = S("Steel Ingot"),
  281. inventory_image = "default_steel_ingot.png"
  282. })
  283. minetest.register_craftitem("default:stick", {
  284. description = S("Stick"),
  285. inventory_image = "default_stick.png",
  286. groups = {stick = 1, flammable = 2},
  287. })
  288. minetest.register_craftitem("default:tin_ingot", {
  289. description = S("Tin Ingot"),
  290. inventory_image = "default_tin_ingot.png"
  291. })
  292. minetest.register_craftitem("default:tin_lump", {
  293. description = S("Tin Lump"),
  294. inventory_image = "default_tin_lump.png"
  295. })
  296. --
  297. -- Crafting recipes
  298. --
  299. minetest.register_craft({
  300. output = "default:book",
  301. recipe = {
  302. {"default:paper"},
  303. {"default:paper"},
  304. {"default:paper"},
  305. }
  306. })
  307. default.register_craft_metadata_copy("default:book", "default:book_written")
  308. minetest.register_craft({
  309. output = "default:bronze_ingot 9",
  310. recipe = {
  311. {"default:copper_ingot", "default:copper_ingot", "default:copper_ingot"},
  312. {"default:copper_ingot", "default:tin_ingot", "default:copper_ingot"},
  313. {"default:copper_ingot", "default:copper_ingot", "default:copper_ingot"},
  314. }
  315. })
  316. minetest.register_craft({
  317. output = "default:clay_brick 4",
  318. recipe = {
  319. {"default:brick"},
  320. }
  321. })
  322. minetest.register_craft({
  323. output = "default:clay_lump 4",
  324. recipe = {
  325. {"default:clay"},
  326. }
  327. })
  328. minetest.register_craft({
  329. output = "default:coal_lump 9",
  330. recipe = {
  331. {"default:coalblock"},
  332. }
  333. })
  334. minetest.register_craft({
  335. output = "default:copper_ingot 9",
  336. recipe = {
  337. {"default:copperblock"},
  338. }
  339. })
  340. minetest.register_craft({
  341. output = "default:diamond 9",
  342. recipe = {
  343. {"default:diamondblock"},
  344. }
  345. })
  346. minetest.register_craft({
  347. output = "default:gold_ingot 9",
  348. recipe = {
  349. {"default:goldblock"},
  350. }
  351. })
  352. minetest.register_craft({
  353. output = "default:mese_crystal",
  354. recipe = {
  355. {"default:mese_crystal_fragment", "default:mese_crystal_fragment", "default:mese_crystal_fragment"},
  356. {"default:mese_crystal_fragment", "default:mese_crystal_fragment", "default:mese_crystal_fragment"},
  357. {"default:mese_crystal_fragment", "default:mese_crystal_fragment", "default:mese_crystal_fragment"},
  358. }
  359. })
  360. minetest.register_craft({
  361. output = "default:mese_crystal 9",
  362. recipe = {
  363. {"default:mese"},
  364. }
  365. })
  366. minetest.register_craft({
  367. output = "default:mese_crystal_fragment 9",
  368. recipe = {
  369. {"default:mese_crystal"},
  370. }
  371. })
  372. minetest.register_craft({
  373. output = "default:obsidian_shard 9",
  374. recipe = {
  375. {"default:obsidian"}
  376. }
  377. })
  378. minetest.register_craft({
  379. output = "default:paper",
  380. recipe = {
  381. {"default:papyrus", "default:papyrus", "default:papyrus"},
  382. }
  383. })
  384. minetest.register_craft({
  385. output = "default:steel_ingot 9",
  386. recipe = {
  387. {"default:steelblock"},
  388. }
  389. })
  390. minetest.register_craft({
  391. output = "default:stick 4",
  392. recipe = {
  393. {"group:wood"},
  394. }
  395. })
  396. minetest.register_craft({
  397. output = "default:tin_ingot 9",
  398. recipe = {
  399. {"default:tinblock"},
  400. }
  401. })
  402. --
  403. -- Cooking recipes
  404. --
  405. minetest.register_craft({
  406. type = "cooking",
  407. output = "default:clay_brick",
  408. recipe = "default:clay_lump",
  409. })
  410. minetest.register_craft({
  411. type = "cooking",
  412. output = "default:copper_ingot",
  413. recipe = "default:copper_lump",
  414. })
  415. minetest.register_craft({
  416. type = "cooking",
  417. output = "default:gold_ingot",
  418. recipe = "default:gold_lump",
  419. })
  420. minetest.register_craft({
  421. type = "cooking",
  422. output = "default:steel_ingot",
  423. recipe = "default:iron_lump",
  424. })
  425. minetest.register_craft({
  426. type = "cooking",
  427. output = "default:tin_ingot",
  428. recipe = "default:tin_lump",
  429. })
  430. --
  431. -- Fuels
  432. --
  433. minetest.register_craft({
  434. type = "fuel",
  435. recipe = "default:book",
  436. burntime = 3,
  437. })
  438. minetest.register_craft({
  439. type = "fuel",
  440. recipe = "default:book_written",
  441. burntime = 3,
  442. })
  443. minetest.register_craft({
  444. type = "fuel",
  445. recipe = "default:coal_lump",
  446. burntime = 40,
  447. })
  448. minetest.register_craft({
  449. type = "fuel",
  450. recipe = "default:paper",
  451. burntime = 1,
  452. })
  453. minetest.register_craft({
  454. type = "fuel",
  455. recipe = "group:stick",
  456. burntime = 1,
  457. })