User Tools

Site Tools


tutorial:mixin_accessors

This is an old revision of the document!


Mixin Accessors

Introduction

Mixin Accessors allow you to access fields and 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("itemUseCooldown")
    public 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);

Invoker

@Invoker allows you to access methods. Suppose we want to invoke teleportTo method of EndermanEntity class.

@Mixin(EndermanEntity.class)
public interface EndermanEntityInvoker {
  @Invoker
  public boolean teleportTo(double x, double y, double z);
}

Usage:

EndermanEntity enderman = ...;
((EndermanEntityInvoker) enderman).teleportTo(0.0D, 70.0D, 0.0D);
tutorial/mixin_accessors.1598602712.txt.gz · Last modified: 2020/08/28 08:18 by siglong