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 13:17] – [Registry entry] do not convert quotation marks 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>
Line 42: 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 53: 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 69: Line 69:
  
 ==== Registry keys ===== ==== 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, *staticregistries, 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.+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: 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:
Line 86: Line 86:
  
 A **registry entry** (''RegistryEntry'') is a wrapper of contents that belong to types that have registry. They have two types: 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 register, and therefore have their IDs.+  * ''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.   * ''RegistryEntry.Direct'': things not registered in the register. They may be anonymously specified in some ways.
  
Line 93: Line 93:
   * ''%%/loot give @s loot {pools:[{rolls:1, entries:[{type:"item", name:"minecraft: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 encode 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.+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 ==== ==== Registry entry list ====
  
-Similar to registry entries, **registry entry lists** are used in many cases and have two types:+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.Named'': The list defined by [[tags]], which are defined in data packs.
   * ''RegistryEntryList.Direct'': The list defirectly defined by listing all contents.   * ''RegistryEntryList.Direct'': The list defirectly defined by listing all contents.
tutorial/registry.1724591867.txt.gz · Last modified: 2024/08/25 13:17 by solidblock