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
Next revisionBoth sides next revision
tutorial:mixin_accessors [2020/08/28 08:18] – Improve explanation, fix errors siglongtutorial:mixin_accessors [2020/09/06 10:34] – Add how to access static fields and methods siglong
Line 1: Line 1:
 ====== Mixin Accessors ====== ====== Mixin Accessors ======
- 
-===== Introduction ===== 
 Mixin Accessors allow you to access fields and methods that are not visible (private) or final. Mixin Accessors allow you to access fields and methods that are not visible (private) or final.
  
-==== Accessor ====+===== Accessor =====
 ''@Accessor'' allows you to access fields. Suppose we want to access ''itemUseCooldown'' field of ''MinecraftClient'' class. ''@Accessor'' allows you to access fields. Suppose we want to access ''itemUseCooldown'' field of ''MinecraftClient'' class.
  
-=== Getting a value from the field ===+==== Getting a value from the field ====
 <code java> <code java>
 @Mixin(MinecraftClient.class) @Mixin(MinecraftClient.class)
Line 22: Line 20:
 </code> </code>
  
-=== Setting a value to the field ===+==== Setting a value to the field ====
 <code java> <code java>
 @Mixin(MinecraftClient.class) @Mixin(MinecraftClient.class)
Line 37: Line 35:
 </code> </code>
  
-==== Invoker ====+===== Accessor for static fields ===== 
 +Suppose we want to access ''BIOMES'' field of ''VanillaLayeredBiomeSource'' class. 
 + 
 +==== Getting a value from the field ==== 
 +<code java> 
 +@Mixin(VanillaLayeredBiomeSource.class) 
 +public interface VanillaLayeredBiomeSourceAccessor { 
 +  @Accessor("BIOMES"
 +  public static List<RegistryKey<Biome>> getBiomes() { 
 +    throw new AssertionError(); 
 +  } 
 +
 +</code> 
 + 
 +Usage: 
 + 
 +<code java> 
 +List<RegistryKey<Biome>> biomes = VanillaLayeredBiomeSourceAccessor.getBiomes(); 
 +</code> 
 + 
 +==== Setting a value to the field ==== 
 +<code java> 
 +@Mixin(VanillaLayeredBiomeSource.class) 
 +public interface VanillaLayeredBiomeSourceAccessor { 
 +  @Accessor("BIOMES"
 +  public static void setBiomes(List<RegistryKey<Biome>> biomes) { 
 +    throw new AssertionError(); 
 +  } 
 +
 +</code> 
 + 
 +Usage: 
 + 
 +<code java> 
 +VanillaLayeredBiomeSourceAccessor.setBiomes(biomes); 
 +</code> 
 + 
 +===== Invoker =====
 ''@Invoker'' allows you to access methods. Suppose we want to invoke ''teleportTo'' method of ''EndermanEntity'' class. ''@Invoker'' allows you to access methods. Suppose we want to invoke ''teleportTo'' method of ''EndermanEntity'' class.
  
Line 43: Line 78:
 @Mixin(EndermanEntity.class) @Mixin(EndermanEntity.class)
 public interface EndermanEntityInvoker { public interface EndermanEntityInvoker {
-  @Invoker +  @Invoker("teleportTo") 
-  public boolean teleportTo(double x, double y, double z);+  public boolean invokeTeleportTo(double x, double y, double z);
 } }
 </code> </code>
Line 52: Line 87:
 <code java> <code java>
 EndermanEntity enderman = ...; EndermanEntity enderman = ...;
-((EndermanEntityInvoker) enderman).teleportTo(0.0D, 70.0D, 0.0D);+((EndermanEntityInvoker) enderman).invokeTeleportTo(0.0D, 70.0D, 0.0D);
 </code> </code>
  
 +===== Invoker for static methods =====
 +Suppose we want to invoke ''registerPotionType'' method of ''BrewingRecipeRegistry'' class.
 +
 +<code java>
 +@Mixin(BrewingRecipeRegistry.class)
 +public interface BrewingRecipeRegistryInvoker {
 +  @Invoker("registerPotionType")
 +  public static void invokeRegisterPotionType(Item item) {
 +    throw new AssertionError();
 +  }
 +}
 +</code>
 +
 +Usage:
 +
 +<code java>
 +BrewingRecipeRegistryInvoker.invokeRegisterPotionType(item);
 +</code>
tutorial/mixin_accessors.txt · Last modified: 2022/04/29 09:54 by solidblock