tutorial:events
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:events [2019/07/03 00:31] – tell people to return PASS in listeners draylar | tutorial:events [2020/03/29 15:46] – event -> listener. Highly misleading name, the listener is not an event jamieswhiteshirt | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Custom Events ====== | ====== Custom Events ====== | ||
- | Events here, event there! Everyone | + | 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 all, meaning you'll have to use other methods like Mixins to add a hook. You can then choose to implement the hook as 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 | * creating the event callback interface | ||
- | * triggering the event from a mixin | + | * triggering the event from a Mixin |
* creating a test implementation | * creating a test implementation | ||
==== Creating a Callback Interface ==== | ==== Creating a Callback Interface ==== | ||
- | The callback interface | + | The callback interface |
+ | |||
+ | For our Event implementation we will choose | ||
You'll need to create an interface that has an '' | You'll need to create an interface that has an '' | ||
Line 18: | Line 20: | ||
Event< | Event< | ||
(listeners) -> (player, sheep) -> { | (listeners) -> (player, sheep) -> { | ||
- | for (SheepShearCallback | + | for (SheepShearCallback |
- | ActionResult result = event.interact(player, | + | ActionResult result = listener.interact(player, |
if(result != ActionResult.PASS) { | if(result != ActionResult.PASS) { | ||
return result; | return result; | ||
Line 28: | Line 30: | ||
}); | }); | ||
- | ActionResult interact(PlayerEntity player, SheepEntity sheep); | + | |
} | } | ||
</ | </ | ||
Line 37: | Line 39: | ||
for (SheepShearCallback event : listeners) { | for (SheepShearCallback event : listeners) { | ||
</ | </ | ||
- | We then call our method (in this case, '' | + | We then call our method (in this case, '' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
ActionResult result = event.interact(player, | ActionResult result = event.interact(player, | ||
Line 64: | Line 66: | ||
</ | </ | ||
- | ==== Triggering the event from a mixin ==== | + | ==== 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 '' | 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 '' | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" |
tutorial/events.txt · Last modified: 2021/05/28 00:31 by 127.0.0.1