register.lua 19 KB

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