Mixin 访问器和调用器允许你访问不可见的(私有的)或者常量的字段以及调用方法。
@Accessor
允许你访问字段。假如要访问 MinecraftClient
类中的 itemUseCooldown
字段。
@Mixin(MinecraftClient.class) public interface MinecraftClientAccessor { @Accessor int getItemUseCooldown(); }
用法:
int itemUseCooldown = ((MinecraftClientAccessor) MinecraftClient.getInstance()).getItemUseCooldown();
@Mixin(MinecraftClient.class) public interface MinecraftClientAccessor { @Accessor("itemUseCooldown") public void setItemUseCooldown(int itemUseCooldown); }
用法:
((MinecraftClientAccessor) MinecraftClient.getInstance()).setItemUseCooldown(100);
假如要访问 VanillaLayeredBiomeSource
类中的 BIOMES
字段。
@Mixin(VanillaLayeredBiomeSource.class) public interface VanillaLayeredBiomeSourceAccessor { @Accessor("BIOMES") public static List<RegistryKey<Biome>> getBiomes() { throw new AssertionError(); } }
用法:
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(); } }
用法:
VanillaLayeredBiomeSourceAccessor.setBiomes(biomes);
@Invoker
允许你访问方法。假如要调用 EndermanEntity
类的 teleportTo
方法。
@Mixin(EndermanEntity.class) public interface EndermanEntityInvoker { @Invoker("teleportTo") public boolean invokeTeleportTo(double x, double y, double z); }
用法
EndermanEntity enderman = ...; ((EndermanEntityInvoker) enderman).invokeTeleportTo(0.0D, 70.0D, 0.0D);
假如要调用 BrweingRecipeRegistry
类的 registerPotionType
方法。
@Mixin(BrewingRecipeRegistry.class) public interface BrewingRecipeRegistryInvoker { @Invoker("registerPotionType") public static void invokeRegisterPotionType(Item item) { throw new AssertionError(); } }
用法:
BrewingRecipeRegistryInvoker.invokeRegisterPotionType(item);