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_redirectors_methods

Redirecting methods

Method redirectors can use the following injection point references:

  • INVOKE; and
  • INVOKE_STRING.

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. }
tutorial/mixin_redirectors_methods.txt · Last modified: 2021/01/15 04:20 by obw