User Tools

Site Tools


tutorial:registry

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:registry [2024/08/25 12:52] solidblocktutorial:registry [2024/08/25 14:06] (current) solidblock
Line 1: Line 1:
-====== Registry System ======+====== Intro to Registries ======
  
-Minecraft uses the **registry system** to handle almost everything in the game. For mod developers, you will need to register most content you add to the game. This helps with:+Minecraft uses the **registry system** to handle almost everything in the game. When developing mods, you will need to register most content you add to the game. This helps with:
   * Letting the game know your content exists   * Letting the game know your content exists
   * Verifying game content between client & server   * Verifying game content between client & server
Line 19: Line 19:
 Two examples of registries you may use include ''Registries.ITEM'' (for 1.19.3 above)/''Registry.ITEM'' (for 1.19.2 below) for items and ''Registries.BLOCK'' (for 1.19.3 above)/''Registry.BLOCK'' (for 1.19.2 below) for blocks. Two examples of registries you may use include ''Registries.ITEM'' (for 1.19.3 above)/''Registry.ITEM'' (for 1.19.2 below) for items and ''Registries.BLOCK'' (for 1.19.3 above)/''Registry.BLOCK'' (for 1.19.2 below) for blocks.
  
-For a deeper overview and description of all available registries, read the [[registry types]] page.+For a deeper overview and description of all available registries, read the ''Registries'' or ''Registry'' class. A brief description for the registry types can be found in [[registry types]] page.
  
 ===== Basic usages ===== ===== Basic usages =====
 ==== Registering your content ==== ==== Registering your content ====
-Use ''<yarn class_2378>.<yarn method_10230>'' to register your contents into registries. Remember, each content cannot be registered once. The basic syntax is:+Use ''<yarn class_2378>.<yarn method_10230>'' to register your contents into registries. Remember, each content can be registered no more than once. The basic syntax is:
  
 <code java> <code java>
     Registry.register(registry, id, content);     Registry.register(registry, id, content);
 </code> </code>
 +
   * **registry** - an instance of the registry you want to add content to.   * **registry** - an instance of the registry you want to add content to.
   * **id** - the identifier for your content inside the registry.   * **id** - the identifier for your content inside the registry.
Line 41: Line 42:
 ==== Getting the object by ID ==== ==== Getting the object by ID ====
  
-''<yarn method_10223>'' returns the content associated with an ID inside a registry. If the conten doesn’t exist, ''<yarn class_2348>'' returns the default registry value, and ''<yarn class_2370>'' returns null. You can use ''containsId'' or ''getOrEmpty'' method to test whether the id exists. For example:+''<yarn method_10223>'' returns the content associated with an ID inside a registry. If the content doesn’t exist, ''<yarn class_2348>'' returns the default registry value, and ''<yarn class_2370>'' returns null. You can use ''containsId'' or ''getOrEmpty'' method to test whether the id exists. For example:
  
 <yarncode java> <yarncode java>
Line 52: Line 53:
     Registries.ITEM.get(Identifier.ofVanilla("invalid_name")); // returns Items.AIR     Registries.ITEM.get(Identifier.ofVanilla("invalid_name")); // returns Items.AIR
          
-    Registries.ITEM.getOrEmpty(Identifier.ofVanilla("diamond")); // returns Optiona.of(Items.DIAMOND)+    Registries.ITEM.getOrEmpty(Identifier.ofVanilla("diamond")); // returns Optional.of(Items.DIAMOND)
          
-    Registries.ITEM.getOrEmpty(Identifier.ofVanilla("invalid_name")); // returns Optional.of()+    Registries.ITEM.getOrEmpty(Identifier.ofVanilla("invalid_name")); // returns Optional.empty()
 </yarncode> </yarncode>
  
Line 65: Line 66:
 </code> </code>
  
 +===== Related concepts =====
 +
 +==== Registry keys =====
 +Some type of registries are not staticly stored in ''Registries'' class. For example, you cannot find biome registries or loot table registries in ''Registries'' class. But the registries do exist in Minecraft. That's because those registries are //dynamic//: they are loaded between different worlds as they are defined in data packs. In contrary, //static// registries, such as ''Registries.ITEM'' and ''Registries.BLOCK'', are not defined in data packs yet, so they keep unchanged no matter which worlds you are in.
 +
 +However, each registry has a **registry key**, which can be found in ''RegistryKeys''. You can get dynamic registries through registry keys with ''RegistryWrapper.WrapperLookup'' objects (usually ''world.getRegistryManager()'' when you have a ''World'' object, or ''CommandRegistryAccess'' when registering [[commands]]). See the example below, assume that you have a ''World'' object:
 +
 +<code java>
 +    final DynamicRegistryManager registryManager = world.getRegistryManager();
 +    
 +    // both of the following two statements return the ''Biome'' object for desert in the world
 +    registryManager.get(RegistryKeys.BIOME).get(Identifier.ofVanilla("desert"));
 +    registryManager.getWrapperOrThrow(RegistryKeys.BIOME).getOrThrow(RegistryKey.of(RegistryKeys.BIOME, Identifier.ofVanilla("desert"))).value();
 +</code>
 +
 +Not only registries have registry keys. All registry contents have registry keys (as registries themselves also belong to registry contents of ''Registries.REGISTRIES''). Registry contents may change between worlds, but registry keys always keep unchanged.
 +
 +==== Registry entry =====
 +
 +A **registry entry** (''RegistryEntry'') is a wrapper of contents that belong to types that have registry. They have two types:
 +  * ''RegistryEntry.Reference'': things that are registered in the registry, and therefore have their IDs.
 +  * ''RegistryEntry.Direct'': things not registered in the register. They may be anonymously specified in some ways.
 +
 +To explicitly show the differences between two, see the following two commands:
 +  * ''/loot give @s loot minecraft:blocks/stone''
 +  * ''%%/loot give @s loot {pools:[{rolls:1, entries:[{type:"item", name:"minecraft:stone"}]}]}%%''
 +
 +Both the two commands give you a stone. The former one will get the loot table ''minecraft:blocks/stone'' from the loot table registry of the world, so it is ''RegistryEntry.Reference''. The latter one will decode a loot table from a SNBT, which does not have an ID, so it is ''RegistryEntry.Direct''. Both the two type of registry entries can be fetched the actual content by calling ''value()'' method.
 +
 +==== Registry entry list ====
 +
 +Similar to registry entries, **registry entry lists** (''RegistryEntryList'') are used in many cases and also have two types:
 +  * ''RegistryEntryList.Named'': The list defined by [[tags]], which are defined in data packs.
 +  * ''RegistryEntryList.Direct'': The list defirectly defined by listing all contents.
tutorial/registry.1724590333.txt.gz · Last modified: 2024/08/25 12:52 by solidblock