User Tools

Site Tools


Sidebar

← Go back to the homepage

Fabric Tutorials

Setup

Basics

These pages are essential must-reads when modding with Fabric, and modding Minecraft in general, if you are new to modding, it is recommended you read the following.

Items

Blocks and Block Entities

Data Generation

World Generation

Commands

These pages will guide you through Mojang's Brigadier library which allows you to create commands with complex arguments and actions.

Events

These pages will guide you through using the many events included in Fabric API, and how to create your own events for you or other mods to use.

Entities

Fluids

Mixins & ASM

These pages will guide you through the usage of SpongePowered's Mixin library, which is a highly complex topic. We recommend you read these pages thoroughly.

Miscellaneous

Yarn

Contribute to Fabric

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