Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require keeping scroll in your hand during teleport #26

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/main/java/net/blufenix/teleportationrunes/TeleportTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Collections;
Expand All @@ -27,6 +28,7 @@ public class TeleportTask extends BukkitRunnable {
private final Player player;
private final boolean canLeaveArea;
private final boolean requireSneak;
private final ItemStack requiredItem;
private Location sourceLoc;
private Waypoint destWaypoint;
private Location potentialTeleporterLoc;
Expand All @@ -41,14 +43,16 @@ public class TeleportTask extends BukkitRunnable {
this.potentialTeleporterLoc = potentialTeleporterLoc;
canLeaveArea = false;
requireSneak = false;
requiredItem = null;
}

TeleportTask(Player player, Signature waypointSignature, boolean requireSneak, Callback callback) {
TeleportTask(Player player, Signature waypointSignature, ItemStack requiredItem, boolean requireSneak, Callback callback) {
this.player = player;
this.callback = callback;
this.waypointSignature = waypointSignature;
this.canLeaveArea = true;
this.requireSneak = requireSneak;
this.requiredItem = requiredItem;
}

private void lateInit() {
Expand Down Expand Up @@ -136,6 +140,12 @@ public void run() {
return;
}

if (requiredItem != null && !requiredItemInHand()) {
player.sendMessage("You're no longer holding the scroll. Cancelling...");
onSuccessOrFail(false);
return;
}

if (elapsedTicks < countdownTicks) {
if (Config.particleAnimationEnabled) {
animation.update(elapsedTicks);
Expand Down Expand Up @@ -167,7 +177,11 @@ private boolean playerStillAtTeleporter() {
return player.getLocation().distance(sourceLoc) < 2.5;
}

private boolean requiredItemInHand() {
return player.getInventory().getItemInMainHand().equals(requiredItem) || player.getInventory().getItemInOffHand().equals(requiredItem);
}

public static abstract class Callback {
abstract void onFinished(boolean success);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,10 @@ private void handleScrollOfWarpAction(Player player, Location blockLocation) {
} else { // use scroll
if (sig != null) {
Log.d("starting teleport task...");
new TeleportTask(player, sig, true, new TeleportTask.Callback() {
new TeleportTask(player, sig, scrollStack, true, new TeleportTask.Callback() {
@Override
void onFinished(boolean success) {
if (success) {
// TODO prevent player from throwing scrolls on the ground
// ex: remove from inventory before teleport, and add back if failure
scrollStack.setAmount(scrollStack.getAmount() - 1);
}
}
Expand Down