register.lua 17 KB

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