register.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. -- This isn't just legacy, but more of a convenience feature
  227. local toolcaps = tooldef.tool_capabilities
  228. if toolcaps and toolcaps.punch_attack_uses == nil then
  229. for _, cap in pairs(toolcaps.groupcaps or {}) do
  230. local level = (cap.maxlevel or 0) - 1
  231. if (cap.uses or 0) ~= 0 and level >= 0 then
  232. toolcaps.punch_attack_uses = cap.uses * (3 ^ level)
  233. break
  234. end
  235. end
  236. end
  237. core.register_item(name, tooldef)
  238. end
  239. function core.register_alias(name, convert_to)
  240. if forbidden_item_names[name] then
  241. error("Unable to register alias: Name is forbidden: " .. name)
  242. end
  243. if core.registered_items[name] ~= nil then
  244. core.log("warning", "Not registering alias, item with same name" ..
  245. " is already defined: " .. name .. " -> " .. convert_to)
  246. else
  247. --core.log("Registering alias: " .. name .. " -> " .. convert_to)
  248. core.registered_aliases[name] = convert_to
  249. register_alias_raw(name, convert_to)
  250. end
  251. end
  252. function core.register_alias_force(name, convert_to)
  253. if forbidden_item_names[name] then
  254. error("Unable to register alias: Name is forbidden: " .. name)
  255. end
  256. if core.registered_items[name] ~= nil then
  257. core.unregister_item(name)
  258. core.log("info", "Removed item " ..name..
  259. " while attempting to force add an alias")
  260. end
  261. --core.log("Registering alias: " .. name .. " -> " .. convert_to)
  262. core.registered_aliases[name] = convert_to
  263. register_alias_raw(name, convert_to)
  264. end
  265. function core.on_craft(itemstack, player, old_craft_list, craft_inv)
  266. for _, func in ipairs(core.registered_on_crafts) do
  267. itemstack = func(itemstack, player, old_craft_list, craft_inv) or itemstack
  268. end
  269. return itemstack
  270. end
  271. function core.craft_predict(itemstack, player, old_craft_list, craft_inv)
  272. for _, func in ipairs(core.registered_craft_predicts) do
  273. itemstack = func(itemstack, player, old_craft_list, craft_inv) or itemstack
  274. end
  275. return itemstack
  276. end
  277. -- Alias the forbidden item names to "" so they can't be
  278. -- created via itemstrings (e.g. /give)
  279. for name in pairs(forbidden_item_names) do
  280. core.registered_aliases[name] = ""
  281. register_alias_raw(name, "")
  282. end
  283. --
  284. -- Built-in node definitions. Also defined in C.
  285. --
  286. core.register_item(":unknown", {
  287. type = "none",
  288. description = "Unknown Item",
  289. inventory_image = "unknown_item.png",
  290. on_place = core.item_place,
  291. on_secondary_use = core.item_secondary_use,
  292. on_drop = core.item_drop,
  293. groups = {not_in_creative_inventory=1},
  294. diggable = true,
  295. })
  296. core.register_node(":air", {
  297. description = "Air",
  298. inventory_image = "air.png",
  299. wield_image = "air.png",
  300. drawtype = "airlike",
  301. paramtype = "light",
  302. sunlight_propagates = true,
  303. walkable = false,
  304. pointable = false,
  305. diggable = false,
  306. buildable_to = true,
  307. floodable = true,
  308. air_equivalent = true,
  309. drop = "",
  310. groups = {not_in_creative_inventory=1},
  311. })
  312. core.register_node(":ignore", {
  313. description = "Ignore",
  314. inventory_image = "ignore.png",
  315. wield_image = "ignore.png",
  316. drawtype = "airlike",
  317. paramtype = "none",
  318. sunlight_propagates = false,
  319. walkable = false,
  320. pointable = false,
  321. diggable = false,
  322. buildable_to = true, -- A way to remove accidentally placed ignores
  323. air_equivalent = true,
  324. drop = "",
  325. groups = {not_in_creative_inventory=1},
  326. on_place = function(itemstack, placer, pointed_thing)
  327. core.chat_send_player(
  328. placer:get_player_name(),
  329. core.colorize("#FF0000",
  330. "You can't place 'ignore' nodes!"))
  331. return ""
  332. end,
  333. })
  334. -- The hand (bare definition)
  335. core.register_item(":", {
  336. type = "none",
  337. wield_image = "wieldhand.png",
  338. groups = {not_in_creative_inventory=1},
  339. })
  340. function core.override_item(name, redefinition)
  341. if redefinition.name ~= nil then
  342. error("Attempt to redefine name of "..name.." to "..dump(redefinition.name), 2)
  343. end
  344. if redefinition.type ~= nil then
  345. error("Attempt to redefine type of "..name.." to "..dump(redefinition.type), 2)
  346. end
  347. local item = core.registered_items[name]
  348. if not item then
  349. error("Attempt to override non-existent item "..name, 2)
  350. end
  351. for k, v in pairs(redefinition) do
  352. rawset(item, k, v)
  353. end
  354. register_item_raw(item)
  355. end
  356. core.callback_origins = {}
  357. function core.run_callbacks(callbacks, mode, ...)
  358. assert(type(callbacks) == "table")
  359. local cb_len = #callbacks
  360. if cb_len == 0 then
  361. if mode == 2 or mode == 3 then
  362. return true
  363. elseif mode == 4 or mode == 5 then
  364. return false
  365. end
  366. end
  367. local ret = nil
  368. for i = 1, cb_len do
  369. local origin = core.callback_origins[callbacks[i]]
  370. if origin then
  371. core.set_last_run_mod(origin.mod)
  372. end
  373. local cb_ret = callbacks[i](...)
  374. if mode == 0 and i == 1 then
  375. ret = cb_ret
  376. elseif mode == 1 and i == cb_len then
  377. ret = cb_ret
  378. elseif mode == 2 then
  379. if not cb_ret or i == 1 then
  380. ret = cb_ret
  381. end
  382. elseif mode == 3 then
  383. if cb_ret then
  384. return cb_ret
  385. end
  386. ret = cb_ret
  387. elseif mode == 4 then
  388. if (cb_ret and not ret) or i == 1 then
  389. ret = cb_ret
  390. end
  391. elseif mode == 5 and cb_ret then
  392. return cb_ret
  393. end
  394. end
  395. return ret
  396. end
  397. function core.run_priv_callbacks(name, priv, caller, method)
  398. local def = core.registered_privileges[priv]
  399. if not def or not def["on_" .. method] or
  400. not def["on_" .. method](name, caller) then
  401. for _, func in ipairs(core["registered_on_priv_" .. method]) do
  402. if not func(name, caller, priv) then
  403. break
  404. end
  405. end
  406. end
  407. end
  408. --
  409. -- Callback registration
  410. --
  411. local function make_registration()
  412. local t = {}
  413. local registerfunc = function(func)
  414. t[#t + 1] = func
  415. core.callback_origins[func] = {
  416. mod = core.get_current_modname() or "??",
  417. name = debug.getinfo(1, "n").name or "??"
  418. }
  419. --local origin = core.callback_origins[func]
  420. --print(origin.name .. ": " .. origin.mod .. " registering cbk " .. tostring(func))
  421. end
  422. return t, registerfunc
  423. end
  424. local function make_registration_reverse()
  425. local t = {}
  426. local registerfunc = function(func)
  427. table.insert(t, 1, func)
  428. core.callback_origins[func] = {
  429. mod = core.get_current_modname() or "??",
  430. name = debug.getinfo(1, "n").name or "??"
  431. }
  432. --local origin = core.callback_origins[func]
  433. --print(origin.name .. ": " .. origin.mod .. " registering cbk " .. tostring(func))
  434. end
  435. return t, registerfunc
  436. end
  437. local function make_registration_wrap(reg_fn_name, clear_fn_name)
  438. local list = {}
  439. local orig_reg_fn = core[reg_fn_name]
  440. core[reg_fn_name] = function(def)
  441. local retval = orig_reg_fn(def)
  442. if retval ~= nil then
  443. if def.name ~= nil then
  444. list[def.name] = def
  445. else
  446. list[retval] = def
  447. end
  448. end
  449. return retval
  450. end
  451. local orig_clear_fn = core[clear_fn_name]
  452. core[clear_fn_name] = function()
  453. for k in pairs(list) do
  454. list[k] = nil
  455. end
  456. return orig_clear_fn()
  457. end
  458. return list
  459. end
  460. local function make_wrap_deregistration(reg_fn, clear_fn, list)
  461. local unregister = function (key)
  462. if type(key) ~= "string" then
  463. error("key is not a string", 2)
  464. end
  465. if not list[key] then
  466. error("Attempt to unregister non-existent element - '" .. key .. "'", 2)
  467. end
  468. local temporary_list = table.copy(list)
  469. clear_fn()
  470. for k,v in pairs(temporary_list) do
  471. if key ~= k then
  472. reg_fn(v)
  473. end
  474. end
  475. end
  476. return unregister
  477. end
  478. core.registered_on_player_hpchanges = { modifiers = { }, loggers = { } }
  479. function core.registered_on_player_hpchange(player, hp_change, reason)
  480. local last
  481. for i = #core.registered_on_player_hpchanges.modifiers, 1, -1 do
  482. local func = core.registered_on_player_hpchanges.modifiers[i]
  483. hp_change, last = func(player, hp_change, reason)
  484. if type(hp_change) ~= "number" then
  485. local debuginfo = debug.getinfo(func)
  486. error("The register_on_hp_changes function has to return a number at " ..
  487. debuginfo.short_src .. " line " .. debuginfo.linedefined)
  488. end
  489. if last then
  490. break
  491. end
  492. end
  493. for i, func in ipairs(core.registered_on_player_hpchanges.loggers) do
  494. func(player, hp_change, reason)
  495. end
  496. return hp_change
  497. end
  498. function core.register_on_player_hpchange(func, modifier)
  499. if modifier then
  500. core.registered_on_player_hpchanges.modifiers[#core.registered_on_player_hpchanges.modifiers + 1] = func
  501. else
  502. core.registered_on_player_hpchanges.loggers[#core.registered_on_player_hpchanges.loggers + 1] = func
  503. end
  504. core.callback_origins[func] = {
  505. mod = core.get_current_modname() or "??",
  506. name = debug.getinfo(1, "n").name or "??"
  507. }
  508. end
  509. core.registered_biomes = make_registration_wrap("register_biome", "clear_registered_biomes")
  510. core.registered_ores = make_registration_wrap("register_ore", "clear_registered_ores")
  511. core.registered_decorations = make_registration_wrap("register_decoration", "clear_registered_decorations")
  512. core.unregister_biome = make_wrap_deregistration(core.register_biome,
  513. core.clear_registered_biomes, core.registered_biomes)
  514. core.registered_on_chat_messages, core.register_on_chat_message = make_registration()
  515. core.registered_on_chatcommands, core.register_on_chatcommand = make_registration()
  516. core.registered_globalsteps, core.register_globalstep = make_registration()
  517. core.registered_playerevents, core.register_playerevent = make_registration()
  518. core.registered_on_mods_loaded, core.register_on_mods_loaded = make_registration()
  519. core.registered_on_shutdown, core.register_on_shutdown = make_registration()
  520. core.registered_on_punchnodes, core.register_on_punchnode = make_registration()
  521. core.registered_on_placenodes, core.register_on_placenode = make_registration()
  522. core.registered_on_dignodes, core.register_on_dignode = make_registration()
  523. core.registered_on_generateds, core.register_on_generated = make_registration()
  524. core.registered_on_newplayers, core.register_on_newplayer = make_registration()
  525. core.registered_on_dieplayers, core.register_on_dieplayer = make_registration()
  526. core.registered_on_respawnplayers, core.register_on_respawnplayer = make_registration()
  527. core.registered_on_prejoinplayers, core.register_on_prejoinplayer = make_registration()
  528. core.registered_on_joinplayers, core.register_on_joinplayer = make_registration()
  529. core.registered_on_leaveplayers, core.register_on_leaveplayer = make_registration()
  530. core.registered_on_player_receive_fields, core.register_on_player_receive_fields = make_registration_reverse()
  531. core.registered_on_cheats, core.register_on_cheat = make_registration()
  532. core.registered_on_crafts, core.register_on_craft = make_registration()
  533. core.registered_craft_predicts, core.register_craft_predict = make_registration()
  534. core.registered_on_protection_violation, core.register_on_protection_violation = make_registration()
  535. core.registered_on_item_eats, core.register_on_item_eat = make_registration()
  536. core.registered_on_punchplayers, core.register_on_punchplayer = make_registration()
  537. core.registered_on_priv_grant, core.register_on_priv_grant = make_registration()
  538. core.registered_on_priv_revoke, core.register_on_priv_revoke = make_registration()
  539. core.registered_on_authplayers, core.register_on_authplayer = make_registration()
  540. core.registered_can_bypass_userlimit, core.register_can_bypass_userlimit = make_registration()
  541. core.registered_on_modchannel_message, core.register_on_modchannel_message = make_registration()
  542. core.registered_on_player_inventory_actions, core.register_on_player_inventory_action = make_registration()
  543. core.registered_allow_player_inventory_actions, core.register_allow_player_inventory_action = make_registration()
  544. core.registered_on_rightclickplayers, core.register_on_rightclickplayer = make_registration()
  545. --
  546. -- Compatibility for on_mapgen_init()
  547. --
  548. core.register_on_mapgen_init = function(func) func(core.get_mapgen_params()) end