User Tools

Site Tools


tutorial:events

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
Next revisionBoth sides next revision
tutorial:events [2020/02/17 18:36] – Fix indentation jamieswhiteshirttutorial:events [2020/06/09 23:16] – formatting changes draylar
Line 1: Line 1:
 ====== Custom Events ====== ====== Custom Events ======
-Fabric API provides an event system that allows mods to react to events in the game. The purpose of adding events is to add hooks that satisfy common use cases and/or providing enhanced compatibility and performance between mods that hook into the same areas of the code. The use of events often substitutes the use of mixins. Fabric API provides events for some use cases, but not allmeaning you'll have to use other methods like Mixins to add hook. You can then choose to implement the hook as an event. +Fabric API provides system that allows mods to react to events that occur in the game. Events are hooks that satisfy common use cases and/or provide enhanced compatibility and performance between mods that hook into the same areas of the code. The use of events often substitutes the use of mixins. Fabric API provides events for important areas in the Minecraft codebase that multiple modders may be interested in hooking into. Some areas do not have hooksso you can opt to use a mixinor create your own event.
- +
-In this tutorialwe'll look at creating your own event which is triggered when sheep are shearedThe process of creating an event is:+
  
 +In this tutorial, we'll look at creating an event that is triggered when sheep are sheared. The process of creating an event is:
   * creating the event callback interface   * creating the event callback interface
   * triggering the event from a Mixin   * triggering the event from a Mixin
Line 16: Line 15:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-public interface SheepShearCallback +public interface SheepShearCallback { 
-{+
     Event<SheepShearCallback> EVENT = EventFactory.createArrayBacked(SheepShearCallback.class,     Event<SheepShearCallback> EVENT = EventFactory.createArrayBacked(SheepShearCallback.class,
         (listeners) -> (player, sheep) -> {         (listeners) -> (player, sheep) -> {
-            for (SheepShearCallback event : listeners) { +            for (SheepShearCallback listener : listeners) { 
-                ActionResult result = event.interact(player, sheep);+                ActionResult result = listener.interact(player, sheep); 
 +                
                 if(result != ActionResult.PASS) {                 if(result != ActionResult.PASS) {
                     return result;                     return result;
Line 70: Line 70:
 <code java [enable_line_numbers="false"]> <code java [enable_line_numbers="false"]>
 @Mixin(SheepEntity.class) @Mixin(SheepEntity.class)
-public class SheepShearMixin +public class SheepShearMixin { 
-{+
     @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/passive/SheepEntity;dropItems()V"), method = "interactMob", cancellable = true)     @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/passive/SheepEntity;dropItems()V"), method = "interactMob", cancellable = true)
     private void onShear(final PlayerEntity player, final Hand hand, final CallbackInfoReturnable<Boolean> info) {     private void onShear(final PlayerEntity player, final Hand hand, final CallbackInfoReturnable<Boolean> info) {
         ActionResult result = SheepShearCallback.EVENT.invoker().interact(player, (SheepEntity) (Object) this);         ActionResult result = SheepShearCallback.EVENT.invoker().interact(player, (SheepEntity) (Object) this);
 +        
         if(result == ActionResult.FAIL) {         if(result == ActionResult.FAIL) {
             info.cancel();             info.cancel();
Line 86: Line 87:
 Now we need to test our event. You can register a listener in your initialization method (or other areas if you prefer) and add custom logic there. Here's an example that drops a diamond instead of wool at the sheep's feet: Now we need to test our event. You can register a listener in your initialization method (or other areas if you prefer) and add custom logic there. Here's an example that drops a diamond instead of wool at the sheep's feet:
 <code java [enable_line_numbers="false"]> <code java [enable_line_numbers="false"]>
-SheepShearCallback.EVENT.register((player, sheep) -> +SheepShearCallback.EVENT.register((player, sheep) -> {
-{+
     sheep.setSheared(true);     sheep.setSheared(true);
  
Line 101: Line 101:
  
 If you enter into your game and shear a sheep, a diamond should drop instead of wool. If you enter into your game and shear a sheep, a diamond should drop instead of wool.
 +
 {{https://i.imgur.com/dG73Z6G.mp4?400|}} {{https://i.imgur.com/dG73Z6G.mp4?400|}}
tutorial/events.txt · Last modified: 2021/05/28 00:31 by 127.0.0.1