====== Accessor と Invoker ====== Accessor〔アクセサ〕と Invoker〔インヴォーカ〕を使うと、通常はアクセスできない(private な)フィールドにアクセスしたり、メソッドを呼び出したりすることができます。 ===== Accessor ===== ''@Accessor'' はフィールドにアクセスすることを可能にします。例として、''MinecraftClient'' クラスの ''itemUseCooldown'' フィールドにアクセスすることを考えます。 ==== フィールドの値を得る(Getter) ==== @Mixin(MinecraftClient.class) public interface MinecraftClientAccessor { @Accessor int getItemUseCooldown(); } 使い方: int itemUseCooldown = ((MinecraftClientAccessor) MinecraftClient.getInstance()).getItemUseCooldown(); ==== フィールドの値を設定する(Setter) ==== @Mixin(MinecraftClient.class) public interface MinecraftClientAccessor { @Accessor("itemUseCooldown") public void setItemUseCooldown(int itemUseCooldown); } 使い方: ((MinecraftClientAccessor) MinecraftClient.getInstance()).setItemUseCooldown(100); ===== static なフィールドへの Accessor ===== 例として、''VanillaLayeredBiomeSource'' クラスの ''BIOMES'' フィールドにアクセスすることを考えます。 ==== フィールドの値を得る(Getter) ==== @Mixin(VanillaLayeredBiomeSource.class) public interface VanillaLayeredBiomeSourceAccessor { @Accessor("BIOMES") public static List> getBiomes() { throw new AssertionError(); } } 使い方: List> biomes = VanillaLayeredBiomeSourceAccessor.getBiomes(); ==== フィールドの値を設定する(Setter) ==== @Mixin(VanillaLayeredBiomeSource.class) public interface VanillaLayeredBiomeSourceAccessor { @Accessor("BIOMES") public static void setBiomes(List> biomes) { throw new AssertionError(); } } 使い方: VanillaLayeredBiomeSourceAccessor.setBiomes(biomes); ===== Invoker ===== ''@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); ===== static なメソッドへの Invoker ===== 例として、''BrewingRecipeRegistry'' クラスの ''registerPotionType'' メソッドを呼び出すことを考えます。 @Mixin(BrewingRecipeRegistry.class) public interface BrewingRecipeRegistryInvoker { @Invoker("registerPotionType") public static void invokeRegisterPotionType(Item item) { throw new AssertionError(); } } 使い方: BrewingRecipeRegistryInvoker.invokeRegisterPotionType(item);