tutorial:registry
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:registry [2020/08/26 09:30] – Fix getId description elisezerotwo | tutorial:registry [2024/08/25 14:06] (current) – solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | 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 9: | Line 9: | ||
* Abstracting or hiding numerical IDs | * Abstracting or hiding numerical IDs | ||
- | When registering any type of content, you pass in an '' | + | When registering any type of content, you pass in an '' |
Using custom content without registering it can lead to buggy behavior, such as missing textures, world save issues, and crashes. The game will usually let you know if you forget to register something. | Using custom content without registering it can lead to buggy behavior, such as missing textures, world save issues, and crashes. The game will usually let you know if you forget to register something. | ||
Line 15: | Line 15: | ||
===== Registry Types ===== | ===== Registry Types ===== | ||
- | When registering content, you need to specify which registry you are adding content to. The base game provides registries for all vanilla content, which can be found in '' | + | When registering content, you need to specify which registry you are adding content to. The base game provides registries for all vanilla content, which can be found in '' |
- | For a deeper overview and description | + | Two examples |
- | ===== Registering Content ===== | + | For a deeper overview and description of all available registries, read the '' |
- | Use '' | + | ===== Basic usages ===== |
+ | ==== Registering your content ==== | ||
+ | Use '' | ||
<code java> | <code java> | ||
- | public static <T> T register(Registry<? | + | Registry.register(registry, |
- | return ((MutableRegistry)registry).add(id, entry); | + | |
- | } | + | |
</ | </ | ||
- | **registry** - an instance of the registry you want to add content to. A list of all vanilla registries, located in '' | ||
- | **id** - an identifying label for your content inside the registry. | + | |
+ | * **id** - the identifier | ||
+ | * **content** - an instance of the content you want to register. | ||
- | **entry** - an instance of the content you want to register. | + | For example: |
+ | <code java> | ||
+ | Registry.register(Registries.ITEM, Identifier.of(" | ||
+ | </ | ||
- | ===== Registry Methods ===== | + | > Remember: registration should happen in mod initializers. Otherwise, you cannot register as they may have been frozen. |
- | '' | + | ==== Getting |
- | <code java> | + | '' |
- | @Nullable | + | |
- | public abstract T get(@Nullable Identifier id); | + | |
- | </code> | + | |
- | ---- | + | < |
+ | Registries.ITEM.containsId(Identifier.ofVanilla(" | ||
+ | |||
+ | Registries.ITEM.containsId(Identifier.ofVanilla(" | ||
+ | |||
+ | Registries.ITEM.get(Identifier.ofVanilla(" | ||
+ | |||
+ | Registries.ITEM.get(Identifier.ofVanilla(" | ||
+ | |||
+ | Registries.ITEM.getOrEmpty(Identifier.ofVanilla(" | ||
+ | |||
+ | Registries.ITEM.getOrEmpty(Identifier.ofVanilla(" | ||
+ | </ | ||
- | '' | + | ==== Getting the id of an object ==== |
+ | |||
+ | '' | ||
<code java> | <code java> | ||
- | @Nullable | + | Registries.BLOCK.getId(Blocks.STONE); // returns Identifier.ofVanilla(" |
- | public abstract Identifier | + | |
</ | </ | ||
- | ---- | + | ===== Related concepts ===== |
- | '' | + | ==== Registry keys ===== |
+ | Some type of registries are not staticly stored in '' | ||
+ | |||
+ | However, each registry has a **registry | ||
<code java> | <code java> | ||
- | public abstract int getRawId(@Nullable T entry); | + | final DynamicRegistryManager registryManager = world.getRegistryManager(); |
+ | |||
+ | // both of the following two statements return the '' | ||
+ | registryManager.get(RegistryKeys.BIOME).get(Identifier.ofVanilla(" | ||
+ | registryManager.getWrapperOrThrow(RegistryKeys.BIOME).getOrThrow(RegistryKey.of(RegistryKeys.BIOME, | ||
</ | </ | ||
+ | Not only registries have registry keys. All registry contents have registry keys (as registries themselves also belong to registry contents of '' | ||
+ | |||
+ | ==== Registry entry ===== | ||
+ | |||
+ | A **registry entry** ('' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | To explicitly show the differences between two, see the following two commands: | ||
+ | * ''/ | ||
+ | * '' | ||
+ | |||
+ | Both the two commands give you a stone. The former one will get the loot table '' | ||
+ | |||
+ | ==== Registry entry list ==== | ||
+ | |||
+ | Similar to registry entries, **registry entry lists** ('' | ||
+ | * '' | ||
+ | * '' |
tutorial/registry.1598434208.txt.gz · Last modified: 2020/08/26 09:30 by elisezerotwo