misc_register.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. -- Minetest: builtin/misc_register.lua
  2. --
  3. -- Make raw registration functions inaccessible to anyone except this file
  4. --
  5. local register_item_raw = minetest.register_item_raw
  6. minetest.register_item_raw = nil
  7. local register_alias_raw = minetest.register_alias_raw
  8. minetest.register_item_raw = nil
  9. --
  10. -- Item / entity / ABM registration functions
  11. --
  12. minetest.registered_abms = {}
  13. minetest.registered_entities = {}
  14. minetest.registered_items = {}
  15. minetest.registered_nodes = {}
  16. minetest.registered_craftitems = {}
  17. minetest.registered_tools = {}
  18. minetest.registered_aliases = {}
  19. -- For tables that are indexed by item name:
  20. -- If table[X] does not exist, default to table[minetest.registered_aliases[X]]
  21. local alias_metatable = {
  22. __index = function(t, name)
  23. return rawget(t, minetest.registered_aliases[name])
  24. end
  25. }
  26. setmetatable(minetest.registered_items, alias_metatable)
  27. setmetatable(minetest.registered_nodes, alias_metatable)
  28. setmetatable(minetest.registered_craftitems, alias_metatable)
  29. setmetatable(minetest.registered_tools, alias_metatable)
  30. -- These item names may not be used because they would interfere
  31. -- with legacy itemstrings
  32. local forbidden_item_names = {
  33. MaterialItem = true,
  34. MaterialItem2 = true,
  35. MaterialItem3 = true,
  36. NodeItem = true,
  37. node = true,
  38. CraftItem = true,
  39. craft = true,
  40. MBOItem = true,
  41. ToolItem = true,
  42. tool = true,
  43. }
  44. local function check_modname_prefix(name)
  45. if name:sub(1,1) == ":" then
  46. -- Escape the modname prefix enforcement mechanism
  47. return name:sub(2)
  48. else
  49. -- Modname prefix enforcement
  50. local expected_prefix = minetest.get_current_modname() .. ":"
  51. if name:sub(1, #expected_prefix) ~= expected_prefix then
  52. error("Name " .. name .. " does not follow naming conventions: " ..
  53. "\"modname:\" or \":\" prefix required")
  54. end
  55. local subname = name:sub(#expected_prefix+1)
  56. if subname:find("[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]") then
  57. error("Name " .. name .. " does not follow naming conventions: " ..
  58. "contains unallowed characters")
  59. end
  60. return name
  61. end
  62. end
  63. function minetest.register_abm(spec)
  64. -- Add to minetest.registered_abms
  65. minetest.registered_abms[#minetest.registered_abms+1] = spec
  66. end
  67. function minetest.register_entity(name, prototype)
  68. -- Check name
  69. if name == nil then
  70. error("Unable to register entity: Name is nil")
  71. end
  72. name = check_modname_prefix(tostring(name))
  73. prototype.name = name
  74. prototype.__index = prototype -- so that it can be used as a metatable
  75. -- Add to minetest.registered_entities
  76. minetest.registered_entities[name] = prototype
  77. end
  78. function minetest.register_item(name, itemdef)
  79. -- Check name
  80. if name == nil then
  81. error("Unable to register item: Name is nil")
  82. end
  83. name = check_modname_prefix(tostring(name))
  84. if forbidden_item_names[name] then
  85. error("Unable to register item: Name is forbidden: " .. name)
  86. end
  87. itemdef.name = name
  88. -- Apply defaults and add to registered_* table
  89. if itemdef.type == "node" then
  90. -- Use the nodebox as selection box if it's not set manually
  91. if itemdef.drawtype == "nodebox" and not itemdef.selection_box then
  92. itemdef.selection_box = itemdef.node_box
  93. elseif itemdef.drawtype == "fencelike" and not itemdef.selection_box then
  94. itemdef.selection_box = {
  95. type = "fixed",
  96. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2, 1/8},
  97. }
  98. end
  99. setmetatable(itemdef, {__index = minetest.nodedef_default})
  100. minetest.registered_nodes[itemdef.name] = itemdef
  101. elseif itemdef.type == "craft" then
  102. setmetatable(itemdef, {__index = minetest.craftitemdef_default})
  103. minetest.registered_craftitems[itemdef.name] = itemdef
  104. elseif itemdef.type == "tool" then
  105. setmetatable(itemdef, {__index = minetest.tooldef_default})
  106. minetest.registered_tools[itemdef.name] = itemdef
  107. elseif itemdef.type == "none" then
  108. setmetatable(itemdef, {__index = minetest.noneitemdef_default})
  109. else
  110. error("Unable to register item: Type is invalid: " .. dump(itemdef))
  111. end
  112. -- Flowing liquid uses param2
  113. if itemdef.type == "node" and itemdef.liquidtype == "flowing" then
  114. itemdef.paramtype2 = "flowingliquid"
  115. end
  116. -- BEGIN Legacy stuff
  117. if itemdef.cookresult_itemstring ~= nil and itemdef.cookresult_itemstring ~= "" then
  118. minetest.register_craft({
  119. type="cooking",
  120. output=itemdef.cookresult_itemstring,
  121. recipe=itemdef.name,
  122. cooktime=itemdef.furnace_cooktime
  123. })
  124. end
  125. if itemdef.furnace_burntime ~= nil and itemdef.furnace_burntime >= 0 then
  126. minetest.register_craft({
  127. type="fuel",
  128. recipe=itemdef.name,
  129. burntime=itemdef.furnace_burntime
  130. })
  131. end
  132. -- END Legacy stuff
  133. -- Disable all further modifications
  134. getmetatable(itemdef).__newindex = {}
  135. --minetest.log("Registering item: " .. itemdef.name)
  136. minetest.registered_items[itemdef.name] = itemdef
  137. minetest.registered_aliases[itemdef.name] = nil
  138. register_item_raw(itemdef)
  139. end
  140. function minetest.register_node(name, nodedef)
  141. nodedef.type = "node"
  142. minetest.register_item(name, nodedef)
  143. end
  144. function minetest.register_craftitem(name, craftitemdef)
  145. craftitemdef.type = "craft"
  146. -- BEGIN Legacy stuff
  147. if craftitemdef.inventory_image == nil and craftitemdef.image ~= nil then
  148. craftitemdef.inventory_image = craftitemdef.image
  149. end
  150. -- END Legacy stuff
  151. minetest.register_item(name, craftitemdef)
  152. end
  153. function minetest.register_tool(name, tooldef)
  154. tooldef.type = "tool"
  155. tooldef.stack_max = 1
  156. -- BEGIN Legacy stuff
  157. if tooldef.inventory_image == nil and tooldef.image ~= nil then
  158. tooldef.inventory_image = tooldef.image
  159. end
  160. if tooldef.tool_capabilities == nil and
  161. (tooldef.full_punch_interval ~= nil or
  162. tooldef.basetime ~= nil or
  163. tooldef.dt_weight ~= nil or
  164. tooldef.dt_crackiness ~= nil or
  165. tooldef.dt_crumbliness ~= nil or
  166. tooldef.dt_cuttability ~= nil or
  167. tooldef.basedurability ~= nil or
  168. tooldef.dd_weight ~= nil or
  169. tooldef.dd_crackiness ~= nil or
  170. tooldef.dd_crumbliness ~= nil or
  171. tooldef.dd_cuttability ~= nil) then
  172. tooldef.tool_capabilities = {
  173. full_punch_interval = tooldef.full_punch_interval,
  174. basetime = tooldef.basetime,
  175. dt_weight = tooldef.dt_weight,
  176. dt_crackiness = tooldef.dt_crackiness,
  177. dt_crumbliness = tooldef.dt_crumbliness,
  178. dt_cuttability = tooldef.dt_cuttability,
  179. basedurability = tooldef.basedurability,
  180. dd_weight = tooldef.dd_weight,
  181. dd_crackiness = tooldef.dd_crackiness,
  182. dd_crumbliness = tooldef.dd_crumbliness,
  183. dd_cuttability = tooldef.dd_cuttability,
  184. }
  185. end
  186. -- END Legacy stuff
  187. minetest.register_item(name, tooldef)
  188. end
  189. function minetest.register_alias(name, convert_to)
  190. if forbidden_item_names[name] then
  191. error("Unable to register alias: Name is forbidden: " .. name)
  192. end
  193. if minetest.registered_items[name] ~= nil then
  194. minetest.log("WARNING: Not registering alias, item with same name" ..
  195. " is already defined: " .. name .. " -> " .. convert_to)
  196. else
  197. --minetest.log("Registering alias: " .. name .. " -> " .. convert_to)
  198. minetest.registered_aliases[name] = convert_to
  199. register_alias_raw(name, convert_to)
  200. end
  201. end
  202. local register_biome_raw = minetest.register_biome
  203. minetest.registered_biomes = {}
  204. function minetest.register_biome(biome)
  205. minetest.registered_biomes[biome.name] = biome
  206. register_biome_raw(biome)
  207. end
  208. function minetest.on_craft(itemstack, player, old_craft_list, craft_inv)
  209. for _, func in ipairs(minetest.registered_on_crafts) do
  210. itemstack = func(itemstack, player, old_craft_list, craft_inv) or itemstack
  211. end
  212. return itemstack
  213. end
  214. function minetest.craft_predict(itemstack, player, old_craft_list, craft_inv)
  215. for _, func in ipairs(minetest.registered_craft_predicts) do
  216. itemstack = func(itemstack, player, old_craft_list, craft_inv) or itemstack
  217. end
  218. return itemstack
  219. end
  220. -- Alias the forbidden item names to "" so they can't be
  221. -- created via itemstrings (e.g. /give)
  222. local name
  223. for name in pairs(forbidden_item_names) do
  224. minetest.registered_aliases[name] = ""
  225. register_alias_raw(name, "")
  226. end
  227. -- Deprecated:
  228. -- Aliases for minetest.register_alias (how ironic...)
  229. --minetest.alias_node = minetest.register_alias
  230. --minetest.alias_tool = minetest.register_alias
  231. --minetest.alias_craftitem = minetest.register_alias
  232. --
  233. -- Built-in node definitions. Also defined in C.
  234. --
  235. minetest.register_item(":unknown", {
  236. type = "none",
  237. description = "Unknown Item",
  238. inventory_image = "unknown_item.png",
  239. on_place = minetest.item_place,
  240. on_drop = minetest.item_drop,
  241. groups = {not_in_creative_inventory=1},
  242. diggable = true,
  243. })
  244. minetest.register_node(":air", {
  245. description = "Air (you hacker you!)",
  246. inventory_image = "unknown_node.png",
  247. wield_image = "unknown_node.png",
  248. drawtype = "airlike",
  249. paramtype = "light",
  250. sunlight_propagates = true,
  251. walkable = false,
  252. pointable = false,
  253. diggable = false,
  254. buildable_to = true,
  255. air_equivalent = true,
  256. drop = "",
  257. groups = {not_in_creative_inventory=1},
  258. })
  259. minetest.register_node(":ignore", {
  260. description = "Ignore (you hacker you!)",
  261. inventory_image = "unknown_node.png",
  262. wield_image = "unknown_node.png",
  263. drawtype = "airlike",
  264. paramtype = "none",
  265. sunlight_propagates = false,
  266. walkable = false,
  267. pointable = false,
  268. diggable = false,
  269. buildable_to = true, -- A way to remove accidentally placed ignores
  270. air_equivalent = true,
  271. drop = "",
  272. groups = {not_in_creative_inventory=1},
  273. })
  274. -- The hand (bare definition)
  275. minetest.register_item(":", {
  276. type = "none",
  277. groups = {not_in_creative_inventory=1},
  278. })
  279. function minetest.override_item(name, redefinition)
  280. if redefinition.name ~= nil then
  281. error("Attempt to redefine name of "..name.." to "..dump(redefinition.name), 2)
  282. end
  283. if redefinition.type ~= nil then
  284. error("Attempt to redefine type of "..name.." to "..dump(redefinition.type), 2)
  285. end
  286. local item = minetest.registered_items[name]
  287. if not item then
  288. error("Attempt to override non-existent item "..name, 2)
  289. end
  290. for k, v in pairs(redefinition) do
  291. rawset(item, k, v)
  292. end
  293. register_item_raw(item)
  294. end
  295. function minetest.run_callbacks(callbacks, mode, ...)
  296. assert(type(callbacks) == "table")
  297. local cb_len = #callbacks
  298. if cb_len == 0 then
  299. if mode == 2 or mode == 3 then
  300. return true
  301. elseif mode == 4 or mode == 5 then
  302. return false
  303. end
  304. end
  305. local ret = nil
  306. for i = 1, cb_len do
  307. local cb_ret = callbacks[i](...)
  308. if mode == 0 and i == 1 then
  309. ret = cb_ret
  310. elseif mode == 1 and i == cb_len then
  311. ret = cb_ret
  312. elseif mode == 2 then
  313. if not cb_ret or i == 1 then
  314. ret = cb_ret
  315. end
  316. elseif mode == 3 then
  317. if cb_ret then
  318. return cb_ret
  319. end
  320. ret = cb_ret
  321. elseif mode == 4 then
  322. if (cb_ret and not ret) or i == 1 then
  323. ret = cb_ret
  324. end
  325. elseif mode == 5 and cb_ret then
  326. return cb_ret
  327. end
  328. end
  329. return ret
  330. end
  331. --
  332. -- Callback registration
  333. --
  334. local function make_registration()
  335. local t = {}
  336. local registerfunc = function(func) table.insert(t, func) end
  337. return t, registerfunc
  338. end
  339. local function make_registration_reverse()
  340. local t = {}
  341. local registerfunc = function(func) table.insert(t, 1, func) end
  342. return t, registerfunc
  343. end
  344. minetest.registered_on_chat_messages, minetest.register_on_chat_message = make_registration()
  345. minetest.registered_globalsteps, minetest.register_globalstep = make_registration()
  346. minetest.registered_on_mapgen_inits, minetest.register_on_mapgen_init = make_registration()
  347. minetest.registered_on_shutdown, minetest.register_on_shutdown = make_registration()
  348. minetest.registered_on_punchnodes, minetest.register_on_punchnode = make_registration()
  349. minetest.registered_on_placenodes, minetest.register_on_placenode = make_registration()
  350. minetest.registered_on_dignodes, minetest.register_on_dignode = make_registration()
  351. minetest.registered_on_generateds, minetest.register_on_generated = make_registration()
  352. minetest.registered_on_newplayers, minetest.register_on_newplayer = make_registration()
  353. minetest.registered_on_dieplayers, minetest.register_on_dieplayer = make_registration()
  354. minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make_registration()
  355. minetest.registered_on_prejoinplayers, minetest.register_on_prejoinplayer = make_registration()
  356. minetest.registered_on_joinplayers, minetest.register_on_joinplayer = make_registration()
  357. minetest.registered_on_leaveplayers, minetest.register_on_leaveplayer = make_registration()
  358. minetest.registered_on_player_receive_fields, minetest.register_on_player_receive_fields = make_registration_reverse()
  359. minetest.registered_on_cheats, minetest.register_on_cheat = make_registration()
  360. minetest.registered_on_crafts, minetest.register_on_craft = make_registration()
  361. minetest.registered_craft_predicts, minetest.register_craft_predict = make_registration()
  362. minetest.registered_on_protection_violation, minetest.register_on_protection_violation = make_registration()