Mixin accessors and invokers allow you to access fields or invoke methods that are not visible (private) or final.
@Accessor
allows you to access fields. Suppose we want to access itemUseCooldown
field of MinecraftClient
class.
@Mixin(MinecraftClient.class) public interface MinecraftClientAccessor { @Accessor int getItemUseCooldown(); }
Usage:
int itemUseCooldown = ((MinecraftClientAccessor) MinecraftClient.getInstance()).getItemUseCooldown();
@Mixin(MinecraftClient.class) public interface MinecraftClientAccessor { @Accessor("itemUseCooldown") public void setItemUseCooldown(int itemUseCooldown); }
Usage:
((MinecraftClientAccessor) MinecraftClient.getInstance()).setItemUseCooldown(100);
Suppose we want to access BIOMES
field of VanillaLayeredBiomeSource
class.
@Mixin(VanillaLayeredBiomeSource.class) public interface VanillaLayeredBiomeSourceAccessor { @Accessor("BIOMES") public static List<RegistryKey<Biome>> getBiomes() { throw new AssertionError(); } }
Usage:
List<RegistryKey<Biome>> biomes = VanillaLayeredBiomeSourceAccessor.getBiomes();
@Mixin(VanillaLayeredBiomeSource.class) public interface VanillaLayeredBiomeSourceAccessor { @Accessor("BIOMES") public static void setBiomes(List<RegistryKey<Biome>> biomes) { throw new AssertionError(); } }
Usage:
VanillaLayeredBiomeSourceAccessor.setBiomes(biomes);
@Invoker
allows you to access methods. Suppose we want to invoke teleportTo
method of EndermanEntity
class.
@Mixin(EndermanEntity.class) public interface EndermanEntityInvoker { @Invoker("teleportTo") public boolean invokeTeleportTo(double x, double y, double z); }
Usage:
EndermanEntity enderman = ...; ((EndermanEntityInvoker) enderman).invokeTeleportTo(0.0D, 70.0D, 0.0D);
Suppose we want to invoke registerPotionType
method of BrewingRecipeRegistry
class.
@Mixin(BrewingRecipeRegistry.class) public interface BrewingRecipeRegistryInvoker { @Invoker("registerPotionType") public static void invokeRegisterPotionType(Item item) { throw new AssertionError(); } }
Usage:
BrewingRecipeRegistryInvoker.invokeRegisterPotionType(item);