craftitems.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. -- mods/default/craftitems.lua
  2. minetest.register_craftitem("default:stick", {
  3. description = "Stick",
  4. inventory_image = "default_stick.png",
  5. groups = {stick = 1, flammable = 2},
  6. })
  7. minetest.register_craftitem("default:paper", {
  8. description = "Paper",
  9. inventory_image = "default_paper.png",
  10. groups = {flammable = 3},
  11. })
  12. local lpp = 14 -- Lines per book's page
  13. local function book_on_use(itemstack, user)
  14. local player_name = user:get_player_name()
  15. local meta = itemstack:get_meta()
  16. local title, text, owner = "", "", player_name
  17. local page, page_max, lines, string = 1, 1, {}, ""
  18. -- Backwards compatibility
  19. local old_data = minetest.deserialize(itemstack:get_metadata())
  20. if old_data then
  21. meta:from_table({ fields = old_data })
  22. end
  23. local data = meta:to_table().fields
  24. if data.owner then
  25. title = data.title
  26. text = data.text
  27. owner = data.owner
  28. for str in (text .. "\n"):gmatch("([^\n]*)[\n]") do
  29. lines[#lines+1] = str
  30. end
  31. if data.page then
  32. page = data.page
  33. page_max = data.page_max
  34. for i = ((lpp * page) - lpp) + 1, lpp * page do
  35. if not lines[i] then break end
  36. string = string .. lines[i] .. "\n"
  37. end
  38. end
  39. end
  40. local formspec
  41. if owner == player_name then
  42. formspec = "size[8,8]" .. default.gui_bg ..
  43. default.gui_bg_img ..
  44. "field[0.5,1;7.5,0;title;Title:;" ..
  45. minetest.formspec_escape(title) .. "]" ..
  46. "textarea[0.5,1.5;7.5,7;text;Contents:;" ..
  47. minetest.formspec_escape(text) .. "]" ..
  48. "button_exit[2.5,7.5;3,1;save;Save]"
  49. else
  50. formspec = "size[8,8]" .. default.gui_bg ..
  51. default.gui_bg_img ..
  52. "label[0.5,0.5;by " .. owner .. "]" ..
  53. "tablecolumns[color;text]" ..
  54. "tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
  55. "table[0.4,0;7,0.5;title;#FFFF00," .. minetest.formspec_escape(title) .. "]" ..
  56. "textarea[0.5,1.5;7.5,7;;" ..
  57. minetest.formspec_escape(string ~= "" and string or text) .. ";]" ..
  58. "button[2.4,7.6;0.8,0.8;book_prev;<]" ..
  59. "label[3.2,7.7;Page " .. page .. " of " .. page_max .. "]" ..
  60. "button[4.9,7.6;0.8,0.8;book_next;>]"
  61. end
  62. minetest.show_formspec(player_name, "default:book", formspec)
  63. return itemstack
  64. end
  65. local max_text_size = 10000
  66. local max_title_size = 80
  67. local short_title_size = 35
  68. minetest.register_on_player_receive_fields(function(player, formname, fields)
  69. if formname ~= "default:book" then return end
  70. local inv = player:get_inventory()
  71. local stack = player:get_wielded_item()
  72. if fields.save and fields.title and fields.text
  73. and fields.title ~= "" and fields.text ~= "" then
  74. local new_stack, data
  75. if stack:get_name() ~= "default:book_written" then
  76. local count = stack:get_count()
  77. if count == 1 then
  78. stack:set_name("default:book_written")
  79. else
  80. stack:set_count(count - 1)
  81. new_stack = ItemStack("default:book_written")
  82. end
  83. else
  84. data = stack:get_meta():to_table().fields
  85. end
  86. if data and data.owner and data.owner ~= player:get_player_name() then
  87. return
  88. end
  89. if not data then data = {} end
  90. data.title = fields.title:sub(1, max_title_size)
  91. data.owner = player:get_player_name()
  92. local short_title = data.title
  93. -- Don't bother triming the title if the trailing dots would make it longer
  94. if #short_title > short_title_size + 3 then
  95. short_title = short_title:sub(1, short_title_size) .. "..."
  96. end
  97. data.description = "\""..short_title.."\" by "..data.owner
  98. data.text = fields.text:sub(1, max_text_size)
  99. data.page = 1
  100. data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
  101. if new_stack then
  102. new_stack:get_meta():from_table({ fields = data })
  103. if inv:room_for_item("main", new_stack) then
  104. inv:add_item("main", new_stack)
  105. else
  106. minetest.add_item(player:getpos(), new_stack)
  107. end
  108. else
  109. stack:get_meta():from_table({ fields = data })
  110. end
  111. elseif fields.book_next or fields.book_prev then
  112. local data = stack:get_meta():to_table().fields
  113. if not data or not data.page then
  114. return
  115. end
  116. data.page = tonumber(data.page)
  117. data.page_max = tonumber(data.page_max)
  118. if fields.book_next then
  119. data.page = data.page + 1
  120. if data.page > data.page_max then
  121. data.page = 1
  122. end
  123. else
  124. data.page = data.page - 1
  125. if data.page == 0 then
  126. data.page = data.page_max
  127. end
  128. end
  129. stack:get_meta():from_table({fields = data})
  130. stack = book_on_use(stack, player)
  131. end
  132. -- Update stack
  133. player:set_wielded_item(stack)
  134. end)
  135. minetest.register_craftitem("default:book", {
  136. description = "Book",
  137. inventory_image = "default_book.png",
  138. groups = {book = 1, flammable = 3},
  139. on_use = book_on_use,
  140. })
  141. minetest.register_craftitem("default:book_written", {
  142. description = "Book With Text",
  143. inventory_image = "default_book_written.png",
  144. groups = {book = 1, not_in_creative_inventory = 1, flammable = 3},
  145. stack_max = 1,
  146. on_use = book_on_use,
  147. })
  148. minetest.register_craft({
  149. type = "shapeless",
  150. output = "default:book_written",
  151. recipe = {"default:book", "default:book_written"}
  152. })
  153. minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
  154. if itemstack:get_name() ~= "default:book_written" then
  155. return
  156. end
  157. local original
  158. local index
  159. for i = 1, player:get_inventory():get_size("craft") do
  160. if old_craft_grid[i]:get_name() == "default:book_written" then
  161. original = old_craft_grid[i]
  162. index = i
  163. end
  164. end
  165. if not original then
  166. return
  167. end
  168. local copymeta = original:get_meta():to_table()
  169. -- copy of the book held by player's mouse cursor
  170. itemstack:get_meta():from_table(copymeta)
  171. -- put the book with metadata back in the craft grid
  172. craft_inv:set_stack("craft", index, original)
  173. end)
  174. minetest.register_craftitem("default:skeleton_key", {
  175. description = "Skeleton Key",
  176. inventory_image = "default_key_skeleton.png",
  177. groups = {key = 1},
  178. on_use = function(itemstack, user, pointed_thing)
  179. if pointed_thing.type ~= "node" then
  180. return itemstack
  181. end
  182. local pos = pointed_thing.under
  183. local node = minetest.get_node(pos)
  184. if not node then
  185. return itemstack
  186. end
  187. local on_skeleton_key_use = minetest.registered_nodes[node.name].on_skeleton_key_use
  188. if not on_skeleton_key_use then
  189. return itemstack
  190. end
  191. -- make a new key secret in case the node callback needs it
  192. local random = math.random
  193. local newsecret = string.format(
  194. "%04x%04x%04x%04x",
  195. random(2^16) - 1, random(2^16) - 1,
  196. random(2^16) - 1, random(2^16) - 1)
  197. local secret, _, _ = on_skeleton_key_use(pos, user, newsecret)
  198. if secret then
  199. local inv = minetest.get_inventory({type="player", name=user:get_player_name()})
  200. -- update original itemstack
  201. itemstack:take_item()
  202. -- finish and return the new key
  203. local new_stack = ItemStack("default:key")
  204. local meta = new_stack:get_meta()
  205. meta:set_string("secret", secret)
  206. meta:set_string("description", "Key to "..user:get_player_name().."'s "
  207. ..minetest.registered_nodes[node.name].description)
  208. if itemstack:get_count() == 0 then
  209. itemstack = new_stack
  210. else
  211. if inv:add_item("main", new_stack):get_count() > 0 then
  212. minetest.add_item(user:getpos(), new_stack)
  213. end -- else: added to inventory successfully
  214. end
  215. return itemstack
  216. end
  217. end
  218. })
  219. minetest.register_craftitem("default:coal_lump", {
  220. description = "Coal Lump",
  221. inventory_image = "default_coal_lump.png",
  222. groups = {coal = 1, flammable = 1}
  223. })
  224. minetest.register_craftitem("default:iron_lump", {
  225. description = "Iron Lump",
  226. inventory_image = "default_iron_lump.png",
  227. })
  228. minetest.register_craftitem("default:copper_lump", {
  229. description = "Copper Lump",
  230. inventory_image = "default_copper_lump.png",
  231. })
  232. minetest.register_craftitem("default:tin_lump", {
  233. description = "Tin Lump",
  234. inventory_image = "default_tin_lump.png",
  235. })
  236. minetest.register_craftitem("default:mese_crystal", {
  237. description = "Mese Crystal",
  238. inventory_image = "default_mese_crystal.png",
  239. })
  240. minetest.register_craftitem("default:gold_lump", {
  241. description = "Gold Lump",
  242. inventory_image = "default_gold_lump.png",
  243. })
  244. minetest.register_craftitem("default:diamond", {
  245. description = "Diamond",
  246. inventory_image = "default_diamond.png",
  247. })
  248. minetest.register_craftitem("default:clay_lump", {
  249. description = "Clay Lump",
  250. inventory_image = "default_clay_lump.png",
  251. })
  252. minetest.register_craftitem("default:steel_ingot", {
  253. description = "Steel Ingot",
  254. inventory_image = "default_steel_ingot.png",
  255. })
  256. minetest.register_craftitem("default:copper_ingot", {
  257. description = "Copper Ingot",
  258. inventory_image = "default_copper_ingot.png",
  259. })
  260. minetest.register_craftitem("default:tin_ingot", {
  261. description = "Tin Ingot",
  262. inventory_image = "default_tin_ingot.png",
  263. })
  264. minetest.register_craftitem("default:bronze_ingot", {
  265. description = "Bronze Ingot",
  266. inventory_image = "default_bronze_ingot.png",
  267. })
  268. minetest.register_craftitem("default:gold_ingot", {
  269. description = "Gold Ingot",
  270. inventory_image = "default_gold_ingot.png"
  271. })
  272. minetest.register_craftitem("default:mese_crystal_fragment", {
  273. description = "Mese Crystal Fragment",
  274. inventory_image = "default_mese_crystal_fragment.png",
  275. })
  276. minetest.register_craftitem("default:clay_brick", {
  277. description = "Clay Brick",
  278. inventory_image = "default_clay_brick.png",
  279. })
  280. minetest.register_craftitem("default:obsidian_shard", {
  281. description = "Obsidian Shard",
  282. inventory_image = "default_obsidian_shard.png",
  283. })
  284. minetest.register_craftitem("default:flint", {
  285. description = "Flint",
  286. inventory_image = "default_flint.png"
  287. })