====== Mixin 访问器和调用器 ====== 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> getBiomes() { throw new AssertionError(); } } 用法: List> biomes = VanillaLayeredBiomeSourceAccessor.getBiomes(); ==== 设置字段的值 ==== @Mixin(VanillaLayeredBiomeSource.class) public interface VanillaLayeredBiomeSourceAccessor { @Accessor("BIOMES") public static void setBiomes(List> 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);