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 [2020/02/17 18:36] – Fix indentation jamieswhiteshirt | tutorial:events [2020/06/09 23:16] – formatting changes draylar | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Custom Events ====== | ====== Custom Events ====== | ||
- | Fabric API provides | + | Fabric API provides |
- | + | ||
- | In this tutorial, we'll look at creating | + | |
+ | 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=" | <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 70: | Line 70: | ||
<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 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' | 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 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:// | {{https:// |
tutorial/events.txt · Last modified: 2021/05/28 00:31 by 127.0.0.1