init.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. if not minetest.global_exists("tinderbox") then tinderbox = {} end
  2. tinderbox.modpath = minetest.get_modpath("tinderbox")
  3. local TINDERBOX_USES = 256
  4. function tinderbox.on_use(itemstack, user, pt)
  5. if not user or not user:is_player() then return end
  6. local pname = user:get_player_name()
  7. local fakestack = ItemStack("dusts:coal")
  8. fakestack = real_torch.relight(fakestack, user, pt)
  9. if not fakestack or fakestack:get_count() ~= 0 then return end -- Tinderbox was not used.
  10. if itemstack:get_name() == "tinderbox:tinderbox" and itemstack:get_count() > 1 then
  11. -- Multiple tinderboxes are held. Must separate them and add wear.
  12. itemstack:take_item()
  13. local newitemstack = ItemStack("tinderbox:tinderbox " .. itemstack:get_count()) -- Make itemstack copy.
  14. -- Add leftover tinderboxes somewhere else in player's inventory.
  15. -- This has to be done inside an after() invocation, to avoid conflict with the on_use function.
  16. minetest.after(0, function()
  17. local user = minetest.get_player_by_name(pname)
  18. if not user or not user:is_player() then return end
  19. if user:get_hp() == 0 then return end
  20. local inv = user:get_inventory()
  21. if not inv then return end
  22. local leftover = inv:add_item("main", newitemstack)
  23. if leftover:get_count() ~= 0 then
  24. minetest.chat_send_player(pname, "# Server: You dropped your tinderboxes!")
  25. end
  26. minetest.item_drop(leftover, nil, vector.add(user:get_pos(), {x=0, y=1, z=0}))
  27. end)
  28. itemstack = ItemStack("tinderbox:tinderbox_used")
  29. itemstack:add_wear(65535/TINDERBOX_USES)
  30. return itemstack
  31. end
  32. -- Player holds single tinderbox. Must check if it is item or tool.
  33. if itemstack:get_name() == "tinderbox:tinderbox" then
  34. -- It's the craftitem; we must replace it with the tool.
  35. itemstack:set_name("tinderbox:tinderbox_used")
  36. itemstack:add_wear(65535/TINDERBOX_USES)
  37. return itemstack
  38. elseif itemstack:get_name() == "tinderbox:tinderbox_used" then
  39. -- It's the tool variant, we can add wear directly.
  40. itemstack:add_wear(65535/TINDERBOX_USES)
  41. return itemstack
  42. end
  43. end
  44. if not tinderbox.registered then
  45. local DESC = "Tinderbox"
  46. -- Tinderbox item (needed because only craftitems are stackable).
  47. minetest.register_craftitem("tinderbox:tinderbox", {
  48. description = DESC,
  49. inventory_image = "tinderbox_tinderbox.png",
  50. groups = {flammable = 2, not_repaired_by_anvil = 1, disable_repair = 1},
  51. on_use = function(...) return tinderbox.on_use(...) end,
  52. })
  53. -- Tinderbox tool (needed because items cannot be given a wear value).
  54. minetest.register_tool("tinderbox:tinderbox_used", {
  55. description = DESC,
  56. inventory_image = "tinderbox_tinderbox.png",
  57. groups = {flammable = 2, not_repaired_by_anvil = 1, disable_repair = 1},
  58. on_use = function(...) return tinderbox.on_use(...) end,
  59. wear_represents = "tinderbox",
  60. })
  61. -- Tinderbox craft.
  62. minetest.register_craft({
  63. output = "tinderbox:tinderbox",
  64. recipe = {
  65. {'', 'group:stick', ''},
  66. {'group:stick', 'dusts:coal', 'group:stick'},
  67. {'', 'group:stick', 'group:leaves'},
  68. },
  69. })
  70. minetest.register_craft({
  71. output = "tinderbox:tinderbox",
  72. recipe = {
  73. {'', 'group:stick', ''},
  74. {'group:stick', 'kalite:dust', 'group:stick'},
  75. {'', 'group:stick', 'group:leaves'},
  76. },
  77. })
  78. minetest.register_craft({
  79. output = "tinderbox:tinderbox",
  80. recipe = {
  81. {'', 'group:stick', ''},
  82. {'group:stick', 'charcoal:charcoal', 'group:stick'},
  83. {'', 'group:stick', 'group:leaves'},
  84. },
  85. })
  86. local c = "tinderbox:core"
  87. local f = tinderbox.modpath .. "/init.lua"
  88. reload.register_file(c, f, false)
  89. tinderbox.registered = true
  90. end