init.lua 3.6 KB

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