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/02/17 18:36] – Fix indentation jamieswhiteshirt | tutorial:events [2025/12/29 07:00] (current) – gauntrecluse | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Custom Events ====== | + | ~~REDIRECT> |
| - | 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 | + | |
| - | In this tutorial, we'll look at creating your own event which is triggered when sheep are sheared. The process | + | ====== Custom Events ====== |
| + | Fabric API provides a system that allows mods to react to events that occur in the game. Events | ||
| + | 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 17: | ||
| <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 37: | Line 39: | ||
| <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 72: | ||
| <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 89: | ||
| 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 103: | ||
| 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.1581964565.txt.gz · Last modified: 2020/02/17 18:36 by jamieswhiteshirt