craftitems.lua 9.3 KB

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