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 revisionBoth sides next revision
tutorial:mixin_injects [2020/02/07 01:39] – remove formatting in code block draylartutorial:mixin_injects [2020/02/07 01:45] – clean up example draylar
Line 23: Line 23:
 //@Inject// methods always have a void return type. The method name does not matter; using something that describes what the inject does is best. The target method's arguments are placed first in the method's header, followed by a ''CallbackInfo'' object. If the target method has a return type (T), ''CallbackInfoReturnable<T>'' is used instead of ''CallbackInfo''. //@Inject// methods always have a void return type. The method name does not matter; using something that describes what the inject does is best. The target method's arguments are placed first in the method's header, followed by a ''CallbackInfo'' object. If the target method has a return type (T), ''CallbackInfoReturnable<T>'' is used instead of ''CallbackInfo''.
  
-===== Annotation ===== +=== Returning & Cancelling from Inject === 
-===== Method Header ===== +To cancel or return early inside a method, use ''CallbackInfo#cancel'' or ''CallbackInfoReturnable<T>#setReturnValue(T)''. In both instances, ''cancellable' will have to be set to true in the inject annotation: 
-===== Method Body =====+<code java> 
 +@Inject(method "...", at @At("..."), cancellable true) 
 +</code> 
 ===== Practical Example ===== ===== Practical Example =====
-An example is as follows:+The following example injects a print statement at the top of ''TitleScreen#init''
 <code java [enable_line_numbers="false"]> <code java [enable_line_numbers="false"]>
 @Mixin(TitleScreen.class) @Mixin(TitleScreen.class)
Line 37: Line 41:
 } }
 </code> </code>
 +
 +For more information on this particular example, view its usage in the [[Fabric Example Mod repo]].
tutorial/mixin_injects.txt · Last modified: 2022/08/05 19:19 by clomclem