User Tools

Site Tools


tutorial:mixin_accessors

Mixin Accessors & Invokers

Mixin accessors and invokers allow you to access fields or invoke methods that are not visible (private) or final.

Accessor

@Accessor allows you to access fields. Suppose we want to access itemUseCooldown field of MinecraftClient class.

Getting a value from the field

@Mixin(MinecraftClient.class)
public interface MinecraftClientAccessor {
    @Accessor
    int getItemUseCooldown();
}

Usage:

int itemUseCooldown = ((MinecraftClientAccessor) MinecraftClient.getInstance()).getItemUseCooldown();

Setting a value to the field

@Mixin(MinecraftClient.class)
public interface MinecraftClientAccessor {
    @Accessor("itemUseCooldown")
    public void setItemUseCooldown(int itemUseCooldown);
}

Usage:

((MinecraftClientAccessor) MinecraftClient.getInstance()).setItemUseCooldown(100);

Accessor for static fields

Suppose we want to access BIOMES field of VanillaLayeredBiomeSource class.

Getting a value from the field

@Mixin(VanillaLayeredBiomeSource.class)
public interface VanillaLayeredBiomeSourceAccessor {
  @Accessor("BIOMES")
  public static List<RegistryKey<Biome>> getBiomes() {
    throw new AssertionError();
  }
}

Usage:

List<RegistryKey<Biome>> biomes = VanillaLayeredBiomeSourceAccessor.getBiomes();

Setting a value to the field

@Mixin(VanillaLayeredBiomeSource.class)
public interface VanillaLayeredBiomeSourceAccessor {
  @Accessor("BIOMES")
  public static void setBiomes(List<RegistryKey<Biome>> biomes) {
    throw new AssertionError();
  }
}

Usage:

VanillaLayeredBiomeSourceAccessor.setBiomes(biomes);

Invoker

@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);

Invoker for static methods

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);
tutorial/mixin_accessors.txt · Last modified: 2022/04/29 09:54 by solidblock