Table of Contents

Redirecting methods

Method redirectors can use the following injection point references:

INVOKE

The INVOKE injection point reference is used for invocations of target in method, which means that it can be used in order to redirect a method immediately before it is called.

Redirecting a static method

Static method redirectors should have the same parameters as the target.

redirecting the ItemStack::fromTag(ListTag) call in SimpleInventory::readTags to return null:

  1. @Mixin(SimpleInventory.class)
  2. abstract class SimpleInventoryMixin {
  3. @Redirect(method = "readTags",
  4. at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/item/ItemStack;fromTag(Lnet/minecraft/nbt/ListTag;)Lnet/minecraft/item/ItemStack;"))
  5. private static ItemStack returnNull(ListTag tag) {
  6. return null;
  7. }
  8. }

Redirecting an instance method

Instance method redirectors are similar to static methood redirectors, but they should have an additional parameter at the start of their parameter lists for the objects on which their targets are invoked.

redirecting the Entity::dropItem(ItemConvertible, int) call in Entity::dropItem(ItemConvertible) to remove diamonds instead of dropping them by replacing them with air:

  1. @Mixin(Entity.class)
  2. abstract class EntityMixin {
  3. @Redirect(method = "dropItem",
  4. at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;dropItem(Lnet/minecraft/item/ItemConvertible;I)Lnet/minecraft/entity/ItemEntity;"))
  5. private ItemEntity replaceDroppedItem(Entity droppingEntity, ItemConvertible item, int yOffset) {
  6. return droppingEntity.dropItem(item == Items.DIAMOND ? Items.AIR : item, yOffset);
  7. }
  8. }

INVOKE_STRING

The INVOKE_STRING injection point reference is used for matching invocations of target in method if target is a method with a single String parameter and a String literal is passed to it. The String literal to capture should be specified in the args property of At.

redirecting the Profiler::push invocation with “tick” passed to it in MinecraftClient::render in order to modify the location passed in the said invocation:

  1. @Mixin(MinecraftClient.class)
  2. abstract class MinecraftClientMixin {
  3. @Redirect(method = "render",
  4. at = @At(value = "INVOKE_STRING",
  5. target = "Lnet/minecraft/util/profiler/Profiler;push(Ljava/lang/String;)V",
  6. args = "ldc=tick"))
  7. private void redirectPush(Profiler profiler, String location) {
  8. profiler.push("modified tick");
  9. System.out.println(location);
  10. }
  11. }