User Tools

Site Tools


tutorial:mixin_redirectors_methods

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tutorial:mixin_redirectors_methods [2020/08/17 00:58] – created user11681tutorial:mixin_redirectors_methods [2021/01/15 04:20] (current) – Fix instance method example - removed "static" modifier & rename class obw
Line 1: Line 1:
-====== redirecting methods ======+====== Redirecting methods ======
  
 Method redirectors can use the following [[https://github.com/SpongePowered/Mixin/wiki/Injection-Point-Reference|injection point references]]: Method redirectors can use the following [[https://github.com/SpongePowered/Mixin/wiki/Injection-Point-Reference|injection point references]]:
 +
   * ''INVOKE''; and   * ''INVOKE''; and
   * ''INVOKE_STRING''.   * ''INVOKE_STRING''.
Line 9: Line 10:
 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. 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 ====+==== Redirecting a static method ====
  
 Static method redirectors should have the same parameters as the ''target''. Static method redirectors should have the same parameters as the ''target''.
  
 redirecting the ''ItemStack::fromTag(ListTag)'' call in ''SimpleInventory::readTags'' to return ''null'': redirecting the ''ItemStack::fromTag(ListTag)'' call in ''SimpleInventory::readTags'' to return ''null'':
 +
 <code java [enable_line_numbers=true]> <code java [enable_line_numbers=true]>
-@Mixin(ItemStack.class) +@Mixin(SimpleInventory.class) 
-class ItemStackMixin {+abstract class SimpleInventoryMixin {
     @Redirect(method = "readTags",     @Redirect(method = "readTags",
               at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/item/ItemStack;fromTag(Lnet/minecraft/nbt/ListTag;)Lnet/minecraft/item/ItemStack;"))               at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/item/ItemStack;fromTag(Lnet/minecraft/nbt/ListTag;)Lnet/minecraft/item/ItemStack;"))
Line 25: Line 27:
 </code> </code>
  
-==== redirecting an instance method ====+==== 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 ''target''s are invoked. 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 ''target''s 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: redirecting the ''Entity::dropItem(ItemConvertible, int)'' call in ''Entity::dropItem(ItemConvertible)'' to remove diamonds instead of dropping them by replacing them with air:
 +
 <code java [enable_line_numbers=true]> <code java [enable_line_numbers=true]>
 @Mixin(Entity.class) @Mixin(Entity.class)
-class ItemStackMixin {+abstract class EntityMixin {
     @Redirect(method = "dropItem",     @Redirect(method = "dropItem",
               at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;dropItem(Lnet/minecraft/item/ItemConvertible;I)Lnet/minecraft/entity/ItemEntity;"))               at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;dropItem(Lnet/minecraft/item/ItemConvertible;I)Lnet/minecraft/entity/ItemEntity;"))
-    private static ItemEntity replaceDroppedItem(Entity droppingEntity, ItemConvertible item, int yOffset) {+    private ItemEntity replaceDroppedItem(Entity droppingEntity, ItemConvertible item, int yOffset) {
         return droppingEntity.dropItem(item == Items.DIAMOND ? Items.AIR : item, yOffset);         return droppingEntity.dropItem(item == Items.DIAMOND ? Items.AIR : item, yOffset);
     }     }
Line 46: Line 49:
  
 redirecting the ''Profiler::push'' invocation with ''"tick"'' passed to it in ''MinecraftClient::render'' in order to modify the ''location'' passed in the said invocation: redirecting the ''Profiler::push'' invocation with ''"tick"'' passed to it in ''MinecraftClient::render'' in order to modify the ''location'' passed in the said invocation:
 +
 <code java [enable_line_numbers=true]> <code java [enable_line_numbers=true]>
 @Mixin(MinecraftClient.class) @Mixin(MinecraftClient.class)
-class MinecraftClientMixin {+abstract class MinecraftClientMixin {
     @Redirect(method = "render",     @Redirect(method = "render",
               at = @At(value = "INVOKE_STRING",               at = @At(value = "INVOKE_STRING",
Line 59: Line 63:
 } }
 </code> </code>
 +
tutorial/mixin_redirectors_methods.1597625902.txt.gz · Last modified: 2020/08/17 00:58 by user11681