User Tools

Site Tools


zh_cn:tutorial:mixin_accessors

This is an old revision of the document!


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<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);
zh_cn/tutorial/mixin_accessors.1626000201.txt.gz · Last modified: 2021/07/11 10:43 by solidblock