craftitems.lua 12 KB

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