register.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. local builtin_shared = ...
  2. -- Copy all the registration tables over
  3. do
  4. local all = assert(core.transferred_globals)
  5. core.transferred_globals = nil
  6. all.registered_nodes = {}
  7. all.registered_craftitems = {}
  8. all.registered_tools = {}
  9. for k, v in pairs(all.registered_items) do
  10. -- Disable further modification
  11. setmetatable(v, {__newindex = {}})
  12. -- Reassemble the other tables
  13. if v.type == "node" then
  14. getmetatable(v).__index = all.nodedef_default
  15. all.registered_nodes[k] = v
  16. elseif v.type == "craft" then
  17. getmetatable(v).__index = all.craftitemdef_default
  18. all.registered_craftitems[k] = v
  19. elseif v.type == "tool" then
  20. getmetatable(v).__index = all.tooldef_default
  21. all.registered_tools[k] = v
  22. else
  23. getmetatable(v).__index = all.noneitemdef_default
  24. end
  25. end
  26. for k, v in pairs(all) do
  27. core[k] = v
  28. end
  29. end
  30. -- For tables that are indexed by item name:
  31. -- If table[X] does not exist, default to table[core.registered_aliases[X]]
  32. local alias_metatable = {
  33. __index = function(t, name)
  34. return rawget(t, core.registered_aliases[name])
  35. end
  36. }
  37. setmetatable(core.registered_items, alias_metatable)
  38. setmetatable(core.registered_nodes, alias_metatable)
  39. setmetatable(core.registered_craftitems, alias_metatable)
  40. setmetatable(core.registered_tools, alias_metatable)
  41. --
  42. -- Callbacks
  43. --
  44. local make_registration = builtin_shared.make_registration
  45. core.registered_on_mods_loaded, core.register_on_mods_loaded = make_registration()
  46. core.registered_on_generateds, core.register_on_generated = make_registration()
  47. core.registered_on_shutdown, core.register_on_shutdown = make_registration()