Skip to content

Commit

Permalink
feat(api): Add method for controlling warp item consumption in telepo…
Browse files Browse the repository at this point in the history
…rt 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>
  • Loading branch information
edralzar and BlayTheNinth authored Nov 13, 2023
1 parent 62e2340 commit 1eca143
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }

}
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public static Either<List<Entity>, 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 1eca143

Please sign in to comment.