| 1 | package net.minecraft.world;
|
| 2 |
|
| 3 | import net.minecraft.world.item.ItemStack;
|
| 4 | import org.jspecify.annotations.Nullable;
|
| 5 |
|
| 6 | public sealed interface InteractionResult
|
| 7 | permits InteractionResult.Success,
|
| 8 | InteractionResult.Fail,
|
| 9 | InteractionResult.Pass,
|
| 10 | InteractionResult.TryEmptyHandInteraction {
|
| 11 | InteractionResult.Success SUCCESS = new InteractionResult.Success(InteractionResult.SwingSource.PREDICTED, InteractionResult.ItemContext.DEFAULT);
|
| 12 | InteractionResult.Success SUCCESS_SERVER = new InteractionResult.Success(InteractionResult.SwingSource.SERVER_ONLY, InteractionResult.ItemContext.DEFAULT);
|
| 13 | InteractionResult.Success CONSUME = new InteractionResult.Success(InteractionResult.SwingSource.NONE, InteractionResult.ItemContext.DEFAULT);
|
| 14 | InteractionResult.Fail FAIL = new InteractionResult.Fail();
|
| 15 | InteractionResult.Pass PASS = new InteractionResult.Pass();
|
| 16 | InteractionResult.TryEmptyHandInteraction TRY_WITH_EMPTY_HAND = new InteractionResult.TryEmptyHandInteraction();
|
| 17 |
|
| 18 | default boolean consumesAction() {
|
| 19 | return false;
|
| 20 | }
|
| 21 |
|
| 22 | record Fail() implements InteractionResult {
|
| 23 | }
|
| 24 |
|
| 25 | record ItemContext(boolean wasItemInteraction, @Nullable ItemStack heldItemTransformedTo) {
|
| 26 | public static final InteractionResult.ItemContext NONE = new InteractionResult.ItemContext(false, null);
|
| 27 | public static final InteractionResult.ItemContext DEFAULT = new InteractionResult.ItemContext(true, null);
|
| 28 | }
|
| 29 |
|
| 30 | record Pass() implements InteractionResult {
|
| 31 | }
|
| 32 |
|
| 33 | record Success(InteractionResult.SwingSource swingSource, InteractionResult.ItemContext itemContext) implements InteractionResult {
|
| 34 | public boolean shouldSwing() {
|
| 35 | return this.swingSource != InteractionResult.SwingSource.NONE;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public boolean consumesAction() {
|
| 40 | return true;
|
| 41 | }
|
| 42 |
|
| 43 | public InteractionResult.Success heldItemTransformedTo(final ItemStack itemStack) {
|
| 44 | return new InteractionResult.Success(this.swingSource, new InteractionResult.ItemContext(true, itemStack));
|
| 45 | }
|
| 46 |
|
| 47 | public InteractionResult.Success withoutItem() {
|
| 48 | return new InteractionResult.Success(this.swingSource, InteractionResult.ItemContext.NONE);
|
| 49 | }
|
| 50 |
|
| 51 | public boolean wasItemInteraction() {
|
| 52 | return this.itemContext.wasItemInteraction;
|
| 53 | }
|
| 54 |
|
| 55 | public @Nullable ItemStack heldItemTransformedTo() {
|
| 56 | return this.itemContext.heldItemTransformedTo;
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | enum SwingSource {
|
| 61 | NONE,
|
| 62 | PREDICTED,
|
| 63 | SERVER_ONLY;
|
| 64 | }
|
| 65 |
|
| 66 | record TryEmptyHandInteraction() implements InteractionResult {
|
| 67 | }
|
| 68 | }
|
| 69 |
|