User Tools

Site Tools


tutorial:mixin_injects

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:mixin_injects [2025/12/08 00:59] – Add disclaimer on Local capture becoming significantly less brittle on unobfuscated targets such as 26.1+ MC code gauntreclusetutorial:mixin_injects [2025/12/15 16:52] (current) – Added an extra bracket to show that it has to be inside @At more clearly mcgambingpro
Line 34: Line 34:
     /*...*/     /*...*/
     float force = complexCalculation(quantity);     float force = complexCalculation(quantity);
-    haykam.slap(quantity, force);+    haykam.slap(quantity, force / haykam.resistance);
          
     /*...*/     /*...*/
 } }
 </code> </code>
-if we wished to inject right after the ''slap'' call, we could use a [[#shifting|Shift]] to inject after the call with ''@Inject''. However, if we needed to also get the ''force'' variable, it would be instead preferable to use a ''@WrapOperation'' to get the ''force'' parameter without needing to use ''@Local'' or recalculating it.\\+if we wished to inject right after the ''slap'' call, we could use a [[#shifting|Shift]] to inject after the call with ''@Inject''. However, if we needed to also get the ''force / haykam.resistance'' value, it would be instead preferable to use a ''@WrapOperation'' to get it without needing to use ''@Local'' on ''force'' to then recalculate it.\\
 For the sake of showing how to appropriately add our new operations after the original method call outside of void returns, we'll say ''slap'' has a ''boolean'' return value. For the sake of showing how to appropriately add our new operations after the original method call outside of void returns, we'll say ''slap'' has a ''boolean'' return value.
 <code java> <code java>
 @WrapOperation(method = "calculateSlap", at = @At(value = "INVOKE", target = "Lnet/fabricmc/haykam_slapper/people/Person;slap(IF)Z;")) @WrapOperation(method = "calculateSlap", at = @At(value = "INVOKE", target = "Lnet/fabricmc/haykam_slapper/people/Person;slap(IF)Z;"))
-private boolean onHaykamSlapped(Person instance, int quantity, float force, Operation<Boolean> original) { +private boolean onHaykamSlapped(Person instance, int quantity, float forceOverResistance, Operation<Boolean> original) { 
-    boolean originalValue = original.call(instance, quantity, force); +    boolean originalValue = original.call(instance, quantity, forceOverResistance); 
-    newOperations(force, instance);+    newOperations(forceOverResistance, instance);
     return originalValue;     return originalValue;
 } }
 </code> </code>
-This allows us to get the ''force'' variable without needing to separately capture it, which may be especially brittle if there are other ''float'' variables earlier in the method body.+This allows us to get the returned value of the ''force / haykam.resistance'' expression without needing to separately recalculate it via capturing the ''force'' variable, which may be especially brittle if there are other ''float'' variables earlier in the method body.
  
-:!: Note that local capture is particularly brittle only in the context of obfuscated code, mainly Minecraft code. However unofbuscated code such as other mods or as Minecraft will be starting on version 26.1, where obfuscation will be removed from the game. This is because unobfuscated code allows to target and capture locals by name rather than needing to only rely on type, ordinal or index which can often be brittle.+:!: Note that ''@Local'' capture is particularly brittle specifically in the context of obfuscated code, mainly Minecraft code. However unofbuscated code such as other mods or as Minecraft will be starting on version 26.1, where obfuscation will be removed from the game does not imply the same level of brittleness for local capture. This is because unobfuscated code will allow to target and capture locals by name rather than needing to only rely on type, ordinal or index which can often be brittle.
  
 In summary of this subsection, ''@Inject'' should be avoided when it may lack in the context it can naturally about variables or operations it is injecting relative to, or when the intention is to modify individual operations within a method, as there are more precise and expressive injectors for those situations. In summary of this subsection, ''@Inject'' should be avoided when it may lack in the context it can naturally about variables or operations it is injecting relative to, or when the intention is to modify individual operations within a method, as there are more precise and expressive injectors for those situations.
Line 134: Line 134:
 The only common form of shifting comes in the form of using ''shift = At.Shift.AFTER'' within the ''@At''. This shifts the injection point to after the target. The syntax for shifting looks as follows: The only common form of shifting comes in the form of using ''shift = At.Shift.AFTER'' within the ''@At''. This shifts the injection point to after the target. The syntax for shifting looks as follows:
 <code java> <code java>
-@Inject(method = "...", at = @At(value = "...", target = "...", shift = At.Shift.AFTER)+@Inject(method = "...", at = @At(value = "...", target = "...", shift = At.Shift.AFTER))
 </code> </code>
tutorial/mixin_injects.1765155561.txt.gz · Last modified: 2025/12/08 00:59 by gauntrecluse