tutorial:events
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:events [2019/07/03 00:21] – added vid of sheep dropping wool draylar | tutorial:events [2021/05/28 00:31] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Custom Events ====== | ====== Custom Events ====== | ||
- | Events here, event there! Everyone and everything gets an event! | + | Fabric API provides |
- | + | ||
- | Events are a formalized hook you can use to respond | + | |
+ | 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 |
* 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 '' | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | public interface SheepShearCallback | + | public interface SheepShearCallback { |
- | { | + | |
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 35: | Line 37: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
(listeners) -> (player, sheep) -> { | (listeners) -> (player, sheep) -> { | ||
- | for (SheepShearCallback | + | for (SheepShearCallback |
</ | </ | ||
- | 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 = listener.interact(player, |
</ | </ | ||
If the listener says we have to cancel ('' | If the listener says we have to cancel ('' | ||
Line 52: | Line 54: | ||
</ | </ | ||
- | ==== Triggering the event from a mixin ==== | + | In the [[https:// |
+ | <code java [enable_line_numbers=" | ||
+ | /** | ||
+ | * 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 '' | 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=" | ||
@Mixin(SheepEntity.class) | @Mixin(SheepEntity.class) | ||
- | public class SheepShearMixin | + | public class SheepShearMixin { |
- | { | + | |
@Inject(at = @At(value = " | @Inject(at = @At(value = " | ||
private void onShear(final PlayerEntity player, final Hand hand, final CallbackInfoReturnable< | private void onShear(final PlayerEntity player, final Hand hand, final CallbackInfoReturnable< | ||
ActionResult result = SheepShearCallback.EVENT.invoker().interact(player, | ActionResult result = SheepShearCallback.EVENT.invoker().interact(player, | ||
+ | | ||
if(result == ActionResult.FAIL) { | if(result == ActionResult.FAIL) { | ||
info.cancel(); | info.cancel(); | ||
Line 72: | 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' | 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' | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | SheepShearCallback.EVENT.register((player, | + | SheepShearCallback.EVENT.register((player, |
- | { | + | |
sheep.setSheared(true); | sheep.setSheared(true); | ||
Line 84: | Line 98: | ||
}); | }); | ||
</ | </ | ||
+ | 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 '' | ||
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:// | {{https:// |
tutorial/events.txt · Last modified: 2021/05/28 00:31 by 127.0.0.1