register.lua 18 KB

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