tutorial:mixin_accessors
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:mixin_accessors [2020/08/25 10:39] – Rewrite whole page, adding Provider siglong | tutorial:mixin_accessors [2025/11/30 20:08] (current) – gauntrecluse | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== |
| - | ===== Introduction ===== | + | **Accessor Mixins** are special mixins defined as interfaces which must **only** contain '' |
| - | Mixin Accessors allow you to access fields | + | |
| - | ==== Accessor ==== | + | The '' |
| - | '' | + | |
| - | === Set a value to a field === | + | 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. |
| + | |||
| + | '' | ||
| + | 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 ===== | ||
| + | '' | ||
| + | |||
| + | ==== Getting a value from the field ==== | ||
| + | < | ||
| + | @Mixin(class_310.class) | ||
| + | public interface class_310Accessor { | ||
| + | @Accessor(" | ||
| + | int modid$getItemUseCooldown(); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Usage: | ||
| + | |||
| + | < | ||
| + | int field_1752 = ((class_310Accessor) class_310.method_1551()).modid$getItemUseCooldown(); | ||
| + | </ | ||
| + | |||
| + | ==== Setting | ||
| + | < | ||
| + | @Mixin(class_310.class) | ||
| + | public interface class_310Accessor { | ||
| + | @Accessor(" | ||
| + | void modid$setItemUseCooldown(int field_1752); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Usage: | ||
| + | |||
| + | < | ||
| + | ((class_310Accessor) class_310.method_1551()).modid$setItemUseCooldown(100); | ||
| + | </ | ||
| + | |||
| + | When the field is final and you need to set it, use '' | ||
| + | |||
| + | ===== Accessor for static fields ===== | ||
| + | FIXME // | ||
| + | Suppose we want to access the '' | ||
| + | |||
| + | ==== Getting a value from the field ==== | ||
| <code java> | <code java> | ||
| - | @Mixin(MinecraftClient.class) | + | @Mixin(VanillaLayeredBiomeSource.class) |
| - | public interface | + | public interface |
| - | @Accessor(" | + | @Accessor(" |
| - | | + | static List< |
| + | | ||
| + | } | ||
| } | } | ||
| </ | </ | ||
| Line 19: | Line 63: | ||
| <code java> | <code java> | ||
| - | int itemUseCooldown | + | List< |
| </ | </ | ||
| - | === Get a value from a field === | + | ==== Setting |
| <code java> | <code java> | ||
| - | @Mixin(MinecraftClient.class) | + | @Mixin(VanillaLayeredBiomeSource.class) |
| - | public interface | + | public interface |
| - | @Accessor(" | + | @Accessor(" |
| - | | + | |
| + | throw new AssertionError(); | ||
| + | } | ||
| } | } | ||
| </ | </ | ||
| Line 34: | Line 80: | ||
| <code java> | <code java> | ||
| - | ((MinecraftClientAccessor) MinecraftClient.getInstance()).setItemUseCooldown(100); | + | VanillaLayeredBiomeSourceAccessor.setBiomes(biomes); |
| </ | </ | ||
| - | ==== Provider | + | When the field is final and you need to set it, use '' |
| - | '' | + | |
| + | ===== Invoker ===== | ||
| + | '' | ||
| + | |||
| + | < | ||
| + | @Mixin(class_1560.class) | ||
| + | public interface class_1560Invoker { | ||
| + | @Invoker(" | ||
| + | boolean modid$invokeTeleportTo(double x, double y, double z); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Usage: | ||
| + | |||
| + | < | ||
| + | class_1560 enderman = ...; | ||
| + | ((class_1560Invoker) enderman).modid$invokeTeleportTo(0.0D, | ||
| + | </ | ||
| + | |||
| + | ===== Invoker for static methods ===== | ||
| + | FIXME //This example is not fully accurate to latest versions, '' | ||
| + | Suppose we want to invoke '' | ||
| <code java> | <code java> | ||
| - | @Mixin(EndermanEntity.class) | + | @Mixin(BrewingRecipeRegistry.class) |
| - | public interface | + | public interface |
| - | @Provider | + | @Invoker(" |
| - | | + | |
| + | throw new AssertionError(); | ||
| + | } | ||
| } | } | ||
| </ | </ | ||
| Line 51: | Line 120: | ||
| <code java> | <code java> | ||
| - | EndermanEntity enderman = ...; | + | BrewingRecipeRegistryAccessor.invokeRegisterPotionType(item); |
| - | ((EndermanEntityInvoker) enderman).teleportTo(0.0D, | + | |
| </ | </ | ||
| + | ==== Invoker for constructors ==== | ||
| + | |||
| + | Invokers for constructors must be static, pass ''"< | ||
| + | |||
| + | For example, we will use the '' | ||
| + | <code java> | ||
| + | @Mixin(Identifier.class) | ||
| + | public interface IdentifierAccessor { | ||
| + | @Invoker("< | ||
| + | static Identifier newIdentifier(String namespace, String path) { | ||
| + | throw new AssertionError(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Usage: | ||
| + | <code java> | ||
| + | Identifier exampleVariable = IdentifierAccessor.newIdentifier(" | ||
| + | </ | ||
tutorial/mixin_accessors.1598351960.txt.gz · Last modified: 2020/08/25 10:39 by siglong