register.lua 18 KB

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