api.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. --[[
  2. Ingots - allows the placemant of ingots in the world
  3. Copyright (C) 2018 Skamiz Kazzarch
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15. ]]--
  16. -- takes an item name and a texture name and a boolean whether the ingots are big
  17. function ingots.register_ingots(ingot_item, texture, is_big)
  18. --checks, whether the item name is a valid item (thanks 'puzzlecube')
  19. if not minetest.registered_items[ingot_item] then
  20. minetest.log("warning", ingot_item.." is not registered. Skipping ingot registration")
  21. return
  22. end
  23. -- de hardcoded modname, which allows the api to be properly used from within other mods (thanks 'argyle')
  24. local mod_name = minetest.get_current_modname()
  25. local mod_prefix = mod_name .. ":"
  26. local stack_size = 64
  27. local texture_prefix = "ingot_"
  28. --gets item name witout mod part, to be used in the deffinition of the new nodes
  29. local ingot_name = string.sub(ingot_item, string.find(ingot_item, ":", 1, true) +1, -1)
  30. if is_big then
  31. ingot_name = ingot_name .. "_big"
  32. stack_size = 8
  33. texture_prefix = "ingot_big_"
  34. end
  35. --this way there is no need for a separate on_punch function for a stack of 1 ingot
  36. minetest.register_alias(mod_prefix .. ingot_name .."_0", "air")
  37. --gives the ingot_item the ability to be placed and increas already placed stacks of ingots
  38. minetest.override_item(ingot_item, {
  39. on_place = function (itemstack, placer, pointed_thing)
  40. local pos = minetest.get_pointed_thing_position(pointed_thing, true)
  41. if minetest.is_protected(pos, placer:get_player_name()) and not minetest.check_player_privs(placer, "protection_bypass") then
  42. return
  43. end
  44. if pointed_thing["type"] == "node" then
  45. local name = minetest.get_node(pointed_thing.under).name
  46. -- call on_rightclick function of pointed node if aplicable and not sneak
  47. -- might or might not break if item is placed by mod devices
  48. if minetest.registered_nodes[name].on_rightclick and
  49. not placer:get_player_control().sneak
  50. then
  51. minetest.registered_nodes[name].on_rightclick(pointed_thing.under,
  52. minetest.get_node(pointed_thing.under),
  53. placer,
  54. itemstack)
  55. elseif string.find(name, mod_prefix .. ingot_name) then
  56. local count = string.gsub(name, "%D*", "")
  57. if stack_size > minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]._ingot_count then
  58. minetest.set_node(pointed_thing.under, {name = mod_prefix .. ingot_name .."_" .. count + 1, param2 = minetest.get_node(pointed_thing.under).param2})
  59. if not (creative and creative.is_enabled_for and creative.is_enabled_for(placer:get_player_name())) then
  60. itemstack:take_item()
  61. end
  62. elseif minetest.get_node(pointed_thing.above).name == "air" then
  63. minetest.set_node(pointed_thing.above, {name = mod_prefix .. ingot_name .."_1"})
  64. if not (creative and creative.is_enabled_for and creative.is_enabled_for(placer:get_player_name())) then
  65. itemstack:take_item()
  66. end
  67. end
  68. elseif minetest.get_node(pointed_thing.above).name == "air" then
  69. minetest.set_node(pointed_thing.above, {name = mod_prefix .. ingot_name .."_1"})
  70. if not (creative and creative.is_enabled_for and creative.is_enabled_for(placer:get_player_name())) then
  71. itemstack:take_item()
  72. end
  73. end
  74. return itemstack
  75. end
  76. end
  77. })
  78. --registers 'stack_size' number of nodes, each has one more ingot in it than the last
  79. for i = 1, stack_size do
  80. local box = {
  81. type = "fixed",
  82. fixed = {
  83. --rectangular box which encompases all placed ingots
  84. ingots.get_box(is_big, i),
  85. },
  86. }
  87. minetest.register_node(mod_prefix .. ingot_name .. "_" .. i,{
  88. description = "ingots",
  89. drawtype = "mesh",
  90. tiles = {texture},
  91. mesh = texture_prefix .. i .. ".obj",
  92. selection_box = box,
  93. collision_box = box,
  94. paramtype = 'light',
  95. paramtype2 = "facedir",
  96. groups = {cracky = 3, level = 2, not_in_creative_inventory = 1},
  97. drop = ingot_item .. " " .. i,
  98. on_punch = function (pos, node, puncher, pointed_thing)
  99. if puncher then
  100. local wield = puncher:get_wielded_item()
  101. --checks, so that a stack can be taken appart only by hand or relevant ingot_item
  102. if wield:get_name() == ingot_item or
  103. wield:get_count() == 0 then
  104. if minetest.is_protected(pos, puncher:get_player_name()) and not minetest.check_player_privs(puncher, "protection_bypass") then
  105. return
  106. end
  107. minetest.set_node(pos, {name = mod_prefix .. ingot_name .."_" .. i - 1, param2 = node.param2})
  108. if not (creative and creative.is_enabled_for and creative.is_enabled_for(puncher:get_player_name())) then
  109. local stack = ItemStack(ingot_item)
  110. puncher:get_inventory():add_item("main", stack)
  111. end
  112. end
  113. end
  114. end,
  115. _ingot_name = ingot_name,
  116. _ingot_count = i,
  117. })
  118. end
  119. end
  120. --returns an apropriate nodebox for a given number of ingots
  121. function ingots.get_box(is_big, i)
  122. if is_big then return {-0.5, -0.5, -0.5, 0.5, (((i + 1 - ((i +1 )%2)) / 8) - 0.5), 0.5}
  123. else return {-0.5, -0.5, -0.5, 0.5, (((i - 1 - ((i-1)%8)) / 8) - 3) / 8, 0.5}
  124. end
  125. end