tutorial:mixin_accessors
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tutorial:mixin_accessors [2020/08/05 22:26] – created emmanuelmess | tutorial:mixin_accessors [2022/04/29 09:54] (current) – [Mixin Accessors] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Mixin Accessors ====== | + | ====== Mixin Accessors |
+ | Mixin accessors and invokers allow you to access fields or invoke methods that are not visible (private) or final. | ||
- | ===== Introduction | + | ===== Accessor |
+ | '' | ||
- | A type of mixin allows to you to access variables that are not visible (private) or final: | + | ==== Getting a value from the field ==== |
<code java> | <code java> | ||
- | @Mixin(StructuresConfig.class) | + | @Mixin(MinecraftClient.class) |
- | public interface | + | public interface |
- | @Accessor(" | + | @Accessor |
- | | + | |
- | throw new IllegalAccessError(); | + | |
- | } | + | |
} | } | ||
</ | </ | ||
- | The error will never be thrown, you can use the function. | ||
+ | Usage: | ||
+ | <code java> | ||
+ | int itemUseCooldown = ((MinecraftClientAccessor) MinecraftClient.getInstance()).getItemUseCooldown(); | ||
+ | </ | ||
+ | |||
+ | ==== Setting a value to the field ==== | ||
+ | <code java> | ||
+ | @Mixin(MinecraftClient.class) | ||
+ | public interface MinecraftClientAccessor { | ||
+ | @Accessor(" | ||
+ | public void setItemUseCooldown(int itemUseCooldown); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Usage: | ||
+ | |||
+ | <code java> | ||
+ | ((MinecraftClientAccessor) MinecraftClient.getInstance()).setItemUseCooldown(100); | ||
+ | </ | ||
+ | |||
+ | ===== Accessor for static fields ===== | ||
+ | Suppose we want to access '' | ||
+ | |||
+ | ==== Getting a value from the field ==== | ||
+ | <code java> | ||
+ | @Mixin(VanillaLayeredBiomeSource.class) | ||
+ | public interface VanillaLayeredBiomeSourceAccessor { | ||
+ | @Accessor(" | ||
+ | public static List< | ||
+ | throw new AssertionError(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Usage: | ||
+ | |||
+ | <code java> | ||
+ | List< | ||
+ | </ | ||
+ | |||
+ | ==== Setting a value to the field ==== | ||
+ | <code java> | ||
+ | @Mixin(VanillaLayeredBiomeSource.class) | ||
+ | public interface VanillaLayeredBiomeSourceAccessor { | ||
+ | @Accessor(" | ||
+ | public static void setBiomes(List< | ||
+ | throw new AssertionError(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Usage: | ||
+ | |||
+ | <code java> | ||
+ | VanillaLayeredBiomeSourceAccessor.setBiomes(biomes); | ||
+ | </ | ||
+ | |||
+ | ===== Invoker ===== | ||
+ | '' | ||
+ | |||
+ | <code java> | ||
+ | @Mixin(EndermanEntity.class) | ||
+ | public interface EndermanEntityInvoker { | ||
+ | @Invoker(" | ||
+ | public boolean invokeTeleportTo(double x, double y, double z); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Usage: | ||
+ | |||
+ | <code java> | ||
+ | EndermanEntity enderman = ...; | ||
+ | ((EndermanEntityInvoker) enderman).invokeTeleportTo(0.0D, | ||
+ | </ | ||
+ | |||
+ | ===== Invoker for static methods ===== | ||
+ | Suppose we want to invoke '' | ||
+ | |||
+ | <code java> | ||
+ | @Mixin(BrewingRecipeRegistry.class) | ||
+ | public interface BrewingRecipeRegistryInvoker { | ||
+ | @Invoker(" | ||
+ | public static void invokeRegisterPotionType(Item item) { | ||
+ | throw new AssertionError(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Usage: | ||
+ | |||
+ | <code java> | ||
+ | BrewingRecipeRegistryInvoker.invokeRegisterPotionType(item); | ||
+ | </ |
tutorial/mixin_accessors.1596666389.txt.gz · Last modified: 2020/08/05 22:26 by emmanuelmess