User Tools

Site Tools


tutorial:mixin_accessors

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:mixin_accessors [2025/10/17 13:47] – Add warnings for outdated examples, use yarncode plugin for most refs to vanilla code. gauntreclusetutorial:mixin_accessors [2025/11/30 20:08] (current) gauntrecluse
Line 1: Line 1:
 ====== Accessor Mixins ====== ====== Accessor Mixins ======
-FIXME //This page is under revison. This page may change suddenly and should be taken with a grain of salt.// 
  
 **Accessor Mixins** are special mixins defined as interfaces which must **only** contain ''@Accessor'' and ''@Invoker'' handlers. Unlike normal mixins however, **Accessor Mixins** are accessible via user code directly. **Accessor Mixins** are special mixins defined as interfaces which must **only** contain ''@Accessor'' and ''@Invoker'' handlers. Unlike normal mixins however, **Accessor Mixins** are accessible via user code directly.
Line 8: Line 7:
 Unlike typical injectors, accessors do not prefix the merged methods with the modid of the mod that contains them. Additionally as the **Accessor Mixins** are used in user code, the names of the handlers are not mangled, these differences are important to keep in mind when writing **Accessor Mixins** for compatibility and debugging purposes. Unlike typical injectors, accessors do not prefix the merged methods with the modid of the mod that contains them. Additionally as the **Accessor Mixins** are used in user code, the names of the handlers are not mangled, these differences are important to keep in mind when writing **Accessor Mixins** for compatibility and debugging purposes.
  
-''@Accessor''s and ''@Invoker''s can infer the field/method name if they are called ''getFieldName'' / ''call/invokeMethodName'', omitting the requirement for the ''value'' attribute to be specified, however, all handlers should be prefixed as so: ''modid$getFieldName'' instead. This is because when the **Accessor Mixin** is merged the handler will remain unchanged and if there is a problem related to the handler, it's useful to know who owns it. Additionally, if another mod decides to create a Duck interface with a method called ''getFieldName'', when that is merged alongside your **Accessor Mixin** whichever one applies last will overwrite the other and you end up with a conflict that may crash or cause unintended behaviour.+''@Accessor''s and ''@Invoker''s can infer the field/method name if they are called ''getFieldName'' / ''call/invokeMethodName'', omitting the requirement for the ''value'' attribute to be specified, however, all non-static handlers should be prefixed as so: ''modid$getFieldName'' instead. This is because when the **Accessor Mixin** is merged the handler will remain unchanged and if there is a problem related to the handler, it's useful to know who owns it. Additionally, if another mod decides to create a Duck interface with a method called ''getFieldName'', when that is merged alongside your **Accessor Mixin** whichever one applies last will overwrite the other and you end up with a conflict that may crash or cause unintended behaviour.\\ 
 +Prefixing accessor or invoker handlers is not necessary if they are static, as those may not conflict, and the stacktrace provides the Mixin class whose handlers are involved in errors, thus making it sufficient without needing to prefix with the modid.
  
 ===== Accessor ===== ===== Accessor =====
Line 54: Line 54:
 public interface VanillaLayeredBiomeSourceAccessor { public interface VanillaLayeredBiomeSourceAccessor {
   @Accessor("BIOMES")   @Accessor("BIOMES")
-  static List<RegistryKey<Biome>> modid$getBiomes() {+  static List<RegistryKey<Biome>> getBiomes() {
     throw new AssertionError();     throw new AssertionError();
   }   }
Line 63: Line 63:
  
 <code java> <code java>
-List<RegistryKey<Biome>> biomes = VanillaLayeredBiomeSourceAccessor.modid$getBiomes();+List<RegistryKey<Biome>> biomes = VanillaLayeredBiomeSourceAccessor.getBiomes();
 </code> </code>
  
Line 71: Line 71:
 public interface VanillaLayeredBiomeSourceAccessor { public interface VanillaLayeredBiomeSourceAccessor {
   @Accessor("BIOMES")   @Accessor("BIOMES")
-  static void modid$setBiomes(List<RegistryKey<Biome>> biomes) {+  static void setBiomes(List<RegistryKey<Biome>> biomes) {
     throw new AssertionError();     throw new AssertionError();
   }   }
Line 80: Line 80:
  
 <code java> <code java>
-VanillaLayeredBiomeSourceAccessor.modid$setBiomes(biomes);+VanillaLayeredBiomeSourceAccessor.setBiomes(biomes);
 </code> </code>
  
Line 109: Line 109:
 <code java> <code java>
 @Mixin(BrewingRecipeRegistry.class) @Mixin(BrewingRecipeRegistry.class)
-public interface BrewingRecipeRegistryInvoker {+public interface BrewingRecipeRegistryAccessor {
   @Invoker("registerPotionType")   @Invoker("registerPotionType")
-  static void modid$invokeRegisterPotionType(Item item) {+  static void invokeRegisterPotionType(Item item) {
     throw new AssertionError();     throw new AssertionError();
   }   }
Line 120: Line 120:
  
 <code java> <code java>
-BrewingRecipeRegistryInvoker.modid$invokeRegisterPotionType(item);+BrewingRecipeRegistryAccessor.invokeRegisterPotionType(item); 
 +</code> 
 + 
 +==== Invoker for constructors ==== 
 + 
 +Invokers for constructors must be static, pass ''"<init>"'' as the ''@Invoker'' annotation attribute, and return the type of the target class. 
 + 
 +For example, we will use the ''Identifier'' class's private constructor. This is not something that you normally need to do for that class as it has accessible static constructors; it is however a good example for the syntax of a constructor invoker. Using 1.21.1 Yarn mappings: 
 +<code java> 
 +@Mixin(Identifier.class) 
 +public interface IdentifierAccessor { 
 +    @Invoker("<init>"
 +    static Identifier newIdentifier(String namespace, String path) { 
 +        throw new AssertionError(); 
 +    } 
 +
 +</code> 
 + 
 +Usage: 
 +<code java> 
 +Identifier exampleVariable = IdentifierAccessor.newIdentifier("some_namespace", "some_path");
 </code> </code>
tutorial/mixin_accessors.1760708878.txt.gz · Last modified: 2025/10/17 13:47 by gauntrecluse