register.lua 19 KB

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