tutorial:events
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:events [2020/03/29 15:46] – event -> listener. Highly misleading name, the listener is not an event jamieswhiteshirt | tutorial:events [2021/05/28 00:31] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
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 listener : listeners) { | for (SheepShearCallback listener : listeners) { | ||
ActionResult result = listener.interact(player, | ActionResult result = listener.interact(player, | ||
+ | | ||
if(result != ActionResult.PASS) { | if(result != ActionResult.PASS) { | ||
return result; | return result; | ||
Line 37: | 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 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.1585496782.txt.gz · Last modified: 2020/03/29 15:46 by jamieswhiteshirt