User Tools

Site Tools


tutorial:events

This is an old revision of the document!


Custom Events

Events here, event there! Everyone and everything gets an event!

Events are a formalized hook you can use to respond to actions in the game. They serve as a way to prevent 20 mixins in the same spot (because everyone wants to hook after the same thing). In this tutorial, we'll look at creating your own event which is triggered when sheep are sheared. The process of creating an event is:

  • creating the event callback interface
  • triggering the event from a mixin
  • creating a test implementation

Creating a Callback Interface

The callback interface is the core of your event. It stores a list of all registered listeners (which are like responses to an event) and calls them all when the action occurs. Each listener can then say “cancel this,” “approve this,” or “I don't care, leave it to the next guy,” while doing whatever response they need to.

You'll need to create an interface that has an Event instance and method for response implementation. A basic setup for our sheep shear callback is:

  1. public interface SheepShearCallback
  2. {
  3. Event<SheepShearCallback> EVENT = EventFactory.createArrayBacked(SheepShearCallback.class,
  4. (listeners) -> (player, sheep) -> {
  5. for (SheepShearCallback event : listeners) {
  6. ActionResult result = event.interact(player, sheep);
  7. if(result != ActionResult.PASS) {
  8. return result;
  9. }
  10. }
  11.  
  12. return ActionResult.PASS;
  13. });
  14.  
  15. ActionResult interact(PlayerEntity player, SheepEntity sheep);
  16. }

Let's look at this more in depth. When the invoker is called, we iterate over all listeners:

(listeners) -> (player, sheep) -> {
    for (SheepShearCallback event : listeners) {

We then call our method (in this case, interact) on the listener to get it's response:

ActionResult result = event.interact(player, sheep);

If the listener says we have to cancel (ActionResult.FAIL) or fully finish (ActionResult.SUCCESS), the callback returns the result and finishes the loop. ActionResult.PASS moves on to the next listener, and in most cases should result in success if there are no more listeners registered:

// ....
    if(result != ActionResult.PASS) {
        return result;
    }
}
 
return ActionResult.PASS;

In the Fabric API, we add Javadoc comments to the top of callback classes to document what each ActionResult does. In our case, it might be:

/**
 * Callback for shearing a sheep.
 * Called before the sheep is sheared, items are dropped, and items are damaged.
 * Upon return:
 * - SUCCESS cancels further processing and continues with normal shearing behavior.
 * - PASS falls back to further processing and defaults to SUCCESS if no other listeners are available
 * - FAIL cancels further processing and does not shear the sheep.
/**

Triggering the event from a mixin

We now have the basic event skeleton, but we need to trigger it. Because we want to have the event called when a player attempts to shear a sheep, we call the event invoker in SheepEntity#interactMob when dropItems() is called (ie. sheep can be sheared and player is holding shears):

@Mixin(SheepEntity.class)
public class SheepShearMixin
{
    @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) {
        ActionResult result = SheepShearCallback.EVENT.invoker().interact(player, (SheepEntity) (Object) this);
        if(result == ActionResult.FAIL) {
            info.cancel();
        }
    }
}

In this simple mixin, we call the event invoker (SheepShearCallback.EVENT.invoker().[…]), which then calls all active listeners to see what it should do. It returns an ActionResult based on this, and if the result is FAIL, we don't shear the sheep, drop items, or damage the player's item (info.cancel();). Make sure to register your mixin in your mixins.json file!

Testing Event with a Listener

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:

SheepShearCallback.EVENT.register((player, sheep) ->
{
    sheep.setSheared(true);
 
    // create diamond item entity at sheep position
    ItemStack stack = new ItemStack(Items.DIAMOND);
    ItemEntity itemEntity = new ItemEntity(player.world, sheep.x, sheep.y, sheep.z, stack);
    player.world.spawnEntity(itemEntity);
 
    return ActionResult.FAIL;
});

Note that this event also sets the sheep to be sheared manually, as it is normally canceled if we return FAIL. If you don't need to cancel the event, make sure you return PASS so other listeners are allowed to operate as well. Failing to follow these “not spoken rules” may result in angry modders on your doorstep.

If you enter into your game and shear a sheep, a diamond should drop instead of wool.

tutorial/events.1562113905.txt.gz · Last modified: 2019/07/03 00:31 by draylar