From 1eca14340ca4b2a00e93b143451b565f0a727c8c Mon Sep 17 00:00:00 2001 From: Simon B Date: Mon, 13 Nov 2023 21:50:35 +0100 Subject: [PATCH] feat(api): Add method for controlling warp item consumption in teleport context (#747) * Prefer using tp context to decide consumesItem / allowTeleport This commit further prepares the phasing out of WarpMode by transferring some responsibilities to IWaystoneTeleportContext: - getConsumesPredicate to decide if a warp item ItemStack should be consumed - getAllowTeleportPredicate to decide if the teleport event is possible at all The PlayerWaystoneManager now uses these IWaystoneTeleportContext methods rather than WarpMode equivalents. Note that for backwards compatibility there is a default implementation which simply delegates to the WarpMode methods. * review: remove tpPredicate, simplify to isWarpItemConsumed * rename to match other methods in teleport context, add comment --------- Co-authored-by: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> --- .../waystones/api/IWaystoneTeleportContext.java | 7 +++++++ .../mods/waystones/core/PlayerWaystoneManager.java | 2 +- .../waystones/core/WaystoneTeleportContext.java | 13 ++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/shared/src/main/java/net/blay09/mods/waystones/api/IWaystoneTeleportContext.java b/shared/src/main/java/net/blay09/mods/waystones/api/IWaystoneTeleportContext.java index 7f020654..27d7bea5 100644 --- a/shared/src/main/java/net/blay09/mods/waystones/api/IWaystoneTeleportContext.java +++ b/shared/src/main/java/net/blay09/mods/waystones/api/IWaystoneTeleportContext.java @@ -52,4 +52,11 @@ public interface IWaystoneTeleportContext { boolean playsEffect(); void setPlaysEffect(boolean playsEffect); + + default boolean consumesWarpItem() { + return getWarpMode() != null && getWarpMode().consumesItem(); + }; + + default void setConsumesWarpItem(boolean consumesWarpItem) { } + } diff --git a/shared/src/main/java/net/blay09/mods/waystones/core/PlayerWaystoneManager.java b/shared/src/main/java/net/blay09/mods/waystones/core/PlayerWaystoneManager.java index e20c74c1..6190a2ba 100644 --- a/shared/src/main/java/net/blay09/mods/waystones/core/PlayerWaystoneManager.java +++ b/shared/src/main/java/net/blay09/mods/waystones/core/PlayerWaystoneManager.java @@ -258,7 +258,7 @@ public static Either, WaystoneTeleportError> tryTeleport(IWaystoneT } boolean isCreativeMode = entity instanceof Player && ((Player) entity).getAbilities().instabuild; - if (!context.getWarpItem().isEmpty() && event.getConsumeItemResult().withDefault(() -> warpMode.consumesItem() && !isCreativeMode)) { + if (!context.getWarpItem().isEmpty() && event.getConsumeItemResult().withDefault(() -> !isCreativeMode && context.consumesWarpItem())) { context.getWarpItem().shrink(1); } diff --git a/shared/src/main/java/net/blay09/mods/waystones/core/WaystoneTeleportContext.java b/shared/src/main/java/net/blay09/mods/waystones/core/WaystoneTeleportContext.java index 9a03bc6f..f195c370 100644 --- a/shared/src/main/java/net/blay09/mods/waystones/core/WaystoneTeleportContext.java +++ b/shared/src/main/java/net/blay09/mods/waystones/core/WaystoneTeleportContext.java @@ -3,7 +3,6 @@ import net.blay09.mods.waystones.api.IWaystone; import net.blay09.mods.waystones.api.IWaystoneTeleportContext; import net.blay09.mods.waystones.api.TeleportDestination; -import net.minecraft.server.level.ServerLevel; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Mob; import net.minecraft.world.item.ItemStack; @@ -24,6 +23,8 @@ public class WaystoneTeleportContext implements IWaystoneTeleportContext { private WarpMode warpMode = WarpMode.CUSTOM; private ItemStack warpItem = ItemStack.EMPTY; + @Nullable + private Boolean consumesWarpItem; // nullable for now so we can fallback to legacy warp mode implementation private int xpCost; private int cooldown; private boolean playsSound = true; @@ -145,4 +146,14 @@ public boolean playsEffect() { public void setPlaysEffect(boolean playsEffect) { this.playsEffect = playsEffect; } + + @Override + public boolean consumesWarpItem() { + return this.consumesWarpItem == null ? getWarpMode().consumesItem() : this.consumesWarpItem; + } + + @Override + public void setConsumesWarpItem(boolean consumesWarpItem) { + this.consumesWarpItem = consumesWarpItem; + } }