Changes

4,277 bytes removed ,  17:06, 8 September 2022
Some improvements of changes in 1.19; skimmed
Line 1: Line 1: −
Registration is the process of making an object (such as an item or block) known to the game during runtime. If some objects are not registered, this could cause crashes even before the game is fully loaded or arbitrary behaviors such as bottlenecking mod compatibility for world generation.
+
Registration is the process of making an object (such as an item or block) known to the game during runtime with an attached <code>ResourceLocation</code> name. Unregistered objects are a likely cause of game loading crashes or bugs, so it is important to register objects correctly.
   −
Most objects that are known within the game are handled by a <code>Registry</code>. Each registry uniquely defines each object through a "registry name" via a [[Using Resources#ResourceLocation|ResourceLocation]].
+
Most objects that are known within the game are handled by a Vanilla <code>Registry</code> or a Forge <code>IForgeRegistry</code>. Each registry uniquely defines each of its own objects through a "registry name" via a [[Using Resources#ResourceLocation|ResourceLocation]].
 +
Registries themselves have a name and are registered to the Vanilla root registry or <code>RegistryManager</code>.
 +
It is important to keep these distinct; although both are called registry names.
    
{{Tip|In a global context, each object is universally unique through its <code>ResourceKey</code>: a concatenation of its registry's id and the object's registry name.}}
 
{{Tip|In a global context, each object is universally unique through its <code>ResourceKey</code>: a concatenation of its registry's id and the object's registry name.}}
   −
Due to the inconsistent ordering and registration process vanilla uses, Forge wraps most vanilla registries using <code>IForgeRegistry</code>. This guarantees that the loading order for these wrapped registries will be <code>Block</code>, <code>Item</code>, and then the rest of the wrapped registries in alphabetical order. All registries supported by Forge can be found within the <code>ForgeRegistries</code> class. Since all registry names are unique to a specific registry, different registry objects within different registries can have the same name (e.g. a <code>Block</code> and an <code>Item</code> each hold a registry object named <code>examplemod:object</code>.
+
Forge expands Vanilla's registries to add important features for modded environments, such as world saving of integer ids and network syncing of integer ids, to ensure consistency when different mods and entries are present.
   −
{{Tip/Warning|If two registry objects within the same registry have the same name, the second object will override the first. The only registry that will throw an <code>IllegalArgumentException</code> is the <code>DataSerializerEntry</code> registry.}}
+
Forge registries preserves the same loading order of Vanilla registries, with all modded registries fired after Vanilla in alphabetical order by string comparing namespace then path. This loading order determines what order registries have their entries registered. All registries wrapped by Forge can be found within the <code>ForgeRegistries</code> class.
 +
 
 +
{{Tip|All registries have their own set of names and objects, so the same name (e.g. <code>examplemod:object</code>) can be reused in multiple registries, like blocks and items.}}
 +
 
 +
{{Tip/Warning|If two registry objects within the '''same''' registry have the same name, the second object will override the first.}}
    
== Methods for Registering ==
 
== Methods for Registering ==
   −
There are two proper ways to register objects within an associated wrapped Forge registry: the <code>DeferredRegister</code> class, and the <code>RegisterEvent</code> lifecycle event.
+
There are two proper ways to register objects to a Forge registry or Vanilla registry: the <code>DeferredRegister</code> class, and the <code>RegisterEvent</code> lifecycle event.
    
=== DeferredRegister ===
 
=== DeferredRegister ===
Line 158: Line 164:  
=== Using @ObjectHolder ===
 
=== Using @ObjectHolder ===
   −
Forge registry objects can also be injected into <code>public static</code> fields with either their class or that field annotated with <code>@ObjectHolder</code>. There must be enough information to construct a <code>ResourceLocation</code> to identify a single object within a specific registry.
+
Forge registry objects can also be injected into <code>public static final</code> fields by annotating each field with <code>@ObjectHolder</code>. Note that using <code>RegistryObject</code>s is the preferred strategy as ObjectHolders are verbose, clunky, and easy to mess up.
 +
 
 +
ObjectHolders can only be applied to fields and require 2 pieces of information: the registry name of your target registry and the name of your object entry inside the registry.
 +
 
 +
The registry name can be found inside of <code>ForgeRegistries.Keys</code> or <code>Registry</code>.
 +
For blocks, this would be <code>"minecraft:block"</code>.
 +
For items, this would be <code>"minecraft:item"</code>, etc.
 +
 
 +
The name of your entry is dependent on what you called it and requires your modid to be prefixed.
 +
When using <code>@ObjectHolder</code> inside of your main mod class annotated with <code>@Mod</code>, the modid namespace can be omitted.
    
The rules for <code>@ObjectHolder</code> are as follows:
 
The rules for <code>@ObjectHolder</code> are as follows:
   −
* If the class is annotated with <code>@ObjectHolder</code>, its value will be the default namespace for all fields within if not explicitly defined
   
* If the class is annotated with <code>@Mod</code>, the modid will be the default namespace for all annotated fields within if not explicitly defined  
 
* If the class is annotated with <code>@Mod</code>, the modid will be the default namespace for all annotated fields within if not explicitly defined  
 
* A field is considered for injection if:
 
* A field is considered for injection if:
** it has at least the modifiers <code>public static</code>; and
+
** it has at least the modifiers <code>public static</code> and optionally <code>final</code>, and
** one of the following conditions are true:
+
** the '''field''' is annotated with <code>@ObjectHolder</code>, and:
*** the '''enclosing class''' has an <code>@ObjectHolder</code> annotation, and the field is <code>final</code>, and:
+
*** the entry name value is explicitly defined, and
**** the name value is the field's name; and
+
*** the registry name value is explicitly defined
**** the namespace value is the enclosing class's namespace
  −
**** ''An exception is thrown if the namespace value cannot be found and inherited''
  −
*** the '''field''' is annotated with <code>@ObjectHolder</code>, and:
  −
**** the name value is explicitly defined; and
  −
**** the namespace value is either explicitly defined or the enclosing class's namespace
  −
** the field type or one of its supertypes corresponds to a valid registry (e.g. <code>Item</code> or <code>ArrowItem</code> for the <code>Item</code> registry)
  −
** ''An exception is thrown if a field does not have a corresponding registry.''
   
* ''An exception is thrown if the resulting <code>ResourceLocation</code> is incomplete or invalid (non-valid characters in path)''
 
* ''An exception is thrown if the resulting <code>ResourceLocation</code> is incomplete or invalid (non-valid characters in path)''
 
* If no other errors or exceptions occur, the field will be injected
 
* If no other errors or exceptions occur, the field will be injected
 
* If all of the above rules do not apply, no action will be taken (and a message may be logged)
 
* If all of the above rules do not apply, no action will be taken (and a message may be logged)
   −
<code>@ObjectHolder</code> annotated fields are injected with their associated object values after <code>RegisterEvent</code> is fired for their registry, along with <code>RegistryObject</code>s.
+
<code>@ObjectHolder</code> annotated fields are injected with their associated object values after <code>RegisterEvent</code> is fired for their registry, the same time that <code>RegistryObject</code>s are filled. ObjectHolders will remain empty if the associated registry does not exist.
    
{{Tip/Warning|If the object does not exist in the registry when it is to be injected, a debug message will be logged, and no value will be injected. If the object is found, but the field cannot be set, a warning message will be logged instead.}}
 
{{Tip/Warning|If the object does not exist in the registry when it is to be injected, a debug message will be logged, and no value will be injected. If the object is found, but the field cannot be set, a warning message will be logged instead.}}
Line 189: Line 196:  
<div class="mw-collapsible-content" style="overflow: auto; white-space: nowrap;">
 
<div class="mw-collapsible-content" style="overflow: auto; white-space: nowrap;">
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
@ObjectHolder("minecraft") // Inheritable resource namespace: "minecraft"
+
class Holder {
class AnnotatedHolder {
+
     @ObjectHolder(registryName = "minecraft:enchantment", value = "minecraft:flame")
    public static final Block diamond_block = null;  // No annotation. [public static final] is required.
  −
                                                      // Block has a corresponding registry: [Block]
  −
                                                      // Name path is the name of the field: "diamond_block"
  −
                                                      // Namespace is not explicitly defined.
  −
                                                      // So, namespace is inherited from class annotation: "minecraft"
  −
                                                      // To inject: "minecraft:diamond_block" from the [Block] registry
  −
 
  −
    @ObjectHolder("ambient.cave")
  −
    public static SoundEvent ambient_sound = null;    // Annotation present. [public static] is required.
  −
                                                      // SoundEvent has a corresponding registry: [SoundEvent]
  −
                                                      // Name path is the value of the annotation: "ambient.cave"
  −
                                                      // Namespace is not explicitly defined.
  −
                                                      // So, namespace is inherited from class annotation: "minecraft"
  −
                                                      // To inject: "minecraft:ambient.cave" from the [SoundEvent] registry
  −
 
  −
    // Assume for the next entry that [ManaType] is a valid registry.         
  −
    @ObjectHolder("neomagicae:coffeinum")
  −
    public static final ManaType coffeinum = null;    // Annotation present. [public static] is required. [final] is optional.
  −
                                                      // ManaType has a corresponding registry: [ManaType] (custom registry)
  −
                                                      // Resource location is explicitly defined: "neomagicae:coffeinum"
  −
                                                      // To inject: "neomagicae:coffeinum" from the [ManaType] registry
  −
 
  −
    public static final Item ENDER_PEARL = null;      // No annotation. [public static final] is required.
  −
                                                      // Item has a corresponding registry: [Item].
  −
                                                      // Name path is the name of the field: "ENDER_PEARL" -> "ender_pearl"
  −
                                                      // !! ^ Field name is valid, because they are
  −
                                                      //      converted to lowercase automatically.
  −
                                                      // Namespace is not explicitly defined.
  −
                                                      // So, namespace is inherited from class annotation: "minecraft"
  −
                                                      // To inject: "minecraft:ender_pearl" from the [Item] registry
  −
 
  −
     @ObjectHolder("minecraft:arrow")
  −
    public static final ArrowItem arrow = null;      // Annotation present. [public static] is required. [final] is optional.
  −
                                                      // ArrowItem does not have a corresponding registry.
  −
                                                      // ArrowItem's supertype of Item has a corresponding registry: [Item]
  −
                                                      // Resource location is explicitly defined: "minecraft:arrow"
  −
                                                      // To inject: "minecraft:arrow" from the [Item] registry                                                   
  −
 
  −
    public static Block bedrock = null;              // No annotation, so [public static final] is required.
  −
                                                      // Therefore, the field is ignored.
  −
   
  −
    public static final CreativeModeTab group = null; // No annotation. [public static final] is required.
  −
                                                      // CreativeModeTab does not have a corresponding registry.
  −
                                                      // No supertypes of CreativeModeTab has a corresponding registry.
  −
                                                      // Therefore, THIS WILL PRODUCE AN EXCEPTION.
  −
}
  −
 
  −
class UnannotatedHolder { // Note the lack of an @ObjectHolder annotation on this class.
  −
    @ObjectHolder("minecraft:flame")
   
     public static final Enchantment flame = null;    // Annotation present. [public static] is required. [final] is optional.
 
     public static final Enchantment flame = null;    // Annotation present. [public static] is required. [final] is optional.
                                                       // Enchantment has corresponding registry: [Enchantment].
+
                                                       // Registry name is explicitly defined: "minecraft:enchantment"
 
                                                       // Resource location is explicitly defined: "minecraft:flame"
 
                                                       // Resource location is explicitly defined: "minecraft:flame"
 
                                                       // To inject: "minecraft:flame" from the [Enchantment] registry
 
                                                       // To inject: "minecraft:flame" from the [Enchantment] registry
   −
     public static final Biome ice_flat = null;        // No annotation on the enclosing class.
+
     public static final Biome ice_flat = null;        // No annotation on the field.
 
                                                       // Therefore, the field is ignored.
 
                                                       // Therefore, the field is ignored.
    
     @ObjectHolder("minecraft:creeper")
 
     @ObjectHolder("minecraft:creeper")
     public static Entity creeper = null;             // Annotation present. [public static] is required.
+
     public static final Entity creeper = null;       // Annotation present. [public static] is required. [final] is optional.
                                                       // Entity does not have a corresponding registry.
+
                                                       // The registry name has not been specified on the field.
                                                      // No supertypes of Entity has a corresponding registry.
+
                                                       // Therefore, this will not compile.
                                                       // Therefore, THIS WILL PRODUCE AN EXCEPTION.
     −
     @ObjectHolder("levitation")
+
     @ObjectHolder(registryName = "minecraft:potion")
 
     public static final Potion levitation = null;    // Annotation present. [public static] is required. [final] is optional.
 
     public static final Potion levitation = null;    // Annotation present. [public static] is required. [final] is optional.
                                                       // Potion has a corresponding registry: [Potion].
+
                                                       // Registry name is explicitly defined: "minecraft:potion"
                                                      // Name path is the value of the annotation: "levitation"
+
                                                       // The entry's name value has not been specified on the field.
                                                       // Namespace is not explicitly defined.
+
                                                       // Therefore, this will not compile.
                                                      // No annotation in enclosing class.
  −
                                                       // Therefore, THIS WILL PRODUCE AN EXCEPTION.
   
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 284: Line 239:  
|kotlin=val EXAMPLE: DeferredRegister<ExampleRegistry> = DeferredRegister.create(ResourceLocation(MODID, "example_registry"), MODID)
 
|kotlin=val EXAMPLE: DeferredRegister<ExampleRegistry> = DeferredRegister.create(ResourceLocation(MODID, "example_registry"), MODID)
   −
val REGISTRY: IForgeRegistry<ExampleRegistry> by lazy {
+
val REGISTRY: IForgeRegistry<ExampleRegistry> by EXAMPLE.makeRegistry(::RegistryBuilder).let {
    EXAMPLE.makeRegistry(::RegistryBuilder).get()
+
    lazy {
 +
        it.get()
 +
    }
 
}
 
}
 
|scala=final val EXAMPLE = DeferredRegister.create(new ResourceLocation(MODID, "example_registry"), MODID)
 
|scala=final val EXAMPLE = DeferredRegister.create(new ResourceLocation(MODID, "example_registry"), MODID)
   −
final lazy val REGISTRY = EXAMPLE.makeRegistry(() => new RegistryBuilder).get
+
private final val PRIVATE_REGISTRY = EXAMPLE.makeRegistry(() => new RegistryBuilder)
 +
final lazy val REGISTRY = PRIVATE_REGISTRY.get
 
|}}
 
|}}