register.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. -- Minetest: builtin/register.lua
  2. local builtin_shared = ...
  3. local S = core.get_translator("__builtin")
  4. --
  5. -- Make raw registration functions inaccessible to anyone except this file
  6. --
  7. local register_item_raw = core.register_item_raw
  8. core.register_item_raw = nil
  9. local unregister_item_raw = core.unregister_item_raw
  10. core.unregister_item_raw = nil
  11. local register_alias_raw = core.register_alias_raw
  12. core.register_alias_raw = nil
  13. --
  14. -- Item / entity / ABM / LBM registration functions
  15. --
  16. core.registered_abms = {}
  17. core.registered_lbms = {}
  18. core.registered_entities = {}
  19. core.registered_items = {}
  20. core.registered_nodes = {}
  21. core.registered_craftitems = {}
  22. core.registered_tools = {}
  23. core.registered_aliases = {}
  24. -- For tables that are indexed by item name:
  25. -- If table[X] does not exist, default to table[core.registered_aliases[X]]
  26. local alias_metatable = {
  27. __index = function(t, name)
  28. return rawget(t, core.registered_aliases[name])
  29. end
  30. }
  31. setmetatable(core.registered_items, alias_metatable)
  32. setmetatable(core.registered_nodes, alias_metatable)
  33. setmetatable(core.registered_craftitems, alias_metatable)
  34. setmetatable(core.registered_tools, alias_metatable)
  35. -- These item names may not be used because they would interfere
  36. -- with legacy itemstrings
  37. local forbidden_item_names = {
  38. MaterialItem = true,
  39. MaterialItem2 = true,
  40. MaterialItem3 = true,
  41. NodeItem = true,
  42. node = true,
  43. CraftItem = true,
  44. craft = true,
  45. MBOItem = true,
  46. ToolItem = true,
  47. tool = true,
  48. }
  49. local function check_modname_prefix(name)
  50. if name:sub(1,1) == ":" then
  51. -- If the name starts with a colon, we can skip the modname prefix
  52. -- mechanism.
  53. return name:sub(2)
  54. else
  55. -- Enforce that the name starts with the correct mod name.
  56. local expected_prefix = core.get_current_modname() .. ":"
  57. if name:sub(1, #expected_prefix) ~= expected_prefix then
  58. error("Name " .. name .. " does not follow naming conventions: " ..
  59. "\"" .. expected_prefix .. "\" or \":\" prefix required")
  60. end
  61. -- Enforce that the name only contains letters, numbers and underscores.
  62. local subname = name:sub(#expected_prefix+1)
  63. if subname:find("[^%w_]") then
  64. error("Name " .. name .. " does not follow naming conventions: " ..
  65. "contains unallowed characters")
  66. end
  67. return name
  68. end
  69. end
  70. local function check_node_list(list, field)
  71. local t = type(list)
  72. if t == "table" then
  73. for _, entry in pairs(list) do
  74. assert(type(entry) == "string",
  75. "Field '" .. field .. "' contains non-string entry")
  76. end
  77. elseif t ~= "string" and t ~= "nil" then
  78. error("Field '" .. field .. "' has invalid type " .. t)
  79. end
  80. end
  81. function core.register_abm(spec)
  82. -- Add to core.registered_abms
  83. check_node_list(spec.nodenames, "nodenames")
  84. check_node_list(spec.neighbors, "neighbors")
  85. assert(type(spec.action) == "function", "Required field 'action' of type function")
  86. core.registered_abms[#core.registered_abms + 1] = spec
  87. spec.mod_origin = core.get_current_modname() or "??"
  88. end
  89. function core.register_lbm(spec)
  90. -- Add to core.registered_lbms
  91. check_modname_prefix(spec.name)
  92. check_node_list(spec.nodenames, "nodenames")
  93. assert(type(spec.action) == "function", "Required field 'action' of type function")
  94. core.registered_lbms[#core.registered_lbms + 1] = spec
  95. spec.mod_origin = core.get_current_modname() or "??"
  96. end
  97. function core.register_entity(name, prototype)
  98. -- Check name
  99. if name == nil then
  100. error("Unable to register entity: Name is nil")
  101. end
  102. name = check_modname_prefix(tostring(name))
  103. prototype.name = name
  104. prototype.__index = prototype -- so that it can be used as a metatable
  105. -- Add to core.registered_entities
  106. core.registered_entities[name] = prototype
  107. prototype.mod_origin = core.get_current_modname() or "??"
  108. end
  109. function core.register_item(name, itemdef)
  110. -- Check name
  111. if name == nil then
  112. error("Unable to register item: Name is nil")
  113. end
  114. name = check_modname_prefix(tostring(name))
  115. if forbidden_item_names[name] then
  116. error("Unable to register item: Name is forbidden: " .. name)
  117. end
  118. itemdef.name = name
  119. -- Apply defaults and add to registered_* table
  120. if itemdef.type == "node" then
  121. -- Use the nodebox as selection box if it's not set manually
  122. if itemdef.drawtype == "nodebox" and not itemdef.selection_box then
  123. itemdef.selection_box = itemdef.node_box
  124. elseif itemdef.drawtype == "fencelike" and not itemdef.selection_box then
  125. itemdef.selection_box = {
  126. type = "fixed",
  127. fixed = {-1/8, -1/2, -1/8, 1/8, 1/2, 1/8},
  128. }
  129. end
  130. if itemdef.light_source and itemdef.light_source > core.LIGHT_MAX then
  131. itemdef.light_source = core.LIGHT_MAX
  132. core.log("warning", "Node 'light_source' value exceeds maximum," ..
  133. " limiting to maximum: " ..name)
  134. end
  135. setmetatable(itemdef, {__index = core.nodedef_default})
  136. core.registered_nodes[itemdef.name] = itemdef
  137. elseif itemdef.type == "craft" then
  138. setmetatable(itemdef, {__index = core.craftitemdef_default})
  139. core.registered_craftitems[itemdef.name] = itemdef
  140. elseif itemdef.type == "tool" then
  141. setmetatable(itemdef, {__index = core.tooldef_default})
  142. core.registered_tools[itemdef.name] = itemdef
  143. elseif itemdef.type == "none" then
  144. setmetatable(itemdef, {__index = core.noneitemdef_default})
  145. else
  146. error("Unable to register item: Type is invalid: " .. dump(itemdef))
  147. end
  148. -- Flowing liquid uses param2
  149. if itemdef.type == "node" and itemdef.liquidtype == "flowing" then
  150. itemdef.paramtype2 = "flowingliquid"
  151. end
  152. -- BEGIN Legacy stuff
  153. if itemdef.cookresult_itemstring ~= nil and itemdef.cookresult_itemstring ~= "" then
  154. core.register_craft({
  155. type="cooking",
  156. output=itemdef.cookresult_itemstring,
  157. recipe=itemdef.name,
  158. cooktime=itemdef.furnace_cooktime
  159. })
  160. end
  161. if itemdef.furnace_burntime ~= nil and itemdef.furnace_burntime >= 0 then
  162. core.register_craft({
  163. type="fuel",
  164. recipe=itemdef.name,
  165. burntime=itemdef.furnace_burntime
  166. })
  167. end
  168. -- END Legacy stuff
  169. itemdef.mod_origin = core.get_current_modname() or "??"
  170. -- Disable all further modifications
  171. getmetatable(itemdef).__newindex = {}
  172. --core.log("Registering item: " .. itemdef.name)
  173. core.registered_items[itemdef.name] = itemdef
  174. core.registered_aliases[itemdef.name] = nil
  175. register_item_raw(itemdef)
  176. end
  177. function core.unregister_item(name)
  178. if not core.registered_items[name] then
  179. core.log("warning", "Not unregistering item " ..name..
  180. " because it doesn't exist.")
  181. return
  182. end
  183. -- Erase from registered_* table
  184. local type = core.registered_items[name].type
  185. if type == "node" then
  186. core.registered_nodes[name] = nil
  187. elseif type == "craft" then
  188. core.registered_craftitems[name] = nil
  189. elseif type == "tool" then
  190. core.registered_tools[name] = nil
  191. end
  192. core.registered_items[name] = nil
  193. unregister_item_raw(name)
  194. end
  195. function core.register_node(name, nodedef)
  196. nodedef.type = "node"
  197. core.register_item(name, nodedef)
  198. end
  199. function core.register_craftitem(name, craftitemdef)
  200. craftitemdef.type = "craft"
  201. -- BEGIN Legacy stuff
  202. if craftitemdef.inventory_image == nil and craftitemdef.image ~= nil then
  203. craftitemdef.inventory_image = craftitemdef.image
  204. end
  205. -- END Legacy stuff
  206. core.register_item(name, craftitemdef)
  207. end
  208. function core.register_tool(name, tooldef)
  209. tooldef.type = "tool"
  210. tooldef.stack_max = 1
  211. -- BEGIN Legacy stuff
  212. if tooldef.inventory_image == nil and tooldef.image ~= nil then
  213. tooldef.inventory_image = tooldef.image
  214. end
  215. if tooldef.tool_capabilities == nil and
  216. (tooldef.full_punch_interval ~= nil or
  217. tooldef.basetime ~= nil or
  218. tooldef.dt_weight ~= nil or
  219. tooldef.dt_crackiness ~= nil or
  220. tooldef.dt_crumbliness ~= nil or
  221. tooldef.dt_cuttability ~= nil or
  222. tooldef.basedurability ~= nil or
  223. tooldef.dd_weight ~= nil or
  224. tooldef.dd_crackiness ~= nil or
  225. tooldef.dd_crumbliness ~= nil or
  226. tooldef.dd_cuttability ~= nil) then
  227. tooldef.tool_capabilities = {
  228. full_punch_interval = tooldef.full_punch_interval,
  229. basetime = tooldef.basetime,
  230. dt_weight = tooldef.dt_weight,
  231. dt_crackiness = tooldef.dt_crackiness,
  232. dt_crumbliness = tooldef.dt_crumbliness,
  233. dt_cuttability = tooldef.dt_cuttability,
  234. basedurability = tooldef.basedurability,
  235. dd_weight = tooldef.dd_weight,
  236. dd_crackiness = tooldef.dd_crackiness,
  237. dd_crumbliness = tooldef.dd_crumbliness,
  238. dd_cuttability = tooldef.dd_cuttability,
  239. }
  240. end
  241. -- END Legacy stuff
  242. -- This isn't just legacy, but more of a convenience feature
  243. local toolcaps = tooldef.tool_capabilities
  244. if toolcaps and toolcaps.punch_attack_uses == nil then
  245. for _, cap in pairs(toolcaps.groupcaps or {}) do
  246. local level = (cap.maxlevel or 0) - 1
  247. if (cap.uses or 0) ~= 0 and level >= 0 then
  248. toolcaps.punch_attack_uses = cap.uses * (3 ^ level)
  249. break
  250. end
  251. end
  252. end
  253. core.register_item(name, tooldef)
  254. end
  255. function core.register_alias(name, convert_to)
  256. if forbidden_item_names[name] then
  257. error("Unable to register alias: Name is forbidden: " .. name)
  258. end
  259. if core.registered_items[name] ~= nil then
  260. core.log("warning", "Not registering alias, item with same name" ..
  261. " is already defined: " .. name .. " -> " .. convert_to)
  262. else
  263. --core.log("Registering alias: " .. name .. " -> " .. convert_to)
  264. core.registered_aliases[name] = convert_to
  265. register_alias_raw(name, convert_to)
  266. end
  267. end
  268. function core.register_alias_force(name, convert_to)
  269. if forbidden_item_names[name] then
  270. error("Unable to register alias: Name is forbidden: " .. name)
  271. end
  272. if core.registered_items[name] ~= nil then
  273. core.unregister_item(name)
  274. core.log("info", "Removed item " ..name..
  275. " while attempting to force add an alias")
  276. end
  277. --core.log("Registering alias: " .. name .. " -> " .. convert_to)
  278. core.registered_aliases[name] = convert_to
  279. register_alias_raw(name, convert_to)
  280. end
  281. function core.on_craft(itemstack, player, old_craft_list, craft_inv)
  282. for _, func in ipairs(core.registered_on_crafts) do
  283. -- cast to ItemStack since func() could return a string
  284. itemstack = ItemStack(func(itemstack, player, old_craft_list, craft_inv) or itemstack)
  285. end
  286. return itemstack
  287. end
  288. function core.craft_predict(itemstack, player, old_craft_list, craft_inv)
  289. for _, func in ipairs(core.registered_craft_predicts) do
  290. -- cast to ItemStack since func() could return a string
  291. itemstack = ItemStack(func(itemstack, player, old_craft_list, craft_inv) or itemstack)
  292. end
  293. return itemstack
  294. end
  295. -- Alias the forbidden item names to "" so they can't be
  296. -- created via itemstrings (e.g. /give)
  297. for name in pairs(forbidden_item_names) do
  298. core.registered_aliases[name] = ""
  299. register_alias_raw(name, "")
  300. end
  301. --
  302. -- Built-in node definitions. Also defined in C.
  303. --
  304. core.register_item(":unknown", {
  305. type = "none",
  306. description = S("Unknown Item"),
  307. inventory_image = "unknown_item.png",
  308. on_place = core.item_place,
  309. on_secondary_use = core.item_secondary_use,
  310. on_drop = core.item_drop,
  311. groups = {not_in_creative_inventory=1},
  312. diggable = true,
  313. })
  314. core.register_node(":air", {
  315. description = S("Air"),
  316. inventory_image = "air.png",
  317. wield_image = "air.png",
  318. drawtype = "airlike",
  319. paramtype = "light",
  320. sunlight_propagates = true,
  321. walkable = false,
  322. pointable = false,
  323. diggable = false,
  324. buildable_to = true,
  325. floodable = true,
  326. air_equivalent = true,
  327. drop = "",
  328. groups = {not_in_creative_inventory=1},
  329. })
  330. core.register_node(":ignore", {
  331. description = S("Ignore"),
  332. inventory_image = "ignore.png",
  333. wield_image = "ignore.png",
  334. drawtype = "airlike",
  335. paramtype = "none",
  336. sunlight_propagates = false,
  337. walkable = false,
  338. pointable = false,
  339. diggable = false,
  340. buildable_to = true, -- A way to remove accidentally placed ignores
  341. air_equivalent = true,
  342. drop = "",
  343. groups = {not_in_creative_inventory=1},
  344. node_placement_prediction = "",
  345. on_place = function(itemstack, placer, pointed_thing)
  346. core.chat_send_player(
  347. placer:get_player_name(),
  348. core.colorize("#FF0000",
  349. S("You can't place 'ignore' nodes!")))
  350. return ""
  351. end,
  352. })
  353. -- The hand (bare definition)
  354. core.register_item(":", {
  355. type = "none",
  356. wield_image = "wieldhand.png",
  357. groups = {not_in_creative_inventory=1},
  358. })
  359. function core.override_item(name, redefinition, del_fields)
  360. if redefinition.name ~= nil then
  361. error("Attempt to redefine name of "..name.." to "..dump(redefinition.name), 2)
  362. end
  363. if redefinition.type ~= nil then
  364. error("Attempt to redefine type of "..name.." to "..dump(redefinition.type), 2)
  365. end
  366. local item = core.registered_items[name]
  367. if not item then
  368. error("Attempt to override non-existent item "..name, 2)
  369. end
  370. for k, v in pairs(redefinition) do
  371. rawset(item, k, v)
  372. end
  373. for _, field in ipairs(del_fields or {}) do
  374. rawset(item, field, nil)
  375. end
  376. register_item_raw(item)
  377. end
  378. function core.run_priv_callbacks(name, priv, caller, method)
  379. local def = core.registered_privileges[priv]
  380. if not def or not def["on_" .. method] or
  381. not def["on_" .. method](name, caller) then
  382. for _, func in ipairs(core["registered_on_priv_" .. method]) do
  383. if not func(name, caller, priv) then
  384. break
  385. end
  386. end
  387. end
  388. end
  389. --
  390. -- Callback registration
  391. --
  392. local function make_registration_wrap(reg_fn_name, clear_fn_name)
  393. local list = {}
  394. local orig_reg_fn = core[reg_fn_name]
  395. core[reg_fn_name] = function(def)
  396. local retval = orig_reg_fn(def)
  397. if retval ~= nil then
  398. if def.name ~= nil then
  399. list[def.name] = def
  400. else
  401. list[retval] = def
  402. end
  403. end
  404. return retval
  405. end
  406. local orig_clear_fn = core[clear_fn_name]
  407. core[clear_fn_name] = function()
  408. for k in pairs(list) do
  409. list[k] = nil
  410. end
  411. return orig_clear_fn()
  412. end
  413. return list
  414. end
  415. local function make_wrap_deregistration(reg_fn, clear_fn, list)
  416. local unregister = function (key)
  417. if type(key) ~= "string" then
  418. error("key is not a string", 2)
  419. end
  420. if not list[key] then
  421. error("Attempt to unregister non-existent element - '" .. key .. "'", 2)
  422. end
  423. local temporary_list = table.copy(list)
  424. clear_fn()
  425. for k,v in pairs(temporary_list) do
  426. if key ~= k then
  427. reg_fn(v)
  428. end
  429. end
  430. end
  431. return unregister
  432. end
  433. core.registered_on_player_hpchanges = { modifiers = { }, loggers = { } }
  434. function core.registered_on_player_hpchange(player, hp_change, reason)
  435. local last
  436. for i = #core.registered_on_player_hpchanges.modifiers, 1, -1 do
  437. local func = core.registered_on_player_hpchanges.modifiers[i]
  438. hp_change, last = func(player, hp_change, reason)
  439. if type(hp_change) ~= "number" then
  440. local debuginfo = debug.getinfo(func)
  441. error("The register_on_hp_changes function has to return a number at " ..
  442. debuginfo.short_src .. " line " .. debuginfo.linedefined)
  443. end
  444. if last then
  445. break
  446. end
  447. end
  448. for i, func in ipairs(core.registered_on_player_hpchanges.loggers) do
  449. func(player, hp_change, reason)
  450. end
  451. return hp_change
  452. end
  453. function core.register_on_player_hpchange(func, modifier)
  454. if modifier then
  455. core.registered_on_player_hpchanges.modifiers[#core.registered_on_player_hpchanges.modifiers + 1] = func
  456. else
  457. core.registered_on_player_hpchanges.loggers[#core.registered_on_player_hpchanges.loggers + 1] = func
  458. end
  459. core.callback_origins[func] = {
  460. mod = core.get_current_modname() or "??",
  461. name = debug.getinfo(1, "n").name or "??"
  462. }
  463. end
  464. core.registered_biomes = make_registration_wrap("register_biome", "clear_registered_biomes")
  465. core.registered_ores = make_registration_wrap("register_ore", "clear_registered_ores")
  466. core.registered_decorations = make_registration_wrap("register_decoration", "clear_registered_decorations")
  467. core.unregister_biome = make_wrap_deregistration(core.register_biome,
  468. core.clear_registered_biomes, core.registered_biomes)
  469. local make_registration = builtin_shared.make_registration
  470. local make_registration_reverse = builtin_shared.make_registration_reverse
  471. core.registered_on_chat_messages, core.register_on_chat_message = make_registration()
  472. core.registered_on_chatcommands, core.register_on_chatcommand = make_registration()
  473. core.registered_globalsteps, core.register_globalstep = make_registration()
  474. core.registered_playerevents, core.register_playerevent = make_registration()
  475. core.registered_on_mods_loaded, core.register_on_mods_loaded = make_registration()
  476. core.registered_on_shutdown, core.register_on_shutdown = make_registration()
  477. core.registered_on_punchnodes, core.register_on_punchnode = make_registration()
  478. core.registered_on_placenodes, core.register_on_placenode = make_registration()
  479. core.registered_on_dignodes, core.register_on_dignode = make_registration()
  480. core.registered_on_generateds, core.register_on_generated = make_registration()
  481. core.registered_on_newplayers, core.register_on_newplayer = make_registration()
  482. core.registered_on_dieplayers, core.register_on_dieplayer = make_registration()
  483. core.registered_on_respawnplayers, core.register_on_respawnplayer = make_registration()
  484. core.registered_on_prejoinplayers, core.register_on_prejoinplayer = make_registration()
  485. core.registered_on_joinplayers, core.register_on_joinplayer = make_registration()
  486. core.registered_on_leaveplayers, core.register_on_leaveplayer = make_registration()
  487. core.registered_on_player_receive_fields, core.register_on_player_receive_fields = make_registration_reverse()
  488. core.registered_on_cheats, core.register_on_cheat = make_registration()
  489. core.registered_on_crafts, core.register_on_craft = make_registration()
  490. core.registered_craft_predicts, core.register_craft_predict = make_registration()
  491. core.registered_on_protection_violation, core.register_on_protection_violation = make_registration()
  492. core.registered_on_item_eats, core.register_on_item_eat = make_registration()
  493. core.registered_on_item_pickups, core.register_on_item_pickup = make_registration()
  494. core.registered_on_punchplayers, core.register_on_punchplayer = make_registration()
  495. core.registered_on_priv_grant, core.register_on_priv_grant = make_registration()
  496. core.registered_on_priv_revoke, core.register_on_priv_revoke = make_registration()
  497. core.registered_on_authplayers, core.register_on_authplayer = make_registration()
  498. core.registered_can_bypass_userlimit, core.register_can_bypass_userlimit = make_registration()
  499. core.registered_on_modchannel_message, core.register_on_modchannel_message = make_registration()
  500. core.registered_on_player_inventory_actions, core.register_on_player_inventory_action = make_registration()
  501. core.registered_allow_player_inventory_actions, core.register_allow_player_inventory_action = make_registration()
  502. core.registered_on_rightclickplayers, core.register_on_rightclickplayer = make_registration()
  503. core.registered_on_liquid_transformed, core.register_on_liquid_transformed = make_registration()
  504. core.registered_on_mapblocks_changed, core.register_on_mapblocks_changed = make_registration()
  505. core.register_on_mods_loaded(function()
  506. core.after(0, function()
  507. setmetatable(core.registered_on_mapblocks_changed, {
  508. __newindex = function()
  509. error("on_mapblocks_changed callbacks must be registered at load time")
  510. end,
  511. })
  512. end)
  513. end)
  514. --
  515. -- Compatibility for on_mapgen_init()
  516. --
  517. core.register_on_mapgen_init = function(func) func(core.get_mapgen_params()) end