Skip to content

How to read a private field with a mixin accessor (Minecraft mod)

An accessor is a mixin written as an interface. @Accessor on a method makes the game class implement it and read or write one field. @Invoker does the same for a private method. Cast the object to your interface to use it: ((MyAccessor) entity).getValue(). It needs no widening of the class and works for any field you name. Put the interface in the mixin package and list it in the mixins file like any mixin.

Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.

  1. Write an interface annotated with @Mixin(TargetClass.class).
  2. Add a method with @Accessor(“fieldName”) for the getter, and one for the setter.
  3. List it in the mixins json file.
  4. Cast an instance of the class to your interface where you need the field.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Fabric 1.16.5 to 1.20.3, Fabric and NeoForge 1.21.2 to 26.3, Forge 1.17.1 to 1.20.1

Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Fabric 1.16.5 to 1.20.3, Fabric and NeoForge 1.21.2 to 26.3, Forge 1.17.1 to 1.20.1”

Uses:

  • @Mixin
  • @Accessor
  • LivingEntity.jumping

src/main/java/com/example/examplemod/mixin/JumpAccessMixin.java

package com.example.examplemod.mixin;
import net.minecraft.world.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
// Jump Access: lets code read and write LivingEntity's protected "jumping" field. Use it as ((LivingEntity) e) cast to JumpAccessMixin.
@Mixin(LivingEntity.class)
public interface JumpAccessMixin {
@Accessor("jumping")
boolean examplemod$isJumping();
@Accessor("jumping")
void examplemod$setJumping(boolean jumping);
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"JumpAccessMixin"

Uses:

  • @Mixin
  • @Accessor
  • LivingEntity.jumping

src/main/java/com/example/examplemod/mixin/JumpAccessMixin.java

package com.example.examplemod.mixin;
import net.minecraft.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
// Jump Access: lets code read and write LivingEntity's protected "jumping" field. Use it as ((LivingEntity) e) cast to JumpAccessMixin.
@Mixin(LivingEntity.class)
public interface JumpAccessMixin {
@Accessor("jumping")
boolean examplemod$isJumping();
@Accessor("jumping")
void examplemod$setJumping(boolean jumping);
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"JumpAccessMixin"

Uses:

  • @Mixin
  • @Accessor
  • EntityLivingBase.jumping

src/main/java/com/example/examplemod/mixin/JumpAccessMixin.java

package com.example.examplemod.mixin;
import net.minecraft.entity.EntityLivingBase;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
// Jump Access: lets code read and write EntityLivingBase's protected "isJumping" field. Use it as ((EntityLivingBase) e) cast to JumpAccessMixin.
@Mixin(EntityLivingBase.class)
public interface JumpAccessMixin {
@Accessor("isJumping")
boolean examplemod$isJumping();
@Accessor("isJumping")
void examplemod$setJumping(boolean jumping);
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"JumpAccessMixin"

Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.