From 0e1cb48d07bf2faacb41c6d6fd5276274ff97344 Mon Sep 17 00:00:00 2001 From: JRoy Date: Sat, 28 Mar 2020 15:33:47 -0400 Subject: [PATCH 01/36] Reduce sync teleporting loads --- .../src/com/earth2me/essentials/Teleport.java | 42 ++++++++++++------- .../essentials/utils/LocationUtil.java | 37 +++++++++++----- 2 files changed, 52 insertions(+), 27 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java index e5bd413053d..8d7b4b9c99b 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -121,28 +121,38 @@ public void now(Player entity, boolean cooldown, TeleportCause cause) throws Exc protected void now(IUser teleportee, ITarget target, TeleportCause cause) throws Exception { cancel(false); teleportee.setLastLocation(); - Location loc = target.getLocation(); - if (LocationUtil.isBlockUnsafeForUser(teleportee, loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) { - if (ess.getSettings().isTeleportSafetyEnabled()) { - if (ess.getSettings().isForceDisableTeleportSafety()) { - PaperLib.teleportAsync(teleportee.getBase(), loc, cause); + PaperLib.getChunkAtAsync(target.getLocation()).thenAccept(chunk -> { + Location loc = target.getLocation(); + if (LocationUtil.isBlockUnsafeForUser(teleportee, chunk, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) { + if (ess.getSettings().isTeleportSafetyEnabled()) { + if (ess.getSettings().isForceDisableTeleportSafety()) { + PaperLib.teleportAsync(teleportee.getBase(), loc, cause); + } else { + try { + PaperLib.teleportAsync(teleportee.getBase(), LocationUtil.getSafeDestination(ess, teleportee, loc), cause); + } catch (Exception e) { + e.printStackTrace(); + } + } } else { - PaperLib.teleportAsync(teleportee.getBase(), LocationUtil.getSafeDestination(ess, teleportee, loc), cause); + try { + throw new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + } catch (Exception e) { + e.printStackTrace(); + } } } else { - throw new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - } - } else { - if (ess.getSettings().isForceDisableTeleportSafety()) { - PaperLib.teleportAsync(teleportee.getBase(), loc, cause); - } else { - if (ess.getSettings().isTeleportToCenterLocation()) { - loc = LocationUtil.getRoundedDestination(loc); + if (ess.getSettings().isForceDisableTeleportSafety()) { + PaperLib.teleportAsync(teleportee.getBase(), loc, cause); + } else { + if (ess.getSettings().isTeleportToCenterLocation()) { + loc = LocationUtil.getRoundedDestination(loc); + } + PaperLib.teleportAsync(teleportee.getBase(), loc, cause); } - PaperLib.teleportAsync(teleportee.getBase(), loc, cause); } - } + }); } //The teleportPlayer function is used when you want to normally teleportPlayer someone to a location or player. diff --git a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java index 2683e1e7d7f..1fd32f8fa26 100644 --- a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java @@ -2,10 +2,7 @@ import com.earth2me.essentials.IEssentials; import net.ess3.api.IUser; -import org.bukkit.GameMode; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.entity.LivingEntity; import org.bukkit.inventory.ItemStack; @@ -89,18 +86,28 @@ public static Location getTarget(final LivingEntity entity) throws Exception { } public static boolean isBlockAboveAir(final World world, final int x, final int y, final int z) { - return y > world.getMaxHeight() || HOLLOW_MATERIALS.contains(world.getBlockAt(x, y - 1, z).getType()); + return isBlockAboveAir(world.getChunkAt(x, z), x, y, z); + } + + public static boolean isBlockAboveAir(final Chunk chunk, int x, final int y, int z) { + x = x & 0xF; + z = z & 0xF; + return y > chunk.getWorld().getMaxHeight() || HOLLOW_MATERIALS.contains(chunk.getBlock(x, y - 1, z).getType()); } public static boolean isBlockUnsafeForUser(final IUser user, final World world, final int x, final int y, final int z) { - if (user.getBase().isOnline() && world.equals(user.getBase().getWorld()) && (user.getBase().getGameMode() == GameMode.CREATIVE || user.getBase().getGameMode() == GameMode.SPECTATOR || user.isGodModeEnabled()) && user.getBase().getAllowFlight()) { + return isBlockUnsafeForUser(user, world.getChunkAt(x, z), x, y, z); + } + + public static boolean isBlockUnsafeForUser(final IUser user, final Chunk chunk, final int x, final int y, final int z) { + if (user.getBase().isOnline() && chunk.getWorld().equals(user.getBase().getWorld()) && (user.getBase().getGameMode() == GameMode.CREATIVE || user.getBase().getGameMode() == GameMode.SPECTATOR || user.isGodModeEnabled()) && user.getBase().getAllowFlight()) { return false; } - if (isBlockDamaging(world, x, y, z)) { + if (isBlockDamaging(chunk, x, y, z)) { return true; } - return isBlockAboveAir(world, x, y, z); + return isBlockAboveAir(chunk, x, y, z); } public static boolean isBlockUnsafe(final World world, final int x, final int y, final int z) { @@ -108,7 +115,13 @@ public static boolean isBlockUnsafe(final World world, final int x, final int y, } public static boolean isBlockDamaging(final World world, final int x, final int y, final int z) { - final Block below = world.getBlockAt(x, y - 1, z); + return isBlockDamaging(world.getChunkAt(x, z), x, y, z); + } + + public static boolean isBlockDamaging(final Chunk chunk, int x, final int y, int z) { + x = x & 0xF; + z = z & 0xF; + final Block below = chunk.getBlock(x, y - 1, z); switch (below.getType()) { case LAVA: @@ -128,11 +141,13 @@ public static boolean isBlockDamaging(final World world, final int x, final int Material PORTAL = EnumUtil.getMaterial("NETHER_PORTAL", "PORTAL"); - if (world.getBlockAt(x, y, z).getType() == PORTAL) { + Block block = chunk.getBlock(x, y, z); + + if (block.getType() == PORTAL) { return true; } - return (!HOLLOW_MATERIALS.contains(world.getBlockAt(x, y, z).getType())) || (!HOLLOW_MATERIALS.contains(world.getBlockAt(x, y + 1, z).getType())); + return (!HOLLOW_MATERIALS.contains(block.getType())) || (!HOLLOW_MATERIALS.contains(chunk.getBlock(x, y + 1, z).getType())); } // Not needed if using getSafeDestination(loc) From 61830099b4c9ea2d32fe2b8433b428d41078c2af Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 30 Mar 2020 21:14:34 -0400 Subject: [PATCH 02/36] Avoid argument re-assigning --- .../earth2me/essentials/utils/LocationUtil.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java index 1fd32f8fa26..ab43e9ab3b7 100644 --- a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java @@ -89,10 +89,8 @@ public static boolean isBlockAboveAir(final World world, final int x, final int return isBlockAboveAir(world.getChunkAt(x, z), x, y, z); } - public static boolean isBlockAboveAir(final Chunk chunk, int x, final int y, int z) { - x = x & 0xF; - z = z & 0xF; - return y > chunk.getWorld().getMaxHeight() || HOLLOW_MATERIALS.contains(chunk.getBlock(x, y - 1, z).getType()); + public static boolean isBlockAboveAir(final Chunk chunk, final int x, final int y, int z) { + return y > chunk.getWorld().getMaxHeight() || HOLLOW_MATERIALS.contains(chunk.getBlock(x & 0xF, y - 1, z & 0xF).getType()); } public static boolean isBlockUnsafeForUser(final IUser user, final World world, final int x, final int y, final int z) { @@ -118,10 +116,8 @@ public static boolean isBlockDamaging(final World world, final int x, final int return isBlockDamaging(world.getChunkAt(x, z), x, y, z); } - public static boolean isBlockDamaging(final Chunk chunk, int x, final int y, int z) { - x = x & 0xF; - z = z & 0xF; - final Block below = chunk.getBlock(x, y - 1, z); + public static boolean isBlockDamaging(final Chunk chunk, final int x, final int y, final int z) { + final Block below = chunk.getBlock(x & 0xF, y - 1, z & 0xF); switch (below.getType()) { case LAVA: @@ -141,13 +137,13 @@ public static boolean isBlockDamaging(final Chunk chunk, int x, final int y, int Material PORTAL = EnumUtil.getMaterial("NETHER_PORTAL", "PORTAL"); - Block block = chunk.getBlock(x, y, z); + Block block = chunk.getBlock(x & 0xF, y, z & 0xF); if (block.getType() == PORTAL) { return true; } - return (!HOLLOW_MATERIALS.contains(block.getType())) || (!HOLLOW_MATERIALS.contains(chunk.getBlock(x, y + 1, z).getType())); + return (!HOLLOW_MATERIALS.contains(block.getType())) || (!HOLLOW_MATERIALS.contains(chunk.getBlock(x & 0xF, y + 1, z & 0xF).getType())); } // Not needed if using getSafeDestination(loc) From af869f6a9d25d58ce7bb22555c5df7b8c5323875 Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 30 Mar 2020 21:39:19 -0400 Subject: [PATCH 03/36] Remove async teleports when unnecessary --- Essentials/src/com/earth2me/essentials/Teleport.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java index 8d7b4b9c99b..9a90f99a735 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -127,9 +127,11 @@ protected void now(IUser teleportee, ITarget target, TeleportCause cause) throws if (LocationUtil.isBlockUnsafeForUser(teleportee, chunk, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) { if (ess.getSettings().isTeleportSafetyEnabled()) { if (ess.getSettings().isForceDisableTeleportSafety()) { - PaperLib.teleportAsync(teleportee.getBase(), loc, cause); + //The chunk we're teleporting to is 100% going to be loaded here, no need to teleport async. + teleportee.getBase().teleport(loc, cause); } else { try { + //There's a chance the safer location is outside the loaded chunk so still teleport async here. PaperLib.teleportAsync(teleportee.getBase(), LocationUtil.getSafeDestination(ess, teleportee, loc), cause); } catch (Exception e) { e.printStackTrace(); @@ -144,11 +146,13 @@ protected void now(IUser teleportee, ITarget target, TeleportCause cause) throws } } else { if (ess.getSettings().isForceDisableTeleportSafety()) { - PaperLib.teleportAsync(teleportee.getBase(), loc, cause); + //The chunk we're teleporting to is 100% going to be loaded here, no need to teleport async. + teleportee.getBase().teleport(loc, cause); } else { if (ess.getSettings().isTeleportToCenterLocation()) { loc = LocationUtil.getRoundedDestination(loc); } + //There's a *small* chance the rounded destination produces a location outside the loaded chunk so still teleport async here. PaperLib.teleportAsync(teleportee.getBase(), loc, cause); } } From 709bb830ef2810f18f5c11ce21562d095df6bfe6 Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 30 Mar 2020 22:50:29 -0400 Subject: [PATCH 04/36] Make exceptions cleaner --- Essentials/src/com/earth2me/essentials/Teleport.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java index 9a90f99a735..6a588a5ffd6 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -118,7 +118,7 @@ public void now(Player entity, boolean cooldown, TeleportCause cause) throws Exc teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); } - protected void now(IUser teleportee, ITarget target, TeleportCause cause) throws Exception { + protected void now(IUser teleportee, ITarget target, TeleportCause cause) { cancel(false); teleportee.setLastLocation(); @@ -138,11 +138,7 @@ protected void now(IUser teleportee, ITarget target, TeleportCause cause) throws } } } else { - try { - throw new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - } catch (Exception e) { - e.printStackTrace(); - } + new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())).printStackTrace(); } } else { if (ess.getSettings().isForceDisableTeleportSafety()) { From 7b58655d5650f8fb6b75037d2832cd529227f7fa Mon Sep 17 00:00:00 2001 From: JRoy Date: Fri, 3 Apr 2020 20:39:34 -0400 Subject: [PATCH 05/36] Async all the things Made an async version of every method with fallbacks for deprecated methods. --- .../src/com/earth2me/essentials/Teleport.java | 318 ++++++++++++++---- .../earth2me/essentials/TimedTeleport.java | 7 +- .../src/com/earth2me/essentials/Trade.java | 51 ++- .../earth2me/essentials/api/ITeleport.java | 179 +++++++++- .../essentials/commands/Commandback.java | 3 +- 5 files changed, 489 insertions(+), 69 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java index 276650c23ea..549bf92cab0 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -1,13 +1,15 @@ package com.earth2me.essentials; +import com.earth2me.essentials.commands.WarpNotFoundException; import com.earth2me.essentials.utils.DateUtil; import com.earth2me.essentials.utils.LocationUtil; import io.papermc.lib.PaperLib; import net.ess3.api.IEssentials; import net.ess3.api.ITeleport; import net.ess3.api.IUser; -import net.ess3.api.events.UserWarpEvent; +import net.ess3.api.InvalidWorldException; import net.ess3.api.events.UserTeleportEvent; +import net.ess3.api.events.UserWarpEvent; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Entity; @@ -18,6 +20,7 @@ import java.math.BigDecimal; import java.util.Calendar; import java.util.GregorianCalendar; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -42,6 +45,13 @@ public enum TeleportType { } public void cooldown(boolean check) throws Exception { + CompletableFuture exceptionFuture = new CompletableFuture<>(); + if (cooldown(check, exceptionFuture)) { + throw exceptionFuture.get(); + } + } + + public boolean cooldown(boolean check, CompletableFuture exceptionFuture) { final Calendar time = new GregorianCalendar(); if (teleportOwner.getLastTeleportTimestamp() > 0) { // Take the current time, and remove the delay from it. @@ -59,19 +69,21 @@ public void cooldown(boolean check) throws Exception { // This is to make sure time didn't get messed up on last teleportPlayer use. // If this happens, let's give the user the benifit of the doubt. teleportOwner.setLastTeleportTimestamp(time.getTimeInMillis()); - return; + return false; } else if (lastTime > earliestLong - && cooldownApplies()) { + && cooldownApplies()) { time.setTimeInMillis(lastTime); time.add(Calendar.SECOND, (int) cooldown); time.add(Calendar.MILLISECOND, (int) ((cooldown * 1000.0) % 1000.0)); - throw new Exception(tl("timeBeforeTeleport", DateUtil.formatDateDiff(time.getTimeInMillis()))); + exceptionFuture.complete(new Exception(tl("timeBeforeTeleport", DateUtil.formatDateDiff(time.getTimeInMillis())))); + return true; } } // if justCheck is set, don't update lastTeleport; we're just checking if (!check) { teleportOwner.setLastTeleportTimestamp(time.getTimeInMillis()); } + return false; } private boolean cooldownApplies() { @@ -83,11 +95,11 @@ private boolean cooldownApplies() { break; case BACK: applies = !(teleportOwner.isAuthorized(globalBypassPerm) && - teleportOwner.isAuthorized("essentials.teleport.cooldown.bypass.back")); + teleportOwner.isAuthorized("essentials.teleport.cooldown.bypass.back")); break; case TPA: applies = !(teleportOwner.isAuthorized(globalBypassPerm) && - teleportOwner.isAuthorized("essentials.teleport.cooldown.bypass.tpa")); + teleportOwner.isAuthorized("essentials.teleport.cooldown.bypass.tpa")); break; } return applies; @@ -102,36 +114,75 @@ private void warnUser(final IUser user, final double delay) { //The now function is used when you want to skip tp delay when teleporting someone to a location or player. @Override + @Deprecated public void now(Location loc, boolean cooldown, TeleportCause cause) throws Exception { - if (cooldown) { - cooldown(false); + CompletableFuture exceptionFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + now(loc, cooldown, cause, exceptionFuture, future); + if (!future.get()) { + throw exceptionFuture.get(); + } + } + + @Override + public void now(Location loc, boolean cooldown, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + if (cooldown && cooldown(false, exceptionFuture)) { + future.complete(false); + return; } final ITarget target = new LocationTarget(loc); - now(teleportOwner, target, cause); + nowAsync(teleportOwner, target, cause, exceptionFuture, future); } @Override + @Deprecated public void now(Player entity, boolean cooldown, TeleportCause cause) throws Exception { - if (cooldown) { - cooldown(false); + CompletableFuture exceptionFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + now(entity, cooldown, cause, exceptionFuture, future); + if (!future.get()) { + throw exceptionFuture.get(); + } + } + + @Override + public void now(Player entity, boolean cooldown, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + if (cooldown && cooldown(false, exceptionFuture)) { + future.complete(false); + return; } final ITarget target = new PlayerTarget(entity); - now(teleportOwner, target, cause); - teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); + nowAsync(teleportOwner, target, cause, exceptionFuture, future); + future.thenAccept(success -> { + if (success) { + teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); + } + }); + } + + protected void Gnow(IUser teleportee, ITarget target, TeleportCause cause) throws Exception { + CompletableFuture exceptionFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + nowAsync(teleportee, target, cause, exceptionFuture, future); + if (!future.get()) { + throw exceptionFuture.get(); + } } - protected void now(IUser teleportee, ITarget target, TeleportCause cause) { + protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { cancel(false); - + UserTeleportEvent event = new UserTeleportEvent(teleportee, cause, target.getLocation()); Bukkit.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { return; } - + if (!teleportee.getBase().getPassengers().isEmpty()) { if (!ess.getSettings().isTeleportPassengerDismount()) { - throw new Exception(tl("passengerTeleportFail")); + exceptionFuture.complete(new Exception(tl("passengerTeleportFail"))); + future.complete(false); + return; } for (Entity entity : teleportee.getBase().getPassengers()) { entity.leaveVehicle(); @@ -150,11 +201,15 @@ protected void now(IUser teleportee, ITarget target, TeleportCause cause) { //There's a chance the safer location is outside the loaded chunk so still teleport async here. PaperLib.teleportAsync(teleportee.getBase(), LocationUtil.getSafeDestination(ess, teleportee, loc), cause); } catch (Exception e) { - e.printStackTrace(); + exceptionFuture.complete(e); + future.complete(false); + return; } } } else { - new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())).printStackTrace(); + exceptionFuture.complete(new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()))); + future.complete(false); + return; } } else { if (ess.getSettings().isForceDisableTeleportSafety()) { @@ -168,6 +223,7 @@ protected void now(IUser teleportee, ITarget target, TeleportCause cause) { PaperLib.teleportAsync(teleportee.getBase(), loc, cause); } } + future.complete(true); }); } @@ -180,40 +236,91 @@ public void teleport(Location loc, Trade chargeFor) throws Exception { } @Override + @Deprecated public void teleport(Location loc, Trade chargeFor, TeleportCause cause) throws Exception { - teleport(teleportOwner, new LocationTarget(loc), chargeFor, cause); + CompletableFuture exceptionFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + teleport(loc, chargeFor, cause, exceptionFuture, future); + if (!future.get()) { + throw exceptionFuture.get(); + } + } + + @Override + public void teleport(Location loc, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + teleport(teleportOwner, new LocationTarget(loc), chargeFor, cause, exceptionFuture, future); } //This is used when teleporting to a player @Override + @Deprecated public void teleport(Player entity, Trade chargeFor, TeleportCause cause) throws Exception { - ITarget target = new PlayerTarget(entity); + CompletableFuture exceptionFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + teleport(entity, chargeFor, cause, exceptionFuture, future); + if (!future.get()) { + throw exceptionFuture.get(); + } + } + + @Override + public void teleport(Player entity, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { teleportOwner.sendMessage(tl("teleportToPlayer", entity.getDisplayName())); - teleport(teleportOwner, target, chargeFor, cause); + teleport(teleportOwner, new PlayerTarget(entity), chargeFor, cause, exceptionFuture, future); } //This is used when teleporting to stored location @Override + @Deprecated public void teleportPlayer(IUser teleportee, Location loc, Trade chargeFor, TeleportCause cause) throws Exception { - teleport(teleportee, new LocationTarget(loc), chargeFor, cause); + CompletableFuture exceptionFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + teleportPlayer(teleportee, loc, chargeFor, cause, exceptionFuture, future); + if (!future.get()) { + throw exceptionFuture.get(); + } + } + + @Override + public void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + teleport(otherUser, new LocationTarget(loc), chargeFor, cause, exceptionFuture, future); } //This is used on /tphere @Override + @Deprecated public void teleportPlayer(IUser teleportee, Player entity, Trade chargeFor, TeleportCause cause) throws Exception { + CompletableFuture exceptionFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + teleportPlayer(teleportee, entity, chargeFor, cause, exceptionFuture, future); + if (!future.get()) { + throw exceptionFuture.get(); + } + } + + @Override + public void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { ITarget target = new PlayerTarget(entity); - teleport(teleportee, target, chargeFor, cause); - teleportee.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); - teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); + teleport(otherUser, target, chargeFor, cause, exceptionFuture, future); + future.thenAccept(success -> { + if (success) { + otherUser.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); + teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); + } + }); } - private void teleport(IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause) throws Exception { + private void teleport(IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { double delay = ess.getSettings().getTeleportDelay(); Trade cashCharge = chargeFor; if (chargeFor != null) { - chargeFor.isAffordableFor(teleportOwner); + chargeFor.isAffordableFor(teleportOwner, exceptionFuture); + if (exceptionFuture.isDone()) { + future.complete(false); + return; + } //This code is to make sure that commandcosts are checked in the initial world, and not in the resulting world. if (!chargeFor.getCommandCost(teleportOwner).equals(BigDecimal.ZERO)) { @@ -222,12 +329,22 @@ private void teleport(IUser teleportee, ITarget target, Trade chargeFor, Telepor } } - cooldown(true); + if (cooldown(true, exceptionFuture)) { + future.complete(false); + return; + } if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") || teleportee.isAuthorized("essentials.teleport.timer.bypass")) { - cooldown(false); - now(teleportee, target, cause); + if (cooldown(false, exceptionFuture)) { + future.complete(false); + return; + } + nowAsync(teleportee, target, cause, exceptionFuture, future); if (cashCharge != null) { - cashCharge.charge(teleportOwner); + cashCharge.charge(teleportOwner, exceptionFuture); + if (exceptionFuture.isDone()) { + future.complete(false); + return; + } } return; } @@ -237,13 +354,17 @@ private void teleport(IUser teleportee, ITarget target, Trade chargeFor, Telepor initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false); } - private void teleportOther(IUser teleporter, IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause) throws Exception { + private void teleportOther(IUser teleporter, IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { double delay = ess.getSettings().getTeleportDelay(); Trade cashCharge = chargeFor; if (teleporter != null && chargeFor != null) { - chargeFor.isAffordableFor(teleporter); + chargeFor.isAffordableFor(teleporter, exceptionFuture); + if (exceptionFuture.isDone()) { + future.complete(false); + return; + } //This code is to make sure that commandcosts are checked in the initial world, and not in the resulting world. if (!chargeFor.getCommandCost(teleporter).equals(BigDecimal.ZERO)) { @@ -252,15 +373,26 @@ private void teleportOther(IUser teleporter, IUser teleportee, ITarget target, T } } - cooldown(true); + if (cooldown(true, exceptionFuture)) { + future.complete(false); + return; + } if (delay <= 0 || teleporter == null - || teleporter.isAuthorized("essentials.teleport.timer.bypass") - || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") - || teleportee.isAuthorized("essentials.teleport.timer.bypass")) { - cooldown(false); - now(teleportee, target, cause); + || teleporter.isAuthorized("essentials.teleport.timer.bypass") + || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") + || teleportee.isAuthorized("essentials.teleport.timer.bypass")) { + if (cooldown(false, exceptionFuture)) { + future.complete(false); + return; + } + + nowAsync(teleportee, target, cause, exceptionFuture, future); if (teleporter != null && cashCharge != null) { - cashCharge.charge(teleporter); + cashCharge.charge(teleporter, exceptionFuture); + if (exceptionFuture.isDone()) { + future.complete(false); + return; + } } return; } @@ -272,17 +404,38 @@ private void teleportOther(IUser teleporter, IUser teleportee, ITarget target, T //The respawn function is a wrapper used to handle tp fallback, on /jail and /home @Override + @Deprecated public void respawn(final Trade chargeFor, TeleportCause cause) throws Exception { + CompletableFuture exceptionFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + respawn(chargeFor, cause, exceptionFuture, future); + if (!future.get()) { + throw exceptionFuture.get(); + } + } + + @Override + public void respawn(Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { double delay = ess.getSettings().getTeleportDelay(); if (chargeFor != null) { - chargeFor.isAffordableFor(teleportOwner); + chargeFor.isAffordableFor(teleportOwner, exceptionFuture); + if (exceptionFuture.isDone()) { + future.complete(false); + return; + } + } + if (cooldown(true, exceptionFuture)) { + future.complete(false); + return; } - cooldown(true); if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass")) { - cooldown(false); - respawnNow(teleportOwner, cause); + if (cooldown(false, exceptionFuture)) { + future.complete(false); + return; + } + respawnNow(teleportOwner, cause, exceptionFuture, future); if (chargeFor != null) { - chargeFor.charge(teleportOwner); + chargeFor.charge(teleportOwner, exceptionFuture); } return; } @@ -292,58 +445,109 @@ public void respawn(final Trade chargeFor, TeleportCause cause) throws Exception initTimer((long) (delay * 1000.0), teleportOwner, null, chargeFor, cause, true); } - void respawnNow(IUser teleportee, TeleportCause cause) throws Exception { + void respawnNow(IUser teleportee, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { final Player player = teleportee.getBase(); Location bed = player.getBedSpawnLocation(); if (bed != null) { - now(teleportee, new LocationTarget(bed), cause); + nowAsync(teleportee, new LocationTarget(bed), cause, exceptionFuture, future); } else { if (ess.getSettings().isDebug()) { ess.getLogger().info("Could not find bed spawn, forcing respawn event."); } final PlayerRespawnEvent pre = new PlayerRespawnEvent(player, player.getWorld().getSpawnLocation(), false); ess.getServer().getPluginManager().callEvent(pre); - now(teleportee, new LocationTarget(pre.getRespawnLocation()), cause); + nowAsync(teleportee, new LocationTarget(pre.getRespawnLocation()), cause, exceptionFuture, future); } } //The warp function is a wrapper used to teleportPlayer a player to a /warp @Override + @Deprecated public void warp(IUser teleportee, String warp, Trade chargeFor, TeleportCause cause) throws Exception { - UserWarpEvent event = new UserWarpEvent(teleportee, warp, chargeFor); + CompletableFuture exceptionFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + warp(teleportee, warp, chargeFor, cause, exceptionFuture, future); + if (!future.get()) { + throw exceptionFuture.get(); + } + } + + @Override + public void warp(IUser otherUser, String warp, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + UserWarpEvent event = new UserWarpEvent(otherUser, warp, chargeFor); Bukkit.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { return; } warp = event.getWarp(); - Location loc = ess.getWarps().getWarp(warp); - teleportee.sendMessage(tl("warpingTo", warp, loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - if (!teleportee.equals(teleportOwner)) { + Location loc; + try { + loc = ess.getWarps().getWarp(warp); + } catch (WarpNotFoundException | InvalidWorldException e) { + exceptionFuture.complete(e); + future.complete(false); + return; + } + otherUser.sendMessage(tl("warpingTo", warp, loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + if (!otherUser.equals(teleportOwner)) { teleportOwner.sendMessage(tl("warpingTo", warp, loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); } - teleport(teleportee, new LocationTarget(loc), chargeFor, cause); + teleport(otherUser, new LocationTarget(loc), chargeFor, cause, exceptionFuture, future); } //The back function is a wrapper used to teleportPlayer a player /back to their previous location. @Override + @Deprecated public void back(Trade chargeFor) throws Exception { - back(teleportOwner, chargeFor); + CompletableFuture exceptionFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + back(chargeFor, exceptionFuture, future); + if (!future.get()) { + throw exceptionFuture.get(); + } + } + + @Override + public void back(Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future) { + back(teleportOwner, chargeFor, exceptionFuture, future); } //This function is a wrapper over the other back function for cases where another player performs back for them @Override + @Deprecated public void back(IUser teleporter, Trade chargeFor) throws Exception { + CompletableFuture exceptionFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + back(teleporter, chargeFor, exceptionFuture, future); + if (!future.get()) { + throw exceptionFuture.get(); + } + } + + @Override + public void back(IUser teleporter, Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future) { tpType = TeleportType.BACK; final Location loc = teleportOwner.getLastLocation(); teleportOwner.sendMessage(tl("backUsageMsg", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - teleportOther(teleporter, teleportOwner, new LocationTarget(loc), chargeFor, TeleportCause.COMMAND); + teleportOther(teleporter, teleportOwner, new LocationTarget(loc), chargeFor, TeleportCause.COMMAND, exceptionFuture, future); } //This function is used to throw a user back after a jail sentence @Override + @Deprecated public void back() throws Exception { - now(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()), TeleportCause.COMMAND); + CompletableFuture exceptionFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + back(exceptionFuture, future); + if (!future.get()) { + throw exceptionFuture.get(); + } + } + + @Override + public void back(CompletableFuture exceptionFuture, CompletableFuture future) { + nowAsync(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()), TeleportCause.COMMAND, exceptionFuture, future); } public void setTpType(TeleportType tpType) { diff --git a/Essentials/src/com/earth2me/essentials/TimedTeleport.java b/Essentials/src/com/earth2me/essentials/TimedTeleport.java index c3e959f5821..35a23ae5f8e 100644 --- a/Essentials/src/com/earth2me/essentials/TimedTeleport.java +++ b/Essentials/src/com/earth2me/essentials/TimedTeleport.java @@ -6,6 +6,7 @@ import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -99,13 +100,15 @@ public void run() { teleportUser.sendMessage(tl("teleportationCommencing")); try { + CompletableFuture future = new CompletableFuture<>(); + CompletableFuture future1 = new CompletableFuture<>(); if (timer_chargeFor != null) { timer_chargeFor.isAffordableFor(teleportOwner); } if (timer_respawn) { - teleport.respawnNow(teleportUser, timer_cause); + teleport.respawnNow(teleportUser, timer_cause, future, future1); } else { - teleport.now(teleportUser, timer_teleportTarget, timer_cause); + teleport.nowAsync(teleportUser, timer_teleportTarget, timer_cause, future, future1); } if (timer_chargeFor != null) { timer_chargeFor.charge(teleportOwner); diff --git a/Essentials/src/com/earth2me/essentials/Trade.java b/Essentials/src/com/earth2me/essentials/Trade.java index e003dccacf2..0395a10d973 100644 --- a/Essentials/src/com/earth2me/essentials/Trade.java +++ b/Essentials/src/com/earth2me/essentials/Trade.java @@ -18,6 +18,8 @@ import java.util.Date; import java.util.Locale; import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.logging.Level; import java.util.logging.Logger; @@ -81,26 +83,42 @@ private Trade(final String command, final Trade fallback, final BigDecimal money } public void isAffordableFor(final IUser user) throws ChargeException { + CompletableFuture exceptionFuture = new CompletableFuture<>(); + isAffordableFor(user, exceptionFuture); + if (exceptionFuture.isDone()) { + try { + Exception exception = exceptionFuture.get(); + throw (ChargeException) exception; + } catch (InterruptedException | ExecutionException e) { //This would rarely, if ever, happen. + e.printStackTrace(); + } + } + } + + public void isAffordableFor(final IUser user, CompletableFuture exceptionFuture) { if (ess.getSettings().isDebug()) { ess.getLogger().log(Level.INFO, "checking if " + user.getName() + " can afford charge."); } if (getMoney() != null && getMoney().signum() > 0 && !user.canAfford(getMoney())) { - throw new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(getMoney(), ess))); + exceptionFuture.complete(new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(getMoney(), ess)))); + return; } if (getItemStack() != null && !user.getBase().getInventory().containsAtLeast(itemStack, itemStack.getAmount())) { - throw new ChargeException(tl("missingItems", getItemStack().getAmount(), ess.getItemDb().name(getItemStack()))); + exceptionFuture.complete(new ChargeException(tl("missingItems", getItemStack().getAmount(), ess.getItemDb().name(getItemStack())))); + return; } BigDecimal money; if (command != null && !command.isEmpty() && (money = getCommandCost(user)).signum() > 0 && !user.canAfford(money)) { - throw new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(money, ess))); + exceptionFuture.complete(new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(money, ess)))); + return; } if (exp != null && exp > 0 && SetExpFix.getTotalExperience(user.getBase()) < exp) { - throw new ChargeException(tl("notEnoughExperience")); + exceptionFuture.complete(new ChargeException(tl("notEnoughExperience"))); } } @@ -175,6 +193,19 @@ public Map pay(final IUser user, final OverflowType type) th } public void charge(final IUser user) throws ChargeException { + CompletableFuture exceptionFuture = new CompletableFuture<>(); + charge(user, exceptionFuture); + if (exceptionFuture.isDone()) { + try { + Exception exception = exceptionFuture.get(); + throw (ChargeException) exception; + } catch (InterruptedException | ExecutionException e) { //This would rarely, if ever, happen. + e.printStackTrace(); + } + } + } + + public void charge(final IUser user, CompletableFuture exceptionFuture) { if (ess.getSettings().isDebug()) { ess.getLogger().log(Level.INFO, "attempting to charge user " + user.getName()); } @@ -183,7 +214,8 @@ public void charge(final IUser user) throws ChargeException { ess.getLogger().log(Level.INFO, "charging user " + user.getName() + " money " + getMoney().toPlainString()); } if (!user.canAfford(getMoney()) && getMoney().signum() > 0) { - throw new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(getMoney(), ess))); + exceptionFuture.complete(new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(getMoney(), ess)))); + return; } user.takeMoney(getMoney()); } @@ -192,7 +224,8 @@ public void charge(final IUser user) throws ChargeException { ess.getLogger().log(Level.INFO, "charging user " + user.getName() + " itemstack " + getItemStack().toString()); } if (!user.getBase().getInventory().containsAtLeast(getItemStack(), getItemStack().getAmount())) { - throw new ChargeException(tl("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " "))); + exceptionFuture.complete(new ChargeException(tl("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " ")))); + return; } user.getBase().getInventory().removeItem(getItemStack()); user.getBase().updateInventory(); @@ -200,7 +233,8 @@ public void charge(final IUser user) throws ChargeException { if (command != null) { final BigDecimal cost = getCommandCost(user); if (!user.canAfford(cost) && cost.signum() > 0) { - throw new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(cost, ess))); + exceptionFuture.complete(new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(cost, ess)))); + return; } user.takeMoney(cost); } @@ -210,7 +244,8 @@ public void charge(final IUser user) throws ChargeException { } final int experience = SetExpFix.getTotalExperience(user.getBase()); if (experience < getExperience() && getExperience() > 0) { - throw new ChargeException(tl("notEnoughExperience")); + exceptionFuture.complete(new ChargeException(tl("notEnoughExperience"))); + return; } SetExpFix.setTotalExperience(user.getBase(), experience - getExperience()); } diff --git a/Essentials/src/com/earth2me/essentials/api/ITeleport.java b/Essentials/src/com/earth2me/essentials/api/ITeleport.java index 1ebad87e506..27f8d85d09d 100644 --- a/Essentials/src/com/earth2me/essentials/api/ITeleport.java +++ b/Essentials/src/com/earth2me/essentials/api/ITeleport.java @@ -6,58 +6,126 @@ import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent; +import java.util.concurrent.CompletableFuture; + public interface ITeleport { /** * Used to skip teleportPlayer delay when teleporting someone to a location or player. * + * @deprecated Use {@link ITeleport#now(Location, boolean, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * * @param loc - Where should the player end up * @param cooldown - If cooldown should be enforced * @param cause - The reported teleportPlayer cause * * @throws Exception */ + @Deprecated void now(Location loc, boolean cooldown, PlayerTeleportEvent.TeleportCause cause) throws Exception; /** * Used to skip teleportPlayer delay when teleporting someone to a location or player. * + * @param loc - Where should the player end up + * @param cooldown - If cooldown should be enforced + * @param cause - The reported teleportPlayer cause + * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution + * @param future - Future which is completed with the success status of the execution + * + * @throws Exception + */ + void now(Location loc, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + + /** + * Used to skip teleportPlayer delay when teleporting someone to a location or player. + * + * @deprecated Use {@link ITeleport#now(Player, boolean, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * * @param entity - Where should the player end up * @param cooldown - If cooldown should be enforced * @param cause - The reported teleportPlayer cause * * @throws Exception */ + @Deprecated void now(Player entity, boolean cooldown, PlayerTeleportEvent.TeleportCause cause) throws Exception; + /** + * Used to skip teleportPlayer delay when teleporting someone to a location or player. + * + * @param entity - Where should the player end up + * @param cooldown - If cooldown should be enforced + * @param cause - The reported teleportPlayer cause + * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution + * @param future - Future which is completed with the success status of the execution + * + * @throws Exception + */ + void now(Player entity, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + @Deprecated void teleport(Location loc, Trade chargeFor) throws Exception; /** * Teleport a player to a specific location * + * @deprecated {@link ITeleport#teleport(Location, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * * @param loc - Where should the player end up * @param chargeFor - What the user will be charged if teleportPlayer is successful * @param cause - The reported teleportPlayer cause * * @throws Exception */ + @Deprecated void teleport(Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + /** + * Teleport a player to a specific location + * + * @param loc - Where should the player end up + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause. + * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution + * @param future - Future which is completed with the success status of the execution + * + * @throws Exception + */ + void teleport(Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + /** * Teleport a player to a specific player * + * @deprecated Use {@link ITeleport#teleport(Player, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * * @param entity - Where should the player end up * @param chargeFor - What the user will be charged if teleportPlayer is successful * @param cause - The reported teleportPlayer cause * * @throws Exception */ + @Deprecated void teleport(Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + /** + * Teleport a player to a specific player + * + * @param entity - Where should the player end up + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution + * @param future - Future which is completed with the success status of the execution + * + * @throws Exception + */ + void teleport(Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + /** * Teleport a player to a specific location * + * @deprecated Use {@link ITeleport#teleportPlayer(IUser, Location, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * * @param otherUser - Which user will be teleported * @param loc - Where should the player end up * @param chargeFor - What the user will be charged if teleportPlayer is successful @@ -65,11 +133,28 @@ public interface ITeleport { * * @throws Exception */ + @Deprecated void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + /** + * Teleport a player to a specific location + * + * @param otherUser - Which user will be teleported + * @param loc - Where should the player end up + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution + * @param future - Future which is completed with the success status of the execution + * + * @throws Exception + */ + void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + /** * Teleport a player to a specific player * + * @deprecated Use {@link ITeleport#teleportPlayer(IUser, Player, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * * @param otherUser - Which user will be teleported * @param entity - Where should the player end up * @param chargeFor - What the user will be charged if teleportPlayer is successful @@ -77,21 +162,53 @@ public interface ITeleport { * * @throws Exception */ + @Deprecated void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + /** + * Teleport a player to a specific player + * + * @param otherUser - Which user will be teleported + * @param entity - Where should the player end up + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution + * @param future - Future which is completed with the success status of the execution + * + * @throws Exception + */ + void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + /** * Teleport wrapper used to handle tp fallback on /jail and /home * + * @deprecated Use {@link ITeleport#respawn(Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * * @param chargeFor - What the user will be charged if teleportPlayer is successful * @param cause - The reported teleportPlayer cause * * @throws Exception */ + @Deprecated void respawn(final Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + /** + * Teleport wrapper used to handle tp fallback on /jail and /home + * + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution + * @param future - Future which is completed with the success status of the execution + * + * @throws Exception + */ + void respawn(final Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + /** * Teleport wrapper used to handle /warp teleports * + * @deprecated Use {@link ITeleport#warp(IUser, String, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * * @param otherUser - Which user will be teleported * @param warp - The name of the warp the user will be teleported too. * @param chargeFor - What the user will be charged if teleportPlayer is successful @@ -99,35 +216,95 @@ public interface ITeleport { * * @throws Exception */ + @Deprecated void warp(IUser otherUser, String warp, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + /** + * Teleport wrapper used to handle /warp teleports + * + * @param otherUser - Which user will be teleported + * @param warp - The name of the warp the user will be teleported too. + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution + * @param future - Future which is completed with the success status of the execution + * + * @throws Exception + */ + void warp(IUser otherUser, String warp, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + /** * Teleport wrapper used to handle /back teleports * + * @deprecated {@link ITeleport#back(Trade, CompletableFuture, CompletableFuture)} + * * @param chargeFor - What the user will be charged if teleportPlayer is successful * * @throws Exception */ + @Deprecated void back(Trade chargeFor) throws Exception; + /** + * Teleport wrapper used to handle /back teleports + * + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution + * @param future - Future which is completed with the success status of the execution + * + * @throws Exception + */ + void back(Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future); + /** * Teleport wrapper used to handle /back teleports that * are executed by a different player with this * instance of teleport as a target. * + * @deprecated Use {@link ITeleport#back(IUser, Trade, CompletableFuture, CompletableFuture)} + * * @param teleporter - The user performing the /back command. - * This value may be {@code null} to indicate console. + * This value may be {@code null} to indicate console. * @param chargeFor - What the {@code teleporter} will be charged if teleportPlayer is successful * * @throws Exception */ + @Deprecated void back(IUser teleporter, Trade chargeFor) throws Exception; + /** + * Teleport wrapper used to handle /back teleports that + * are executed by a different player with this + * instance of teleport as a target. + * + * @param teleporter - The user performing the /back command. + * This value may be {@code null} to indicate console. + * @param chargeFor - What the {@code teleporter} will be charged if teleportPlayer is successful + * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution + * @param future - Future which is completed with the success status of the execution + * + * @throws Exception + */ + void back(IUser teleporter, Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future); + /** * Teleport wrapper used to handle throwing user home after a jail sentence * + * @deprecated Use {@link ITeleport#back(CompletableFuture, CompletableFuture)} + * * @throws Exception */ + @Deprecated void back() throws Exception; + /** + * Teleport wrapper used to handle throwing user home after a jail sentence + * + * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution + * @param future - Future which is completed with the success status of the execution + * + * @throws Exception + */ + void back(CompletableFuture exceptionFuture, CompletableFuture future); + } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandback.java b/Essentials/src/com/earth2me/essentials/commands/Commandback.java index e8171359bba..e1b218e3c53 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandback.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandback.java @@ -3,6 +3,7 @@ import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; +import net.ess3.api.IUser; import org.bukkit.Server; import org.bukkit.entity.Player; @@ -77,7 +78,7 @@ private void teleportBack(CommandSource sender, User user) throws Exception { } if (requester == null) { - user.getTeleport().back(null, null); + user.getTeleport().back((IUser) null, null); } else if (!requester.equals(user)) { Trade charge = new Trade(this.getName(), this.ess); charge.isAffordableFor(requester); From bd49c562f77f76230d2d9b5e057e727778aabbe8 Mon Sep 17 00:00:00 2001 From: JRoy Date: Fri, 3 Apr 2020 20:48:30 -0400 Subject: [PATCH 06/36] Remove old now fallback method --- Essentials/src/com/earth2me/essentials/Teleport.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java index 549bf92cab0..b6dd74f819f 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -160,15 +160,6 @@ public void now(Player entity, boolean cooldown, TeleportCause cause, Completabl }); } - protected void Gnow(IUser teleportee, ITarget target, TeleportCause cause) throws Exception { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - nowAsync(teleportee, target, cause, exceptionFuture, future); - if (!future.get()) { - throw exceptionFuture.get(); - } - } - protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { cancel(false); From 80250ffc2592f12c646cc5ebaa19fb6334ab0bbf Mon Sep 17 00:00:00 2001 From: JRoy Date: Fri, 3 Apr 2020 23:15:26 -0400 Subject: [PATCH 07/36] Migrate everything to the new async teleport API --- .../src/com/earth2me/essentials/Jails.java | 22 +++++++++- .../src/com/earth2me/essentials/User.java | 12 ++---- .../com/earth2me/essentials/api/IJails.java | 15 +++++++ .../essentials/commands/Commandback.java | 20 ++++----- .../essentials/commands/Commandhome.java | 26 ++++++----- .../essentials/commands/Commandjump.java | 4 +- .../commands/Commandtogglejail.java | 10 ++++- .../essentials/commands/Commandtop.java | 12 ++++-- .../essentials/commands/Commandtp.java | 43 +++++++++++++------ .../essentials/commands/Commandtpa.java | 12 ++++-- .../essentials/commands/Commandtpaccept.java | 42 +++++++++++------- .../essentials/commands/Commandtpall.java | 15 +++---- .../essentials/commands/Commandtphere.java | 4 +- .../essentials/commands/Commandtpo.java | 14 ++++-- .../essentials/commands/Commandtpoffline.java | 6 ++- .../essentials/commands/Commandtpohere.java | 6 ++- .../essentials/commands/Commandtppos.java | 7 ++- .../essentials/commands/Commandwarp.java | 16 +++---- .../essentials/commands/Commandworld.java | 4 +- .../commands/EssentialsCommand.java | 18 ++++++++ .../commands/IEssentialsCommand.java | 4 ++ .../earth2me/essentials/signs/SignWarp.java | 17 +++++--- .../essentials/spawn/Commandspawn.java | 37 ++++++++++------ .../spawn/EssentialsSpawnPlayerListener.java | 13 +++--- 24 files changed, 256 insertions(+), 123 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Jails.java b/Essentials/src/com/earth2me/essentials/Jails.java index 6771ef9636e..6836e9cc5c1 100644 --- a/Essentials/src/com/earth2me/essentials/Jails.java +++ b/Essentials/src/com/earth2me/essentials/Jails.java @@ -22,6 +22,7 @@ import java.io.File; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.logging.Level; import java.util.logging.Logger; @@ -117,12 +118,31 @@ public void removeJail(final String jail) throws Exception { } @Override + @Deprecated public void sendToJail(final IUser user, final String jail) throws Exception { + CompletableFuture eFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + sendToJail(user, jail, eFuture, future); + if (!future.get()) { + throw eFuture.get(); + } + } + + @Override + public void sendToJail(IUser user, String jail, CompletableFuture exceptionFuture, CompletableFuture future) throws Exception { acquireReadLock(); try { if (user.getBase().isOnline()) { Location loc = getJail(jail); - user.getTeleport().now(loc, false, TeleportCause.COMMAND); + CompletableFuture future1 = new CompletableFuture<>(); + future1.thenAccept(success -> { + if (success) { + user.setJail(jail); + } + future.complete(success); + }); + user.getTeleport().now(loc, false, TeleportCause.COMMAND, exceptionFuture, future1); + return; } user.setJail(jail); } finally { diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index e889ff21c01..0e5043f6df6 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -29,6 +29,7 @@ import java.math.BigDecimal; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.logging.Level; import java.util.logging.Logger; @@ -572,14 +573,9 @@ public boolean checkJailTimeout(final long currentTime) { sendMessage(tl("haveBeenReleased")); setJail(null); if (ess.getSettings().isTeleportBackWhenFreedFromJail()) { - try { - getTeleport().back(); - } catch (Exception ex) { - try { - getTeleport().respawn(null, TeleportCause.PLUGIN); - } catch (Exception ex1) { - } - } + CompletableFuture eFuture = new CompletableFuture<>(); + eFuture.thenAccept(e -> getTeleport().respawn(null, TeleportCause.PLUGIN, new CompletableFuture<>(), new CompletableFuture<>())); + getTeleport().back(eFuture, new CompletableFuture<>()); } return true; } diff --git a/Essentials/src/com/earth2me/essentials/api/IJails.java b/Essentials/src/com/earth2me/essentials/api/IJails.java index b065c995129..60543288d47 100644 --- a/Essentials/src/com/earth2me/essentials/api/IJails.java +++ b/Essentials/src/com/earth2me/essentials/api/IJails.java @@ -4,6 +4,7 @@ import org.bukkit.Location; import java.util.Collection; +import java.util.concurrent.CompletableFuture; public interface IJails extends IReload { @@ -46,6 +47,8 @@ public interface IJails extends IReload { /** * Attempts to send the given user to the given jail * + * @deprecated Use {@link IJails#sendToJail(IUser, String, CompletableFuture, CompletableFuture)} + * * @param user the user to send to jail * @param jail the jail to send the user to * @@ -53,6 +56,18 @@ public interface IJails extends IReload { */ void sendToJail(IUser user, String jail) throws Exception; + /** + * Attempts to send the given user to the given jail + * + * @param user the user to send to jail + * @param jail the jail to send the user to + * @param exceptionFuture Future which is completed with an exception if one is thrown during execution + * @param future Future which is completed with the success status of the execution + * + * @throws Exception if the user is offline or jail does not exist + */ + void sendToJail(IUser user, String jail, CompletableFuture exceptionFuture, CompletableFuture future) throws Exception; + /** * Set a new jail with the given name and location * diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandback.java b/Essentials/src/com/earth2me/essentials/commands/Commandback.java index e1b218e3c53..65aba66aa71 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandback.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandback.java @@ -3,7 +3,6 @@ import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; -import net.ess3.api.IUser; import org.bukkit.Server; import org.bukkit.entity.Player; @@ -11,6 +10,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -24,11 +24,11 @@ public Commandback() { protected void run(Server server, User user, String commandLabel, String[] args) throws Exception { CommandSource sender = user.getSource(); if (args.length > 0 && user.isAuthorized("essentials.back.others")) { - this.parseCommand(server, sender, args, true); + this.parseCommand(server, sender, args, true, commandLabel); return; } - teleportBack(sender, user); + teleportBack(sender, user, commandLabel); } @Override @@ -37,10 +37,10 @@ protected void run(Server server, CommandSource sender, String commandLabel, Str throw new NotEnoughArgumentsException(); } - this.parseCommand(server, sender, args, true); + this.parseCommand(server, sender, args, true, commandLabel); } - private void parseCommand(Server server, CommandSource sender, String[] args, boolean allowOthers) throws Exception { + private void parseCommand(Server server, CommandSource sender, String[] args, boolean allowOthers, String commandLabel) throws Exception { Collection players = new ArrayList<>(); if (allowOthers && args.length > 0 && args[0].trim().length() > 2) { @@ -53,11 +53,11 @@ private void parseCommand(Server server, CommandSource sender, String[] args, bo for (Player player : players) { sender.sendMessage(tl("backOther", player.getName())); - teleportBack(sender, ess.getUser(player)); + teleportBack(sender, ess.getUser(player), commandLabel); } } - private void teleportBack(CommandSource sender, User user) throws Exception { + private void teleportBack(CommandSource sender, User user, String commandLabel) throws Exception { if (user.getLastLocation() == null) { throw new Exception(tl("noLocationFound")); } @@ -78,15 +78,15 @@ private void teleportBack(CommandSource sender, User user) throws Exception { } if (requester == null) { - user.getTeleport().back((IUser) null, null); + user.getTeleport().back(null, null, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); } else if (!requester.equals(user)) { Trade charge = new Trade(this.getName(), this.ess); charge.isAffordableFor(requester); - user.getTeleport().back(requester, charge); + user.getTeleport().back(requester, charge, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); } else { Trade charge = new Trade(this.getName(), this.ess); charge.isAffordableFor(user); - user.getTeleport().back(charge); + user.getTeleport().back(charge, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); } throw new NoChargeException(); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java index cd4a0309ede..c713080ecd0 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java @@ -7,10 +7,10 @@ import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Locale; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -38,30 +38,32 @@ public void run(final Server server, final User user, final String commandLabel, } } } + CompletableFuture eFuture = new CompletableFuture<>(); + eFuture.thenAccept(e -> showError(user.getBase(), e, commandLabel)); try { if ("bed".equalsIgnoreCase(homeName) && user.isAuthorized("essentials.home.bed")) { final Location bed = player.getBase().getBedSpawnLocation(); if (bed != null) { - user.getTeleport().teleport(bed, charge, TeleportCause.COMMAND); - throw new NoChargeException(); + user.getTeleport().teleport(bed, charge, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + return; } else { throw new Exception(tl("bedMissing")); } } - goHome(user, player, homeName.toLowerCase(Locale.ENGLISH), charge); + goHome(user, player, homeName.toLowerCase(Locale.ENGLISH), charge, eFuture); } catch (NotEnoughArgumentsException e) { Location bed = player.getBase().getBedSpawnLocation(); final List homes = player.getHomes(); if (homes.isEmpty() && player.equals(user)) { if (ess.getSettings().isSpawnIfNoHome()) { - user.getTeleport().respawn(charge, TeleportCause.COMMAND); + user.getTeleport().respawn(charge, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); } else { throw new Exception(tl("noHomeSetPlayer")); } } else if (homes.isEmpty()) { throw new Exception(tl("noHomeSetPlayer")); } else if (homes.size() == 1 && player.equals(user)) { - goHome(user, player, homes.get(0), charge); + goHome(user, player, homes.get(0), charge, eFuture); } else { final int count = homes.size(); if (user.isAuthorized("essentials.home.bed")) { @@ -74,7 +76,6 @@ public void run(final Server server, final User user, final String commandLabel, user.sendMessage(tl("homes", StringUtil.joinList(homes), count, getHomeLimit(player))); } } - throw new NoChargeException(); } private String getHomeLimit(final User player) { @@ -87,7 +88,7 @@ private String getHomeLimit(final User player) { return Integer.toString(ess.getSettings().getHomeLimit(player)); } - private void goHome(final User user, final User player, final String home, final Trade charge) throws Exception { + private void goHome(final User user, final User player, final String home, final Trade charge, CompletableFuture eFuture) throws Exception { if (home.length() < 1) { throw new NotEnoughArgumentsException(); } @@ -98,8 +99,13 @@ private void goHome(final User user, final User player, final String home, final if (user.getWorld() != loc.getWorld() && ess.getSettings().isWorldHomePermissions() && !user.isAuthorized("essentials.worlds." + loc.getWorld().getName())) { throw new Exception(tl("noPerm", "essentials.worlds." + loc.getWorld().getName())); } - user.getTeleport().teleport(loc, charge, TeleportCause.COMMAND); - user.sendMessage(tl("teleportHome", home)); + CompletableFuture future = new CompletableFuture<>(); + future.thenAccept(success -> { + if (success) { + user.sendMessage(tl("teleportHome", home)); + } + }); + user.getTeleport().teleport(loc, charge, TeleportCause.COMMAND, eFuture, future); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandjump.java b/Essentials/src/com/earth2me/essentials/commands/Commandjump.java index 046fd61a864..bee26b2759f 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandjump.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandjump.java @@ -10,6 +10,7 @@ import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -46,8 +47,7 @@ public void run(final Server server, final User user, final String commandLabel, final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); - user.getTeleport().teleport(loc, charge, TeleportCause.COMMAND); - throw new NoChargeException(); + user.getTeleport().teleport(loc, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java b/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java index 080f665925f..80b3d343ba8 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -91,7 +92,14 @@ public void run(final Server server, final CommandSource sender, final String co player.sendMessage(tl("jailReleasedPlayerNotify")); player.setJail(null); if (player.getBase().isOnline()) { - player.getTeleport().back(); + CompletableFuture future = new CompletableFuture<>(); + future.thenAccept(success -> { + if (success) { + sender.sendMessage(tl("jailReleased", player.getName())); + } + }); + player.getTeleport().back(getNewExceptionFuture(sender, commandLabel), future); + return; } sender.sendMessage(tl("jailReleased", player.getName())); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtop.java b/Essentials/src/com/earth2me/essentials/commands/Commandtop.java index 6b60d2de647..42171be93f0 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtop.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtop.java @@ -7,6 +7,8 @@ import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import java.util.concurrent.CompletableFuture; + import static com.earth2me.essentials.I18n.tl; @@ -22,8 +24,12 @@ public void run(final Server server, final User user, final String commandLabel, final float pitch = user.getLocation().getPitch(); final float yaw = user.getLocation().getYaw(); final Location loc = LocationUtil.getSafeDestination(new Location(user.getWorld(), topX, user.getWorld().getMaxHeight(), topZ, yaw, pitch)); - user.getTeleport().teleport(loc, new Trade(this.getName(), ess), TeleportCause.COMMAND); - user.sendMessage(tl("teleportTop", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - + CompletableFuture future = new CompletableFuture<>(); + future.thenAccept(success -> { + if (success) { + user.sendMessage(tl("teleportTop", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + } + }); + user.getTeleport().teleport(loc, new Trade(this.getName(), ess), TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtp.java b/Essentials/src/com/earth2me/essentials/commands/Commandtp.java index 717308c7c13..68ca636ff54 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtp.java @@ -4,14 +4,13 @@ import com.earth2me.essentials.Console; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Server; -import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -23,6 +22,9 @@ public Commandtp() { @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { + CompletableFuture eFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + eFuture.thenAccept(e -> showError(user.getBase(), e, commandLabel)); switch (args.length) { case 0: throw new NotEnoughArgumentsException(); @@ -43,8 +45,8 @@ public void run(final Server server, final User user, final String commandLabel, } final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); - user.getTeleport().teleport(player.getBase(), charge, TeleportCause.COMMAND); - throw new NoChargeException(); + user.getTeleport().teleport(player.getBase(), charge, TeleportCause.COMMAND, eFuture, future); + break; case 3: if (!user.isAuthorized("essentials.tp.position")) { throw new Exception(tl("noPerm", "essentials.tp.position")); @@ -56,8 +58,12 @@ public void run(final Server server, final User user, final String commandLabel, throw new NotEnoughArgumentsException(tl("teleportInvalidLocation")); } final Location locpos = new Location(user.getWorld(), x2, y2, z2, user.getLocation().getYaw(), user.getLocation().getPitch()); - user.getTeleport().now(locpos, false, TeleportCause.COMMAND); - user.sendMessage(tl("teleporting", locpos.getWorld().getName(), locpos.getBlockX(), locpos.getBlockY(), locpos.getBlockZ())); + user.getTeleport().now(locpos, false, TeleportCause.COMMAND, eFuture, future); + future.thenAccept(success -> { + if (success) { + user.sendMessage(tl("teleporting", locpos.getWorld().getName(), locpos.getBlockX(), locpos.getBlockY(), locpos.getBlockZ())); + } + }); break; case 4: if (!user.isAuthorized("essentials.tp.others")) { @@ -78,8 +84,12 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("teleportDisabled", target2.getDisplayName())); } user.sendMessage(tl("teleporting", locposother.getWorld().getName(), locposother.getBlockX(), locposother.getBlockY(), locposother.getBlockZ())); - target2.getTeleport().now(locposother, false, TeleportCause.COMMAND); - target2.sendMessage(tl("teleporting", locposother.getWorld().getName(), locposother.getBlockX(), locposother.getBlockY(), locposother.getBlockZ())); + target2.getTeleport().now(locposother, false, TeleportCause.COMMAND, eFuture, future); + future.thenAccept(success -> { + if (success) { + target2.sendMessage(tl("teleporting", locposother.getWorld().getName(), locposother.getBlockX(), locposother.getBlockY(), locposother.getBlockZ())); + } + }); break; case 2: default: @@ -98,7 +108,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("noPerm", "essentials.worlds." + toPlayer.getWorld().getName())); } target.sendMessage(tl("teleportAtoB", user.getDisplayName(), toPlayer.getDisplayName())); - target.getTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND); + target.getTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, eFuture, future); break; } } @@ -113,7 +123,9 @@ public void run(final Server server, final CommandSource sender, final String co if (args.length == 2) { final User toPlayer = getPlayer(server, args, 1, true, false); target.sendMessage(tl("teleportAtoB", Console.NAME, toPlayer.getDisplayName())); - target.getTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND); + CompletableFuture eFuture = new CompletableFuture<>(); + target.getTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + eFuture.thenAccept(e -> showError(sender.getSender(), e, commandLabel)); } else if (args.length > 3) { final double x = args[1].startsWith("~") ? target.getLocation().getX() + (args[1].length() > 1 ? Double.parseDouble(args[1].substring(1)) : 0) : Double.parseDouble(args[1]); final double y = args[2].startsWith("~") ? target.getLocation().getY() + (args[2].length() > 1 ? Double.parseDouble(args[2].substring(1)) : 0) : Double.parseDouble(args[2]); @@ -123,8 +135,15 @@ public void run(final Server server, final CommandSource sender, final String co } final Location loc = new Location(target.getWorld(), x, y, z, target.getLocation().getYaw(), target.getLocation().getPitch()); sender.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - target.getTeleport().now(loc, false, TeleportCause.COMMAND); - target.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + CompletableFuture eFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + target.getTeleport().now(loc, false, TeleportCause.COMMAND, eFuture, future); + future.thenAccept(success -> { + if (success) { + target.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + } + }); + eFuture.thenAccept(e -> showError(sender.getSender(), e, commandLabel)); } else { throw new NotEnoughArgumentsException(); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java index 9d57137f7cb..e44aa839270 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java @@ -9,6 +9,7 @@ import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -43,9 +44,14 @@ public void run(Server server, User user, String commandLabel, String[] args) th final Trade charge = new Trade(this.getName(), ess); Teleport teleport = user.getTeleport(); teleport.setTpType(Teleport.TeleportType.TPA); - teleport.teleport(player.getBase(), charge, PlayerTeleportEvent.TeleportCause.COMMAND); - player.sendMessage(tl("requestAcceptedAuto", user.getDisplayName())); - user.sendMessage(tl("requestAcceptedFromAuto", player.getDisplayName())); + CompletableFuture future = new CompletableFuture<>(); + future.thenAccept(success -> { + if (success) { + player.sendMessage(tl("requestAcceptedAuto", user.getDisplayName())); + user.sendMessage(tl("requestAcceptedFromAuto", player.getDisplayName())); + } + }); + teleport.teleport(player.getBase(), charge, PlayerTeleportEvent.TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); return; } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java index ab7a6127066..c5a59d7e5f3 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java @@ -7,6 +7,8 @@ import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import java.util.concurrent.CompletableFuture; + import static com.earth2me.essentials.I18n.tl; @@ -49,24 +51,32 @@ public void run(final Server server, final User user, final String commandLabel, user.sendMessage(tl("requestAccepted")); requester.sendMessage(tl("requestAcceptedFrom", user.getDisplayName())); - try { - if (user.isTpRequestHere()) { - final Location loc = user.getTpRequestLocation(); - Teleport teleport = requester.getTeleport(); - teleport.setTpType(Teleport.TeleportType.TPA); - teleport.teleportPlayer(user, user.getTpRequestLocation(), charge, TeleportCause.COMMAND); - requester.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - } else { - Teleport teleport = requester.getTeleport(); - teleport.setTpType(Teleport.TeleportType.TPA); - teleport.teleport(user.getBase(), charge, TeleportCause.COMMAND); - } - } catch (Exception ex) { + CompletableFuture eFuture = new CompletableFuture<>(); + eFuture.thenAccept(e -> { user.sendMessage(tl("pendingTeleportCancelled")); - ess.showError(requester.getSource(), ex, commandLabel); + ess.showError(requester.getSource(), e, commandLabel); + }); + CompletableFuture future = new CompletableFuture<>(); + future.thenAccept(success -> { + if (success) { + user.requestTeleport(null, false); + } + }); + if (user.isTpRequestHere()) { + final Location loc = user.getTpRequestLocation(); + Teleport teleport = requester.getTeleport(); + teleport.setTpType(Teleport.TeleportType.TPA); + future.thenAccept(success -> { + if (success) { + requester.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + } + }); + teleport.teleportPlayer(user, user.getTpRequestLocation(), charge, TeleportCause.COMMAND, eFuture, future); + } else { + Teleport teleport = requester.getTeleport(); + teleport.setTpType(Teleport.TeleportType.TPA); + teleport.teleport(user.getBase(), charge, TeleportCause.COMMAND, eFuture, future); } - user.requestTeleport(null, false); - throw new NoChargeException(); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java index aa3ee8e7ed3..07a3ffb8e08 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java @@ -8,6 +8,7 @@ import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -21,17 +22,17 @@ public Commandtpall() { public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception { if (args.length < 1) { if (sender.isPlayer()) { - teleportAllPlayers(server, sender, ess.getUser(sender.getPlayer())); + teleportAllPlayers(server, sender, ess.getUser(sender.getPlayer()), commandLabel); return; } throw new NotEnoughArgumentsException(); } final User target = getPlayer(server, sender, args, 0); - teleportAllPlayers(server, sender, target); + teleportAllPlayers(server, sender, target, commandLabel); } - private void teleportAllPlayers(Server server, CommandSource sender, User target) { + private void teleportAllPlayers(Server server, CommandSource sender, User target, String label) { sender.sendMessage(tl("teleportAll")); final Location loc = target.getLocation(); for (User player : ess.getOnlineUsers()) { @@ -41,11 +42,9 @@ private void teleportAllPlayers(Server server, CommandSource sender, User target if (sender.equals(target.getBase()) && target.getWorld() != player.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !target.isAuthorized("essentials.worlds." + target.getWorld().getName())) { continue; } - try { - player.getTeleport().now(loc, false, TeleportCause.COMMAND); - } catch (Exception ex) { - ess.showError(sender, ex, getName()); - } + CompletableFuture eFuture = new CompletableFuture<>(); + eFuture.thenAccept(e -> showError(sender.getSender(), e, label)); + player.getTeleport().now(loc, false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtphere.java b/Essentials/src/com/earth2me/essentials/commands/Commandtphere.java index c8177c246d9..757733eef53 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtphere.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtphere.java @@ -7,6 +7,7 @@ import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -25,8 +26,7 @@ public void run(final Server server, final User user, final String commandLabel, if (user.getWorld() != player.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + user.getWorld().getName())) { throw new Exception(tl("noPerm", "essentials.worlds." + user.getWorld().getName())); } - user.getTeleport().teleportPlayer(player, user.getBase(), new Trade(this.getName(), ess), TeleportCause.COMMAND); - throw new NoChargeException(); + user.getTeleport().teleportPlayer(player, user.getBase(), new Trade(this.getName(), ess), TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java index 0ce0132caed..cc65f803c29 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java @@ -6,6 +6,7 @@ import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -17,6 +18,8 @@ public Commandtpo() { @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { + CompletableFuture eFuture = new CompletableFuture<>(); + eFuture.thenAccept(e -> showError(user.getBase(), e, commandLabel)); switch (args.length) { case 0: throw new NotEnoughArgumentsException(); @@ -26,7 +29,7 @@ public void run(final Server server, final User user, final String commandLabel, if (user.getWorld() != player.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + player.getWorld().getName())) { throw new Exception(tl("noPerm", "essentials.worlds." + player.getWorld().getName())); } - user.getTeleport().now(player.getBase(), false, TeleportCause.COMMAND); + user.getTeleport().now(player.getBase(), false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); break; default: @@ -40,8 +43,13 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("noPerm", "essentials.worlds." + toPlayer.getWorld().getName())); } - target.getTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND); - target.sendMessage(tl("teleportAtoB", user.getDisplayName(), toPlayer.getDisplayName())); + CompletableFuture future = new CompletableFuture<>(); + future.thenAccept(success -> { + if (success) { + target.sendMessage(tl("teleportAtoB", user.getDisplayName(), toPlayer.getDisplayName())); + } + }); + target.getTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, eFuture, future); break; } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java index ad998aa391b..aae4b683f9a 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java @@ -5,6 +5,8 @@ import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent; +import java.util.concurrent.CompletableFuture; + import static com.earth2me.essentials.I18n.tl; public class Commandtpoffline extends EssentialsCommand { @@ -28,7 +30,9 @@ public void run(final Server server, final User user, final String label, final } user.sendMessage(tl("teleporting", logout.getWorld().getName(), logout.getBlockX(), logout.getBlockY(), logout.getBlockZ())); - user.getTeleport().now(logout, false, PlayerTeleportEvent.TeleportCause.COMMAND); + CompletableFuture eFuture = new CompletableFuture<>(); + eFuture.thenAccept(e -> showError(user.getBase(), e, label)); + user.getTeleport().now(logout, false, PlayerTeleportEvent.TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); } } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java index 39016cb10ef..c92f5887892 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java @@ -6,6 +6,7 @@ import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -28,8 +29,9 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("noPerm", "essentials.worlds." + user.getWorld().getName())); } - // Verify permission - player.getTeleport().now(user.getBase(), false, TeleportCause.COMMAND); + CompletableFuture eFuture = new CompletableFuture<>(); + eFuture.thenAccept(e -> showError(user.getBase(), e, commandLabel)); + player.getTeleport().now(user.getBase(), false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java b/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java index 763e5cec855..87ee93c7dea 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java @@ -12,6 +12,7 @@ import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -55,8 +56,7 @@ public void run(final Server server, final User user, final String commandLabel, final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); user.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - user.getTeleport().teleport(loc, charge, TeleportCause.COMMAND); - throw new NoChargeException(); + user.getTeleport().teleport(loc, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } @Override @@ -85,8 +85,7 @@ public void run(final Server server, final CommandSource sender, final String co } sender.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); user.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - user.getTeleport().teleport(loc, null, TeleportCause.COMMAND); - + user.getTeleport().teleport(loc, null, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java b/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java index 07abc89b6e6..5b77c16f7e1 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java @@ -15,6 +15,7 @@ import java.util.Collections; import java.util.List; import java.util.Locale; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import static com.earth2me.essentials.I18n.tl; @@ -41,11 +42,10 @@ public void run(final Server server, final User user, final String commandLabel, User otherUser = null; if (args.length == 2 && (user.isAuthorized("essentials.warp.otherplayers") || user.isAuthorized("essentials.warp.others"))) { otherUser = getPlayer(server, user, args, 1); - warpUser(user, otherUser, args[0]); - throw new NoChargeException(); + warpUser(user, otherUser, args[0], commandLabel); + return; } - warpUser(user, user, args[0]); - throw new NoChargeException(); + warpUser(user, user, args[0], commandLabel); } } @@ -56,9 +56,7 @@ public void run(final Server server, final CommandSource sender, final String co throw new NoChargeException(); } User otherUser = getPlayer(server, args, 1, true, false); - otherUser.getTeleport().warp(otherUser, args[0], null, TeleportCause.COMMAND); - throw new NoChargeException(); - + otherUser.getTeleport().warp(otherUser, args[0], null, TeleportCause.COMMAND, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); } //TODO: Use one of the new text classes, like /help ? @@ -91,7 +89,7 @@ private void warpList(final CommandSource sender, final String[] args, final IUs } } - private void warpUser(final User owner, final User user, final String name) throws Exception { + private void warpUser(final User owner, final User user, final String name, final String commandLabel) throws Exception { final Trade chargeWarp = new Trade("warp-" + name.toLowerCase(Locale.ENGLISH).replace('_', '-'), ess); final Trade chargeCmd = new Trade(this.getName(), ess); final BigDecimal fullCharge = chargeWarp.getCommandCost(user).add(chargeCmd.getCommandCost(user)); @@ -100,7 +98,7 @@ private void warpUser(final User owner, final User user, final String name) thro if (ess.getSettings().getPerWarpPermission() && !owner.isAuthorized("essentials.warps." + name)) { throw new Exception(tl("warpUsePermission")); } - owner.getTeleport().warp(user, name, charge, TeleportCause.COMMAND); + owner.getTeleport().warp(user, name, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } private List getAvailableWarpsFor(final IUser user) { diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandworld.java b/Essentials/src/com/earth2me/essentials/commands/Commandworld.java index 07a8da408ed..301d47725fe 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandworld.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandworld.java @@ -10,6 +10,7 @@ import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -66,8 +67,7 @@ protected void run(final Server server, final User user, final String commandLab final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); - user.getTeleport().teleport(target, charge, TeleportCause.COMMAND); - throw new NoChargeException(); + user.getTeleport().teleport(target, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java index d499a2c859f..a3111ecc96e 100644 --- a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java +++ b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java @@ -1,5 +1,6 @@ package com.earth2me.essentials.commands; +import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; import com.earth2me.essentials.CommandSource; @@ -24,6 +25,8 @@ import java.util.Locale; import java.util.Map; import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.logging.Level; import java.util.logging.Logger; import static com.earth2me.essentials.I18n.tl; @@ -344,6 +347,21 @@ protected final List tabCompleteCommand(CommandSource sender, Server ser return command.tabComplete(sender.getSender(), label, effectiveArgs); } + @Override + public void showError(CommandSender sender, Throwable throwable, String commandLabel) { + sender.sendMessage(tl("errorWithMessage", throwable.getMessage())); + if (ess.getSettings().isDebug()) { + ess.getLogger().log(Level.INFO, tl("errorCallingCommand", commandLabel), throwable); + throwable.printStackTrace(); + } + } + + public CompletableFuture getNewExceptionFuture(CommandSource sender, String commandLabel) { + CompletableFuture future = new CompletableFuture<>(); + future.thenAccept(e -> showError(sender.getSender(), e, commandLabel)); + return future; + } + /** * Common time durations (in seconds), for use in tab completion. */ diff --git a/Essentials/src/com/earth2me/essentials/commands/IEssentialsCommand.java b/Essentials/src/com/earth2me/essentials/commands/IEssentialsCommand.java index 07cb08abe84..86e08877aca 100644 --- a/Essentials/src/com/earth2me/essentials/commands/IEssentialsCommand.java +++ b/Essentials/src/com/earth2me/essentials/commands/IEssentialsCommand.java @@ -6,6 +6,8 @@ import net.ess3.api.IEssentials; import org.bukkit.Server; import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + import java.util.List; @@ -23,4 +25,6 @@ public interface IEssentialsCommand { void setEssentials(IEssentials ess); void setEssentialsModule(IEssentialsModule module); + + void showError(CommandSender sender, Throwable throwable, String commandLabel); } diff --git a/Essentials/src/com/earth2me/essentials/signs/SignWarp.java b/Essentials/src/com/earth2me/essentials/signs/SignWarp.java index 2bdc5afdbb7..e267e4a6ae3 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignWarp.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignWarp.java @@ -6,6 +6,8 @@ import net.ess3.api.IEssentials; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import java.util.concurrent.CompletableFuture; + import static com.earth2me.essentials.I18n.tl; @@ -52,12 +54,15 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri } final Trade charge = getTrade(sign, 3, ess); - try { - player.getTeleport().warp(player, warpName, charge, TeleportCause.PLUGIN); - Trade.log("Sign", "Warp", "Interact", username, null, username, charge, sign.getBlock().getLocation(), ess); - } catch (Exception ex) { - throw new SignException(ex.getMessage(), ex); - } + CompletableFuture eFuture = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + eFuture.thenAccept(e -> ess.showError(player.getSource(), e, "\\ sign: " + signName)); + future.thenAccept(success -> { + if (success) { + Trade.log("Sign", "Warp", "Interact", username, null, username, charge, sign.getBlock().getLocation(), ess); + } + }); + player.getTeleport().warp(player, warpName, charge, TeleportCause.PLUGIN, eFuture, future); return true; } } diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java index c903f404fee..db905ddf03a 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java @@ -5,12 +5,13 @@ import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import com.earth2me.essentials.commands.EssentialsCommand; -import com.earth2me.essentials.commands.NoChargeException; import com.earth2me.essentials.commands.NotEnoughArgumentsException; import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import java.util.concurrent.CompletableFuture; + import static com.earth2me.essentials.I18n.tl; @@ -25,14 +26,18 @@ public void run(final Server server, final User user, final String commandLabel, charge.isAffordableFor(user); if (args.length > 0 && user.isAuthorized("essentials.spawn.others")) { final User otherUser = getPlayer(server, user, args, 0); - respawn(user.getSource(), user, otherUser, charge); - if (!otherUser.equals(user)) { - otherUser.sendMessage(tl("teleportAtoB", user.getDisplayName(), "spawn")); - } + CompletableFuture future = new CompletableFuture<>(); + future.thenAccept(success -> { + if (success) { + if (!otherUser.equals(user)) { + otherUser.sendMessage(tl("teleportAtoB", user.getDisplayName(), "spawn")); + } + } + }); + respawn(user.getSource(), user, otherUser, charge, commandLabel, future); } else { - respawn(user.getSource(), user, user, charge); + respawn(user.getSource(), user, user, charge, commandLabel, new CompletableFuture<>()); } - throw new NoChargeException(); } @Override @@ -41,19 +46,25 @@ protected void run(final Server server, final CommandSource sender, final String throw new NotEnoughArgumentsException(); } final User user = getPlayer(server, args, 0, true, false); - respawn(sender, null, user, null); - user.sendMessage(tl("teleportAtoB", Console.NAME, "spawn")); - + CompletableFuture future = new CompletableFuture<>(); + respawn(sender, null, user, null, commandLabel, future); + future.thenAccept(success -> { + if (success) { + user.sendMessage(tl("teleportAtoB", Console.NAME, "spawn")); + } + }); } - private void respawn(final CommandSource sender, final User teleportOwner, final User teleportee, final Trade charge) throws Exception { + private void respawn(final CommandSource sender, final User teleportOwner, final User teleportee, final Trade charge, String commandLabel, CompletableFuture future) throws Exception { final SpawnStorage spawns = (SpawnStorage) this.module; final Location spawn = spawns.getSpawn(teleportee.getGroup()); sender.sendMessage(tl("teleporting", spawn.getWorld().getName(), spawn.getBlockX(), spawn.getBlockY(), spawn.getBlockZ())); + CompletableFuture eFuture = new CompletableFuture<>(); + eFuture.thenAccept(e -> showError(sender.getSender(), e, commandLabel)); if (teleportOwner == null) { - teleportee.getTeleport().now(spawn, false, TeleportCause.COMMAND); + teleportee.getTeleport().now(spawn, false, TeleportCause.COMMAND, eFuture, future); } else { - teleportOwner.getTeleport().teleportPlayer(teleportee, spawn, charge, TeleportCause.COMMAND); + teleportOwner.getTeleport().teleportPlayer(teleportee, spawn, charge, TeleportCause.COMMAND, eFuture, future); } } } diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java index 05db07ed80a..5f8b4e28436 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java @@ -17,6 +17,7 @@ import java.util.List; import java.util.Locale; +import java.util.concurrent.CompletableFuture; import java.util.logging.Level; import java.util.logging.Logger; @@ -135,13 +136,11 @@ public void run() { return; } - try { - final Location spawn = spawns.getSpawn(ess.getSettings().getNewbieSpawn()); - if (spawn != null) { - user.getTeleport().now(spawn, false, TeleportCause.PLUGIN); - } - } catch (Exception ex) { - logger.log(Level.WARNING, tl("teleportNewPlayerError"), ex); + final Location spawn = spawns.getSpawn(ess.getSettings().getNewbieSpawn()); + if (spawn != null) { + CompletableFuture eFuture = new CompletableFuture<>(); + eFuture.thenAccept(e -> logger.log(Level.WARNING, tl("teleportNewPlayerError"), e)); + user.getTeleport().now(spawn, false, TeleportCause.PLUGIN, eFuture, new CompletableFuture<>()); } } } From c299234645235c7d19193f429cdd9b8593644bf0 Mon Sep 17 00:00:00 2001 From: JRoy Date: Fri, 3 Apr 2020 23:17:27 -0400 Subject: [PATCH 08/36] Update ITeleport javadocs --- .../earth2me/essentials/api/ITeleport.java | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/api/ITeleport.java b/Essentials/src/com/earth2me/essentials/api/ITeleport.java index 27f8d85d09d..59a946ad4ac 100644 --- a/Essentials/src/com/earth2me/essentials/api/ITeleport.java +++ b/Essentials/src/com/earth2me/essentials/api/ITeleport.java @@ -32,8 +32,6 @@ public interface ITeleport { * @param cause - The reported teleportPlayer cause * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution - * - * @throws Exception */ void now(Location loc, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); @@ -59,8 +57,6 @@ public interface ITeleport { * @param cause - The reported teleportPlayer cause * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution - * - * @throws Exception */ void now(Player entity, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); @@ -89,8 +85,6 @@ public interface ITeleport { * @param cause - The reported teleportPlayer cause. * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution - * - * @throws Exception */ void teleport(Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); @@ -116,8 +110,6 @@ public interface ITeleport { * @param cause - The reported teleportPlayer cause * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution - * - * @throws Exception */ void teleport(Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); @@ -145,8 +137,6 @@ public interface ITeleport { * @param cause - The reported teleportPlayer cause * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution - * - * @throws Exception */ void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); @@ -174,8 +164,6 @@ public interface ITeleport { * @param cause - The reported teleportPlayer cause * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution - * - * @throws Exception */ void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); @@ -199,8 +187,6 @@ public interface ITeleport { * @param cause - The reported teleportPlayer cause * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution - * - * @throws Exception */ void respawn(final Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); @@ -228,8 +214,6 @@ public interface ITeleport { * @param cause - The reported teleportPlayer cause * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution - * - * @throws Exception */ void warp(IUser otherUser, String warp, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); @@ -251,8 +235,6 @@ public interface ITeleport { * @param chargeFor - What the user will be charged if teleportPlayer is successful * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution - * - * @throws Exception */ void back(Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future); @@ -282,8 +264,6 @@ public interface ITeleport { * @param chargeFor - What the {@code teleporter} will be charged if teleportPlayer is successful * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution - * - * @throws Exception */ void back(IUser teleporter, Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future); @@ -302,8 +282,6 @@ public interface ITeleport { * * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution - * - * @throws Exception */ void back(CompletableFuture exceptionFuture, CompletableFuture future); From 24cd053ce5b6390454fb9e7035a388dacf88e3d3 Mon Sep 17 00:00:00 2001 From: JRoy Date: Sat, 4 Apr 2020 16:36:14 -0400 Subject: [PATCH 09/36] Fix invoking via async context --- .../src/com/earth2me/essentials/Teleport.java | 14 +++++++++++++- .../src/net/ess3/api/events/UserTeleportEvent.java | 4 +++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java index a05d70891da..869f2e185c4 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -20,6 +20,7 @@ import java.util.Calendar; import java.util.GregorianCalendar; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import static com.earth2me.essentials.I18n.tl; @@ -175,7 +176,18 @@ protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, C future.complete(false); return; } - teleportee.getBase().eject(); + CompletableFuture dismountFuture = new CompletableFuture<>(); + Bukkit.getScheduler().runTask(ess, () -> { + teleportee.getBase().eject(); + dismountFuture.complete(new Object()); + }); + try { + dismountFuture.get(); //EntityDismountEvent requires sync context we also want to wait for it to finish + } catch (InterruptedException | ExecutionException e) { + exceptionFuture.complete(e); + future.complete(false); + return; + } } teleportee.setLastLocation(); PaperLib.getChunkAtAsync(target.getLocation()).thenAccept(chunk -> { diff --git a/Essentials/src/net/ess3/api/events/UserTeleportEvent.java b/Essentials/src/net/ess3/api/events/UserTeleportEvent.java index 8410aace281..cd9ae33deb3 100644 --- a/Essentials/src/net/ess3/api/events/UserTeleportEvent.java +++ b/Essentials/src/net/ess3/api/events/UserTeleportEvent.java @@ -1,11 +1,12 @@ package net.ess3.api.events; import net.ess3.api.IUser; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; /** * Called when the player teleports */ @@ -18,6 +19,7 @@ public class UserTeleportEvent extends Event implements Cancellable { private boolean cancelled = false; public UserTeleportEvent(IUser user, TeleportCause cause, Location target) { + super(!Bukkit.isPrimaryThread()); this.user = user; this.cause = cause; this.target = target; From f1e3bf72ed0a60e67da37e79a156e88a0ce37a76 Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 6 Apr 2020 15:34:25 -0400 Subject: [PATCH 10/36] Fix /jail using deprecated method --- .../commands/Commandtogglejail.java | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java b/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java index 80b3d343ba8..25aba5d7f90 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java @@ -44,23 +44,33 @@ public void run(final Server server, final CommandSource sender, final String co ess.getServer().getPluginManager().callEvent(event); if (!event.isCancelled()) { + long preTimeDiff = 0; + if (args.length > 2) { + final String time = getFinalArg(args, 2); + preTimeDiff = DateUtil.parseDateDiff(time, true); + + } + final long timeDiff = preTimeDiff; + CompletableFuture future = new CompletableFuture<>(); + future.thenAccept(success -> { + if (success) { + player.setJailed(true); + player.sendMessage(tl("userJailed")); + player.setJail(null); + player.setJail(args[1]); + if (args.length > 2) { + player.setJailTimeout(timeDiff); + } + sender.sendMessage((timeDiff > 0 ? tl("playerJailedFor", player.getName(), DateUtil.formatDateDiff(timeDiff)) : tl("playerJailed", player.getName()))); + } + }); if (player.getBase().isOnline()) { - ess.getJails().sendToJail(player, args[1]); + ess.getJails().sendToJail(player, args[1], getNewExceptionFuture(sender, commandLabel), future); } else { // Check if jail exists ess.getJails().getJail(args[1]); + future.complete(true); } - player.setJailed(true); - player.sendMessage(tl("userJailed")); - player.setJail(null); - player.setJail(args[1]); - long timeDiff = 0; - if (args.length > 2) { - final String time = getFinalArg(args, 2); - timeDiff = DateUtil.parseDateDiff(time, true); - player.setJailTimeout(timeDiff); - } - sender.sendMessage((timeDiff > 0 ? tl("playerJailedFor", player.getName(), DateUtil.formatDateDiff(timeDiff)) : tl("playerJailed", player.getName()))); } return; } From ca9ebc1f5c5ff85af696ce569bd232465ed6d69a Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 6 Apr 2020 15:45:00 -0400 Subject: [PATCH 11/36] Fix jail join handler using deprecated method --- .../src/com/earth2me/essentials/Jails.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Jails.java b/Essentials/src/com/earth2me/essentials/Jails.java index 6836e9cc5c1..b3c192cf24b 100644 --- a/Essentials/src/com/earth2me/essentials/Jails.java +++ b/Essentials/src/com/earth2me/essentials/Jails.java @@ -274,16 +274,24 @@ public void onJailPlayerJoin(final PlayerJoinEvent event) { return; } - try { - sendToJail(user, user.getJail()); - } catch (Exception ex) { + CompletableFuture eFuture = new CompletableFuture<>(); + eFuture.thenAccept(ex -> { if (ess.getSettings().isDebug()) { LOGGER.log(Level.INFO, tl("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage()), ex); } else { LOGGER.log(Level.INFO, tl("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage())); } + }); + CompletableFuture future = new CompletableFuture<>(); + future.thenAccept(success -> { + user.sendMessage(tl("jailMessage")); + }); + + try { + sendToJail(user, user.getJail(), eFuture, future); + } catch (Exception ex) { + eFuture.complete(ex); } - user.sendMessage(tl("jailMessage")); } } } From 30ce336b7f3ccd5874b4b2b5185676fc55d02d73 Mon Sep 17 00:00:00 2001 From: JRoy Date: Fri, 10 Apr 2020 14:53:22 -0400 Subject: [PATCH 12/36] Rename all teleport classes to indicate async --- .../{Teleport.java => AsyncTeleport.java} | 6 ++--- .../src/com/earth2me/essentials/IUser.java | 4 ++-- .../earth2me/essentials/TimedTeleport.java | 4 ++-- .../src/com/earth2me/essentials/User.java | 6 ++--- .../{ITeleport.java => IAsyncTeleport.java} | 24 +++++++++---------- .../essentials/commands/Commandtpa.java | 6 ++--- .../essentials/commands/Commandtpaccept.java | 10 ++++---- .../src/net/ess3/api/IAsyncTeleport.java | 6 +++++ Essentials/src/net/ess3/api/ITeleport.java | 6 ----- 9 files changed, 36 insertions(+), 36 deletions(-) rename Essentials/src/com/earth2me/essentials/{Teleport.java => AsyncTeleport.java} (99%) rename Essentials/src/com/earth2me/essentials/api/{ITeleport.java => IAsyncTeleport.java} (90%) create mode 100644 Essentials/src/net/ess3/api/IAsyncTeleport.java delete mode 100644 Essentials/src/net/ess3/api/ITeleport.java diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/AsyncTeleport.java similarity index 99% rename from Essentials/src/com/earth2me/essentials/Teleport.java rename to Essentials/src/com/earth2me/essentials/AsyncTeleport.java index 869f2e185c4..67b37705192 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/AsyncTeleport.java @@ -5,7 +5,7 @@ import com.earth2me.essentials.utils.LocationUtil; import io.papermc.lib.PaperLib; import net.ess3.api.IEssentials; -import net.ess3.api.ITeleport; +import net.ess3.api.IAsyncTeleport; import net.ess3.api.IUser; import net.ess3.api.InvalidWorldException; import net.ess3.api.events.UserTeleportEvent; @@ -25,14 +25,14 @@ import static com.earth2me.essentials.I18n.tl; -public class Teleport implements ITeleport { +public class AsyncTeleport implements IAsyncTeleport { private final IUser teleportOwner; private final IEssentials ess; private TimedTeleport timedTeleport; private TeleportType tpType; - public Teleport(IUser user, IEssentials ess) { + public AsyncTeleport(IUser user, IEssentials ess) { this.teleportOwner = user; this.ess = ess; tpType = TeleportType.NORMAL; diff --git a/Essentials/src/com/earth2me/essentials/IUser.java b/Essentials/src/com/earth2me/essentials/IUser.java index 0650cf3eb18..c5cbdf81a31 100644 --- a/Essentials/src/com/earth2me/essentials/IUser.java +++ b/Essentials/src/com/earth2me/essentials/IUser.java @@ -1,7 +1,7 @@ package com.earth2me.essentials; import com.earth2me.essentials.commands.IEssentialsCommand; -import net.ess3.api.ITeleport; +import net.ess3.api.IAsyncTeleport; import net.ess3.api.MaxMoneyException; import net.ess3.api.events.AfkStatusChangeEvent; import org.bukkit.Location; @@ -54,7 +54,7 @@ public interface IUser { */ boolean hasOutstandingTeleportRequest(); - ITeleport getTeleport(); + IAsyncTeleport getTeleport(); BigDecimal getMoney(); diff --git a/Essentials/src/com/earth2me/essentials/TimedTeleport.java b/Essentials/src/com/earth2me/essentials/TimedTeleport.java index 35a23ae5f8e..039f3e8088e 100644 --- a/Essentials/src/com/earth2me/essentials/TimedTeleport.java +++ b/Essentials/src/com/earth2me/essentials/TimedTeleport.java @@ -15,7 +15,7 @@ public class TimedTeleport implements Runnable { private static final double MOVE_CONSTANT = 0.3; private final IUser teleportOwner; private final IEssentials ess; - private final Teleport teleport; + private final AsyncTeleport teleport; private final UUID timer_teleportee; private int timer_task; private final long timer_started; // time this task was initiated @@ -33,7 +33,7 @@ public class TimedTeleport implements Runnable { private final Trade timer_chargeFor; private final TeleportCause timer_cause; - TimedTeleport(IUser user, IEssentials ess, Teleport teleport, long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) { + TimedTeleport(IUser user, IEssentials ess, AsyncTeleport teleport, long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) { this.teleportOwner = user; this.ess = ess; this.teleport = teleport; diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index 0e5043f6df6..e2075b95f7d 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -43,7 +43,7 @@ public class User extends UserData implements Comparable, IMessageRecipien private transient boolean teleportRequestHere; private transient Location teleportLocation; private transient boolean vanished; - private transient final Teleport teleport; + private transient final AsyncTeleport teleport; private transient long teleportRequestTime; private transient long lastOnlineActivity; private transient long lastThrottledAction; @@ -64,7 +64,7 @@ public class User extends UserData implements Comparable, IMessageRecipien public User(final Player base, final IEssentials ess) { super(base, ess); - teleport = new Teleport(this, ess); + teleport = new AsyncTeleport(this, ess); if (isAfk()) { afkPosition = this.getLocation(); } @@ -407,7 +407,7 @@ public String getDisplayName() { } @Override - public Teleport getTeleport() { + public AsyncTeleport getTeleport() { return teleport; } diff --git a/Essentials/src/com/earth2me/essentials/api/ITeleport.java b/Essentials/src/com/earth2me/essentials/api/IAsyncTeleport.java similarity index 90% rename from Essentials/src/com/earth2me/essentials/api/ITeleport.java rename to Essentials/src/com/earth2me/essentials/api/IAsyncTeleport.java index 59a946ad4ac..f6dca4d46c5 100644 --- a/Essentials/src/com/earth2me/essentials/api/ITeleport.java +++ b/Essentials/src/com/earth2me/essentials/api/IAsyncTeleport.java @@ -9,11 +9,11 @@ import java.util.concurrent.CompletableFuture; -public interface ITeleport { +public interface IAsyncTeleport { /** * Used to skip teleportPlayer delay when teleporting someone to a location or player. * - * @deprecated Use {@link ITeleport#now(Location, boolean, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * @deprecated Use {@link IAsyncTeleport#now(Location, boolean, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} * * @param loc - Where should the player end up * @param cooldown - If cooldown should be enforced @@ -38,7 +38,7 @@ public interface ITeleport { /** * Used to skip teleportPlayer delay when teleporting someone to a location or player. * - * @deprecated Use {@link ITeleport#now(Player, boolean, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * @deprecated Use {@link IAsyncTeleport#now(Player, boolean, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} * * @param entity - Where should the player end up * @param cooldown - If cooldown should be enforced @@ -66,7 +66,7 @@ public interface ITeleport { /** * Teleport a player to a specific location * - * @deprecated {@link ITeleport#teleport(Location, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * @deprecated {@link IAsyncTeleport#teleport(Location, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} * * @param loc - Where should the player end up * @param chargeFor - What the user will be charged if teleportPlayer is successful @@ -91,7 +91,7 @@ public interface ITeleport { /** * Teleport a player to a specific player * - * @deprecated Use {@link ITeleport#teleport(Player, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * @deprecated Use {@link IAsyncTeleport#teleport(Player, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} * * @param entity - Where should the player end up * @param chargeFor - What the user will be charged if teleportPlayer is successful @@ -116,7 +116,7 @@ public interface ITeleport { /** * Teleport a player to a specific location * - * @deprecated Use {@link ITeleport#teleportPlayer(IUser, Location, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * @deprecated Use {@link IAsyncTeleport#teleportPlayer(IUser, Location, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} * * @param otherUser - Which user will be teleported * @param loc - Where should the player end up @@ -143,7 +143,7 @@ public interface ITeleport { /** * Teleport a player to a specific player * - * @deprecated Use {@link ITeleport#teleportPlayer(IUser, Player, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * @deprecated Use {@link IAsyncTeleport#teleportPlayer(IUser, Player, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} * * @param otherUser - Which user will be teleported * @param entity - Where should the player end up @@ -170,7 +170,7 @@ public interface ITeleport { /** * Teleport wrapper used to handle tp fallback on /jail and /home * - * @deprecated Use {@link ITeleport#respawn(Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * @deprecated Use {@link IAsyncTeleport#respawn(Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} * * @param chargeFor - What the user will be charged if teleportPlayer is successful * @param cause - The reported teleportPlayer cause @@ -193,7 +193,7 @@ public interface ITeleport { /** * Teleport wrapper used to handle /warp teleports * - * @deprecated Use {@link ITeleport#warp(IUser, String, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} + * @deprecated Use {@link IAsyncTeleport#warp(IUser, String, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} * * @param otherUser - Which user will be teleported * @param warp - The name of the warp the user will be teleported too. @@ -220,7 +220,7 @@ public interface ITeleport { /** * Teleport wrapper used to handle /back teleports * - * @deprecated {@link ITeleport#back(Trade, CompletableFuture, CompletableFuture)} + * @deprecated {@link IAsyncTeleport#back(Trade, CompletableFuture, CompletableFuture)} * * @param chargeFor - What the user will be charged if teleportPlayer is successful * @@ -243,7 +243,7 @@ public interface ITeleport { * are executed by a different player with this * instance of teleport as a target. * - * @deprecated Use {@link ITeleport#back(IUser, Trade, CompletableFuture, CompletableFuture)} + * @deprecated Use {@link IAsyncTeleport#back(IUser, Trade, CompletableFuture, CompletableFuture)} * * @param teleporter - The user performing the /back command. * This value may be {@code null} to indicate console. @@ -270,7 +270,7 @@ public interface ITeleport { /** * Teleport wrapper used to handle throwing user home after a jail sentence * - * @deprecated Use {@link ITeleport#back(CompletableFuture, CompletableFuture)} + * @deprecated Use {@link IAsyncTeleport#back(CompletableFuture, CompletableFuture)} * * @throws Exception */ diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java index e44aa839270..da94ef73d8e 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.commands; -import com.earth2me.essentials.Teleport; +import com.earth2me.essentials.AsyncTeleport; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import net.ess3.api.events.TPARequestEvent; @@ -42,8 +42,8 @@ public void run(Server server, User user, String commandLabel, String[] args) th } if (player.isAutoTeleportEnabled() && !player.isIgnoredPlayer(user)) { final Trade charge = new Trade(this.getName(), ess); - Teleport teleport = user.getTeleport(); - teleport.setTpType(Teleport.TeleportType.TPA); + AsyncTeleport teleport = user.getTeleport(); + teleport.setTpType(AsyncTeleport.TeleportType.TPA); CompletableFuture future = new CompletableFuture<>(); future.thenAccept(success -> { if (success) { diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java index c5a59d7e5f3..5d2a41158af 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java @@ -1,6 +1,6 @@ package com.earth2me.essentials.commands; -import com.earth2me.essentials.Teleport; +import com.earth2me.essentials.AsyncTeleport; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import org.bukkit.Location; @@ -64,8 +64,8 @@ public void run(final Server server, final User user, final String commandLabel, }); if (user.isTpRequestHere()) { final Location loc = user.getTpRequestLocation(); - Teleport teleport = requester.getTeleport(); - teleport.setTpType(Teleport.TeleportType.TPA); + AsyncTeleport teleport = requester.getTeleport(); + teleport.setTpType(AsyncTeleport.TeleportType.TPA); future.thenAccept(success -> { if (success) { requester.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); @@ -73,8 +73,8 @@ public void run(final Server server, final User user, final String commandLabel, }); teleport.teleportPlayer(user, user.getTpRequestLocation(), charge, TeleportCause.COMMAND, eFuture, future); } else { - Teleport teleport = requester.getTeleport(); - teleport.setTpType(Teleport.TeleportType.TPA); + AsyncTeleport teleport = requester.getTeleport(); + teleport.setTpType(AsyncTeleport.TeleportType.TPA); teleport.teleport(user.getBase(), charge, TeleportCause.COMMAND, eFuture, future); } } diff --git a/Essentials/src/net/ess3/api/IAsyncTeleport.java b/Essentials/src/net/ess3/api/IAsyncTeleport.java new file mode 100644 index 00000000000..81f30fc0332 --- /dev/null +++ b/Essentials/src/net/ess3/api/IAsyncTeleport.java @@ -0,0 +1,6 @@ +package net.ess3.api; + + +public interface IAsyncTeleport extends com.earth2me.essentials.api.IAsyncTeleport { + +} diff --git a/Essentials/src/net/ess3/api/ITeleport.java b/Essentials/src/net/ess3/api/ITeleport.java deleted file mode 100644 index 65c68076131..00000000000 --- a/Essentials/src/net/ess3/api/ITeleport.java +++ /dev/null @@ -1,6 +0,0 @@ -package net.ess3.api; - - -public interface ITeleport extends com.earth2me.essentials.api.ITeleport { - -} From b2f6ce9100a6ad613611bb1976e50a84dd231127 Mon Sep 17 00:00:00 2001 From: JRoy Date: Fri, 10 Apr 2020 14:56:01 -0400 Subject: [PATCH 13/36] Remove deprecated methods --- .../earth2me/essentials/AsyncTeleport.java | 137 ---------------- .../essentials/api/IAsyncTeleport.java | 154 ------------------ 2 files changed, 291 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/AsyncTeleport.java b/Essentials/src/com/earth2me/essentials/AsyncTeleport.java index 67b37705192..b878dbabc7b 100644 --- a/Essentials/src/com/earth2me/essentials/AsyncTeleport.java +++ b/Essentials/src/com/earth2me/essentials/AsyncTeleport.java @@ -112,17 +112,6 @@ private void warnUser(final IUser user, final double delay) { user.sendMessage(tl("dontMoveMessage", DateUtil.formatDateDiff(c.getTimeInMillis()))); } - //The now function is used when you want to skip tp delay when teleporting someone to a location or player. - @Override - @Deprecated - public void now(Location loc, boolean cooldown, TeleportCause cause) throws Exception { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - now(loc, cooldown, cause, exceptionFuture, future); - if (!future.get()) { - throw exceptionFuture.get(); - } - } @Override public void now(Location loc, boolean cooldown, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { @@ -134,17 +123,6 @@ public void now(Location loc, boolean cooldown, TeleportCause cause, Completable nowAsync(teleportOwner, target, cause, exceptionFuture, future); } - @Override - @Deprecated - public void now(Player entity, boolean cooldown, TeleportCause cause) throws Exception { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - now(entity, cooldown, cause, exceptionFuture, future); - if (!future.get()) { - throw exceptionFuture.get(); - } - } - @Override public void now(Player entity, boolean cooldown, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { if (cooldown && cooldown(false, exceptionFuture)) { @@ -228,77 +206,22 @@ protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, C }); } - //The teleportPlayer function is used when you want to normally teleportPlayer someone to a location or player. - //This method is nolonger used internally and will be removed. - @Deprecated - @Override - public void teleport(Location loc, Trade chargeFor) throws Exception { - teleport(loc, chargeFor, TeleportCause.PLUGIN); - } - - @Override - @Deprecated - public void teleport(Location loc, Trade chargeFor, TeleportCause cause) throws Exception { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - teleport(loc, chargeFor, cause, exceptionFuture, future); - if (!future.get()) { - throw exceptionFuture.get(); - } - } - @Override public void teleport(Location loc, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { teleport(teleportOwner, new LocationTarget(loc), chargeFor, cause, exceptionFuture, future); } - //This is used when teleporting to a player - @Override - @Deprecated - public void teleport(Player entity, Trade chargeFor, TeleportCause cause) throws Exception { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - teleport(entity, chargeFor, cause, exceptionFuture, future); - if (!future.get()) { - throw exceptionFuture.get(); - } - } - @Override public void teleport(Player entity, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { teleportOwner.sendMessage(tl("teleportToPlayer", entity.getDisplayName())); teleport(teleportOwner, new PlayerTarget(entity), chargeFor, cause, exceptionFuture, future); } - //This is used when teleporting to stored location - @Override - @Deprecated - public void teleportPlayer(IUser teleportee, Location loc, Trade chargeFor, TeleportCause cause) throws Exception { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - teleportPlayer(teleportee, loc, chargeFor, cause, exceptionFuture, future); - if (!future.get()) { - throw exceptionFuture.get(); - } - } - @Override public void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { teleport(otherUser, new LocationTarget(loc), chargeFor, cause, exceptionFuture, future); } - //This is used on /tphere - @Override - @Deprecated - public void teleportPlayer(IUser teleportee, Player entity, Trade chargeFor, TeleportCause cause) throws Exception { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - teleportPlayer(teleportee, entity, chargeFor, cause, exceptionFuture, future); - if (!future.get()) { - throw exceptionFuture.get(); - } - } - @Override public void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { ITarget target = new PlayerTarget(entity); @@ -403,18 +326,6 @@ private void teleportOther(IUser teleporter, IUser teleportee, ITarget target, T initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false); } - //The respawn function is a wrapper used to handle tp fallback, on /jail and /home - @Override - @Deprecated - public void respawn(final Trade chargeFor, TeleportCause cause) throws Exception { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - respawn(chargeFor, cause, exceptionFuture, future); - if (!future.get()) { - throw exceptionFuture.get(); - } - } - @Override public void respawn(Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { double delay = ess.getSettings().getTeleportDelay(); @@ -461,18 +372,6 @@ void respawnNow(IUser teleportee, TeleportCause cause, CompletableFuture exceptionFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - warp(teleportee, warp, chargeFor, cause, exceptionFuture, future); - if (!future.get()) { - throw exceptionFuture.get(); - } - } - @Override public void warp(IUser otherUser, String warp, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { UserWarpEvent event = new UserWarpEvent(otherUser, warp, chargeFor); @@ -497,35 +396,11 @@ public void warp(IUser otherUser, String warp, Trade chargeFor, TeleportCause ca teleport(otherUser, new LocationTarget(loc), chargeFor, cause, exceptionFuture, future); } - //The back function is a wrapper used to teleportPlayer a player /back to their previous location. - @Override - @Deprecated - public void back(Trade chargeFor) throws Exception { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - back(chargeFor, exceptionFuture, future); - if (!future.get()) { - throw exceptionFuture.get(); - } - } - @Override public void back(Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future) { back(teleportOwner, chargeFor, exceptionFuture, future); } - //This function is a wrapper over the other back function for cases where another player performs back for them - @Override - @Deprecated - public void back(IUser teleporter, Trade chargeFor) throws Exception { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - back(teleporter, chargeFor, exceptionFuture, future); - if (!future.get()) { - throw exceptionFuture.get(); - } - } - @Override public void back(IUser teleporter, Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future) { tpType = TeleportType.BACK; @@ -534,18 +409,6 @@ public void back(IUser teleporter, Trade chargeFor, CompletableFuture teleportOther(teleporter, teleportOwner, new LocationTarget(loc), chargeFor, TeleportCause.COMMAND, exceptionFuture, future); } - //This function is used to throw a user back after a jail sentence - @Override - @Deprecated - public void back() throws Exception { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - back(exceptionFuture, future); - if (!future.get()) { - throw exceptionFuture.get(); - } - } - @Override public void back(CompletableFuture exceptionFuture, CompletableFuture future) { nowAsync(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()), TeleportCause.COMMAND, exceptionFuture, future); diff --git a/Essentials/src/com/earth2me/essentials/api/IAsyncTeleport.java b/Essentials/src/com/earth2me/essentials/api/IAsyncTeleport.java index f6dca4d46c5..9a51a2128c7 100644 --- a/Essentials/src/com/earth2me/essentials/api/IAsyncTeleport.java +++ b/Essentials/src/com/earth2me/essentials/api/IAsyncTeleport.java @@ -10,19 +10,6 @@ public interface IAsyncTeleport { - /** - * Used to skip teleportPlayer delay when teleporting someone to a location or player. - * - * @deprecated Use {@link IAsyncTeleport#now(Location, boolean, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} - * - * @param loc - Where should the player end up - * @param cooldown - If cooldown should be enforced - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void now(Location loc, boolean cooldown, PlayerTeleportEvent.TeleportCause cause) throws Exception; /** * Used to skip teleportPlayer delay when teleporting someone to a location or player. @@ -35,20 +22,6 @@ public interface IAsyncTeleport { */ void now(Location loc, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); - /** - * Used to skip teleportPlayer delay when teleporting someone to a location or player. - * - * @deprecated Use {@link IAsyncTeleport#now(Player, boolean, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} - * - * @param entity - Where should the player end up - * @param cooldown - If cooldown should be enforced - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void now(Player entity, boolean cooldown, PlayerTeleportEvent.TeleportCause cause) throws Exception; - /** * Used to skip teleportPlayer delay when teleporting someone to a location or player. * @@ -60,23 +33,6 @@ public interface IAsyncTeleport { */ void now(Player entity, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); - @Deprecated - void teleport(Location loc, Trade chargeFor) throws Exception; - - /** - * Teleport a player to a specific location - * - * @deprecated {@link IAsyncTeleport#teleport(Location, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} - * - * @param loc - Where should the player end up - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void teleport(Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; - /** * Teleport a player to a specific location * @@ -88,20 +44,6 @@ public interface IAsyncTeleport { */ void teleport(Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); - /** - * Teleport a player to a specific player - * - * @deprecated Use {@link IAsyncTeleport#teleport(Player, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} - * - * @param entity - Where should the player end up - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void teleport(Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; - /** * Teleport a player to a specific player * @@ -113,21 +55,6 @@ public interface IAsyncTeleport { */ void teleport(Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); - /** - * Teleport a player to a specific location - * - * @deprecated Use {@link IAsyncTeleport#teleportPlayer(IUser, Location, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} - * - * @param otherUser - Which user will be teleported - * @param loc - Where should the player end up - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; - /** * Teleport a player to a specific location * @@ -140,21 +67,6 @@ public interface IAsyncTeleport { */ void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); - /** - * Teleport a player to a specific player - * - * @deprecated Use {@link IAsyncTeleport#teleportPlayer(IUser, Player, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} - * - * @param otherUser - Which user will be teleported - * @param entity - Where should the player end up - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; - /** * Teleport a player to a specific player * @@ -167,19 +79,6 @@ public interface IAsyncTeleport { */ void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); - /** - * Teleport wrapper used to handle tp fallback on /jail and /home - * - * @deprecated Use {@link IAsyncTeleport#respawn(Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} - * - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void respawn(final Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; - /** * Teleport wrapper used to handle tp fallback on /jail and /home * @@ -190,21 +89,6 @@ public interface IAsyncTeleport { */ void respawn(final Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); - /** - * Teleport wrapper used to handle /warp teleports - * - * @deprecated Use {@link IAsyncTeleport#warp(IUser, String, Trade, PlayerTeleportEvent.TeleportCause, CompletableFuture, CompletableFuture)} - * - * @param otherUser - Which user will be teleported - * @param warp - The name of the warp the user will be teleported too. - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void warp(IUser otherUser, String warp, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; - /** * Teleport wrapper used to handle /warp teleports * @@ -217,18 +101,6 @@ public interface IAsyncTeleport { */ void warp(IUser otherUser, String warp, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); - /** - * Teleport wrapper used to handle /back teleports - * - * @deprecated {@link IAsyncTeleport#back(Trade, CompletableFuture, CompletableFuture)} - * - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * - * @throws Exception - */ - @Deprecated - void back(Trade chargeFor) throws Exception; - /** * Teleport wrapper used to handle /back teleports * @@ -238,22 +110,6 @@ public interface IAsyncTeleport { */ void back(Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future); - /** - * Teleport wrapper used to handle /back teleports that - * are executed by a different player with this - * instance of teleport as a target. - * - * @deprecated Use {@link IAsyncTeleport#back(IUser, Trade, CompletableFuture, CompletableFuture)} - * - * @param teleporter - The user performing the /back command. - * This value may be {@code null} to indicate console. - * @param chargeFor - What the {@code teleporter} will be charged if teleportPlayer is successful - * - * @throws Exception - */ - @Deprecated - void back(IUser teleporter, Trade chargeFor) throws Exception; - /** * Teleport wrapper used to handle /back teleports that * are executed by a different player with this @@ -267,16 +123,6 @@ public interface IAsyncTeleport { */ void back(IUser teleporter, Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future); - /** - * Teleport wrapper used to handle throwing user home after a jail sentence - * - * @deprecated Use {@link IAsyncTeleport#back(CompletableFuture, CompletableFuture)} - * - * @throws Exception - */ - @Deprecated - void back() throws Exception; - /** * Teleport wrapper used to handle throwing user home after a jail sentence * From 8e87db821cfb9f4e0dfb95895ef68d10bcf491d0 Mon Sep 17 00:00:00 2001 From: JRoy Date: Fri, 10 Apr 2020 15:25:12 -0400 Subject: [PATCH 14/36] Add (and deprecate) old teleport api --- .../earth2me/essentials/AsyncTeleport.java | 6 +- .../essentials/AsyncTimedTeleport.java | 144 +++++++ .../src/com/earth2me/essentials/IUser.java | 38 +- .../src/com/earth2me/essentials/Jails.java | 2 +- .../src/com/earth2me/essentials/Teleport.java | 379 ++++++++++++++++++ .../earth2me/essentials/TimedTeleport.java | 235 ++++++----- .../src/com/earth2me/essentials/User.java | 16 +- .../earth2me/essentials/api/ITeleport.java | 147 +++++++ .../essentials/commands/Commandback.java | 6 +- .../essentials/commands/Commandhome.java | 6 +- .../essentials/commands/Commandjump.java | 2 +- .../commands/Commandtogglejail.java | 2 +- .../essentials/commands/Commandtop.java | 2 +- .../essentials/commands/Commandtp.java | 12 +- .../essentials/commands/Commandtpa.java | 2 +- .../essentials/commands/Commandtpaccept.java | 4 +- .../essentials/commands/Commandtpall.java | 2 +- .../essentials/commands/Commandtphere.java | 2 +- .../essentials/commands/Commandtpo.java | 4 +- .../essentials/commands/Commandtpoffline.java | 2 +- .../essentials/commands/Commandtpohere.java | 2 +- .../essentials/commands/Commandtppos.java | 4 +- .../essentials/commands/Commandwarp.java | 4 +- .../essentials/commands/Commandworld.java | 2 +- .../earth2me/essentials/signs/SignWarp.java | 2 +- .../src/net/ess3/api/IAsyncTeleport.java | 6 - Essentials/src/net/ess3/api/ITeleport.java | 6 + .../essentials/spawn/Commandspawn.java | 4 +- .../spawn/EssentialsSpawnPlayerListener.java | 2 +- 29 files changed, 862 insertions(+), 183 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java create mode 100644 Essentials/src/com/earth2me/essentials/Teleport.java create mode 100644 Essentials/src/com/earth2me/essentials/api/ITeleport.java delete mode 100644 Essentials/src/net/ess3/api/IAsyncTeleport.java create mode 100644 Essentials/src/net/ess3/api/ITeleport.java diff --git a/Essentials/src/com/earth2me/essentials/AsyncTeleport.java b/Essentials/src/com/earth2me/essentials/AsyncTeleport.java index b878dbabc7b..7910fbf8436 100644 --- a/Essentials/src/com/earth2me/essentials/AsyncTeleport.java +++ b/Essentials/src/com/earth2me/essentials/AsyncTeleport.java @@ -1,11 +1,11 @@ package com.earth2me.essentials; +import com.earth2me.essentials.api.IAsyncTeleport; import com.earth2me.essentials.commands.WarpNotFoundException; import com.earth2me.essentials.utils.DateUtil; import com.earth2me.essentials.utils.LocationUtil; import io.papermc.lib.PaperLib; import net.ess3.api.IEssentials; -import net.ess3.api.IAsyncTeleport; import net.ess3.api.IUser; import net.ess3.api.InvalidWorldException; import net.ess3.api.events.UserTeleportEvent; @@ -28,7 +28,7 @@ public class AsyncTeleport implements IAsyncTeleport { private final IUser teleportOwner; private final IEssentials ess; - private TimedTeleport timedTeleport; + private AsyncTimedTeleport timedTeleport; private TeleportType tpType; @@ -427,6 +427,6 @@ private void cancel(boolean notifyUser) { } private void initTimer(long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) { - timedTeleport = new TimedTeleport(teleportOwner, ess, this, delay, teleportUser, target, chargeFor, cause, respawn); + timedTeleport = new AsyncTimedTeleport(teleportOwner, ess, this, delay, teleportUser, target, chargeFor, cause, respawn); } } diff --git a/Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java b/Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java new file mode 100644 index 00000000000..a9960666459 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java @@ -0,0 +1,144 @@ +package com.earth2me.essentials; + +import net.ess3.api.IEssentials; +import net.ess3.api.IUser; +import org.bukkit.Location; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; + +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +import static com.earth2me.essentials.I18n.tl; + + +public class AsyncTimedTeleport implements Runnable { + private static final double MOVE_CONSTANT = 0.3; + private final IUser teleportOwner; + private final IEssentials ess; + private final AsyncTeleport teleport; + private final UUID timer_teleportee; + private int timer_task; + private final long timer_started; // time this task was initiated + private final long timer_delay; // how long to delay the teleportPlayer + private double timer_health; + // note that I initially stored a clone of the location for reference, but... + // when comparing locations, I got incorrect mismatches (rounding errors, looked like) + // so, the X/Y/Z values are stored instead and rounded off + private final long timer_initX; + private final long timer_initY; + private final long timer_initZ; + private final ITarget timer_teleportTarget; + private final boolean timer_respawn; + private final boolean timer_canMove; + private final Trade timer_chargeFor; + private final TeleportCause timer_cause; + + AsyncTimedTeleport(IUser user, IEssentials ess, AsyncTeleport teleport, long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) { + this.teleportOwner = user; + this.ess = ess; + this.teleport = teleport; + this.timer_started = System.currentTimeMillis(); + this.timer_delay = delay; + this.timer_health = teleportUser.getBase().getHealth(); + this.timer_initX = Math.round(teleportUser.getBase().getLocation().getX() * MOVE_CONSTANT); + this.timer_initY = Math.round(teleportUser.getBase().getLocation().getY() * MOVE_CONSTANT); + this.timer_initZ = Math.round(teleportUser.getBase().getLocation().getZ() * MOVE_CONSTANT); + this.timer_teleportee = teleportUser.getBase().getUniqueId(); + this.timer_teleportTarget = target; + this.timer_chargeFor = chargeFor; + this.timer_cause = cause; + this.timer_respawn = respawn; + this.timer_canMove = user.isAuthorized("essentials.teleport.timer.move"); + + timer_task = ess.runTaskTimerAsynchronously(this, 20, 20).getTaskId(); + } + + @Override + public void run() { + + if (teleportOwner == null || !teleportOwner.getBase().isOnline() || teleportOwner.getBase().getLocation() == null) { + cancelTimer(false); + return; + } + + final IUser teleportUser = ess.getUser(this.timer_teleportee); + + if (teleportUser == null || !teleportUser.getBase().isOnline()) { + cancelTimer(false); + return; + } + + final Location currLocation = teleportUser.getBase().getLocation(); + if (currLocation == null) { + cancelTimer(false); + return; + } + + if (!timer_canMove && (Math.round(currLocation.getX() * MOVE_CONSTANT) != timer_initX || Math.round(currLocation.getY() * MOVE_CONSTANT) != timer_initY || Math.round(currLocation.getZ() * MOVE_CONSTANT) != timer_initZ || teleportUser.getBase().getHealth() < timer_health)) { + // user moved, cancelTimer teleportPlayer + cancelTimer(true); + return; + } + + class DelayedTeleportTask implements Runnable { + @Override + public void run() { + + timer_health = teleportUser.getBase().getHealth(); // in case user healed, then later gets injured + final long now = System.currentTimeMillis(); + if (now > timer_started + timer_delay) { + try { + teleport.cooldown(false); + } catch (Exception ex) { + teleportOwner.sendMessage(tl("cooldownWithMessage", ex.getMessage())); + if (teleportOwner != teleportUser) { + teleportUser.sendMessage(tl("cooldownWithMessage", ex.getMessage())); + } + } + try { + cancelTimer(false); + teleportUser.sendMessage(tl("teleportationCommencing")); + + try { + CompletableFuture future = new CompletableFuture<>(); + CompletableFuture future1 = new CompletableFuture<>(); + if (timer_chargeFor != null) { + timer_chargeFor.isAffordableFor(teleportOwner); + } + if (timer_respawn) { + teleport.respawnNow(teleportUser, timer_cause, future, future1); + } else { + teleport.nowAsync(teleportUser, timer_teleportTarget, timer_cause, future, future1); + } + if (timer_chargeFor != null) { + timer_chargeFor.charge(teleportOwner); + } + } catch (Exception ignored) {} + + } catch (Exception ex) { + ess.showError(teleportOwner.getSource(), ex, "\\ teleport"); + } + } + } + } + ess.scheduleSyncDelayedTask(new DelayedTeleportTask()); + } + + //If we need to cancelTimer a pending teleportPlayer call this method + void cancelTimer(boolean notifyUser) { + if (timer_task == -1) { + return; + } + try { + ess.getServer().getScheduler().cancelTask(timer_task); + if (notifyUser) { + teleportOwner.sendMessage(tl("pendingTeleportCancelled")); + if (timer_teleportee != null && !timer_teleportee.equals(teleportOwner.getBase().getUniqueId())) { + ess.getUser(timer_teleportee).sendMessage(tl("pendingTeleportCancelled")); + } + } + } finally { + timer_task = -1; + } + } +} diff --git a/Essentials/src/com/earth2me/essentials/IUser.java b/Essentials/src/com/earth2me/essentials/IUser.java index c5cbdf81a31..e5b627dcd8f 100644 --- a/Essentials/src/com/earth2me/essentials/IUser.java +++ b/Essentials/src/com/earth2me/essentials/IUser.java @@ -1,7 +1,8 @@ package com.earth2me.essentials; +import com.earth2me.essentials.api.IAsyncTeleport; +import com.earth2me.essentials.api.ITeleport; import com.earth2me.essentials.commands.IEssentialsCommand; -import net.ess3.api.IAsyncTeleport; import net.ess3.api.MaxMoneyException; import net.ess3.api.events.AfkStatusChangeEvent; import org.bukkit.Location; @@ -15,7 +16,6 @@ import java.util.Set; import java.util.regex.Pattern; - public interface IUser { boolean isAuthorized(String node); @@ -54,7 +54,13 @@ public interface IUser { */ boolean hasOutstandingTeleportRequest(); - IAsyncTeleport getTeleport(); + /** + * @deprecated This API is not asynchronous. Use {@link com.earth2me.essentials.api.IAsyncTeleport IAsyncTeleport} + */ + @Deprecated + ITeleport getTeleport(); + + IAsyncTeleport getAsyncTeleport(); BigDecimal getMoney(); @@ -70,8 +76,7 @@ public interface IUser { * supported plugins. Use isVanished() if you want to check if a user is vanished by Essentials. * * @return If the user is hidden or not - * - * @see isVanished + * @see IUser#isVanished() */ boolean isHidden(); @@ -99,8 +104,7 @@ public interface IUser { * plugin. * * @return If the user is vanished or not - * - * @see isHidden + * @see IUser#isHidden() */ boolean isVanished(); @@ -154,13 +158,13 @@ public interface IUser { Map getConfigMap(); Map getConfigMap(String node); - + Map getCommandCooldowns(); Date getCommandCooldownExpiry(String label); - + void addCommandCooldown(Pattern pattern, Date expiresAt, boolean save); - + boolean clearCommandCooldown(Pattern pattern); /* @@ -175,19 +179,19 @@ public interface IUser { String getAfkMessage(); void setAfkMessage(final String message); - + long getAfkSince(); - + boolean isAcceptingPay(); - + void setAcceptingPay(boolean acceptingPay); - + boolean isPromptingPayConfirm(); - + void setPromptingPayConfirm(boolean prompt); - + boolean isPromptingClearConfirm(); - + void setPromptingClearConfirm(boolean prompt); boolean isLastMessageReplyRecipient(); diff --git a/Essentials/src/com/earth2me/essentials/Jails.java b/Essentials/src/com/earth2me/essentials/Jails.java index b3c192cf24b..7878dd6fb8d 100644 --- a/Essentials/src/com/earth2me/essentials/Jails.java +++ b/Essentials/src/com/earth2me/essentials/Jails.java @@ -141,7 +141,7 @@ public void sendToJail(IUser user, String jail, CompletableFuture exc } future.complete(success); }); - user.getTeleport().now(loc, false, TeleportCause.COMMAND, exceptionFuture, future1); + user.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, exceptionFuture, future1); return; } user.setJail(jail); diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java new file mode 100644 index 00000000000..e6f4c6bde61 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -0,0 +1,379 @@ +package com.earth2me.essentials; + +import com.earth2me.essentials.utils.DateUtil; +import com.earth2me.essentials.utils.LocationUtil; +import io.papermc.lib.PaperLib; +import net.ess3.api.IEssentials; +import net.ess3.api.ITeleport; +import net.ess3.api.IUser; +import net.ess3.api.events.UserTeleportEvent; +import net.ess3.api.events.UserWarpEvent; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerRespawnEvent; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; + +import java.math.BigDecimal; +import java.util.Calendar; +import java.util.GregorianCalendar; + +import static com.earth2me.essentials.I18n.tl; + + +/** + * @deprecated This API is not asynchronous. Use {@link com.earth2me.essentials.AsyncTeleport AsyncTeleport} + */ +@Deprecated +public class Teleport implements ITeleport { + private final IUser teleportOwner; + private final IEssentials ess; + private TimedTeleport timedTeleport; + + private TeleportType tpType; + + @Deprecated + public Teleport(IUser user, IEssentials ess) { + this.teleportOwner = user; + this.ess = ess; + tpType = TeleportType.NORMAL; + } + + public enum TeleportType { + TPA, + BACK, + NORMAL + } + + @Deprecated + public void cooldown(boolean check) throws Exception { + final Calendar time = new GregorianCalendar(); + if (teleportOwner.getLastTeleportTimestamp() > 0) { + // Take the current time, and remove the delay from it. + final double cooldown = ess.getSettings().getTeleportCooldown(); + final Calendar earliestTime = new GregorianCalendar(); + earliestTime.add(Calendar.SECOND, -(int) cooldown); + earliestTime.add(Calendar.MILLISECOND, -(int) ((cooldown * 1000.0) % 1000.0)); + // This value contains the most recent time a teleportPlayer could have been used that would allow another use. + final long earliestLong = earliestTime.getTimeInMillis(); + + // When was the last teleportPlayer used? + final long lastTime = teleportOwner.getLastTeleportTimestamp(); + + if (lastTime > time.getTimeInMillis()) { + // This is to make sure time didn't get messed up on last teleportPlayer use. + // If this happens, let's give the user the benifit of the doubt. + teleportOwner.setLastTeleportTimestamp(time.getTimeInMillis()); + return; + } else if (lastTime > earliestLong + && cooldownApplies()) { + time.setTimeInMillis(lastTime); + time.add(Calendar.SECOND, (int) cooldown); + time.add(Calendar.MILLISECOND, (int) ((cooldown * 1000.0) % 1000.0)); + throw new Exception(tl("timeBeforeTeleport", DateUtil.formatDateDiff(time.getTimeInMillis()))); + } + } + // if justCheck is set, don't update lastTeleport; we're just checking + if (!check) { + teleportOwner.setLastTeleportTimestamp(time.getTimeInMillis()); + } + } + + @Deprecated + private boolean cooldownApplies() { + boolean applies = true; + String globalBypassPerm = "essentials.teleport.cooldown.bypass"; + switch (tpType) { + case NORMAL: + applies = !teleportOwner.isAuthorized(globalBypassPerm); + break; + case BACK: + applies = !(teleportOwner.isAuthorized(globalBypassPerm) && + teleportOwner.isAuthorized("essentials.teleport.cooldown.bypass.back")); + break; + case TPA: + applies = !(teleportOwner.isAuthorized(globalBypassPerm) && + teleportOwner.isAuthorized("essentials.teleport.cooldown.bypass.tpa")); + break; + } + return applies; + } + + @Deprecated + private void warnUser(final IUser user, final double delay) { + Calendar c = new GregorianCalendar(); + c.add(Calendar.SECOND, (int) delay); + c.add(Calendar.MILLISECOND, (int) ((delay * 1000.0) % 1000.0)); + user.sendMessage(tl("dontMoveMessage", DateUtil.formatDateDiff(c.getTimeInMillis()))); + } + + //The now function is used when you want to skip tp delay when teleporting someone to a location or player. + @Override + @Deprecated + public void now(Location loc, boolean cooldown, TeleportCause cause) throws Exception { + if (cooldown) { + cooldown(false); + } + final ITarget target = new LocationTarget(loc); + now(teleportOwner, target, cause); + } + + @Override + @Deprecated + public void now(Player entity, boolean cooldown, TeleportCause cause) throws Exception { + if (cooldown) { + cooldown(false); + } + final ITarget target = new PlayerTarget(entity); + now(teleportOwner, target, cause); + teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); + } + + @Deprecated + protected void now(IUser teleportee, ITarget target, TeleportCause cause) throws Exception { + cancel(false); + Location loc = target.getLocation(); + + UserTeleportEvent event = new UserTeleportEvent(teleportee, cause, loc); + Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + + teleportee.setLastLocation(); + + if (!teleportee.getBase().isEmpty()) { + if (!ess.getSettings().isTeleportPassengerDismount()) { + throw new Exception(tl("passengerTeleportFail")); + } + teleportee.getBase().eject(); + } + + if (LocationUtil.isBlockUnsafeForUser(teleportee, loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) { + if (ess.getSettings().isTeleportSafetyEnabled()) { + if (ess.getSettings().isForceDisableTeleportSafety()) { + PaperLib.teleportAsync(teleportee.getBase(), loc, cause); + } else { + PaperLib.teleportAsync(teleportee.getBase(), LocationUtil.getSafeDestination(ess, teleportee, loc), cause); + } + } else { + throw new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + } + } else { + if (ess.getSettings().isForceDisableTeleportSafety()) { + PaperLib.teleportAsync(teleportee.getBase(), loc, cause); + } else { + if (ess.getSettings().isTeleportToCenterLocation()) { + loc = LocationUtil.getRoundedDestination(loc); + } + PaperLib.teleportAsync(teleportee.getBase(), loc, cause); + } + } + } + + //The teleportPlayer function is used when you want to normally teleportPlayer someone to a location or player. + //This method is nolonger used internally and will be removed. + @Deprecated + @Override + public void teleport(Location loc, Trade chargeFor) throws Exception { + teleport(loc, chargeFor, TeleportCause.PLUGIN); + } + + @Override + @Deprecated + public void teleport(Location loc, Trade chargeFor, TeleportCause cause) throws Exception { + teleport(teleportOwner, new LocationTarget(loc), chargeFor, cause); + } + + //This is used when teleporting to a player + @Override + @Deprecated + public void teleport(Player entity, Trade chargeFor, TeleportCause cause) throws Exception { + ITarget target = new PlayerTarget(entity); + teleportOwner.sendMessage(tl("teleportToPlayer", entity.getDisplayName())); + teleport(teleportOwner, target, chargeFor, cause); + } + + //This is used when teleporting to stored location + @Override + @Deprecated + public void teleportPlayer(IUser teleportee, Location loc, Trade chargeFor, TeleportCause cause) throws Exception { + teleport(teleportee, new LocationTarget(loc), chargeFor, cause); + } + + //This is used on /tphere + @Override + @Deprecated + public void teleportPlayer(IUser teleportee, Player entity, Trade chargeFor, TeleportCause cause) throws Exception { + ITarget target = new PlayerTarget(entity); + teleport(teleportee, target, chargeFor, cause); + teleportee.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); + teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); + } + + @Deprecated + private void teleport(IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause) throws Exception { + double delay = ess.getSettings().getTeleportDelay(); + + Trade cashCharge = chargeFor; + + if (chargeFor != null) { + chargeFor.isAffordableFor(teleportOwner); + + //This code is to make sure that commandcosts are checked in the initial world, and not in the resulting world. + if (!chargeFor.getCommandCost(teleportOwner).equals(BigDecimal.ZERO)) { + //By converting a command cost to a regular cost, the command cost permission isn't checked when executing the charge after teleport. + cashCharge = new Trade(chargeFor.getCommandCost(teleportOwner), ess); + } + } + + cooldown(true); + if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") || teleportee.isAuthorized("essentials.teleport.timer.bypass")) { + cooldown(false); + now(teleportee, target, cause); + if (cashCharge != null) { + cashCharge.charge(teleportOwner); + } + return; + } + + cancel(false); + warnUser(teleportee, delay); + initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false); + } + + @Deprecated + private void teleportOther(IUser teleporter, IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause) throws Exception { + double delay = ess.getSettings().getTeleportDelay(); + + Trade cashCharge = chargeFor; + + if (teleporter != null && chargeFor != null) { + chargeFor.isAffordableFor(teleporter); + + //This code is to make sure that commandcosts are checked in the initial world, and not in the resulting world. + if (!chargeFor.getCommandCost(teleporter).equals(BigDecimal.ZERO)) { + //By converting a command cost to a regular cost, the command cost permission isn't checked when executing the charge after teleport. + cashCharge = new Trade(chargeFor.getCommandCost(teleporter), ess); + } + } + + cooldown(true); + if (delay <= 0 || teleporter == null + || teleporter.isAuthorized("essentials.teleport.timer.bypass") + || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") + || teleportee.isAuthorized("essentials.teleport.timer.bypass")) { + cooldown(false); + now(teleportee, target, cause); + if (teleporter != null && cashCharge != null) { + cashCharge.charge(teleporter); + } + return; + } + + cancel(false); + warnUser(teleportee, delay); + initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false); + } + + //The respawn function is a wrapper used to handle tp fallback, on /jail and /home + @Override + @Deprecated + public void respawn(final Trade chargeFor, TeleportCause cause) throws Exception { + double delay = ess.getSettings().getTeleportDelay(); + if (chargeFor != null) { + chargeFor.isAffordableFor(teleportOwner); + } + cooldown(true); + if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass")) { + cooldown(false); + respawnNow(teleportOwner, cause); + if (chargeFor != null) { + chargeFor.charge(teleportOwner); + } + return; + } + + cancel(false); + warnUser(teleportOwner, delay); + initTimer((long) (delay * 1000.0), teleportOwner, null, chargeFor, cause, true); + } + + @Deprecated + void respawnNow(IUser teleportee, TeleportCause cause) throws Exception { + final Player player = teleportee.getBase(); + Location bed = player.getBedSpawnLocation(); + if (bed != null) { + now(teleportee, new LocationTarget(bed), cause); + } else { + if (ess.getSettings().isDebug()) { + ess.getLogger().info("Could not find bed spawn, forcing respawn event."); + } + final PlayerRespawnEvent pre = new PlayerRespawnEvent(player, player.getWorld().getSpawnLocation(), false); + ess.getServer().getPluginManager().callEvent(pre); + now(teleportee, new LocationTarget(pre.getRespawnLocation()), cause); + } + } + + //The warp function is a wrapper used to teleportPlayer a player to a /warp + @Override + @Deprecated + public void warp(IUser teleportee, String warp, Trade chargeFor, TeleportCause cause) throws Exception { + UserWarpEvent event = new UserWarpEvent(teleportee, warp, chargeFor); + Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + + warp = event.getWarp(); + Location loc = ess.getWarps().getWarp(warp); + teleportee.sendMessage(tl("warpingTo", warp, loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + if (!teleportee.equals(teleportOwner)) { + teleportOwner.sendMessage(tl("warpingTo", warp, loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + } + teleport(teleportee, new LocationTarget(loc), chargeFor, cause); + } + + //The back function is a wrapper used to teleportPlayer a player /back to their previous location. + @Override + @Deprecated + public void back(Trade chargeFor) throws Exception { + back(teleportOwner, chargeFor); + } + + //This function is a wrapper over the other back function for cases where another player performs back for them + @Override + @Deprecated + public void back(IUser teleporter, Trade chargeFor) throws Exception { + tpType = TeleportType.BACK; + final Location loc = teleportOwner.getLastLocation(); + teleportOwner.sendMessage(tl("backUsageMsg", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + teleportOther(teleporter, teleportOwner, new LocationTarget(loc), chargeFor, TeleportCause.COMMAND); + } + + //This function is used to throw a user back after a jail sentence + @Override + @Deprecated + public void back() throws Exception { + now(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()), TeleportCause.COMMAND); + } + + @Deprecated + public void setTpType(TeleportType tpType) { + this.tpType = tpType; + } + + //If we need to cancelTimer a pending teleportPlayer call this method + @Deprecated + private void cancel(boolean notifyUser) { + if (timedTeleport != null) { + timedTeleport.cancelTimer(notifyUser); + timedTeleport = null; + } + } + + @Deprecated + private void initTimer(long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) { + timedTeleport = new TimedTeleport(teleportOwner, ess, this, delay, teleportUser, target, chargeFor, cause, respawn); + } +} \ No newline at end of file diff --git a/Essentials/src/com/earth2me/essentials/TimedTeleport.java b/Essentials/src/com/earth2me/essentials/TimedTeleport.java index 039f3e8088e..df1439867ed 100644 --- a/Essentials/src/com/earth2me/essentials/TimedTeleport.java +++ b/Essentials/src/com/earth2me/essentials/TimedTeleport.java @@ -6,139 +6,136 @@ import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import java.util.UUID; -import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; public class TimedTeleport implements Runnable { - private static final double MOVE_CONSTANT = 0.3; - private final IUser teleportOwner; - private final IEssentials ess; - private final AsyncTeleport teleport; - private final UUID timer_teleportee; - private int timer_task; - private final long timer_started; // time this task was initiated - private final long timer_delay; // how long to delay the teleportPlayer - private double timer_health; - // note that I initially stored a clone of the location for reference, but... - // when comparing locations, I got incorrect mismatches (rounding errors, looked like) - // so, the X/Y/Z values are stored instead and rounded off - private final long timer_initX; - private final long timer_initY; - private final long timer_initZ; - private final ITarget timer_teleportTarget; - private final boolean timer_respawn; - private final boolean timer_canMove; - private final Trade timer_chargeFor; - private final TeleportCause timer_cause; - - TimedTeleport(IUser user, IEssentials ess, AsyncTeleport teleport, long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) { - this.teleportOwner = user; - this.ess = ess; - this.teleport = teleport; - this.timer_started = System.currentTimeMillis(); - this.timer_delay = delay; - this.timer_health = teleportUser.getBase().getHealth(); - this.timer_initX = Math.round(teleportUser.getBase().getLocation().getX() * MOVE_CONSTANT); - this.timer_initY = Math.round(teleportUser.getBase().getLocation().getY() * MOVE_CONSTANT); - this.timer_initZ = Math.round(teleportUser.getBase().getLocation().getZ() * MOVE_CONSTANT); - this.timer_teleportee = teleportUser.getBase().getUniqueId(); - this.timer_teleportTarget = target; - this.timer_chargeFor = chargeFor; - this.timer_cause = cause; - this.timer_respawn = respawn; - this.timer_canMove = user.isAuthorized("essentials.teleport.timer.move"); - - timer_task = ess.runTaskTimerAsynchronously(this, 20, 20).getTaskId(); + private static final double MOVE_CONSTANT = 0.3; + private final IUser teleportOwner; + private final IEssentials ess; + private final Teleport teleport; + private final UUID timer_teleportee; + private int timer_task; + private final long timer_started; // time this task was initiated + private final long timer_delay; // how long to delay the teleportPlayer + private double timer_health; + // note that I initially stored a clone of the location for reference, but... + // when comparing locations, I got incorrect mismatches (rounding errors, looked like) + // so, the X/Y/Z values are stored instead and rounded off + private final long timer_initX; + private final long timer_initY; + private final long timer_initZ; + private final ITarget timer_teleportTarget; + private final boolean timer_respawn; + private final boolean timer_canMove; + private final Trade timer_chargeFor; + private final TeleportCause timer_cause; + + TimedTeleport(IUser user, IEssentials ess, Teleport teleport, long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) { + this.teleportOwner = user; + this.ess = ess; + this.teleport = teleport; + this.timer_started = System.currentTimeMillis(); + this.timer_delay = delay; + this.timer_health = teleportUser.getBase().getHealth(); + this.timer_initX = Math.round(teleportUser.getBase().getLocation().getX() * MOVE_CONSTANT); + this.timer_initY = Math.round(teleportUser.getBase().getLocation().getY() * MOVE_CONSTANT); + this.timer_initZ = Math.round(teleportUser.getBase().getLocation().getZ() * MOVE_CONSTANT); + this.timer_teleportee = teleportUser.getBase().getUniqueId(); + this.timer_teleportTarget = target; + this.timer_chargeFor = chargeFor; + this.timer_cause = cause; + this.timer_respawn = respawn; + this.timer_canMove = user.isAuthorized("essentials.teleport.timer.move"); + + timer_task = ess.runTaskTimerAsynchronously(this, 20, 20).getTaskId(); + } + + @Override + public void run() { + + if (teleportOwner == null || !teleportOwner.getBase().isOnline() || teleportOwner.getBase().getLocation() == null) { + cancelTimer(false); + return; } - @Override - public void run() { + final IUser teleportUser = ess.getUser(this.timer_teleportee); - if (teleportOwner == null || !teleportOwner.getBase().isOnline() || teleportOwner.getBase().getLocation() == null) { - cancelTimer(false); - return; - } - - final IUser teleportUser = ess.getUser(this.timer_teleportee); - - if (teleportUser == null || !teleportUser.getBase().isOnline()) { - cancelTimer(false); - return; - } + if (teleportUser == null || !teleportUser.getBase().isOnline()) { + cancelTimer(false); + return; + } - final Location currLocation = teleportUser.getBase().getLocation(); - if (currLocation == null) { - cancelTimer(false); - return; - } + final Location currLocation = teleportUser.getBase().getLocation(); + if (currLocation == null) { + cancelTimer(false); + return; + } - if (!timer_canMove && (Math.round(currLocation.getX() * MOVE_CONSTANT) != timer_initX || Math.round(currLocation.getY() * MOVE_CONSTANT) != timer_initY || Math.round(currLocation.getZ() * MOVE_CONSTANT) != timer_initZ || teleportUser.getBase().getHealth() < timer_health)) { - // user moved, cancelTimer teleportPlayer - cancelTimer(true); - return; - } + if (!timer_canMove && (Math.round(currLocation.getX() * MOVE_CONSTANT) != timer_initX || Math.round(currLocation.getY() * MOVE_CONSTANT) != timer_initY || Math.round(currLocation.getZ() * MOVE_CONSTANT) != timer_initZ || teleportUser.getBase().getHealth() < timer_health)) { + // user moved, cancelTimer teleportPlayer + cancelTimer(true); + return; + } - class DelayedTeleportTask implements Runnable { - @Override - public void run() { - - timer_health = teleportUser.getBase().getHealth(); // in case user healed, then later gets injured - final long now = System.currentTimeMillis(); - if (now > timer_started + timer_delay) { - try { - teleport.cooldown(false); - } catch (Exception ex) { - teleportOwner.sendMessage(tl("cooldownWithMessage", ex.getMessage())); - if (teleportOwner != teleportUser) { - teleportUser.sendMessage(tl("cooldownWithMessage", ex.getMessage())); - } - } - try { - cancelTimer(false); - teleportUser.sendMessage(tl("teleportationCommencing")); - - try { - CompletableFuture future = new CompletableFuture<>(); - CompletableFuture future1 = new CompletableFuture<>(); - if (timer_chargeFor != null) { - timer_chargeFor.isAffordableFor(teleportOwner); - } - if (timer_respawn) { - teleport.respawnNow(teleportUser, timer_cause, future, future1); - } else { - teleport.nowAsync(teleportUser, timer_teleportTarget, timer_cause, future, future1); - } - if (timer_chargeFor != null) { - timer_chargeFor.charge(teleportOwner); - } - } catch (Exception ignored) {} - - } catch (Exception ex) { - ess.showError(teleportOwner.getSource(), ex, "\\ teleport"); - } - } + class DelayedTeleportTask implements Runnable { + @Override + public void run() { + + timer_health = teleportUser.getBase().getHealth(); // in case user healed, then later gets injured + final long now = System.currentTimeMillis(); + if (now > timer_started + timer_delay) { + try { + teleport.cooldown(false); + } catch (Exception ex) { + teleportOwner.sendMessage(tl("cooldownWithMessage", ex.getMessage())); + if (teleportOwner != teleportUser) { + teleportUser.sendMessage(tl("cooldownWithMessage", ex.getMessage())); } + } + try { + cancelTimer(false); + teleportUser.sendMessage(tl("teleportationCommencing")); + + try { + if (timer_chargeFor != null) { + timer_chargeFor.isAffordableFor(teleportOwner); + } + if (timer_respawn) { + teleport.respawnNow(teleportUser, timer_cause); + } else { + teleport.now(teleportUser, timer_teleportTarget, timer_cause); + } + if (timer_chargeFor != null) { + timer_chargeFor.charge(teleportOwner); + } + } catch (Exception ignored) {} + + } catch (Exception ex) { + ess.showError(teleportOwner.getSource(), ex, "\\ teleport"); + } } - ess.scheduleSyncDelayedTask(new DelayedTeleportTask()); + } } + ess.scheduleSyncDelayedTask(new DelayedTeleportTask()); + } - //If we need to cancelTimer a pending teleportPlayer call this method - void cancelTimer(boolean notifyUser) { - if (timer_task == -1) { - return; - } - try { - ess.getServer().getScheduler().cancelTask(timer_task); - if (notifyUser) { - teleportOwner.sendMessage(tl("pendingTeleportCancelled")); - if (timer_teleportee != null && !timer_teleportee.equals(teleportOwner.getBase().getUniqueId())) { - ess.getUser(timer_teleportee).sendMessage(tl("pendingTeleportCancelled")); - } - } - } finally { - timer_task = -1; + //If we need to cancelTimer a pending teleportPlayer call this method + void cancelTimer(boolean notifyUser) { + if (timer_task == -1) { + return; + } + try { + ess.getServer().getScheduler().cancelTask(timer_task); + if (notifyUser) { + teleportOwner.sendMessage(tl("pendingTeleportCancelled")); + if (timer_teleportee != null && !timer_teleportee.equals(teleportOwner.getBase().getUniqueId())) { + ess.getUser(timer_teleportee).sendMessage(tl("pendingTeleportCancelled")); } + } + } finally { + timer_task = -1; } -} + } +} \ No newline at end of file diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index e2075b95f7d..6061695ca52 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -1,5 +1,7 @@ package com.earth2me.essentials; +import com.earth2me.essentials.api.IAsyncTeleport; +import com.earth2me.essentials.api.ITeleport; import com.earth2me.essentials.commands.IEssentialsCommand; import com.earth2me.essentials.messaging.IMessageRecipient; import com.earth2me.essentials.messaging.SimpleMessageRecipient; @@ -16,7 +18,6 @@ import net.ess3.api.events.MuteStatusChangeEvent; import net.ess3.api.events.UserBalanceUpdateEvent; import net.ess3.nms.refl.ReflUtil; - import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; @@ -44,6 +45,7 @@ public class User extends UserData implements Comparable, IMessageRecipien private transient Location teleportLocation; private transient boolean vanished; private transient final AsyncTeleport teleport; + private transient final Teleport legacyTeleport; private transient long teleportRequestTime; private transient long lastOnlineActivity; private transient long lastThrottledAction; @@ -65,6 +67,7 @@ public class User extends UserData implements Comparable, IMessageRecipien public User(final Player base, final IEssentials ess) { super(base, ess); teleport = new AsyncTeleport(this, ess); + legacyTeleport = new Teleport(this, ess); if (isAfk()) { afkPosition = this.getLocation(); } @@ -407,10 +410,15 @@ public String getDisplayName() { } @Override - public AsyncTeleport getTeleport() { + public IAsyncTeleport getAsyncTeleport() { return teleport; } + @Override + public ITeleport getTeleport() { + return legacyTeleport; + } + public long getLastOnlineActivity() { return lastOnlineActivity; } @@ -574,8 +582,8 @@ public boolean checkJailTimeout(final long currentTime) { setJail(null); if (ess.getSettings().isTeleportBackWhenFreedFromJail()) { CompletableFuture eFuture = new CompletableFuture<>(); - eFuture.thenAccept(e -> getTeleport().respawn(null, TeleportCause.PLUGIN, new CompletableFuture<>(), new CompletableFuture<>())); - getTeleport().back(eFuture, new CompletableFuture<>()); + eFuture.thenAccept(e -> getAsyncTeleport().respawn(null, TeleportCause.PLUGIN, new CompletableFuture<>(), new CompletableFuture<>())); + getAsyncTeleport().back(eFuture, new CompletableFuture<>()); } return true; } diff --git a/Essentials/src/com/earth2me/essentials/api/ITeleport.java b/Essentials/src/com/earth2me/essentials/api/ITeleport.java new file mode 100644 index 00000000000..3743f868e0c --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/api/ITeleport.java @@ -0,0 +1,147 @@ +package com.earth2me.essentials.api; + +import com.earth2me.essentials.Trade; +import net.ess3.api.IUser; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerTeleportEvent; + + +/** + * @deprecated This API is not asynchronous. Use {@link com.earth2me.essentials.api.IAsyncTeleport IAsyncTeleport} + */ +public interface ITeleport { + /** + * Used to skip teleportPlayer delay when teleporting someone to a location or player. + * + * @param loc - Where should the player end up + * @param cooldown - If cooldown should be enforced + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void now(Location loc, boolean cooldown, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Used to skip teleportPlayer delay when teleporting someone to a location or player. + * + * @param entity - Where should the player end up + * @param cooldown - If cooldown should be enforced + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void now(Player entity, boolean cooldown, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + @Deprecated + void teleport(Location loc, Trade chargeFor) throws Exception; + + /** + * Teleport a player to a specific location + * + * @param loc - Where should the player end up + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void teleport(Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Teleport a player to a specific player + * + * @param entity - Where should the player end up + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void teleport(Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Teleport a player to a specific location + * + * @param otherUser - Which user will be teleported + * @param loc - Where should the player end up + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Teleport a player to a specific player + * + * @param otherUser - Which user will be teleported + * @param entity - Where should the player end up + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Teleport wrapper used to handle tp fallback on /jail and /home + * + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void respawn(final Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Teleport wrapper used to handle /warp teleports + * + * @param otherUser - Which user will be teleported + * @param warp - The name of the warp the user will be teleported too. + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void warp(IUser otherUser, String warp, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Teleport wrapper used to handle /back teleports + * + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * + * @throws Exception + */ + @Deprecated + void back(Trade chargeFor) throws Exception; + + /** + * Teleport wrapper used to handle /back teleports that + * are executed by a different player with this + * instance of teleport as a target. + * + * @param teleporter - The user performing the /back command. + * This value may be {@code null} to indicate console. + * @param chargeFor - What the {@code teleporter} will be charged if teleportPlayer is successful + * + * @throws Exception + */ + @Deprecated + void back(IUser teleporter, Trade chargeFor) throws Exception; + + /** + * Teleport wrapper used to handle throwing user home after a jail sentence + * + * @throws Exception + */ + @Deprecated + void back() throws Exception; + +} \ No newline at end of file diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandback.java b/Essentials/src/com/earth2me/essentials/commands/Commandback.java index 65aba66aa71..7411978bc03 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandback.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandback.java @@ -78,15 +78,15 @@ private void teleportBack(CommandSource sender, User user, String commandLabel) } if (requester == null) { - user.getTeleport().back(null, null, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().back(null, null, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); } else if (!requester.equals(user)) { Trade charge = new Trade(this.getName(), this.ess); charge.isAffordableFor(requester); - user.getTeleport().back(requester, charge, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().back(requester, charge, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); } else { Trade charge = new Trade(this.getName(), this.ess); charge.isAffordableFor(user); - user.getTeleport().back(charge, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().back(charge, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); } throw new NoChargeException(); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java index c713080ecd0..827e0d82194 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java @@ -44,7 +44,7 @@ public void run(final Server server, final User user, final String commandLabel, if ("bed".equalsIgnoreCase(homeName) && user.isAuthorized("essentials.home.bed")) { final Location bed = player.getBase().getBedSpawnLocation(); if (bed != null) { - user.getTeleport().teleport(bed, charge, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + user.getAsyncTeleport().teleport(bed, charge, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); return; } else { throw new Exception(tl("bedMissing")); @@ -56,7 +56,7 @@ public void run(final Server server, final User user, final String commandLabel, final List homes = player.getHomes(); if (homes.isEmpty() && player.equals(user)) { if (ess.getSettings().isSpawnIfNoHome()) { - user.getTeleport().respawn(charge, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + user.getAsyncTeleport().respawn(charge, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); } else { throw new Exception(tl("noHomeSetPlayer")); } @@ -105,7 +105,7 @@ private void goHome(final User user, final User player, final String home, final user.sendMessage(tl("teleportHome", home)); } }); - user.getTeleport().teleport(loc, charge, TeleportCause.COMMAND, eFuture, future); + user.getAsyncTeleport().teleport(loc, charge, TeleportCause.COMMAND, eFuture, future); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandjump.java b/Essentials/src/com/earth2me/essentials/commands/Commandjump.java index bee26b2759f..aa0f9f98ea4 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandjump.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandjump.java @@ -47,7 +47,7 @@ public void run(final Server server, final User user, final String commandLabel, final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); - user.getTeleport().teleport(loc, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().teleport(loc, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java b/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java index 25aba5d7f90..45788b23f3f 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java @@ -108,7 +108,7 @@ public void run(final Server server, final CommandSource sender, final String co sender.sendMessage(tl("jailReleased", player.getName())); } }); - player.getTeleport().back(getNewExceptionFuture(sender, commandLabel), future); + player.getAsyncTeleport().back(getNewExceptionFuture(sender, commandLabel), future); return; } sender.sendMessage(tl("jailReleased", player.getName())); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtop.java b/Essentials/src/com/earth2me/essentials/commands/Commandtop.java index 42171be93f0..dc912a552dd 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtop.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtop.java @@ -30,6 +30,6 @@ public void run(final Server server, final User user, final String commandLabel, user.sendMessage(tl("teleportTop", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); } }); - user.getTeleport().teleport(loc, new Trade(this.getName(), ess), TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); + user.getAsyncTeleport().teleport(loc, new Trade(this.getName(), ess), TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtp.java b/Essentials/src/com/earth2me/essentials/commands/Commandtp.java index 68ca636ff54..04e25664381 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtp.java @@ -45,7 +45,7 @@ public void run(final Server server, final User user, final String commandLabel, } final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); - user.getTeleport().teleport(player.getBase(), charge, TeleportCause.COMMAND, eFuture, future); + user.getAsyncTeleport().teleport(player.getBase(), charge, TeleportCause.COMMAND, eFuture, future); break; case 3: if (!user.isAuthorized("essentials.tp.position")) { @@ -58,7 +58,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new NotEnoughArgumentsException(tl("teleportInvalidLocation")); } final Location locpos = new Location(user.getWorld(), x2, y2, z2, user.getLocation().getYaw(), user.getLocation().getPitch()); - user.getTeleport().now(locpos, false, TeleportCause.COMMAND, eFuture, future); + user.getAsyncTeleport().now(locpos, false, TeleportCause.COMMAND, eFuture, future); future.thenAccept(success -> { if (success) { user.sendMessage(tl("teleporting", locpos.getWorld().getName(), locpos.getBlockX(), locpos.getBlockY(), locpos.getBlockZ())); @@ -84,7 +84,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("teleportDisabled", target2.getDisplayName())); } user.sendMessage(tl("teleporting", locposother.getWorld().getName(), locposother.getBlockX(), locposother.getBlockY(), locposother.getBlockZ())); - target2.getTeleport().now(locposother, false, TeleportCause.COMMAND, eFuture, future); + target2.getAsyncTeleport().now(locposother, false, TeleportCause.COMMAND, eFuture, future); future.thenAccept(success -> { if (success) { target2.sendMessage(tl("teleporting", locposother.getWorld().getName(), locposother.getBlockX(), locposother.getBlockY(), locposother.getBlockZ())); @@ -108,7 +108,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("noPerm", "essentials.worlds." + toPlayer.getWorld().getName())); } target.sendMessage(tl("teleportAtoB", user.getDisplayName(), toPlayer.getDisplayName())); - target.getTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, eFuture, future); + target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, eFuture, future); break; } } @@ -124,7 +124,7 @@ public void run(final Server server, final CommandSource sender, final String co final User toPlayer = getPlayer(server, args, 1, true, false); target.sendMessage(tl("teleportAtoB", Console.NAME, toPlayer.getDisplayName())); CompletableFuture eFuture = new CompletableFuture<>(); - target.getTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); eFuture.thenAccept(e -> showError(sender.getSender(), e, commandLabel)); } else if (args.length > 3) { final double x = args[1].startsWith("~") ? target.getLocation().getX() + (args[1].length() > 1 ? Double.parseDouble(args[1].substring(1)) : 0) : Double.parseDouble(args[1]); @@ -137,7 +137,7 @@ public void run(final Server server, final CommandSource sender, final String co sender.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); CompletableFuture eFuture = new CompletableFuture<>(); CompletableFuture future = new CompletableFuture<>(); - target.getTeleport().now(loc, false, TeleportCause.COMMAND, eFuture, future); + target.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, eFuture, future); future.thenAccept(success -> { if (success) { target.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java index da94ef73d8e..20fe12c74ea 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java @@ -42,7 +42,7 @@ public void run(Server server, User user, String commandLabel, String[] args) th } if (player.isAutoTeleportEnabled() && !player.isIgnoredPlayer(user)) { final Trade charge = new Trade(this.getName(), ess); - AsyncTeleport teleport = user.getTeleport(); + AsyncTeleport teleport = (AsyncTeleport) user.getAsyncTeleport(); teleport.setTpType(AsyncTeleport.TeleportType.TPA); CompletableFuture future = new CompletableFuture<>(); future.thenAccept(success -> { diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java index 5d2a41158af..251a71820da 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java @@ -64,7 +64,7 @@ public void run(final Server server, final User user, final String commandLabel, }); if (user.isTpRequestHere()) { final Location loc = user.getTpRequestLocation(); - AsyncTeleport teleport = requester.getTeleport(); + AsyncTeleport teleport = (AsyncTeleport) requester.getAsyncTeleport(); teleport.setTpType(AsyncTeleport.TeleportType.TPA); future.thenAccept(success -> { if (success) { @@ -73,7 +73,7 @@ public void run(final Server server, final User user, final String commandLabel, }); teleport.teleportPlayer(user, user.getTpRequestLocation(), charge, TeleportCause.COMMAND, eFuture, future); } else { - AsyncTeleport teleport = requester.getTeleport(); + AsyncTeleport teleport = (AsyncTeleport) requester.getAsyncTeleport(); teleport.setTpType(AsyncTeleport.TeleportType.TPA); teleport.teleport(user.getBase(), charge, TeleportCause.COMMAND, eFuture, future); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java index 07a3ffb8e08..c097b6396ca 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java @@ -44,7 +44,7 @@ private void teleportAllPlayers(Server server, CommandSource sender, User target } CompletableFuture eFuture = new CompletableFuture<>(); eFuture.thenAccept(e -> showError(sender.getSender(), e, label)); - player.getTeleport().now(loc, false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + player.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtphere.java b/Essentials/src/com/earth2me/essentials/commands/Commandtphere.java index 757733eef53..3aafcfcd860 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtphere.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtphere.java @@ -26,7 +26,7 @@ public void run(final Server server, final User user, final String commandLabel, if (user.getWorld() != player.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + user.getWorld().getName())) { throw new Exception(tl("noPerm", "essentials.worlds." + user.getWorld().getName())); } - user.getTeleport().teleportPlayer(player, user.getBase(), new Trade(this.getName(), ess), TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().teleportPlayer(player, user.getBase(), new Trade(this.getName(), ess), TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java index cc65f803c29..881823e0873 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java @@ -29,7 +29,7 @@ public void run(final Server server, final User user, final String commandLabel, if (user.getWorld() != player.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + player.getWorld().getName())) { throw new Exception(tl("noPerm", "essentials.worlds." + player.getWorld().getName())); } - user.getTeleport().now(player.getBase(), false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + user.getAsyncTeleport().now(player.getBase(), false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); break; default: @@ -49,7 +49,7 @@ public void run(final Server server, final User user, final String commandLabel, target.sendMessage(tl("teleportAtoB", user.getDisplayName(), toPlayer.getDisplayName())); } }); - target.getTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, eFuture, future); + target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, eFuture, future); break; } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java index aae4b683f9a..6fb0684779b 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java @@ -32,7 +32,7 @@ public void run(final Server server, final User user, final String label, final user.sendMessage(tl("teleporting", logout.getWorld().getName(), logout.getBlockX(), logout.getBlockY(), logout.getBlockZ())); CompletableFuture eFuture = new CompletableFuture<>(); eFuture.thenAccept(e -> showError(user.getBase(), e, label)); - user.getTeleport().now(logout, false, PlayerTeleportEvent.TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + user.getAsyncTeleport().now(logout, false, PlayerTeleportEvent.TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); } } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java index c92f5887892..7b81c039f01 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java @@ -31,7 +31,7 @@ public void run(final Server server, final User user, final String commandLabel, CompletableFuture eFuture = new CompletableFuture<>(); eFuture.thenAccept(e -> showError(user.getBase(), e, commandLabel)); - player.getTeleport().now(user.getBase(), false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + player.getAsyncTeleport().now(user.getBase(), false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java b/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java index 87ee93c7dea..17e82e28da7 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java @@ -56,7 +56,7 @@ public void run(final Server server, final User user, final String commandLabel, final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); user.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - user.getTeleport().teleport(loc, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().teleport(loc, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } @Override @@ -85,7 +85,7 @@ public void run(final Server server, final CommandSource sender, final String co } sender.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); user.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - user.getTeleport().teleport(loc, null, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().teleport(loc, null, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java b/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java index 5b77c16f7e1..9a82ba1dd6a 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java @@ -56,7 +56,7 @@ public void run(final Server server, final CommandSource sender, final String co throw new NoChargeException(); } User otherUser = getPlayer(server, args, 1, true, false); - otherUser.getTeleport().warp(otherUser, args[0], null, TeleportCause.COMMAND, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); + otherUser.getAsyncTeleport().warp(otherUser, args[0], null, TeleportCause.COMMAND, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); } //TODO: Use one of the new text classes, like /help ? @@ -98,7 +98,7 @@ private void warpUser(final User owner, final User user, final String name, fina if (ess.getSettings().getPerWarpPermission() && !owner.isAuthorized("essentials.warps." + name)) { throw new Exception(tl("warpUsePermission")); } - owner.getTeleport().warp(user, name, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + owner.getAsyncTeleport().warp(user, name, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } private List getAvailableWarpsFor(final IUser user) { diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandworld.java b/Essentials/src/com/earth2me/essentials/commands/Commandworld.java index 301d47725fe..46459a25c6b 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandworld.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandworld.java @@ -67,7 +67,7 @@ protected void run(final Server server, final User user, final String commandLab final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); - user.getTeleport().teleport(target, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().teleport(target, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } @Override diff --git a/Essentials/src/com/earth2me/essentials/signs/SignWarp.java b/Essentials/src/com/earth2me/essentials/signs/SignWarp.java index e267e4a6ae3..7181a8d05b0 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignWarp.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignWarp.java @@ -62,7 +62,7 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri Trade.log("Sign", "Warp", "Interact", username, null, username, charge, sign.getBlock().getLocation(), ess); } }); - player.getTeleport().warp(player, warpName, charge, TeleportCause.PLUGIN, eFuture, future); + player.getAsyncTeleport().warp(player, warpName, charge, TeleportCause.PLUGIN, eFuture, future); return true; } } diff --git a/Essentials/src/net/ess3/api/IAsyncTeleport.java b/Essentials/src/net/ess3/api/IAsyncTeleport.java deleted file mode 100644 index 81f30fc0332..00000000000 --- a/Essentials/src/net/ess3/api/IAsyncTeleport.java +++ /dev/null @@ -1,6 +0,0 @@ -package net.ess3.api; - - -public interface IAsyncTeleport extends com.earth2me.essentials.api.IAsyncTeleport { - -} diff --git a/Essentials/src/net/ess3/api/ITeleport.java b/Essentials/src/net/ess3/api/ITeleport.java new file mode 100644 index 00000000000..65c68076131 --- /dev/null +++ b/Essentials/src/net/ess3/api/ITeleport.java @@ -0,0 +1,6 @@ +package net.ess3.api; + + +public interface ITeleport extends com.earth2me.essentials.api.ITeleport { + +} diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java index db905ddf03a..c3109af5d5c 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java @@ -62,9 +62,9 @@ private void respawn(final CommandSource sender, final User teleportOwner, final CompletableFuture eFuture = new CompletableFuture<>(); eFuture.thenAccept(e -> showError(sender.getSender(), e, commandLabel)); if (teleportOwner == null) { - teleportee.getTeleport().now(spawn, false, TeleportCause.COMMAND, eFuture, future); + teleportee.getAsyncTeleport().now(spawn, false, TeleportCause.COMMAND, eFuture, future); } else { - teleportOwner.getTeleport().teleportPlayer(teleportee, spawn, charge, TeleportCause.COMMAND, eFuture, future); + teleportOwner.getAsyncTeleport().teleportPlayer(teleportee, spawn, charge, TeleportCause.COMMAND, eFuture, future); } } } diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java index 5f8b4e28436..98f1339f7fe 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java @@ -140,7 +140,7 @@ public void run() { if (spawn != null) { CompletableFuture eFuture = new CompletableFuture<>(); eFuture.thenAccept(e -> logger.log(Level.WARNING, tl("teleportNewPlayerError"), e)); - user.getTeleport().now(spawn, false, TeleportCause.PLUGIN, eFuture, new CompletableFuture<>()); + user.getAsyncTeleport().now(spawn, false, TeleportCause.PLUGIN, eFuture, new CompletableFuture<>()); } } } From 07a346522a089dc639fbd18ed1d011ecd697017d Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Fri, 10 Apr 2020 15:37:12 -0400 Subject: [PATCH 15/36] Revert TimedTeleport.java --- .../earth2me/essentials/TimedTeleport.java | 232 +++++++++--------- 1 file changed, 116 insertions(+), 116 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/TimedTeleport.java b/Essentials/src/com/earth2me/essentials/TimedTeleport.java index df1439867ed..c3e959f5821 100644 --- a/Essentials/src/com/earth2me/essentials/TimedTeleport.java +++ b/Essentials/src/com/earth2me/essentials/TimedTeleport.java @@ -11,131 +11,131 @@ public class TimedTeleport implements Runnable { - private static final double MOVE_CONSTANT = 0.3; - private final IUser teleportOwner; - private final IEssentials ess; - private final Teleport teleport; - private final UUID timer_teleportee; - private int timer_task; - private final long timer_started; // time this task was initiated - private final long timer_delay; // how long to delay the teleportPlayer - private double timer_health; - // note that I initially stored a clone of the location for reference, but... - // when comparing locations, I got incorrect mismatches (rounding errors, looked like) - // so, the X/Y/Z values are stored instead and rounded off - private final long timer_initX; - private final long timer_initY; - private final long timer_initZ; - private final ITarget timer_teleportTarget; - private final boolean timer_respawn; - private final boolean timer_canMove; - private final Trade timer_chargeFor; - private final TeleportCause timer_cause; - - TimedTeleport(IUser user, IEssentials ess, Teleport teleport, long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) { - this.teleportOwner = user; - this.ess = ess; - this.teleport = teleport; - this.timer_started = System.currentTimeMillis(); - this.timer_delay = delay; - this.timer_health = teleportUser.getBase().getHealth(); - this.timer_initX = Math.round(teleportUser.getBase().getLocation().getX() * MOVE_CONSTANT); - this.timer_initY = Math.round(teleportUser.getBase().getLocation().getY() * MOVE_CONSTANT); - this.timer_initZ = Math.round(teleportUser.getBase().getLocation().getZ() * MOVE_CONSTANT); - this.timer_teleportee = teleportUser.getBase().getUniqueId(); - this.timer_teleportTarget = target; - this.timer_chargeFor = chargeFor; - this.timer_cause = cause; - this.timer_respawn = respawn; - this.timer_canMove = user.isAuthorized("essentials.teleport.timer.move"); - - timer_task = ess.runTaskTimerAsynchronously(this, 20, 20).getTaskId(); - } - - @Override - public void run() { - - if (teleportOwner == null || !teleportOwner.getBase().isOnline() || teleportOwner.getBase().getLocation() == null) { - cancelTimer(false); - return; + private static final double MOVE_CONSTANT = 0.3; + private final IUser teleportOwner; + private final IEssentials ess; + private final Teleport teleport; + private final UUID timer_teleportee; + private int timer_task; + private final long timer_started; // time this task was initiated + private final long timer_delay; // how long to delay the teleportPlayer + private double timer_health; + // note that I initially stored a clone of the location for reference, but... + // when comparing locations, I got incorrect mismatches (rounding errors, looked like) + // so, the X/Y/Z values are stored instead and rounded off + private final long timer_initX; + private final long timer_initY; + private final long timer_initZ; + private final ITarget timer_teleportTarget; + private final boolean timer_respawn; + private final boolean timer_canMove; + private final Trade timer_chargeFor; + private final TeleportCause timer_cause; + + TimedTeleport(IUser user, IEssentials ess, Teleport teleport, long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) { + this.teleportOwner = user; + this.ess = ess; + this.teleport = teleport; + this.timer_started = System.currentTimeMillis(); + this.timer_delay = delay; + this.timer_health = teleportUser.getBase().getHealth(); + this.timer_initX = Math.round(teleportUser.getBase().getLocation().getX() * MOVE_CONSTANT); + this.timer_initY = Math.round(teleportUser.getBase().getLocation().getY() * MOVE_CONSTANT); + this.timer_initZ = Math.round(teleportUser.getBase().getLocation().getZ() * MOVE_CONSTANT); + this.timer_teleportee = teleportUser.getBase().getUniqueId(); + this.timer_teleportTarget = target; + this.timer_chargeFor = chargeFor; + this.timer_cause = cause; + this.timer_respawn = respawn; + this.timer_canMove = user.isAuthorized("essentials.teleport.timer.move"); + + timer_task = ess.runTaskTimerAsynchronously(this, 20, 20).getTaskId(); } - final IUser teleportUser = ess.getUser(this.timer_teleportee); + @Override + public void run() { - if (teleportUser == null || !teleportUser.getBase().isOnline()) { - cancelTimer(false); - return; - } + if (teleportOwner == null || !teleportOwner.getBase().isOnline() || teleportOwner.getBase().getLocation() == null) { + cancelTimer(false); + return; + } - final Location currLocation = teleportUser.getBase().getLocation(); - if (currLocation == null) { - cancelTimer(false); - return; - } + final IUser teleportUser = ess.getUser(this.timer_teleportee); - if (!timer_canMove && (Math.round(currLocation.getX() * MOVE_CONSTANT) != timer_initX || Math.round(currLocation.getY() * MOVE_CONSTANT) != timer_initY || Math.round(currLocation.getZ() * MOVE_CONSTANT) != timer_initZ || teleportUser.getBase().getHealth() < timer_health)) { - // user moved, cancelTimer teleportPlayer - cancelTimer(true); - return; - } + if (teleportUser == null || !teleportUser.getBase().isOnline()) { + cancelTimer(false); + return; + } - class DelayedTeleportTask implements Runnable { - @Override - public void run() { - - timer_health = teleportUser.getBase().getHealth(); // in case user healed, then later gets injured - final long now = System.currentTimeMillis(); - if (now > timer_started + timer_delay) { - try { - teleport.cooldown(false); - } catch (Exception ex) { - teleportOwner.sendMessage(tl("cooldownWithMessage", ex.getMessage())); - if (teleportOwner != teleportUser) { - teleportUser.sendMessage(tl("cooldownWithMessage", ex.getMessage())); - } - } - try { + final Location currLocation = teleportUser.getBase().getLocation(); + if (currLocation == null) { cancelTimer(false); - teleportUser.sendMessage(tl("teleportationCommencing")); - - try { - if (timer_chargeFor != null) { - timer_chargeFor.isAffordableFor(teleportOwner); - } - if (timer_respawn) { - teleport.respawnNow(teleportUser, timer_cause); - } else { - teleport.now(teleportUser, timer_teleportTarget, timer_cause); - } - if (timer_chargeFor != null) { - timer_chargeFor.charge(teleportOwner); - } - } catch (Exception ignored) {} - - } catch (Exception ex) { - ess.showError(teleportOwner.getSource(), ex, "\\ teleport"); - } + return; + } + + if (!timer_canMove && (Math.round(currLocation.getX() * MOVE_CONSTANT) != timer_initX || Math.round(currLocation.getY() * MOVE_CONSTANT) != timer_initY || Math.round(currLocation.getZ() * MOVE_CONSTANT) != timer_initZ || teleportUser.getBase().getHealth() < timer_health)) { + // user moved, cancelTimer teleportPlayer + cancelTimer(true); + return; } - } - } - ess.scheduleSyncDelayedTask(new DelayedTeleportTask()); - } - //If we need to cancelTimer a pending teleportPlayer call this method - void cancelTimer(boolean notifyUser) { - if (timer_task == -1) { - return; + class DelayedTeleportTask implements Runnable { + @Override + public void run() { + + timer_health = teleportUser.getBase().getHealth(); // in case user healed, then later gets injured + final long now = System.currentTimeMillis(); + if (now > timer_started + timer_delay) { + try { + teleport.cooldown(false); + } catch (Exception ex) { + teleportOwner.sendMessage(tl("cooldownWithMessage", ex.getMessage())); + if (teleportOwner != teleportUser) { + teleportUser.sendMessage(tl("cooldownWithMessage", ex.getMessage())); + } + } + try { + cancelTimer(false); + teleportUser.sendMessage(tl("teleportationCommencing")); + + try { + if (timer_chargeFor != null) { + timer_chargeFor.isAffordableFor(teleportOwner); + } + if (timer_respawn) { + teleport.respawnNow(teleportUser, timer_cause); + } else { + teleport.now(teleportUser, timer_teleportTarget, timer_cause); + } + if (timer_chargeFor != null) { + timer_chargeFor.charge(teleportOwner); + } + } catch (Exception ignored) {} + + } catch (Exception ex) { + ess.showError(teleportOwner.getSource(), ex, "\\ teleport"); + } + } + } + } + ess.scheduleSyncDelayedTask(new DelayedTeleportTask()); } - try { - ess.getServer().getScheduler().cancelTask(timer_task); - if (notifyUser) { - teleportOwner.sendMessage(tl("pendingTeleportCancelled")); - if (timer_teleportee != null && !timer_teleportee.equals(teleportOwner.getBase().getUniqueId())) { - ess.getUser(timer_teleportee).sendMessage(tl("pendingTeleportCancelled")); + + //If we need to cancelTimer a pending teleportPlayer call this method + void cancelTimer(boolean notifyUser) { + if (timer_task == -1) { + return; + } + try { + ess.getServer().getScheduler().cancelTask(timer_task); + if (notifyUser) { + teleportOwner.sendMessage(tl("pendingTeleportCancelled")); + if (timer_teleportee != null && !timer_teleportee.equals(teleportOwner.getBase().getUniqueId())) { + ess.getUser(timer_teleportee).sendMessage(tl("pendingTeleportCancelled")); + } + } + } finally { + timer_task = -1; } - } - } finally { - timer_task = -1; } - } -} \ No newline at end of file +} From 5aa587856faa3005d1987a28886e1ba51427e975 Mon Sep 17 00:00:00 2001 From: JRoy Date: Fri, 10 Apr 2020 15:42:56 -0400 Subject: [PATCH 16/36] Reduce Diff --- .../src/com/earth2me/essentials/IUser.java | 1 + .../src/com/earth2me/essentials/Teleport.java | 634 +++++++++--------- .../com/earth2me/essentials/api/IJails.java | 1 + .../earth2me/essentials/api/ITeleport.java | 264 ++++---- 4 files changed, 451 insertions(+), 449 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/IUser.java b/Essentials/src/com/earth2me/essentials/IUser.java index e5b627dcd8f..cc6c154cb68 100644 --- a/Essentials/src/com/earth2me/essentials/IUser.java +++ b/Essentials/src/com/earth2me/essentials/IUser.java @@ -16,6 +16,7 @@ import java.util.Set; import java.util.regex.Pattern; + public interface IUser { boolean isAuthorized(String node); diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java index e6f4c6bde61..c9e17358852 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -26,354 +26,354 @@ */ @Deprecated public class Teleport implements ITeleport { - private final IUser teleportOwner; - private final IEssentials ess; - private TimedTeleport timedTeleport; - - private TeleportType tpType; - - @Deprecated - public Teleport(IUser user, IEssentials ess) { - this.teleportOwner = user; - this.ess = ess; - tpType = TeleportType.NORMAL; - } - - public enum TeleportType { - TPA, - BACK, - NORMAL - } - - @Deprecated - public void cooldown(boolean check) throws Exception { - final Calendar time = new GregorianCalendar(); - if (teleportOwner.getLastTeleportTimestamp() > 0) { - // Take the current time, and remove the delay from it. - final double cooldown = ess.getSettings().getTeleportCooldown(); - final Calendar earliestTime = new GregorianCalendar(); - earliestTime.add(Calendar.SECOND, -(int) cooldown); - earliestTime.add(Calendar.MILLISECOND, -(int) ((cooldown * 1000.0) % 1000.0)); - // This value contains the most recent time a teleportPlayer could have been used that would allow another use. - final long earliestLong = earliestTime.getTimeInMillis(); - - // When was the last teleportPlayer used? - final long lastTime = teleportOwner.getLastTeleportTimestamp(); - - if (lastTime > time.getTimeInMillis()) { - // This is to make sure time didn't get messed up on last teleportPlayer use. - // If this happens, let's give the user the benifit of the doubt. - teleportOwner.setLastTeleportTimestamp(time.getTimeInMillis()); - return; - } else if (lastTime > earliestLong - && cooldownApplies()) { - time.setTimeInMillis(lastTime); - time.add(Calendar.SECOND, (int) cooldown); - time.add(Calendar.MILLISECOND, (int) ((cooldown * 1000.0) % 1000.0)); - throw new Exception(tl("timeBeforeTeleport", DateUtil.formatDateDiff(time.getTimeInMillis()))); - } - } - // if justCheck is set, don't update lastTeleport; we're just checking - if (!check) { - teleportOwner.setLastTeleportTimestamp(time.getTimeInMillis()); + private final IUser teleportOwner; + private final IEssentials ess; + private TimedTeleport timedTeleport; + + private TeleportType tpType; + + @Deprecated + public Teleport(IUser user, IEssentials ess) { + this.teleportOwner = user; + this.ess = ess; + tpType = TeleportType.NORMAL; } - } - - @Deprecated - private boolean cooldownApplies() { - boolean applies = true; - String globalBypassPerm = "essentials.teleport.cooldown.bypass"; - switch (tpType) { - case NORMAL: - applies = !teleportOwner.isAuthorized(globalBypassPerm); - break; - case BACK: - applies = !(teleportOwner.isAuthorized(globalBypassPerm) && - teleportOwner.isAuthorized("essentials.teleport.cooldown.bypass.back")); - break; - case TPA: - applies = !(teleportOwner.isAuthorized(globalBypassPerm) && - teleportOwner.isAuthorized("essentials.teleport.cooldown.bypass.tpa")); - break; + + public enum TeleportType { + TPA, + BACK, + NORMAL } - return applies; - } - - @Deprecated - private void warnUser(final IUser user, final double delay) { - Calendar c = new GregorianCalendar(); - c.add(Calendar.SECOND, (int) delay); - c.add(Calendar.MILLISECOND, (int) ((delay * 1000.0) % 1000.0)); - user.sendMessage(tl("dontMoveMessage", DateUtil.formatDateDiff(c.getTimeInMillis()))); - } - - //The now function is used when you want to skip tp delay when teleporting someone to a location or player. - @Override - @Deprecated - public void now(Location loc, boolean cooldown, TeleportCause cause) throws Exception { - if (cooldown) { - cooldown(false); + + @Deprecated + public void cooldown(boolean check) throws Exception { + final Calendar time = new GregorianCalendar(); + if (teleportOwner.getLastTeleportTimestamp() > 0) { + // Take the current time, and remove the delay from it. + final double cooldown = ess.getSettings().getTeleportCooldown(); + final Calendar earliestTime = new GregorianCalendar(); + earliestTime.add(Calendar.SECOND, -(int) cooldown); + earliestTime.add(Calendar.MILLISECOND, -(int) ((cooldown * 1000.0) % 1000.0)); + // This value contains the most recent time a teleportPlayer could have been used that would allow another use. + final long earliestLong = earliestTime.getTimeInMillis(); + + // When was the last teleportPlayer used? + final long lastTime = teleportOwner.getLastTeleportTimestamp(); + + if (lastTime > time.getTimeInMillis()) { + // This is to make sure time didn't get messed up on last teleportPlayer use. + // If this happens, let's give the user the benifit of the doubt. + teleportOwner.setLastTeleportTimestamp(time.getTimeInMillis()); + return; + } else if (lastTime > earliestLong + && cooldownApplies()) { + time.setTimeInMillis(lastTime); + time.add(Calendar.SECOND, (int) cooldown); + time.add(Calendar.MILLISECOND, (int) ((cooldown * 1000.0) % 1000.0)); + throw new Exception(tl("timeBeforeTeleport", DateUtil.formatDateDiff(time.getTimeInMillis()))); + } + } + // if justCheck is set, don't update lastTeleport; we're just checking + if (!check) { + teleportOwner.setLastTeleportTimestamp(time.getTimeInMillis()); + } } - final ITarget target = new LocationTarget(loc); - now(teleportOwner, target, cause); - } - - @Override - @Deprecated - public void now(Player entity, boolean cooldown, TeleportCause cause) throws Exception { - if (cooldown) { - cooldown(false); + + @Deprecated + private boolean cooldownApplies() { + boolean applies = true; + String globalBypassPerm = "essentials.teleport.cooldown.bypass"; + switch (tpType) { + case NORMAL: + applies = !teleportOwner.isAuthorized(globalBypassPerm); + break; + case BACK: + applies = !(teleportOwner.isAuthorized(globalBypassPerm) && + teleportOwner.isAuthorized("essentials.teleport.cooldown.bypass.back")); + break; + case TPA: + applies = !(teleportOwner.isAuthorized(globalBypassPerm) && + teleportOwner.isAuthorized("essentials.teleport.cooldown.bypass.tpa")); + break; + } + return applies; } - final ITarget target = new PlayerTarget(entity); - now(teleportOwner, target, cause); - teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); - } - - @Deprecated - protected void now(IUser teleportee, ITarget target, TeleportCause cause) throws Exception { - cancel(false); - Location loc = target.getLocation(); - - UserTeleportEvent event = new UserTeleportEvent(teleportee, cause, loc); - Bukkit.getServer().getPluginManager().callEvent(event); - if (event.isCancelled()) { - return; + + @Deprecated + private void warnUser(final IUser user, final double delay) { + Calendar c = new GregorianCalendar(); + c.add(Calendar.SECOND, (int) delay); + c.add(Calendar.MILLISECOND, (int) ((delay * 1000.0) % 1000.0)); + user.sendMessage(tl("dontMoveMessage", DateUtil.formatDateDiff(c.getTimeInMillis()))); } - teleportee.setLastLocation(); + //The now function is used when you want to skip tp delay when teleporting someone to a location or player. + @Override + @Deprecated + public void now(Location loc, boolean cooldown, TeleportCause cause) throws Exception { + if (cooldown) { + cooldown(false); + } + final ITarget target = new LocationTarget(loc); + now(teleportOwner, target, cause); + } - if (!teleportee.getBase().isEmpty()) { - if (!ess.getSettings().isTeleportPassengerDismount()) { - throw new Exception(tl("passengerTeleportFail")); - } - teleportee.getBase().eject(); + @Override + @Deprecated + public void now(Player entity, boolean cooldown, TeleportCause cause) throws Exception { + if (cooldown) { + cooldown(false); + } + final ITarget target = new PlayerTarget(entity); + now(teleportOwner, target, cause); + teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); } - if (LocationUtil.isBlockUnsafeForUser(teleportee, loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) { - if (ess.getSettings().isTeleportSafetyEnabled()) { - if (ess.getSettings().isForceDisableTeleportSafety()) { - PaperLib.teleportAsync(teleportee.getBase(), loc, cause); - } else { - PaperLib.teleportAsync(teleportee.getBase(), LocationUtil.getSafeDestination(ess, teleportee, loc), cause); + @Deprecated + protected void now(IUser teleportee, ITarget target, TeleportCause cause) throws Exception { + cancel(false); + Location loc = target.getLocation(); + + UserTeleportEvent event = new UserTeleportEvent(teleportee, cause, loc); + Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; } - } else { - throw new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - } - } else { - if (ess.getSettings().isForceDisableTeleportSafety()) { - PaperLib.teleportAsync(teleportee.getBase(), loc, cause); - } else { - if (ess.getSettings().isTeleportToCenterLocation()) { - loc = LocationUtil.getRoundedDestination(loc); + + teleportee.setLastLocation(); + + if (!teleportee.getBase().isEmpty()) { + if (!ess.getSettings().isTeleportPassengerDismount()) { + throw new Exception(tl("passengerTeleportFail")); + } + teleportee.getBase().eject(); } - PaperLib.teleportAsync(teleportee.getBase(), loc, cause); - } + + if (LocationUtil.isBlockUnsafeForUser(teleportee, loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) { + if (ess.getSettings().isTeleportSafetyEnabled()) { + if (ess.getSettings().isForceDisableTeleportSafety()) { + PaperLib.teleportAsync(teleportee.getBase(), loc, cause); + } else { + PaperLib.teleportAsync(teleportee.getBase(), LocationUtil.getSafeDestination(ess, teleportee, loc), cause); + } + } else { + throw new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + } + } else { + if (ess.getSettings().isForceDisableTeleportSafety()) { + PaperLib.teleportAsync(teleportee.getBase(), loc, cause); + } else { + if (ess.getSettings().isTeleportToCenterLocation()) { + loc = LocationUtil.getRoundedDestination(loc); + } + PaperLib.teleportAsync(teleportee.getBase(), loc, cause); + } + } + } + + //The teleportPlayer function is used when you want to normally teleportPlayer someone to a location or player. + //This method is nolonger used internally and will be removed. + @Deprecated + @Override + public void teleport(Location loc, Trade chargeFor) throws Exception { + teleport(loc, chargeFor, TeleportCause.PLUGIN); + } + + @Override + @Deprecated + public void teleport(Location loc, Trade chargeFor, TeleportCause cause) throws Exception { + teleport(teleportOwner, new LocationTarget(loc), chargeFor, cause); + } + + //This is used when teleporting to a player + @Override + @Deprecated + public void teleport(Player entity, Trade chargeFor, TeleportCause cause) throws Exception { + ITarget target = new PlayerTarget(entity); + teleportOwner.sendMessage(tl("teleportToPlayer", entity.getDisplayName())); + teleport(teleportOwner, target, chargeFor, cause); + } + + //This is used when teleporting to stored location + @Override + @Deprecated + public void teleportPlayer(IUser teleportee, Location loc, Trade chargeFor, TeleportCause cause) throws Exception { + teleport(teleportee, new LocationTarget(loc), chargeFor, cause); } - } - - //The teleportPlayer function is used when you want to normally teleportPlayer someone to a location or player. - //This method is nolonger used internally and will be removed. - @Deprecated - @Override - public void teleport(Location loc, Trade chargeFor) throws Exception { - teleport(loc, chargeFor, TeleportCause.PLUGIN); - } - - @Override - @Deprecated - public void teleport(Location loc, Trade chargeFor, TeleportCause cause) throws Exception { - teleport(teleportOwner, new LocationTarget(loc), chargeFor, cause); - } - - //This is used when teleporting to a player - @Override - @Deprecated - public void teleport(Player entity, Trade chargeFor, TeleportCause cause) throws Exception { - ITarget target = new PlayerTarget(entity); - teleportOwner.sendMessage(tl("teleportToPlayer", entity.getDisplayName())); - teleport(teleportOwner, target, chargeFor, cause); - } - - //This is used when teleporting to stored location - @Override - @Deprecated - public void teleportPlayer(IUser teleportee, Location loc, Trade chargeFor, TeleportCause cause) throws Exception { - teleport(teleportee, new LocationTarget(loc), chargeFor, cause); - } - - //This is used on /tphere - @Override - @Deprecated - public void teleportPlayer(IUser teleportee, Player entity, Trade chargeFor, TeleportCause cause) throws Exception { - ITarget target = new PlayerTarget(entity); - teleport(teleportee, target, chargeFor, cause); - teleportee.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); - teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); - } - - @Deprecated - private void teleport(IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause) throws Exception { - double delay = ess.getSettings().getTeleportDelay(); - - Trade cashCharge = chargeFor; - - if (chargeFor != null) { - chargeFor.isAffordableFor(teleportOwner); - - //This code is to make sure that commandcosts are checked in the initial world, and not in the resulting world. - if (!chargeFor.getCommandCost(teleportOwner).equals(BigDecimal.ZERO)) { - //By converting a command cost to a regular cost, the command cost permission isn't checked when executing the charge after teleport. - cashCharge = new Trade(chargeFor.getCommandCost(teleportOwner), ess); - } + + //This is used on /tphere + @Override + @Deprecated + public void teleportPlayer(IUser teleportee, Player entity, Trade chargeFor, TeleportCause cause) throws Exception { + ITarget target = new PlayerTarget(entity); + teleport(teleportee, target, chargeFor, cause); + teleportee.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); + teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); } - cooldown(true); - if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") || teleportee.isAuthorized("essentials.teleport.timer.bypass")) { - cooldown(false); - now(teleportee, target, cause); - if (cashCharge != null) { - cashCharge.charge(teleportOwner); - } - return; + @Deprecated + private void teleport(IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause) throws Exception { + double delay = ess.getSettings().getTeleportDelay(); + + Trade cashCharge = chargeFor; + + if (chargeFor != null) { + chargeFor.isAffordableFor(teleportOwner); + + //This code is to make sure that commandcosts are checked in the initial world, and not in the resulting world. + if (!chargeFor.getCommandCost(teleportOwner).equals(BigDecimal.ZERO)) { + //By converting a command cost to a regular cost, the command cost permission isn't checked when executing the charge after teleport. + cashCharge = new Trade(chargeFor.getCommandCost(teleportOwner), ess); + } + } + + cooldown(true); + if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") || teleportee.isAuthorized("essentials.teleport.timer.bypass")) { + cooldown(false); + now(teleportee, target, cause); + if (cashCharge != null) { + cashCharge.charge(teleportOwner); + } + return; + } + + cancel(false); + warnUser(teleportee, delay); + initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false); } - cancel(false); - warnUser(teleportee, delay); - initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false); - } + @Deprecated + private void teleportOther(IUser teleporter, IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause) throws Exception { + double delay = ess.getSettings().getTeleportDelay(); - @Deprecated - private void teleportOther(IUser teleporter, IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause) throws Exception { - double delay = ess.getSettings().getTeleportDelay(); + Trade cashCharge = chargeFor; - Trade cashCharge = chargeFor; + if (teleporter != null && chargeFor != null) { + chargeFor.isAffordableFor(teleporter); - if (teleporter != null && chargeFor != null) { - chargeFor.isAffordableFor(teleporter); + //This code is to make sure that commandcosts are checked in the initial world, and not in the resulting world. + if (!chargeFor.getCommandCost(teleporter).equals(BigDecimal.ZERO)) { + //By converting a command cost to a regular cost, the command cost permission isn't checked when executing the charge after teleport. + cashCharge = new Trade(chargeFor.getCommandCost(teleporter), ess); + } + } - //This code is to make sure that commandcosts are checked in the initial world, and not in the resulting world. - if (!chargeFor.getCommandCost(teleporter).equals(BigDecimal.ZERO)) { - //By converting a command cost to a regular cost, the command cost permission isn't checked when executing the charge after teleport. - cashCharge = new Trade(chargeFor.getCommandCost(teleporter), ess); - } + cooldown(true); + if (delay <= 0 || teleporter == null + || teleporter.isAuthorized("essentials.teleport.timer.bypass") + || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") + || teleportee.isAuthorized("essentials.teleport.timer.bypass")) { + cooldown(false); + now(teleportee, target, cause); + if (teleporter != null && cashCharge != null) { + cashCharge.charge(teleporter); + } + return; + } + + cancel(false); + warnUser(teleportee, delay); + initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false); } - cooldown(true); - if (delay <= 0 || teleporter == null - || teleporter.isAuthorized("essentials.teleport.timer.bypass") - || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") - || teleportee.isAuthorized("essentials.teleport.timer.bypass")) { - cooldown(false); - now(teleportee, target, cause); - if (teleporter != null && cashCharge != null) { - cashCharge.charge(teleporter); - } - return; + //The respawn function is a wrapper used to handle tp fallback, on /jail and /home + @Override + @Deprecated + public void respawn(final Trade chargeFor, TeleportCause cause) throws Exception { + double delay = ess.getSettings().getTeleportDelay(); + if (chargeFor != null) { + chargeFor.isAffordableFor(teleportOwner); + } + cooldown(true); + if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass")) { + cooldown(false); + respawnNow(teleportOwner, cause); + if (chargeFor != null) { + chargeFor.charge(teleportOwner); + } + return; + } + + cancel(false); + warnUser(teleportOwner, delay); + initTimer((long) (delay * 1000.0), teleportOwner, null, chargeFor, cause, true); } - cancel(false); - warnUser(teleportee, delay); - initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false); - } - - //The respawn function is a wrapper used to handle tp fallback, on /jail and /home - @Override - @Deprecated - public void respawn(final Trade chargeFor, TeleportCause cause) throws Exception { - double delay = ess.getSettings().getTeleportDelay(); - if (chargeFor != null) { - chargeFor.isAffordableFor(teleportOwner); + @Deprecated + void respawnNow(IUser teleportee, TeleportCause cause) throws Exception { + final Player player = teleportee.getBase(); + Location bed = player.getBedSpawnLocation(); + if (bed != null) { + now(teleportee, new LocationTarget(bed), cause); + } else { + if (ess.getSettings().isDebug()) { + ess.getLogger().info("Could not find bed spawn, forcing respawn event."); + } + final PlayerRespawnEvent pre = new PlayerRespawnEvent(player, player.getWorld().getSpawnLocation(), false); + ess.getServer().getPluginManager().callEvent(pre); + now(teleportee, new LocationTarget(pre.getRespawnLocation()), cause); + } } - cooldown(true); - if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass")) { - cooldown(false); - respawnNow(teleportOwner, cause); - if (chargeFor != null) { - chargeFor.charge(teleportOwner); - } - return; + + //The warp function is a wrapper used to teleportPlayer a player to a /warp + @Override + @Deprecated + public void warp(IUser teleportee, String warp, Trade chargeFor, TeleportCause cause) throws Exception { + UserWarpEvent event = new UserWarpEvent(teleportee, warp, chargeFor); + Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return; + } + + warp = event.getWarp(); + Location loc = ess.getWarps().getWarp(warp); + teleportee.sendMessage(tl("warpingTo", warp, loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + if (!teleportee.equals(teleportOwner)) { + teleportOwner.sendMessage(tl("warpingTo", warp, loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + } + teleport(teleportee, new LocationTarget(loc), chargeFor, cause); } - cancel(false); - warnUser(teleportOwner, delay); - initTimer((long) (delay * 1000.0), teleportOwner, null, chargeFor, cause, true); - } - - @Deprecated - void respawnNow(IUser teleportee, TeleportCause cause) throws Exception { - final Player player = teleportee.getBase(); - Location bed = player.getBedSpawnLocation(); - if (bed != null) { - now(teleportee, new LocationTarget(bed), cause); - } else { - if (ess.getSettings().isDebug()) { - ess.getLogger().info("Could not find bed spawn, forcing respawn event."); - } - final PlayerRespawnEvent pre = new PlayerRespawnEvent(player, player.getWorld().getSpawnLocation(), false); - ess.getServer().getPluginManager().callEvent(pre); - now(teleportee, new LocationTarget(pre.getRespawnLocation()), cause); + //The back function is a wrapper used to teleportPlayer a player /back to their previous location. + @Override + @Deprecated + public void back(Trade chargeFor) throws Exception { + back(teleportOwner, chargeFor); } - } - - //The warp function is a wrapper used to teleportPlayer a player to a /warp - @Override - @Deprecated - public void warp(IUser teleportee, String warp, Trade chargeFor, TeleportCause cause) throws Exception { - UserWarpEvent event = new UserWarpEvent(teleportee, warp, chargeFor); - Bukkit.getServer().getPluginManager().callEvent(event); - if (event.isCancelled()) { - return; + + //This function is a wrapper over the other back function for cases where another player performs back for them + @Override + @Deprecated + public void back(IUser teleporter, Trade chargeFor) throws Exception { + tpType = TeleportType.BACK; + final Location loc = teleportOwner.getLastLocation(); + teleportOwner.sendMessage(tl("backUsageMsg", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + teleportOther(teleporter, teleportOwner, new LocationTarget(loc), chargeFor, TeleportCause.COMMAND); + } + + //This function is used to throw a user back after a jail sentence + @Override + @Deprecated + public void back() throws Exception { + now(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()), TeleportCause.COMMAND); } - warp = event.getWarp(); - Location loc = ess.getWarps().getWarp(warp); - teleportee.sendMessage(tl("warpingTo", warp, loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - if (!teleportee.equals(teleportOwner)) { - teleportOwner.sendMessage(tl("warpingTo", warp, loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + @Deprecated + public void setTpType(TeleportType tpType) { + this.tpType = tpType; } - teleport(teleportee, new LocationTarget(loc), chargeFor, cause); - } - - //The back function is a wrapper used to teleportPlayer a player /back to their previous location. - @Override - @Deprecated - public void back(Trade chargeFor) throws Exception { - back(teleportOwner, chargeFor); - } - - //This function is a wrapper over the other back function for cases where another player performs back for them - @Override - @Deprecated - public void back(IUser teleporter, Trade chargeFor) throws Exception { - tpType = TeleportType.BACK; - final Location loc = teleportOwner.getLastLocation(); - teleportOwner.sendMessage(tl("backUsageMsg", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - teleportOther(teleporter, teleportOwner, new LocationTarget(loc), chargeFor, TeleportCause.COMMAND); - } - - //This function is used to throw a user back after a jail sentence - @Override - @Deprecated - public void back() throws Exception { - now(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()), TeleportCause.COMMAND); - } - - @Deprecated - public void setTpType(TeleportType tpType) { - this.tpType = tpType; - } - - //If we need to cancelTimer a pending teleportPlayer call this method - @Deprecated - private void cancel(boolean notifyUser) { - if (timedTeleport != null) { - timedTeleport.cancelTimer(notifyUser); - timedTeleport = null; + + //If we need to cancelTimer a pending teleportPlayer call this method + @Deprecated + private void cancel(boolean notifyUser) { + if (timedTeleport != null) { + timedTeleport.cancelTimer(notifyUser); + timedTeleport = null; + } } - } - @Deprecated - private void initTimer(long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) { - timedTeleport = new TimedTeleport(teleportOwner, ess, this, delay, teleportUser, target, chargeFor, cause, respawn); - } + @Deprecated + private void initTimer(long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) { + timedTeleport = new TimedTeleport(teleportOwner, ess, this, delay, teleportUser, target, chargeFor, cause, respawn); + } } \ No newline at end of file diff --git a/Essentials/src/com/earth2me/essentials/api/IJails.java b/Essentials/src/com/earth2me/essentials/api/IJails.java index 60543288d47..9199b3e60ea 100644 --- a/Essentials/src/com/earth2me/essentials/api/IJails.java +++ b/Essentials/src/com/earth2me/essentials/api/IJails.java @@ -54,6 +54,7 @@ public interface IJails extends IReload { * * @throws Exception if the user is offline or jail does not exist */ + @Deprecated void sendToJail(IUser user, String jail) throws Exception; /** diff --git a/Essentials/src/com/earth2me/essentials/api/ITeleport.java b/Essentials/src/com/earth2me/essentials/api/ITeleport.java index 3743f868e0c..354205cf190 100644 --- a/Essentials/src/com/earth2me/essentials/api/ITeleport.java +++ b/Essentials/src/com/earth2me/essentials/api/ITeleport.java @@ -11,137 +11,137 @@ * @deprecated This API is not asynchronous. Use {@link com.earth2me.essentials.api.IAsyncTeleport IAsyncTeleport} */ public interface ITeleport { - /** - * Used to skip teleportPlayer delay when teleporting someone to a location or player. - * - * @param loc - Where should the player end up - * @param cooldown - If cooldown should be enforced - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void now(Location loc, boolean cooldown, PlayerTeleportEvent.TeleportCause cause) throws Exception; - - /** - * Used to skip teleportPlayer delay when teleporting someone to a location or player. - * - * @param entity - Where should the player end up - * @param cooldown - If cooldown should be enforced - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void now(Player entity, boolean cooldown, PlayerTeleportEvent.TeleportCause cause) throws Exception; - - @Deprecated - void teleport(Location loc, Trade chargeFor) throws Exception; - - /** - * Teleport a player to a specific location - * - * @param loc - Where should the player end up - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void teleport(Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; - - /** - * Teleport a player to a specific player - * - * @param entity - Where should the player end up - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void teleport(Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; - - /** - * Teleport a player to a specific location - * - * @param otherUser - Which user will be teleported - * @param loc - Where should the player end up - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; - - /** - * Teleport a player to a specific player - * - * @param otherUser - Which user will be teleported - * @param entity - Where should the player end up - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; - - /** - * Teleport wrapper used to handle tp fallback on /jail and /home - * - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void respawn(final Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; - - /** - * Teleport wrapper used to handle /warp teleports - * - * @param otherUser - Which user will be teleported - * @param warp - The name of the warp the user will be teleported too. - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param cause - The reported teleportPlayer cause - * - * @throws Exception - */ - @Deprecated - void warp(IUser otherUser, String warp, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; - - /** - * Teleport wrapper used to handle /back teleports - * - * @param chargeFor - What the user will be charged if teleportPlayer is successful - * - * @throws Exception - */ - @Deprecated - void back(Trade chargeFor) throws Exception; - - /** - * Teleport wrapper used to handle /back teleports that - * are executed by a different player with this - * instance of teleport as a target. - * - * @param teleporter - The user performing the /back command. - * This value may be {@code null} to indicate console. - * @param chargeFor - What the {@code teleporter} will be charged if teleportPlayer is successful - * - * @throws Exception - */ - @Deprecated - void back(IUser teleporter, Trade chargeFor) throws Exception; - - /** - * Teleport wrapper used to handle throwing user home after a jail sentence - * - * @throws Exception - */ - @Deprecated - void back() throws Exception; + /** + * Used to skip teleportPlayer delay when teleporting someone to a location or player. + * + * @param loc - Where should the player end up + * @param cooldown - If cooldown should be enforced + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void now(Location loc, boolean cooldown, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Used to skip teleportPlayer delay when teleporting someone to a location or player. + * + * @param entity - Where should the player end up + * @param cooldown - If cooldown should be enforced + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void now(Player entity, boolean cooldown, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + @Deprecated + void teleport(Location loc, Trade chargeFor) throws Exception; + + /** + * Teleport a player to a specific location + * + * @param loc - Where should the player end up + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void teleport(Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Teleport a player to a specific player + * + * @param entity - Where should the player end up + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + + * @throws Exception + */ + @Deprecated + void teleport(Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Teleport a player to a specific location + * + * @param otherUser - Which user will be teleported + * @param loc - Where should the player end up + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Teleport a player to a specific player + * + * @param otherUser - Which user will be teleported + * @param entity - Where should the player end up + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Teleport wrapper used to handle tp fallback on /jail and /home + * + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void respawn(final Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Teleport wrapper used to handle /warp teleports + * + * @param otherUser - Which user will be teleported + * @param warp - The name of the warp the user will be teleported too. + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * @param cause - The reported teleportPlayer cause + * + * @throws Exception + */ + @Deprecated + void warp(IUser otherUser, String warp, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause) throws Exception; + + /** + * Teleport wrapper used to handle /back teleports + * + * @param chargeFor - What the user will be charged if teleportPlayer is successful + * + * @throws Exception + */ + @Deprecated + void back(Trade chargeFor) throws Exception; + + /** + * Teleport wrapper used to handle /back teleports that + * are executed by a different player with this + * instance of teleport as a target. + * + * @param teleporter - The user performing the /back command. + * This value may be {@code null} to indicate console. + * @param chargeFor - What the {@code teleporter} will be charged if teleportPlayer is successful + * + * @throws Exception + */ + @Deprecated + void back(IUser teleporter, Trade chargeFor) throws Exception; + + /** + * Teleport wrapper used to handle throwing user home after a jail sentence + * + * @throws Exception + */ + @Deprecated + void back() throws Exception; } \ No newline at end of file From 9302e413e97ff077192c403594c148a279ed2991 Mon Sep 17 00:00:00 2001 From: JRoy Date: Fri, 10 Apr 2020 15:46:15 -0400 Subject: [PATCH 17/36] Add legacy sendToJail method --- .../src/com/earth2me/essentials/Jails.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Jails.java b/Essentials/src/com/earth2me/essentials/Jails.java index 7878dd6fb8d..3e03e0dab01 100644 --- a/Essentials/src/com/earth2me/essentials/Jails.java +++ b/Essentials/src/com/earth2me/essentials/Jails.java @@ -117,14 +117,21 @@ public void removeJail(final String jail) throws Exception { } } + /** + * @deprecated This method does not use asynchronous teleportation. Use {@link Jails#sendToJail(IUser, String, CompletableFuture, CompletableFuture)} + */ @Override @Deprecated public void sendToJail(final IUser user, final String jail) throws Exception { - CompletableFuture eFuture = new CompletableFuture<>(); - CompletableFuture future = new CompletableFuture<>(); - sendToJail(user, jail, eFuture, future); - if (!future.get()) { - throw eFuture.get(); + acquireReadLock(); + try { + if (user.getBase().isOnline()) { + Location loc = getJail(jail); + user.getTeleport().now(loc, false, TeleportCause.COMMAND); + } + user.setJail(jail); + } finally { + unlock(); } } From fd0ce61530843bd862ccd8dd0e55137fa1a3035c Mon Sep 17 00:00:00 2001 From: JRoy Date: Fri, 10 Apr 2020 15:49:59 -0400 Subject: [PATCH 18/36] Reduce Diff Further --- Essentials/src/com/earth2me/essentials/Teleport.java | 2 +- Essentials/src/com/earth2me/essentials/api/ITeleport.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java index c9e17358852..1e939b05cd1 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -376,4 +376,4 @@ private void cancel(boolean notifyUser) { private void initTimer(long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn) { timedTeleport = new TimedTeleport(teleportOwner, ess, this, delay, teleportUser, target, chargeFor, cause, respawn); } -} \ No newline at end of file +} diff --git a/Essentials/src/com/earth2me/essentials/api/ITeleport.java b/Essentials/src/com/earth2me/essentials/api/ITeleport.java index 354205cf190..5993b2a8a0a 100644 --- a/Essentials/src/com/earth2me/essentials/api/ITeleport.java +++ b/Essentials/src/com/earth2me/essentials/api/ITeleport.java @@ -56,7 +56,7 @@ public interface ITeleport { * @param entity - Where should the player end up * @param chargeFor - What the user will be charged if teleportPlayer is successful * @param cause - The reported teleportPlayer cause - + * * @throws Exception */ @Deprecated @@ -144,4 +144,4 @@ public interface ITeleport { @Deprecated void back() throws Exception; -} \ No newline at end of file +} From 332545e675dc506fcb54c193a248756194b53c0c Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 13 Apr 2020 10:15:00 -0400 Subject: [PATCH 19/36] Use getNewExceptionFuture in Commandtpo --- .../src/com/earth2me/essentials/commands/Commandtpo.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java index 881823e0873..5573dc6826c 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java @@ -18,8 +18,6 @@ public Commandtpo() { @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { - CompletableFuture eFuture = new CompletableFuture<>(); - eFuture.thenAccept(e -> showError(user.getBase(), e, commandLabel)); switch (args.length) { case 0: throw new NotEnoughArgumentsException(); @@ -29,7 +27,7 @@ public void run(final Server server, final User user, final String commandLabel, if (user.getWorld() != player.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + player.getWorld().getName())) { throw new Exception(tl("noPerm", "essentials.worlds." + player.getWorld().getName())); } - user.getAsyncTeleport().now(player.getBase(), false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + user.getAsyncTeleport().now(player.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); break; default: @@ -49,7 +47,7 @@ public void run(final Server server, final User user, final String commandLabel, target.sendMessage(tl("teleportAtoB", user.getDisplayName(), toPlayer.getDisplayName())); } }); - target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, eFuture, future); + target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); break; } } From d4df801271f808abb4160a242a8625e849f03465 Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 13 Apr 2020 11:44:59 -0400 Subject: [PATCH 20/36] Use getNewExceptionFuture everywhere --- .../com/earth2me/essentials/commands/Commandhome.java | 10 ++++------ .../com/earth2me/essentials/commands/Commandtp.java | 10 ++++------ .../earth2me/essentials/commands/Commandtpaccept.java | 3 +-- .../com/earth2me/essentials/commands/Commandtpall.java | 4 +--- .../earth2me/essentials/commands/Commandtpoffline.java | 4 +--- .../earth2me/essentials/commands/Commandtpohere.java | 4 +--- 6 files changed, 12 insertions(+), 23 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java index 827e0d82194..67e353cd971 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java @@ -38,32 +38,30 @@ public void run(final Server server, final User user, final String commandLabel, } } } - CompletableFuture eFuture = new CompletableFuture<>(); - eFuture.thenAccept(e -> showError(user.getBase(), e, commandLabel)); try { if ("bed".equalsIgnoreCase(homeName) && user.isAuthorized("essentials.home.bed")) { final Location bed = player.getBase().getBedSpawnLocation(); if (bed != null) { - user.getAsyncTeleport().teleport(bed, charge, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + user.getAsyncTeleport().teleport(bed, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); return; } else { throw new Exception(tl("bedMissing")); } } - goHome(user, player, homeName.toLowerCase(Locale.ENGLISH), charge, eFuture); + goHome(user, player, homeName.toLowerCase(Locale.ENGLISH), charge, getNewExceptionFuture(user.getSource(), commandLabel)); } catch (NotEnoughArgumentsException e) { Location bed = player.getBase().getBedSpawnLocation(); final List homes = player.getHomes(); if (homes.isEmpty() && player.equals(user)) { if (ess.getSettings().isSpawnIfNoHome()) { - user.getAsyncTeleport().respawn(charge, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + user.getAsyncTeleport().respawn(charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } else { throw new Exception(tl("noHomeSetPlayer")); } } else if (homes.isEmpty()) { throw new Exception(tl("noHomeSetPlayer")); } else if (homes.size() == 1 && player.equals(user)) { - goHome(user, player, homes.get(0), charge, eFuture); + goHome(user, player, homes.get(0), charge, getNewExceptionFuture(user.getSource(), commandLabel)); } else { final int count = homes.size(); if (user.isAuthorized("essentials.home.bed")) { diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtp.java b/Essentials/src/com/earth2me/essentials/commands/Commandtp.java index 04e25664381..5b0f0e05e9c 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtp.java @@ -22,9 +22,7 @@ public Commandtp() { @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { - CompletableFuture eFuture = new CompletableFuture<>(); CompletableFuture future = new CompletableFuture<>(); - eFuture.thenAccept(e -> showError(user.getBase(), e, commandLabel)); switch (args.length) { case 0: throw new NotEnoughArgumentsException(); @@ -45,7 +43,7 @@ public void run(final Server server, final User user, final String commandLabel, } final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); - user.getAsyncTeleport().teleport(player.getBase(), charge, TeleportCause.COMMAND, eFuture, future); + user.getAsyncTeleport().teleport(player.getBase(), charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); break; case 3: if (!user.isAuthorized("essentials.tp.position")) { @@ -58,7 +56,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new NotEnoughArgumentsException(tl("teleportInvalidLocation")); } final Location locpos = new Location(user.getWorld(), x2, y2, z2, user.getLocation().getYaw(), user.getLocation().getPitch()); - user.getAsyncTeleport().now(locpos, false, TeleportCause.COMMAND, eFuture, future); + user.getAsyncTeleport().now(locpos, false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); future.thenAccept(success -> { if (success) { user.sendMessage(tl("teleporting", locpos.getWorld().getName(), locpos.getBlockX(), locpos.getBlockY(), locpos.getBlockZ())); @@ -84,7 +82,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("teleportDisabled", target2.getDisplayName())); } user.sendMessage(tl("teleporting", locposother.getWorld().getName(), locposother.getBlockX(), locposother.getBlockY(), locposother.getBlockZ())); - target2.getAsyncTeleport().now(locposother, false, TeleportCause.COMMAND, eFuture, future); + target2.getAsyncTeleport().now(locposother, false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); future.thenAccept(success -> { if (success) { target2.sendMessage(tl("teleporting", locposother.getWorld().getName(), locposother.getBlockX(), locposother.getBlockY(), locposother.getBlockZ())); @@ -108,7 +106,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("noPerm", "essentials.worlds." + toPlayer.getWorld().getName())); } target.sendMessage(tl("teleportAtoB", user.getDisplayName(), toPlayer.getDisplayName())); - target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, eFuture, future); + target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); break; } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java index 251a71820da..4dc10482de9 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java @@ -51,10 +51,9 @@ public void run(final Server server, final User user, final String commandLabel, user.sendMessage(tl("requestAccepted")); requester.sendMessage(tl("requestAcceptedFrom", user.getDisplayName())); - CompletableFuture eFuture = new CompletableFuture<>(); + CompletableFuture eFuture = getNewExceptionFuture(requester.getSource(), commandLabel); eFuture.thenAccept(e -> { user.sendMessage(tl("pendingTeleportCancelled")); - ess.showError(requester.getSource(), e, commandLabel); }); CompletableFuture future = new CompletableFuture<>(); future.thenAccept(success -> { diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java index c097b6396ca..d29da4181b1 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java @@ -42,9 +42,7 @@ private void teleportAllPlayers(Server server, CommandSource sender, User target if (sender.equals(target.getBase()) && target.getWorld() != player.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !target.isAuthorized("essentials.worlds." + target.getWorld().getName())) { continue; } - CompletableFuture eFuture = new CompletableFuture<>(); - eFuture.thenAccept(e -> showError(sender.getSender(), e, label)); - player.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + player.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, getNewExceptionFuture(sender, label), new CompletableFuture<>()); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java index 6fb0684779b..c5e51b76c04 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java @@ -30,9 +30,7 @@ public void run(final Server server, final User user, final String label, final } user.sendMessage(tl("teleporting", logout.getWorld().getName(), logout.getBlockX(), logout.getBlockY(), logout.getBlockZ())); - CompletableFuture eFuture = new CompletableFuture<>(); - eFuture.thenAccept(e -> showError(user.getBase(), e, label)); - user.getAsyncTeleport().now(logout, false, PlayerTeleportEvent.TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + user.getAsyncTeleport().now(logout, false, PlayerTeleportEvent.TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), label), new CompletableFuture<>()); } } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java index 7b81c039f01..bb945c13570 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java @@ -29,9 +29,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("noPerm", "essentials.worlds." + user.getWorld().getName())); } - CompletableFuture eFuture = new CompletableFuture<>(); - eFuture.thenAccept(e -> showError(user.getBase(), e, commandLabel)); - player.getAsyncTeleport().now(user.getBase(), false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); + player.getAsyncTeleport().now(user.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); } @Override From 944f112d557b181f4b4755ae63fbaef22b0805e2 Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 13 Apr 2020 15:34:53 -0400 Subject: [PATCH 21/36] Fix even more usages --- .../src/com/earth2me/essentials/commands/Commandtp.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtp.java b/Essentials/src/com/earth2me/essentials/commands/Commandtp.java index 5b0f0e05e9c..ae9c2568fdd 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtp.java @@ -121,9 +121,7 @@ public void run(final Server server, final CommandSource sender, final String co if (args.length == 2) { final User toPlayer = getPlayer(server, args, 1, true, false); target.sendMessage(tl("teleportAtoB", Console.NAME, toPlayer.getDisplayName())); - CompletableFuture eFuture = new CompletableFuture<>(); - target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, eFuture, new CompletableFuture<>()); - eFuture.thenAccept(e -> showError(sender.getSender(), e, commandLabel)); + target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); } else if (args.length > 3) { final double x = args[1].startsWith("~") ? target.getLocation().getX() + (args[1].length() > 1 ? Double.parseDouble(args[1].substring(1)) : 0) : Double.parseDouble(args[1]); final double y = args[2].startsWith("~") ? target.getLocation().getY() + (args[2].length() > 1 ? Double.parseDouble(args[2].substring(1)) : 0) : Double.parseDouble(args[2]); @@ -133,15 +131,13 @@ public void run(final Server server, final CommandSource sender, final String co } final Location loc = new Location(target.getWorld(), x, y, z, target.getLocation().getYaw(), target.getLocation().getPitch()); sender.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - CompletableFuture eFuture = new CompletableFuture<>(); CompletableFuture future = new CompletableFuture<>(); - target.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, eFuture, future); + target.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, getNewExceptionFuture(sender, commandLabel), future); future.thenAccept(success -> { if (success) { target.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); } }); - eFuture.thenAccept(e -> showError(sender.getSender(), e, commandLabel)); } else { throw new NotEnoughArgumentsException(); } From 015eeedc07957d2dcf3f9ef5f09585211dfce91c Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Mon, 20 Apr 2020 12:36:48 -0400 Subject: [PATCH 22/36] Revert LocationUtil.java --- .../essentials/utils/LocationUtil.java | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java index ab43e9ab3b7..2683e1e7d7f 100644 --- a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java @@ -2,7 +2,10 @@ import com.earth2me.essentials.IEssentials; import net.ess3.api.IUser; -import org.bukkit.*; +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.LivingEntity; import org.bukkit.inventory.ItemStack; @@ -86,26 +89,18 @@ public static Location getTarget(final LivingEntity entity) throws Exception { } public static boolean isBlockAboveAir(final World world, final int x, final int y, final int z) { - return isBlockAboveAir(world.getChunkAt(x, z), x, y, z); - } - - public static boolean isBlockAboveAir(final Chunk chunk, final int x, final int y, int z) { - return y > chunk.getWorld().getMaxHeight() || HOLLOW_MATERIALS.contains(chunk.getBlock(x & 0xF, y - 1, z & 0xF).getType()); + return y > world.getMaxHeight() || HOLLOW_MATERIALS.contains(world.getBlockAt(x, y - 1, z).getType()); } public static boolean isBlockUnsafeForUser(final IUser user, final World world, final int x, final int y, final int z) { - return isBlockUnsafeForUser(user, world.getChunkAt(x, z), x, y, z); - } - - public static boolean isBlockUnsafeForUser(final IUser user, final Chunk chunk, final int x, final int y, final int z) { - if (user.getBase().isOnline() && chunk.getWorld().equals(user.getBase().getWorld()) && (user.getBase().getGameMode() == GameMode.CREATIVE || user.getBase().getGameMode() == GameMode.SPECTATOR || user.isGodModeEnabled()) && user.getBase().getAllowFlight()) { + if (user.getBase().isOnline() && world.equals(user.getBase().getWorld()) && (user.getBase().getGameMode() == GameMode.CREATIVE || user.getBase().getGameMode() == GameMode.SPECTATOR || user.isGodModeEnabled()) && user.getBase().getAllowFlight()) { return false; } - if (isBlockDamaging(chunk, x, y, z)) { + if (isBlockDamaging(world, x, y, z)) { return true; } - return isBlockAboveAir(chunk, x, y, z); + return isBlockAboveAir(world, x, y, z); } public static boolean isBlockUnsafe(final World world, final int x, final int y, final int z) { @@ -113,11 +108,7 @@ public static boolean isBlockUnsafe(final World world, final int x, final int y, } public static boolean isBlockDamaging(final World world, final int x, final int y, final int z) { - return isBlockDamaging(world.getChunkAt(x, z), x, y, z); - } - - public static boolean isBlockDamaging(final Chunk chunk, final int x, final int y, final int z) { - final Block below = chunk.getBlock(x & 0xF, y - 1, z & 0xF); + final Block below = world.getBlockAt(x, y - 1, z); switch (below.getType()) { case LAVA: @@ -137,13 +128,11 @@ public static boolean isBlockDamaging(final Chunk chunk, final int x, final int Material PORTAL = EnumUtil.getMaterial("NETHER_PORTAL", "PORTAL"); - Block block = chunk.getBlock(x & 0xF, y, z & 0xF); - - if (block.getType() == PORTAL) { + if (world.getBlockAt(x, y, z).getType() == PORTAL) { return true; } - return (!HOLLOW_MATERIALS.contains(block.getType())) || (!HOLLOW_MATERIALS.contains(chunk.getBlock(x & 0xF, y + 1, z & 0xF).getType())); + return (!HOLLOW_MATERIALS.contains(world.getBlockAt(x, y, z).getType())) || (!HOLLOW_MATERIALS.contains(world.getBlockAt(x, y + 1, z).getType())); } // Not needed if using getSafeDestination(loc) From 1013dd8ab912c0574d0694112ba0e54fc0d97f17 Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 20 Apr 2020 12:41:39 -0400 Subject: [PATCH 23/36] Fix issue causing unsafe locations to not work properly --- Essentials/src/com/earth2me/essentials/AsyncTeleport.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Essentials/src/com/earth2me/essentials/AsyncTeleport.java b/Essentials/src/com/earth2me/essentials/AsyncTeleport.java index 7910fbf8436..b6a3bc1b1b0 100644 --- a/Essentials/src/com/earth2me/essentials/AsyncTeleport.java +++ b/Essentials/src/com/earth2me/essentials/AsyncTeleport.java @@ -170,7 +170,7 @@ protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, C teleportee.setLastLocation(); PaperLib.getChunkAtAsync(target.getLocation()).thenAccept(chunk -> { Location loc = target.getLocation(); - if (LocationUtil.isBlockUnsafeForUser(teleportee, chunk, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) { + if (LocationUtil.isBlockUnsafeForUser(teleportee, chunk.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) { if (ess.getSettings().isTeleportSafetyEnabled()) { if (ess.getSettings().isForceDisableTeleportSafety()) { //The chunk we're teleporting to is 100% going to be loaded here, no need to teleport async. From c9d35b4945f55f2d3db000f0078df0dcd2148f07 Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 20 Apr 2020 16:01:24 -0400 Subject: [PATCH 24/36] Add deprecated notice in IUser implementation --- Essentials/src/com/earth2me/essentials/IUser.java | 2 +- Essentials/src/com/earth2me/essentials/User.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Essentials/src/com/earth2me/essentials/IUser.java b/Essentials/src/com/earth2me/essentials/IUser.java index cc6c154cb68..8ca4dfe5931 100644 --- a/Essentials/src/com/earth2me/essentials/IUser.java +++ b/Essentials/src/com/earth2me/essentials/IUser.java @@ -56,7 +56,7 @@ public interface IUser { boolean hasOutstandingTeleportRequest(); /** - * @deprecated This API is not asynchronous. Use {@link com.earth2me.essentials.api.IAsyncTeleport IAsyncTeleport} + * @deprecated This API is not asynchronous. Use {@link com.earth2me.essentials.api.IAsyncTeleport IAsyncTeleport} with {@link IUser#getAsyncTeleport()} */ @Deprecated ITeleport getTeleport(); diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index 6061695ca52..080a2e83297 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -414,7 +414,11 @@ public IAsyncTeleport getAsyncTeleport() { return teleport; } + /** + * @deprecated This API is not asynchronous. Use {@link User#getAsyncTeleport()} + */ @Override + @Deprecated public ITeleport getTeleport() { return legacyTeleport; } From 606b2040eef2b5425243706b1bd183d0abccd4f9 Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 20 Apr 2020 16:37:12 -0400 Subject: [PATCH 25/36] Use CompletableFuture#completeExceptionally for exceptions --- .../earth2me/essentials/AsyncTeleport.java | 139 ++++++++---------- .../essentials/AsyncTimedTeleport.java | 19 ++- .../src/com/earth2me/essentials/Jails.java | 28 ++-- .../src/com/earth2me/essentials/Trade.java | 46 +++--- .../src/com/earth2me/essentials/User.java | 9 +- .../essentials/api/IAsyncTeleport.java | 33 ++--- .../com/earth2me/essentials/api/IJails.java | 5 +- .../essentials/commands/Commandback.java | 7 +- .../essentials/commands/Commandhome.java | 15 +- .../essentials/commands/Commandjump.java | 3 +- .../commands/Commandtogglejail.java | 14 +- .../essentials/commands/Commandtop.java | 2 +- .../essentials/commands/Commandtp.java | 16 +- .../essentials/commands/Commandtpa.java | 12 +- .../essentials/commands/Commandtpaccept.java | 10 +- .../essentials/commands/Commandtpall.java | 3 +- .../essentials/commands/Commandtphere.java | 3 +- .../essentials/commands/Commandtpo.java | 12 +- .../essentials/commands/Commandtpoffline.java | 4 +- .../essentials/commands/Commandtpohere.java | 3 +- .../essentials/commands/Commandtppos.java | 5 +- .../essentials/commands/Commandwarp.java | 5 +- .../essentials/commands/Commandworld.java | 3 +- .../commands/EssentialsCommand.java | 9 +- .../earth2me/essentials/signs/SignWarp.java | 14 +- .../essentials/spawn/Commandspawn.java | 10 +- .../spawn/EssentialsSpawnPlayerListener.java | 9 +- 27 files changed, 207 insertions(+), 231 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/AsyncTeleport.java b/Essentials/src/com/earth2me/essentials/AsyncTeleport.java index b6a3bc1b1b0..46060a6877a 100644 --- a/Essentials/src/com/earth2me/essentials/AsyncTeleport.java +++ b/Essentials/src/com/earth2me/essentials/AsyncTeleport.java @@ -44,14 +44,18 @@ public enum TeleportType { NORMAL } - public void cooldown(boolean check) throws Exception { - CompletableFuture exceptionFuture = new CompletableFuture<>(); + public void cooldown(boolean check) throws Throwable { + CompletableFuture exceptionFuture = new CompletableFuture<>(); if (cooldown(check, exceptionFuture)) { - throw exceptionFuture.get(); + try { + exceptionFuture.get(); + } catch (ExecutionException e) { + throw e.getCause(); + } } } - public boolean cooldown(boolean check, CompletableFuture exceptionFuture) { + public boolean cooldown(boolean check, CompletableFuture future) { final Calendar time = new GregorianCalendar(); if (teleportOwner.getLastTeleportTimestamp() > 0) { // Take the current time, and remove the delay from it. @@ -75,7 +79,7 @@ && cooldownApplies()) { time.setTimeInMillis(lastTime); time.add(Calendar.SECOND, (int) cooldown); time.add(Calendar.MILLISECOND, (int) ((cooldown * 1000.0) % 1000.0)); - exceptionFuture.complete(new Exception(tl("timeBeforeTeleport", DateUtil.formatDateDiff(time.getTimeInMillis())))); + future.completeExceptionally(new Exception(tl("timeBeforeTeleport", DateUtil.formatDateDiff(time.getTimeInMillis())))); return true; } } @@ -114,23 +118,22 @@ private void warnUser(final IUser user, final double delay) { @Override - public void now(Location loc, boolean cooldown, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { - if (cooldown && cooldown(false, exceptionFuture)) { - future.complete(false); + public void now(Location loc, boolean cooldown, TeleportCause cause, CompletableFuture future) { + if (cooldown && cooldown(false, future)) { return; } final ITarget target = new LocationTarget(loc); - nowAsync(teleportOwner, target, cause, exceptionFuture, future); + nowAsync(teleportOwner, target, cause, future); } @Override - public void now(Player entity, boolean cooldown, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { - if (cooldown && cooldown(false, exceptionFuture)) { + public void now(Player entity, boolean cooldown, TeleportCause cause, CompletableFuture future) { + if (cooldown && cooldown(false, future)) { future.complete(false); return; } final ITarget target = new PlayerTarget(entity); - nowAsync(teleportOwner, target, cause, exceptionFuture, future); + nowAsync(teleportOwner, target, cause, future); future.thenAccept(success -> { if (success) { teleportOwner.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); @@ -138,7 +141,7 @@ public void now(Player entity, boolean cooldown, TeleportCause cause, Completabl }); } - protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, CompletableFuture future) { cancel(false); UserTeleportEvent event = new UserTeleportEvent(teleportee, cause, target.getLocation()); @@ -150,8 +153,7 @@ protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, C if (!teleportee.getBase().isEmpty()) { if (!ess.getSettings().isTeleportPassengerDismount()) { - exceptionFuture.complete(new Exception(tl("passengerTeleportFail"))); - future.complete(false); + future.completeExceptionally(new Exception(tl("passengerTeleportFail"))); return; } CompletableFuture dismountFuture = new CompletableFuture<>(); @@ -162,8 +164,7 @@ protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, C try { dismountFuture.get(); //EntityDismountEvent requires sync context we also want to wait for it to finish } catch (InterruptedException | ExecutionException e) { - exceptionFuture.complete(e); - future.complete(false); + future.completeExceptionally(e); return; } } @@ -180,14 +181,12 @@ protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, C //There's a chance the safer location is outside the loaded chunk so still teleport async here. PaperLib.teleportAsync(teleportee.getBase(), LocationUtil.getSafeDestination(ess, teleportee, loc), cause); } catch (Exception e) { - exceptionFuture.complete(e); - future.complete(false); + future.completeExceptionally(e); return; } } } else { - exceptionFuture.complete(new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()))); - future.complete(false); + future.completeExceptionally(new Exception(tl("unsafeTeleportDestination", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()))); return; } } else { @@ -207,25 +206,25 @@ protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, C } @Override - public void teleport(Location loc, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { - teleport(teleportOwner, new LocationTarget(loc), chargeFor, cause, exceptionFuture, future); + public void teleport(Location loc, Trade chargeFor, TeleportCause cause, CompletableFuture future) { + teleport(teleportOwner, new LocationTarget(loc), chargeFor, cause, future); } @Override - public void teleport(Player entity, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + public void teleport(Player entity, Trade chargeFor, TeleportCause cause, CompletableFuture future) { teleportOwner.sendMessage(tl("teleportToPlayer", entity.getDisplayName())); - teleport(teleportOwner, new PlayerTarget(entity), chargeFor, cause, exceptionFuture, future); + teleport(teleportOwner, new PlayerTarget(entity), chargeFor, cause, future); } @Override - public void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { - teleport(otherUser, new LocationTarget(loc), chargeFor, cause, exceptionFuture, future); + public void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, TeleportCause cause, CompletableFuture future) { + teleport(otherUser, new LocationTarget(loc), chargeFor, cause, future); } @Override - public void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + public void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, TeleportCause cause, CompletableFuture future) { ITarget target = new PlayerTarget(entity); - teleport(otherUser, target, chargeFor, cause, exceptionFuture, future); + teleport(otherUser, target, chargeFor, cause, future); future.thenAccept(success -> { if (success) { otherUser.sendMessage(tl("teleporting", target.getLocation().getWorld().getName(), target.getLocation().getBlockX(), target.getLocation().getBlockY(), target.getLocation().getBlockZ())); @@ -234,15 +233,14 @@ public void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, Tele }); } - private void teleport(IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + private void teleport(IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause, CompletableFuture future) { double delay = ess.getSettings().getTeleportDelay(); Trade cashCharge = chargeFor; if (chargeFor != null) { - chargeFor.isAffordableFor(teleportOwner, exceptionFuture); - if (exceptionFuture.isDone()) { - future.complete(false); + chargeFor.isAffordableFor(teleportOwner, future); + if (future.isCompletedExceptionally()) { return; } @@ -253,20 +251,17 @@ private void teleport(IUser teleportee, ITarget target, Trade chargeFor, Telepor } } - if (cooldown(true, exceptionFuture)) { - future.complete(false); + if (cooldown(true, future)) { return; } if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") || teleportee.isAuthorized("essentials.teleport.timer.bypass")) { - if (cooldown(false, exceptionFuture)) { - future.complete(false); + if (cooldown(false, future)) { return; } - nowAsync(teleportee, target, cause, exceptionFuture, future); + nowAsync(teleportee, target, cause, future); if (cashCharge != null) { - cashCharge.charge(teleportOwner, exceptionFuture); - if (exceptionFuture.isDone()) { - future.complete(false); + cashCharge.charge(teleportOwner, future); + if (future.isCompletedExceptionally()) { return; } } @@ -278,15 +273,14 @@ private void teleport(IUser teleportee, ITarget target, Trade chargeFor, Telepor initTimer((long) (delay * 1000.0), teleportee, target, cashCharge, cause, false); } - private void teleportOther(IUser teleporter, IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + private void teleportOther(IUser teleporter, IUser teleportee, ITarget target, Trade chargeFor, TeleportCause cause, CompletableFuture future) { double delay = ess.getSettings().getTeleportDelay(); Trade cashCharge = chargeFor; if (teleporter != null && chargeFor != null) { - chargeFor.isAffordableFor(teleporter, exceptionFuture); - if (exceptionFuture.isDone()) { - future.complete(false); + chargeFor.isAffordableFor(teleporter, future); + if (future.isCompletedExceptionally()) { return; } @@ -297,24 +291,21 @@ private void teleportOther(IUser teleporter, IUser teleportee, ITarget target, T } } - if (cooldown(true, exceptionFuture)) { - future.complete(false); + if (cooldown(true, future)) { return; } if (delay <= 0 || teleporter == null || teleporter.isAuthorized("essentials.teleport.timer.bypass") || teleportOwner.isAuthorized("essentials.teleport.timer.bypass") || teleportee.isAuthorized("essentials.teleport.timer.bypass")) { - if (cooldown(false, exceptionFuture)) { - future.complete(false); + if (cooldown(false, future)) { return; } - nowAsync(teleportee, target, cause, exceptionFuture, future); + nowAsync(teleportee, target, cause, future); if (teleporter != null && cashCharge != null) { - cashCharge.charge(teleporter, exceptionFuture); - if (exceptionFuture.isDone()) { - future.complete(false); + cashCharge.charge(teleporter, future); + if (future.isCompletedExceptionally()) { return; } } @@ -327,27 +318,24 @@ private void teleportOther(IUser teleporter, IUser teleportee, ITarget target, T } @Override - public void respawn(Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + public void respawn(Trade chargeFor, TeleportCause cause, CompletableFuture future) { double delay = ess.getSettings().getTeleportDelay(); if (chargeFor != null) { - chargeFor.isAffordableFor(teleportOwner, exceptionFuture); - if (exceptionFuture.isDone()) { - future.complete(false); + chargeFor.isAffordableFor(teleportOwner, future); + if (future.isCompletedExceptionally()) { return; } } - if (cooldown(true, exceptionFuture)) { - future.complete(false); + if (cooldown(true, future)) { return; } if (delay <= 0 || teleportOwner.isAuthorized("essentials.teleport.timer.bypass")) { - if (cooldown(false, exceptionFuture)) { - future.complete(false); + if (cooldown(false, future)) { return; } - respawnNow(teleportOwner, cause, exceptionFuture, future); + respawnNow(teleportOwner, cause, future); if (chargeFor != null) { - chargeFor.charge(teleportOwner, exceptionFuture); + chargeFor.charge(teleportOwner, future); } return; } @@ -357,23 +345,23 @@ public void respawn(Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + void respawnNow(IUser teleportee, TeleportCause cause, CompletableFuture future) { final Player player = teleportee.getBase(); Location bed = player.getBedSpawnLocation(); if (bed != null) { - nowAsync(teleportee, new LocationTarget(bed), cause, exceptionFuture, future); + nowAsync(teleportee, new LocationTarget(bed), cause, future); } else { if (ess.getSettings().isDebug()) { ess.getLogger().info("Could not find bed spawn, forcing respawn event."); } final PlayerRespawnEvent pre = new PlayerRespawnEvent(player, player.getWorld().getSpawnLocation(), false); ess.getServer().getPluginManager().callEvent(pre); - nowAsync(teleportee, new LocationTarget(pre.getRespawnLocation()), cause, exceptionFuture, future); + nowAsync(teleportee, new LocationTarget(pre.getRespawnLocation()), cause, future); } } @Override - public void warp(IUser otherUser, String warp, Trade chargeFor, TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future) { + public void warp(IUser otherUser, String warp, Trade chargeFor, TeleportCause cause, CompletableFuture future) { UserWarpEvent event = new UserWarpEvent(otherUser, warp, chargeFor); Bukkit.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { @@ -385,33 +373,32 @@ public void warp(IUser otherUser, String warp, Trade chargeFor, TeleportCause ca try { loc = ess.getWarps().getWarp(warp); } catch (WarpNotFoundException | InvalidWorldException e) { - exceptionFuture.complete(e); - future.complete(false); + future.completeExceptionally(e); return; } otherUser.sendMessage(tl("warpingTo", warp, loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); if (!otherUser.equals(teleportOwner)) { teleportOwner.sendMessage(tl("warpingTo", warp, loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); } - teleport(otherUser, new LocationTarget(loc), chargeFor, cause, exceptionFuture, future); + teleport(otherUser, new LocationTarget(loc), chargeFor, cause, future); } @Override - public void back(Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future) { - back(teleportOwner, chargeFor, exceptionFuture, future); + public void back(Trade chargeFor, CompletableFuture future) { + back(teleportOwner, chargeFor, future); } @Override - public void back(IUser teleporter, Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future) { + public void back(IUser teleporter, Trade chargeFor, CompletableFuture future) { tpType = TeleportType.BACK; final Location loc = teleportOwner.getLastLocation(); teleportOwner.sendMessage(tl("backUsageMsg", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - teleportOther(teleporter, teleportOwner, new LocationTarget(loc), chargeFor, TeleportCause.COMMAND, exceptionFuture, future); + teleportOther(teleporter, teleportOwner, new LocationTarget(loc), chargeFor, TeleportCause.COMMAND, future); } @Override - public void back(CompletableFuture exceptionFuture, CompletableFuture future) { - nowAsync(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()), TeleportCause.COMMAND, exceptionFuture, future); + public void back(CompletableFuture future) { + nowAsync(teleportOwner, new LocationTarget(teleportOwner.getLastLocation()), TeleportCause.COMMAND, future); } public void setTpType(TeleportType tpType) { diff --git a/Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java b/Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java index a9960666459..20f515adf67 100644 --- a/Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java +++ b/Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java @@ -89,7 +89,7 @@ public void run() { if (now > timer_started + timer_delay) { try { teleport.cooldown(false); - } catch (Exception ex) { + } catch (Throwable ex) { teleportOwner.sendMessage(tl("cooldownWithMessage", ex.getMessage())); if (teleportOwner != teleportUser) { teleportUser.sendMessage(tl("cooldownWithMessage", ex.getMessage())); @@ -100,19 +100,22 @@ public void run() { teleportUser.sendMessage(tl("teleportationCommencing")); try { - CompletableFuture future = new CompletableFuture<>(); - CompletableFuture future1 = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); if (timer_chargeFor != null) { timer_chargeFor.isAffordableFor(teleportOwner); } if (timer_respawn) { - teleport.respawnNow(teleportUser, timer_cause, future, future1); + teleport.respawnNow(teleportUser, timer_cause, future); } else { - teleport.nowAsync(teleportUser, timer_teleportTarget, timer_cause, future, future1); - } - if (timer_chargeFor != null) { - timer_chargeFor.charge(teleportOwner); + teleport.nowAsync(teleportUser, timer_teleportTarget, timer_cause, future); } + future.thenAccept(success -> { + if (timer_chargeFor != null) { + try { + timer_chargeFor.charge(teleportOwner); + } catch (ChargeException ignored) { } + } + }); } catch (Exception ignored) {} } catch (Exception ex) { diff --git a/Essentials/src/com/earth2me/essentials/Jails.java b/Essentials/src/com/earth2me/essentials/Jails.java index 3e03e0dab01..54941d1ddb0 100644 --- a/Essentials/src/com/earth2me/essentials/Jails.java +++ b/Essentials/src/com/earth2me/essentials/Jails.java @@ -118,7 +118,7 @@ public void removeJail(final String jail) throws Exception { } /** - * @deprecated This method does not use asynchronous teleportation. Use {@link Jails#sendToJail(IUser, String, CompletableFuture, CompletableFuture)} + * @deprecated This method does not use asynchronous teleportation. Use {@link Jails#sendToJail(IUser, String, CompletableFuture)} */ @Override @Deprecated @@ -136,19 +136,15 @@ public void sendToJail(final IUser user, final String jail) throws Exception { } @Override - public void sendToJail(IUser user, String jail, CompletableFuture exceptionFuture, CompletableFuture future) throws Exception { + public void sendToJail(IUser user, String jail, CompletableFuture future) throws Exception { acquireReadLock(); try { if (user.getBase().isOnline()) { Location loc = getJail(jail); - CompletableFuture future1 = new CompletableFuture<>(); - future1.thenAccept(success -> { - if (success) { - user.setJail(jail); - } - future.complete(success); + user.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, future); + future.thenAccept(success -> { + user.setJail(jail); }); - user.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, exceptionFuture, future1); return; } user.setJail(jail); @@ -281,23 +277,21 @@ public void onJailPlayerJoin(final PlayerJoinEvent event) { return; } - CompletableFuture eFuture = new CompletableFuture<>(); - eFuture.thenAccept(ex -> { + CompletableFuture future = new CompletableFuture<>(); + future.exceptionally(ex -> { if (ess.getSettings().isDebug()) { LOGGER.log(Level.INFO, tl("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage()), ex); } else { LOGGER.log(Level.INFO, tl("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage())); } + return false; }); - CompletableFuture future = new CompletableFuture<>(); - future.thenAccept(success -> { - user.sendMessage(tl("jailMessage")); - }); + future.thenAccept(success -> user.sendMessage(tl("jailMessage"))); try { - sendToJail(user, user.getJail(), eFuture, future); + sendToJail(user, user.getJail(), future); } catch (Exception ex) { - eFuture.complete(ex); + future.completeExceptionally(ex); } } } diff --git a/Essentials/src/com/earth2me/essentials/Trade.java b/Essentials/src/com/earth2me/essentials/Trade.java index 0395a10d973..b364867a627 100644 --- a/Essentials/src/com/earth2me/essentials/Trade.java +++ b/Essentials/src/com/earth2me/essentials/Trade.java @@ -83,42 +83,43 @@ private Trade(final String command, final Trade fallback, final BigDecimal money } public void isAffordableFor(final IUser user) throws ChargeException { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - isAffordableFor(user, exceptionFuture); - if (exceptionFuture.isDone()) { + CompletableFuture future = new CompletableFuture<>(); + isAffordableFor(user, future); + if (future.isCompletedExceptionally()) { try { - Exception exception = exceptionFuture.get(); - throw (ChargeException) exception; - } catch (InterruptedException | ExecutionException e) { //This would rarely, if ever, happen. + future.get(); + } catch (InterruptedException e) { //If this happens, we have bigger problems... e.printStackTrace(); + } catch (ExecutionException e) { + throw (ChargeException) e.getCause(); } } } - public void isAffordableFor(final IUser user, CompletableFuture exceptionFuture) { + public void isAffordableFor(final IUser user, CompletableFuture future) { if (ess.getSettings().isDebug()) { ess.getLogger().log(Level.INFO, "checking if " + user.getName() + " can afford charge."); } if (getMoney() != null && getMoney().signum() > 0 && !user.canAfford(getMoney())) { - exceptionFuture.complete(new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(getMoney(), ess)))); + future.completeExceptionally(new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(getMoney(), ess)))); return; } if (getItemStack() != null && !user.getBase().getInventory().containsAtLeast(itemStack, itemStack.getAmount())) { - exceptionFuture.complete(new ChargeException(tl("missingItems", getItemStack().getAmount(), ess.getItemDb().name(getItemStack())))); + future.completeExceptionally(new ChargeException(tl("missingItems", getItemStack().getAmount(), ess.getItemDb().name(getItemStack())))); return; } BigDecimal money; if (command != null && !command.isEmpty() && (money = getCommandCost(user)).signum() > 0 && !user.canAfford(money)) { - exceptionFuture.complete(new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(money, ess)))); + future.completeExceptionally(new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(money, ess)))); return; } if (exp != null && exp > 0 && SetExpFix.getTotalExperience(user.getBase()) < exp) { - exceptionFuture.complete(new ChargeException(tl("notEnoughExperience"))); + future.completeExceptionally(new ChargeException(tl("notEnoughExperience"))); } } @@ -193,19 +194,20 @@ public Map pay(final IUser user, final OverflowType type) th } public void charge(final IUser user) throws ChargeException { - CompletableFuture exceptionFuture = new CompletableFuture<>(); - charge(user, exceptionFuture); - if (exceptionFuture.isDone()) { + CompletableFuture future = new CompletableFuture<>(); + charge(user, future); + if (future.isCompletedExceptionally()) { try { - Exception exception = exceptionFuture.get(); - throw (ChargeException) exception; - } catch (InterruptedException | ExecutionException e) { //This would rarely, if ever, happen. + future.get(); + } catch (InterruptedException e) { //If this happens, we have bigger problems... e.printStackTrace(); + } catch (ExecutionException e) { + throw (ChargeException) e.getCause(); } } } - public void charge(final IUser user, CompletableFuture exceptionFuture) { + public void charge(final IUser user, CompletableFuture future) { if (ess.getSettings().isDebug()) { ess.getLogger().log(Level.INFO, "attempting to charge user " + user.getName()); } @@ -214,7 +216,7 @@ public void charge(final IUser user, CompletableFuture exceptionFutur ess.getLogger().log(Level.INFO, "charging user " + user.getName() + " money " + getMoney().toPlainString()); } if (!user.canAfford(getMoney()) && getMoney().signum() > 0) { - exceptionFuture.complete(new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(getMoney(), ess)))); + future.completeExceptionally(new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(getMoney(), ess)))); return; } user.takeMoney(getMoney()); @@ -224,7 +226,7 @@ public void charge(final IUser user, CompletableFuture exceptionFutur ess.getLogger().log(Level.INFO, "charging user " + user.getName() + " itemstack " + getItemStack().toString()); } if (!user.getBase().getInventory().containsAtLeast(getItemStack(), getItemStack().getAmount())) { - exceptionFuture.complete(new ChargeException(tl("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " ")))); + future.completeExceptionally(new ChargeException(tl("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " ")))); return; } user.getBase().getInventory().removeItem(getItemStack()); @@ -233,7 +235,7 @@ public void charge(final IUser user, CompletableFuture exceptionFutur if (command != null) { final BigDecimal cost = getCommandCost(user); if (!user.canAfford(cost) && cost.signum() > 0) { - exceptionFuture.complete(new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(cost, ess)))); + future.completeExceptionally(new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(cost, ess)))); return; } user.takeMoney(cost); @@ -244,7 +246,7 @@ public void charge(final IUser user, CompletableFuture exceptionFutur } final int experience = SetExpFix.getTotalExperience(user.getBase()); if (experience < getExperience() && getExperience() > 0) { - exceptionFuture.complete(new ChargeException(tl("notEnoughExperience"))); + future.completeExceptionally(new ChargeException(tl("notEnoughExperience"))); return; } SetExpFix.setTotalExperience(user.getBase(), experience - getExperience()); diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index 080a2e83297..bf2272c5352 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -585,9 +585,12 @@ public boolean checkJailTimeout(final long currentTime) { sendMessage(tl("haveBeenReleased")); setJail(null); if (ess.getSettings().isTeleportBackWhenFreedFromJail()) { - CompletableFuture eFuture = new CompletableFuture<>(); - eFuture.thenAccept(e -> getAsyncTeleport().respawn(null, TeleportCause.PLUGIN, new CompletableFuture<>(), new CompletableFuture<>())); - getAsyncTeleport().back(eFuture, new CompletableFuture<>()); + CompletableFuture future = new CompletableFuture<>(); + getAsyncTeleport().back(future); + future.exceptionally(e -> { + getAsyncTeleport().respawn(null, TeleportCause.PLUGIN, new CompletableFuture<>()); + return false; + }); } return true; } diff --git a/Essentials/src/com/earth2me/essentials/api/IAsyncTeleport.java b/Essentials/src/com/earth2me/essentials/api/IAsyncTeleport.java index 9a51a2128c7..7847986a3c0 100644 --- a/Essentials/src/com/earth2me/essentials/api/IAsyncTeleport.java +++ b/Essentials/src/com/earth2me/essentials/api/IAsyncTeleport.java @@ -17,10 +17,9 @@ public interface IAsyncTeleport { * @param loc - Where should the player end up * @param cooldown - If cooldown should be enforced * @param cause - The reported teleportPlayer cause - * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution */ - void now(Location loc, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + void now(Location loc, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture future); /** * Used to skip teleportPlayer delay when teleporting someone to a location or player. @@ -28,10 +27,9 @@ public interface IAsyncTeleport { * @param entity - Where should the player end up * @param cooldown - If cooldown should be enforced * @param cause - The reported teleportPlayer cause - * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution */ - void now(Player entity, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + void now(Player entity, boolean cooldown, PlayerTeleportEvent.TeleportCause cause, CompletableFuture future); /** * Teleport a player to a specific location @@ -39,10 +37,9 @@ public interface IAsyncTeleport { * @param loc - Where should the player end up * @param chargeFor - What the user will be charged if teleportPlayer is successful * @param cause - The reported teleportPlayer cause. - * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution */ - void teleport(Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + void teleport(Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture future); /** * Teleport a player to a specific player @@ -50,10 +47,9 @@ public interface IAsyncTeleport { * @param entity - Where should the player end up * @param chargeFor - What the user will be charged if teleportPlayer is successful * @param cause - The reported teleportPlayer cause - * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution */ - void teleport(Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + void teleport(Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture future); /** * Teleport a player to a specific location @@ -62,10 +58,9 @@ public interface IAsyncTeleport { * @param loc - Where should the player end up * @param chargeFor - What the user will be charged if teleportPlayer is successful * @param cause - The reported teleportPlayer cause - * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution */ - void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + void teleportPlayer(IUser otherUser, Location loc, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture future); /** * Teleport a player to a specific player @@ -74,20 +69,18 @@ public interface IAsyncTeleport { * @param entity - Where should the player end up * @param chargeFor - What the user will be charged if teleportPlayer is successful * @param cause - The reported teleportPlayer cause - * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution */ - void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + void teleportPlayer(IUser otherUser, Player entity, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture future); /** * Teleport wrapper used to handle tp fallback on /jail and /home * * @param chargeFor - What the user will be charged if teleportPlayer is successful * @param cause - The reported teleportPlayer cause - * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution */ - void respawn(final Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + void respawn(final Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture future); /** * Teleport wrapper used to handle /warp teleports @@ -96,19 +89,17 @@ public interface IAsyncTeleport { * @param warp - The name of the warp the user will be teleported too. * @param chargeFor - What the user will be charged if teleportPlayer is successful * @param cause - The reported teleportPlayer cause - * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution */ - void warp(IUser otherUser, String warp, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture exceptionFuture, CompletableFuture future); + void warp(IUser otherUser, String warp, Trade chargeFor, PlayerTeleportEvent.TeleportCause cause, CompletableFuture future); /** * Teleport wrapper used to handle /back teleports * * @param chargeFor - What the user will be charged if teleportPlayer is successful - * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution */ - void back(Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future); + void back(Trade chargeFor, CompletableFuture future); /** * Teleport wrapper used to handle /back teleports that @@ -118,17 +109,15 @@ public interface IAsyncTeleport { * @param teleporter - The user performing the /back command. * This value may be {@code null} to indicate console. * @param chargeFor - What the {@code teleporter} will be charged if teleportPlayer is successful - * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution */ - void back(IUser teleporter, Trade chargeFor, CompletableFuture exceptionFuture, CompletableFuture future); + void back(IUser teleporter, Trade chargeFor, CompletableFuture future); /** * Teleport wrapper used to handle throwing user home after a jail sentence * - * @param exceptionFuture - Future which is completed with an exception if one is thrown during execution * @param future - Future which is completed with the success status of the execution */ - void back(CompletableFuture exceptionFuture, CompletableFuture future); + void back(CompletableFuture future); } diff --git a/Essentials/src/com/earth2me/essentials/api/IJails.java b/Essentials/src/com/earth2me/essentials/api/IJails.java index 9199b3e60ea..b776faa19d3 100644 --- a/Essentials/src/com/earth2me/essentials/api/IJails.java +++ b/Essentials/src/com/earth2me/essentials/api/IJails.java @@ -47,7 +47,7 @@ public interface IJails extends IReload { /** * Attempts to send the given user to the given jail * - * @deprecated Use {@link IJails#sendToJail(IUser, String, CompletableFuture, CompletableFuture)} + * @deprecated Use {@link IJails#sendToJail(IUser, String, CompletableFuture)} * * @param user the user to send to jail * @param jail the jail to send the user to @@ -62,12 +62,11 @@ public interface IJails extends IReload { * * @param user the user to send to jail * @param jail the jail to send the user to - * @param exceptionFuture Future which is completed with an exception if one is thrown during execution * @param future Future which is completed with the success status of the execution * * @throws Exception if the user is offline or jail does not exist */ - void sendToJail(IUser user, String jail, CompletableFuture exceptionFuture, CompletableFuture future) throws Exception; + void sendToJail(IUser user, String jail, CompletableFuture future) throws Exception; /** * Set a new jail with the given name and location diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandback.java b/Essentials/src/com/earth2me/essentials/commands/Commandback.java index 7411978bc03..7116619ee5a 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandback.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandback.java @@ -10,7 +10,6 @@ import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -78,15 +77,15 @@ private void teleportBack(CommandSource sender, User user, String commandLabel) } if (requester == null) { - user.getAsyncTeleport().back(null, null, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().back(null, null, getNewExceptionFuture(sender, commandLabel)); } else if (!requester.equals(user)) { Trade charge = new Trade(this.getName(), this.ess); charge.isAffordableFor(requester); - user.getAsyncTeleport().back(requester, charge, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().back(requester, charge, getNewExceptionFuture(sender, commandLabel)); } else { Trade charge = new Trade(this.getName(), this.ess); charge.isAffordableFor(user); - user.getAsyncTeleport().back(charge, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().back(charge, getNewExceptionFuture(sender, commandLabel)); } throw new NoChargeException(); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java index 67e353cd971..e3a5397c108 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java @@ -42,7 +42,7 @@ public void run(final Server server, final User user, final String commandLabel, if ("bed".equalsIgnoreCase(homeName) && user.isAuthorized("essentials.home.bed")) { final Location bed = player.getBase().getBedSpawnLocation(); if (bed != null) { - user.getAsyncTeleport().teleport(bed, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().teleport(bed, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); return; } else { throw new Exception(tl("bedMissing")); @@ -54,7 +54,7 @@ public void run(final Server server, final User user, final String commandLabel, final List homes = player.getHomes(); if (homes.isEmpty() && player.equals(user)) { if (ess.getSettings().isSpawnIfNoHome()) { - user.getAsyncTeleport().respawn(charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().respawn(charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); } else { throw new Exception(tl("noHomeSetPlayer")); } @@ -86,7 +86,7 @@ private String getHomeLimit(final User player) { return Integer.toString(ess.getSettings().getHomeLimit(player)); } - private void goHome(final User user, final User player, final String home, final Trade charge, CompletableFuture eFuture) throws Exception { + private void goHome(final User user, final User player, final String home, final Trade charge, CompletableFuture future) throws Exception { if (home.length() < 1) { throw new NotEnoughArgumentsException(); } @@ -97,13 +97,12 @@ private void goHome(final User user, final User player, final String home, final if (user.getWorld() != loc.getWorld() && ess.getSettings().isWorldHomePermissions() && !user.isAuthorized("essentials.worlds." + loc.getWorld().getName())) { throw new Exception(tl("noPerm", "essentials.worlds." + loc.getWorld().getName())); } - CompletableFuture future = new CompletableFuture<>(); + user.getAsyncTeleport().teleport(loc, charge, TeleportCause.COMMAND, future); future.thenAccept(success -> { - if (success) { - user.sendMessage(tl("teleportHome", home)); - } + if (success) { + user.sendMessage(tl("teleportHome", home)); + } }); - user.getAsyncTeleport().teleport(loc, charge, TeleportCause.COMMAND, eFuture, future); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandjump.java b/Essentials/src/com/earth2me/essentials/commands/Commandjump.java index aa0f9f98ea4..82d71b025b6 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandjump.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandjump.java @@ -10,7 +10,6 @@ import java.util.Collections; import java.util.List; -import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -47,7 +46,7 @@ public void run(final Server server, final User user, final String commandLabel, final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); - user.getAsyncTeleport().teleport(loc, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().teleport(loc, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java b/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java index 45788b23f3f..85af0c1a3be 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtogglejail.java @@ -51,7 +51,7 @@ public void run(final Server server, final CommandSource sender, final String co } final long timeDiff = preTimeDiff; - CompletableFuture future = new CompletableFuture<>(); + CompletableFuture future = getNewExceptionFuture(sender, commandLabel); future.thenAccept(success -> { if (success) { player.setJailed(true); @@ -65,7 +65,7 @@ public void run(final Server server, final CommandSource sender, final String co } }); if (player.getBase().isOnline()) { - ess.getJails().sendToJail(player, args[1], getNewExceptionFuture(sender, commandLabel), future); + ess.getJails().sendToJail(player, args[1], future); } else { // Check if jail exists ess.getJails().getJail(args[1]); @@ -102,13 +102,13 @@ public void run(final Server server, final CommandSource sender, final String co player.sendMessage(tl("jailReleasedPlayerNotify")); player.setJail(null); if (player.getBase().isOnline()) { - CompletableFuture future = new CompletableFuture<>(); + CompletableFuture future = getNewExceptionFuture(sender, commandLabel); + player.getAsyncTeleport().back(future); future.thenAccept(success -> { - if (success) { - sender.sendMessage(tl("jailReleased", player.getName())); - } + if (success) { + sender.sendMessage(tl("jailReleased", player.getName())); + } }); - player.getAsyncTeleport().back(getNewExceptionFuture(sender, commandLabel), future); return; } sender.sendMessage(tl("jailReleased", player.getName())); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtop.java b/Essentials/src/com/earth2me/essentials/commands/Commandtop.java index dc912a552dd..47a17e029a1 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtop.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtop.java @@ -30,6 +30,6 @@ public void run(final Server server, final User user, final String commandLabel, user.sendMessage(tl("teleportTop", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); } }); - user.getAsyncTeleport().teleport(loc, new Trade(this.getName(), ess), TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); + user.getAsyncTeleport().teleport(loc, new Trade(this.getName(), ess), TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtp.java b/Essentials/src/com/earth2me/essentials/commands/Commandtp.java index ae9c2568fdd..3903ec8e84f 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtp.java @@ -22,7 +22,7 @@ public Commandtp() { @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { - CompletableFuture future = new CompletableFuture<>(); + CompletableFuture future = getNewExceptionFuture(user.getSource(), commandLabel); switch (args.length) { case 0: throw new NotEnoughArgumentsException(); @@ -43,7 +43,7 @@ public void run(final Server server, final User user, final String commandLabel, } final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); - user.getAsyncTeleport().teleport(player.getBase(), charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); + user.getAsyncTeleport().teleport(player.getBase(), charge, TeleportCause.COMMAND, future); break; case 3: if (!user.isAuthorized("essentials.tp.position")) { @@ -56,7 +56,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new NotEnoughArgumentsException(tl("teleportInvalidLocation")); } final Location locpos = new Location(user.getWorld(), x2, y2, z2, user.getLocation().getYaw(), user.getLocation().getPitch()); - user.getAsyncTeleport().now(locpos, false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); + user.getAsyncTeleport().now(locpos, false, TeleportCause.COMMAND, future); future.thenAccept(success -> { if (success) { user.sendMessage(tl("teleporting", locpos.getWorld().getName(), locpos.getBlockX(), locpos.getBlockY(), locpos.getBlockZ())); @@ -82,7 +82,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("teleportDisabled", target2.getDisplayName())); } user.sendMessage(tl("teleporting", locposother.getWorld().getName(), locposother.getBlockX(), locposother.getBlockY(), locposother.getBlockZ())); - target2.getAsyncTeleport().now(locposother, false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); + target2.getAsyncTeleport().now(locposother, false, TeleportCause.COMMAND, future); future.thenAccept(success -> { if (success) { target2.sendMessage(tl("teleporting", locposother.getWorld().getName(), locposother.getBlockX(), locposother.getBlockY(), locposother.getBlockZ())); @@ -106,7 +106,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("noPerm", "essentials.worlds." + toPlayer.getWorld().getName())); } target.sendMessage(tl("teleportAtoB", user.getDisplayName(), toPlayer.getDisplayName())); - target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); + target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, future); break; } } @@ -121,7 +121,7 @@ public void run(final Server server, final CommandSource sender, final String co if (args.length == 2) { final User toPlayer = getPlayer(server, args, 1, true, false); target.sendMessage(tl("teleportAtoB", Console.NAME, toPlayer.getDisplayName())); - target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); + target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(sender, commandLabel)); } else if (args.length > 3) { final double x = args[1].startsWith("~") ? target.getLocation().getX() + (args[1].length() > 1 ? Double.parseDouble(args[1].substring(1)) : 0) : Double.parseDouble(args[1]); final double y = args[2].startsWith("~") ? target.getLocation().getY() + (args[2].length() > 1 ? Double.parseDouble(args[2].substring(1)) : 0) : Double.parseDouble(args[2]); @@ -131,8 +131,8 @@ public void run(final Server server, final CommandSource sender, final String co } final Location loc = new Location(target.getWorld(), x, y, z, target.getLocation().getYaw(), target.getLocation().getPitch()); sender.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - CompletableFuture future = new CompletableFuture<>(); - target.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, getNewExceptionFuture(sender, commandLabel), future); + CompletableFuture future = getNewExceptionFuture(sender, commandLabel); + target.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, future); future.thenAccept(success -> { if (success) { target.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java index 20fe12c74ea..0d4b8b46e28 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpa.java @@ -44,14 +44,14 @@ public void run(Server server, User user, String commandLabel, String[] args) th final Trade charge = new Trade(this.getName(), ess); AsyncTeleport teleport = (AsyncTeleport) user.getAsyncTeleport(); teleport.setTpType(AsyncTeleport.TeleportType.TPA); - CompletableFuture future = new CompletableFuture<>(); + CompletableFuture future = getNewExceptionFuture(user.getSource(), commandLabel); + teleport.teleport(player.getBase(), charge, PlayerTeleportEvent.TeleportCause.COMMAND, future); future.thenAccept(success -> { - if (success) { - player.sendMessage(tl("requestAcceptedAuto", user.getDisplayName())); - user.sendMessage(tl("requestAcceptedFromAuto", player.getDisplayName())); - } + if (success) { + player.sendMessage(tl("requestAcceptedAuto", user.getDisplayName())); + user.sendMessage(tl("requestAcceptedFromAuto", player.getDisplayName())); + } }); - teleport.teleport(player.getBase(), charge, PlayerTeleportEvent.TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); return; } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java index 4dc10482de9..d1d7dc684f8 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java @@ -51,11 +51,11 @@ public void run(final Server server, final User user, final String commandLabel, user.sendMessage(tl("requestAccepted")); requester.sendMessage(tl("requestAcceptedFrom", user.getDisplayName())); - CompletableFuture eFuture = getNewExceptionFuture(requester.getSource(), commandLabel); - eFuture.thenAccept(e -> { + CompletableFuture future = getNewExceptionFuture(requester.getSource(), commandLabel); + future.exceptionally(e -> { user.sendMessage(tl("pendingTeleportCancelled")); + return false; }); - CompletableFuture future = new CompletableFuture<>(); future.thenAccept(success -> { if (success) { user.requestTeleport(null, false); @@ -70,11 +70,11 @@ public void run(final Server server, final User user, final String commandLabel, requester.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); } }); - teleport.teleportPlayer(user, user.getTpRequestLocation(), charge, TeleportCause.COMMAND, eFuture, future); + teleport.teleportPlayer(user, user.getTpRequestLocation(), charge, TeleportCause.COMMAND, future); } else { AsyncTeleport teleport = (AsyncTeleport) requester.getAsyncTeleport(); teleport.setTpType(AsyncTeleport.TeleportType.TPA); - teleport.teleport(user.getBase(), charge, TeleportCause.COMMAND, eFuture, future); + teleport.teleport(user.getBase(), charge, TeleportCause.COMMAND, future); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java index d29da4181b1..dd8de0e28cd 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpall.java @@ -8,7 +8,6 @@ import java.util.Collections; import java.util.List; -import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -42,7 +41,7 @@ private void teleportAllPlayers(Server server, CommandSource sender, User target if (sender.equals(target.getBase()) && target.getWorld() != player.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !target.isAuthorized("essentials.worlds." + target.getWorld().getName())) { continue; } - player.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, getNewExceptionFuture(sender, label), new CompletableFuture<>()); + player.getAsyncTeleport().now(loc, false, TeleportCause.COMMAND, getNewExceptionFuture(sender, label)); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtphere.java b/Essentials/src/com/earth2me/essentials/commands/Commandtphere.java index 3aafcfcd860..138729589b2 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtphere.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtphere.java @@ -7,7 +7,6 @@ import java.util.Collections; import java.util.List; -import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -26,7 +25,7 @@ public void run(final Server server, final User user, final String commandLabel, if (user.getWorld() != player.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + user.getWorld().getName())) { throw new Exception(tl("noPerm", "essentials.worlds." + user.getWorld().getName())); } - user.getAsyncTeleport().teleportPlayer(player, user.getBase(), new Trade(this.getName(), ess), TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().teleportPlayer(player, user.getBase(), new Trade(this.getName(), ess), TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java index 5573dc6826c..e2f23466231 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpo.java @@ -27,7 +27,7 @@ public void run(final Server server, final User user, final String commandLabel, if (user.getWorld() != player.getWorld() && ess.getSettings().isWorldTeleportPermissions() && !user.isAuthorized("essentials.worlds." + player.getWorld().getName())) { throw new Exception(tl("noPerm", "essentials.worlds." + player.getWorld().getName())); } - user.getAsyncTeleport().now(player.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().now(player.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); break; default: @@ -41,13 +41,13 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("noPerm", "essentials.worlds." + toPlayer.getWorld().getName())); } - CompletableFuture future = new CompletableFuture<>(); + CompletableFuture future = getNewExceptionFuture(user.getSource(), commandLabel); + target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, future); future.thenAccept(success -> { - if (success) { - target.sendMessage(tl("teleportAtoB", user.getDisplayName(), toPlayer.getDisplayName())); - } + if (success) { + target.sendMessage(tl("teleportAtoB", user.getDisplayName(), toPlayer.getDisplayName())); + } }); - target.getAsyncTeleport().now(toPlayer.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), future); break; } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java index c5e51b76c04..46ea3b4933d 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpoffline.java @@ -5,8 +5,6 @@ import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent; -import java.util.concurrent.CompletableFuture; - import static com.earth2me.essentials.I18n.tl; public class Commandtpoffline extends EssentialsCommand { @@ -30,7 +28,7 @@ public void run(final Server server, final User user, final String label, final } user.sendMessage(tl("teleporting", logout.getWorld().getName(), logout.getBlockX(), logout.getBlockY(), logout.getBlockZ())); - user.getAsyncTeleport().now(logout, false, PlayerTeleportEvent.TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), label), new CompletableFuture<>()); + user.getAsyncTeleport().now(logout, false, PlayerTeleportEvent.TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), label)); } } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java b/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java index bb945c13570..03ec4b8c6db 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtpohere.java @@ -6,7 +6,6 @@ import java.util.Collections; import java.util.List; -import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -29,7 +28,7 @@ public void run(final Server server, final User user, final String commandLabel, throw new Exception(tl("noPerm", "essentials.worlds." + user.getWorld().getName())); } - player.getAsyncTeleport().now(user.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + player.getAsyncTeleport().now(user.getBase(), false, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java b/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java index 17e82e28da7..ff6de7d1531 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java @@ -12,7 +12,6 @@ import java.util.Collections; import java.util.List; -import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -56,7 +55,7 @@ public void run(final Server server, final User user, final String commandLabel, final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); user.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - user.getAsyncTeleport().teleport(loc, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().teleport(loc, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); } @Override @@ -85,7 +84,7 @@ public void run(final Server server, final CommandSource sender, final String co } sender.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); user.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); - user.getAsyncTeleport().teleport(loc, null, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().teleport(loc, null, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java b/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java index 9a82ba1dd6a..45967c90d67 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandwarp.java @@ -15,7 +15,6 @@ import java.util.Collections; import java.util.List; import java.util.Locale; -import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import static com.earth2me.essentials.I18n.tl; @@ -56,7 +55,7 @@ public void run(final Server server, final CommandSource sender, final String co throw new NoChargeException(); } User otherUser = getPlayer(server, args, 1, true, false); - otherUser.getAsyncTeleport().warp(otherUser, args[0], null, TeleportCause.COMMAND, getNewExceptionFuture(sender, commandLabel), new CompletableFuture<>()); + otherUser.getAsyncTeleport().warp(otherUser, args[0], null, TeleportCause.COMMAND, getNewExceptionFuture(sender, commandLabel)); } //TODO: Use one of the new text classes, like /help ? @@ -98,7 +97,7 @@ private void warpUser(final User owner, final User user, final String name, fina if (ess.getSettings().getPerWarpPermission() && !owner.isAuthorized("essentials.warps." + name)) { throw new Exception(tl("warpUsePermission")); } - owner.getAsyncTeleport().warp(user, name, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + owner.getAsyncTeleport().warp(user, name, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); } private List getAvailableWarpsFor(final IUser user) { diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandworld.java b/Essentials/src/com/earth2me/essentials/commands/Commandworld.java index 46459a25c6b..486562ece7a 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandworld.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandworld.java @@ -10,7 +10,6 @@ import java.util.Collections; import java.util.List; -import java.util.concurrent.CompletableFuture; import static com.earth2me.essentials.I18n.tl; @@ -67,7 +66,7 @@ protected void run(final Server server, final User user, final String commandLab final Trade charge = new Trade(this.getName(), ess); charge.isAffordableFor(user); - user.getAsyncTeleport().teleport(target, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel), new CompletableFuture<>()); + user.getAsyncTeleport().teleport(target, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java index a3111ecc96e..bb6463c2d04 100644 --- a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java +++ b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java @@ -356,9 +356,12 @@ public void showError(CommandSender sender, Throwable throwable, String commandL } } - public CompletableFuture getNewExceptionFuture(CommandSource sender, String commandLabel) { - CompletableFuture future = new CompletableFuture<>(); - future.thenAccept(e -> showError(sender.getSender(), e, commandLabel)); + public CompletableFuture getNewExceptionFuture(CommandSource sender, String commandLabel) { + CompletableFuture future = new CompletableFuture<>(); + future.exceptionally(e -> { + showError(sender.getSender(), e, commandLabel); + return false; + }); return future; } diff --git a/Essentials/src/com/earth2me/essentials/signs/SignWarp.java b/Essentials/src/com/earth2me/essentials/signs/SignWarp.java index 7181a8d05b0..add12a47c26 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignWarp.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignWarp.java @@ -54,15 +54,17 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri } final Trade charge = getTrade(sign, 3, ess); - CompletableFuture eFuture = new CompletableFuture<>(); CompletableFuture future = new CompletableFuture<>(); - eFuture.thenAccept(e -> ess.showError(player.getSource(), e, "\\ sign: " + signName)); + player.getAsyncTeleport().warp(player, warpName, charge, TeleportCause.PLUGIN, future); future.thenAccept(success -> { - if (success) { - Trade.log("Sign", "Warp", "Interact", username, null, username, charge, sign.getBlock().getLocation(), ess); - } + if (success) { + Trade.log("Sign", "Warp", "Interact", username, null, username, charge, sign.getBlock().getLocation(), ess); + } + }); + future.exceptionally(e -> { + ess.showError(player.getSource(), e, "\\ sign: " + signName); + return false; }); - player.getAsyncTeleport().warp(player, warpName, charge, TeleportCause.PLUGIN, eFuture, future); return true; } } diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java index c3109af5d5c..f3705982236 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java @@ -59,12 +59,14 @@ private void respawn(final CommandSource sender, final User teleportOwner, final final SpawnStorage spawns = (SpawnStorage) this.module; final Location spawn = spawns.getSpawn(teleportee.getGroup()); sender.sendMessage(tl("teleporting", spawn.getWorld().getName(), spawn.getBlockX(), spawn.getBlockY(), spawn.getBlockZ())); - CompletableFuture eFuture = new CompletableFuture<>(); - eFuture.thenAccept(e -> showError(sender.getSender(), e, commandLabel)); + future.exceptionally(e -> { + showError(sender.getSender(), e, commandLabel); + return false; + }); if (teleportOwner == null) { - teleportee.getAsyncTeleport().now(spawn, false, TeleportCause.COMMAND, eFuture, future); + teleportee.getAsyncTeleport().now(spawn, false, TeleportCause.COMMAND, future); } else { - teleportOwner.getAsyncTeleport().teleportPlayer(teleportee, spawn, charge, TeleportCause.COMMAND, eFuture, future); + teleportOwner.getAsyncTeleport().teleportPlayer(teleportee, spawn, charge, TeleportCause.COMMAND, future); } } } diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java index 98f1339f7fe..306875ab69f 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java @@ -138,9 +138,12 @@ public void run() { final Location spawn = spawns.getSpawn(ess.getSettings().getNewbieSpawn()); if (spawn != null) { - CompletableFuture eFuture = new CompletableFuture<>(); - eFuture.thenAccept(e -> logger.log(Level.WARNING, tl("teleportNewPlayerError"), e)); - user.getAsyncTeleport().now(spawn, false, TeleportCause.PLUGIN, eFuture, new CompletableFuture<>()); + CompletableFuture future = new CompletableFuture<>(); + future.exceptionally(e -> { + logger.log(Level.WARNING, tl("teleportNewPlayerError"), e); + return false; + }); + user.getAsyncTeleport().now(spawn, false, TeleportCause.PLUGIN, future); } } } From e95ce2a5d6c849d8eae3e947bef74aaecba43cc2 Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 20 Apr 2020 16:44:18 -0400 Subject: [PATCH 26/36] Use Essentials' logger in EssentialsCommand#showError --- .../src/com/earth2me/essentials/commands/EssentialsCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java index bb6463c2d04..369885c8a8e 100644 --- a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java +++ b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java @@ -351,7 +351,7 @@ protected final List tabCompleteCommand(CommandSource sender, Server ser public void showError(CommandSender sender, Throwable throwable, String commandLabel) { sender.sendMessage(tl("errorWithMessage", throwable.getMessage())); if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, tl("errorCallingCommand", commandLabel), throwable); + logger.log(Level.INFO, tl("errorCallingCommand", commandLabel), throwable); throwable.printStackTrace(); } } From 5a194fbce2ffdc82a5ceeb25e1f084b09fd851eb Mon Sep 17 00:00:00 2001 From: JRoy Date: Tue, 21 Apr 2020 09:47:31 -0400 Subject: [PATCH 27/36] Return implementation rather than interface --- Essentials/src/com/earth2me/essentials/User.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index bf2272c5352..fe457d80b2f 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -1,7 +1,5 @@ package com.earth2me.essentials; -import com.earth2me.essentials.api.IAsyncTeleport; -import com.earth2me.essentials.api.ITeleport; import com.earth2me.essentials.commands.IEssentialsCommand; import com.earth2me.essentials.messaging.IMessageRecipient; import com.earth2me.essentials.messaging.SimpleMessageRecipient; @@ -410,7 +408,7 @@ public String getDisplayName() { } @Override - public IAsyncTeleport getAsyncTeleport() { + public AsyncTeleport getAsyncTeleport() { return teleport; } @@ -419,7 +417,7 @@ public IAsyncTeleport getAsyncTeleport() { */ @Override @Deprecated - public ITeleport getTeleport() { + public Teleport getTeleport() { return legacyTeleport; } From 80a7dcb7894f3fe5d5e8dd2fd09dc01dd6a9483e Mon Sep 17 00:00:00 2001 From: JRoy Date: Sat, 2 May 2020 13:49:55 -0400 Subject: [PATCH 28/36] Avoid possible deadlocks with entity ejections --- .../earth2me/essentials/AsyncTeleport.java | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/AsyncTeleport.java b/Essentials/src/com/earth2me/essentials/AsyncTeleport.java index 46060a6877a..26059dec856 100644 --- a/Essentials/src/com/earth2me/essentials/AsyncTeleport.java +++ b/Essentials/src/com/earth2me/essentials/AsyncTeleport.java @@ -141,6 +141,19 @@ public void now(Player entity, boolean cooldown, TeleportCause cause, Completabl }); } + private void runOnMain(Runnable runnable) throws ExecutionException, InterruptedException { + if (Bukkit.isPrimaryThread()) { + runnable.run(); + return; + } + CompletableFuture taskLock = new CompletableFuture<>(); + Bukkit.getScheduler().runTask(ess, () -> { + runnable.run(); + taskLock.complete(new Object()); + }); + taskLock.get(); + } + protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, CompletableFuture future) { cancel(false); @@ -156,14 +169,10 @@ protected void nowAsync(IUser teleportee, ITarget target, TeleportCause cause, C future.completeExceptionally(new Exception(tl("passengerTeleportFail"))); return; } - CompletableFuture dismountFuture = new CompletableFuture<>(); - Bukkit.getScheduler().runTask(ess, () -> { - teleportee.getBase().eject(); - dismountFuture.complete(new Object()); - }); + try { - dismountFuture.get(); //EntityDismountEvent requires sync context we also want to wait for it to finish - } catch (InterruptedException | ExecutionException e) { + runOnMain(() -> teleportee.getBase().eject()); //EntityDismountEvent requires a sync context. + } catch (ExecutionException | InterruptedException e) { future.completeExceptionally(e); return; } From e72746e628a38df4c77c08ff4b6611a0166005c0 Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 11 May 2020 02:38:46 -0400 Subject: [PATCH 29/36] Nuke some sync loads with homes Took 7 hours and 2 PRs to paper but it's here! --- Essentials/pom.xml | 2 +- .../essentials/commands/Commandhome.java | 62 +++++++++++-------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/Essentials/pom.xml b/Essentials/pom.xml index a2f7695cf03..d6ad7edc6f8 100644 --- a/Essentials/pom.xml +++ b/Essentials/pom.xml @@ -57,7 +57,7 @@ io.papermc paperlib - 1.0.2 + 1.0.3 compile diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java index e3a5397c108..57b6a0ee9c2 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java @@ -3,6 +3,7 @@ import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import com.earth2me.essentials.utils.StringUtil; +import io.papermc.lib.PaperLib; import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; @@ -40,39 +41,46 @@ public void run(final Server server, final User user, final String commandLabel, } try { if ("bed".equalsIgnoreCase(homeName) && user.isAuthorized("essentials.home.bed")) { - final Location bed = player.getBase().getBedSpawnLocation(); - if (bed != null) { - user.getAsyncTeleport().teleport(bed, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); - return; - } else { - throw new Exception(tl("bedMissing")); - } + PaperLib.getBedSpawnLocationAsync(player.getBase(), true).thenAccept(location -> { + if (location != null) { + user.getAsyncTeleport().teleport(location, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); + } else { + showError(user.getBase(), new Exception(tl("bedMissing")), commandLabel); + } + }); + return; } goHome(user, player, homeName.toLowerCase(Locale.ENGLISH), charge, getNewExceptionFuture(user.getSource(), commandLabel)); } catch (NotEnoughArgumentsException e) { - Location bed = player.getBase().getBedSpawnLocation(); - final List homes = player.getHomes(); - if (homes.isEmpty() && player.equals(user)) { - if (ess.getSettings().isSpawnIfNoHome()) { - user.getAsyncTeleport().respawn(charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); - } else { - throw new Exception(tl("noHomeSetPlayer")); - } - } else if (homes.isEmpty()) { - throw new Exception(tl("noHomeSetPlayer")); - } else if (homes.size() == 1 && player.equals(user)) { - goHome(user, player, homes.get(0), charge, getNewExceptionFuture(user.getSource(), commandLabel)); - } else { - final int count = homes.size(); - if (user.isAuthorized("essentials.home.bed")) { - if (bed != null) { - homes.add(tl("bed")); + final User finalPlayer = player; + PaperLib.getBedSpawnLocationAsync(player.getBase(), true).thenAccept(bed -> { + final List homes = finalPlayer.getHomes(); + if (homes.isEmpty() && finalPlayer.equals(user)) { + if (ess.getSettings().isSpawnIfNoHome()) { + user.getAsyncTeleport().respawn(charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); } else { - homes.add(tl("bedNull")); + showError(user.getBase(), new Exception(tl("noHomeSetPlayer")), commandLabel); + } + } else if (homes.isEmpty()) { + showError(user.getBase(), new Exception(tl("noHomeSetPlayer")), commandLabel); + } else if (homes.size() == 1 && finalPlayer.equals(user)) { + try { + goHome(user, finalPlayer, homes.get(0), charge, getNewExceptionFuture(user.getSource(), commandLabel)); + } catch (Exception exception) { + showError(user.getBase(), exception, commandLabel); } + } else { + final int count = homes.size(); + if (user.isAuthorized("essentials.home.bed")) { + if (bed != null) { + homes.add(tl("bed")); + } else { + homes.add(tl("bedNull")); + } + } + user.sendMessage(tl("homes", StringUtil.joinList(homes), count, getHomeLimit(finalPlayer))); } - user.sendMessage(tl("homes", StringUtil.joinList(homes), count, getHomeLimit(player))); - } + }); } } From 318ef7c9ce255c0c30cfd0c54b559c8ad4f8daed Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 11 May 2020 13:02:50 -0400 Subject: [PATCH 30/36] Fix ABI and make the codestyle worse --- Essentials/src/com/earth2me/essentials/IUser.java | 2 +- Essentials/src/net/ess3/api/ITeleport.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Essentials/src/com/earth2me/essentials/IUser.java b/Essentials/src/com/earth2me/essentials/IUser.java index 33c69b2407b..becf5a187e6 100644 --- a/Essentials/src/com/earth2me/essentials/IUser.java +++ b/Essentials/src/com/earth2me/essentials/IUser.java @@ -1,8 +1,8 @@ package com.earth2me.essentials; import com.earth2me.essentials.api.IAsyncTeleport; -import com.earth2me.essentials.api.ITeleport; import com.earth2me.essentials.commands.IEssentialsCommand; +import net.ess3.api.ITeleport; import net.ess3.api.MaxMoneyException; import net.ess3.api.events.AfkStatusChangeEvent; import org.bukkit.Location; diff --git a/Essentials/src/net/ess3/api/ITeleport.java b/Essentials/src/net/ess3/api/ITeleport.java index 65c68076131..c0985217032 100644 --- a/Essentials/src/net/ess3/api/ITeleport.java +++ b/Essentials/src/net/ess3/api/ITeleport.java @@ -1,6 +1,9 @@ package net.ess3.api; +/** + * @deprecated This API is not asynchronous. Use {@link com.earth2me.essentials.api.IAsyncTeleport IAsyncTeleport} + */ public interface ITeleport extends com.earth2me.essentials.api.ITeleport { } From 85a49421620cf8ed4b580d62ab98dca79c5e71ac Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Mon, 11 May 2020 13:14:31 -0400 Subject: [PATCH 31/36] Make the codestyle worse because muh diff --- .../src/com/earth2me/essentials/IUser.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/IUser.java b/Essentials/src/com/earth2me/essentials/IUser.java index becf5a187e6..4dfe92cff29 100644 --- a/Essentials/src/com/earth2me/essentials/IUser.java +++ b/Essentials/src/com/earth2me/essentials/IUser.java @@ -165,9 +165,9 @@ public interface IUser { Map getCommandCooldowns(); Date getCommandCooldownExpiry(String label); - + void addCommandCooldown(Pattern pattern, Date expiresAt, boolean save); - + boolean clearCommandCooldown(Pattern pattern); /* @@ -182,19 +182,19 @@ public interface IUser { String getAfkMessage(); void setAfkMessage(final String message); - + long getAfkSince(); - + boolean isAcceptingPay(); - + void setAcceptingPay(boolean acceptingPay); - + boolean isPromptingPayConfirm(); - + void setPromptingPayConfirm(boolean prompt); - + boolean isPromptingClearConfirm(); - + void setPromptingClearConfirm(boolean prompt); boolean isLastMessageReplyRecipient(); From c3ee63c04f3a2f44e7ca03ca756b50bef80f0141 Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Mon, 11 May 2020 13:15:10 -0400 Subject: [PATCH 32/36] Further ruin the codestyle --- Essentials/src/com/earth2me/essentials/IUser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Essentials/src/com/earth2me/essentials/IUser.java b/Essentials/src/com/earth2me/essentials/IUser.java index 4dfe92cff29..1d334e17a3a 100644 --- a/Essentials/src/com/earth2me/essentials/IUser.java +++ b/Essentials/src/com/earth2me/essentials/IUser.java @@ -161,7 +161,7 @@ public interface IUser { Map getConfigMap(); Map getConfigMap(String node); - + Map getCommandCooldowns(); Date getCommandCooldownExpiry(String label); From dec46ac803656b78b132076bde58a357363ce616 Mon Sep 17 00:00:00 2001 From: JRoy Date: Mon, 25 May 2020 01:37:57 -0400 Subject: [PATCH 33/36] Fix error messages not showing in TimedTeleports --- .../essentials/AsyncTimedTeleport.java | 36 ++++++++++--------- .../earth2me/essentials/TimedTeleport.java | 24 ++++++------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java b/Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java index 20f515adf67..86c91c05788 100644 --- a/Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java +++ b/Essentials/src/com/earth2me/essentials/AsyncTimedTeleport.java @@ -99,24 +99,28 @@ public void run() { cancelTimer(false); teleportUser.sendMessage(tl("teleportationCommencing")); - try { - CompletableFuture future = new CompletableFuture<>(); + CompletableFuture future = new CompletableFuture<>(); + future.exceptionally(e -> { + ess.showError(teleportOwner.getSource(), e, "\\ teleport"); + return false; + }); + if (timer_chargeFor != null) { + timer_chargeFor.isAffordableFor(teleportOwner); + } + if (timer_respawn) { + teleport.respawnNow(teleportUser, timer_cause, future); + } else { + teleport.nowAsync(teleportUser, timer_teleportTarget, timer_cause, future); + } + future.thenAccept(success -> { if (timer_chargeFor != null) { - timer_chargeFor.isAffordableFor(teleportOwner); - } - if (timer_respawn) { - teleport.respawnNow(teleportUser, timer_cause, future); - } else { - teleport.nowAsync(teleportUser, timer_teleportTarget, timer_cause, future); - } - future.thenAccept(success -> { - if (timer_chargeFor != null) { - try { - timer_chargeFor.charge(teleportOwner); - } catch (ChargeException ignored) { } + try { + timer_chargeFor.charge(teleportOwner); + } catch (ChargeException ex) { + ess.showError(teleportOwner.getSource(), ex, "\\ teleport"); } - }); - } catch (Exception ignored) {} + } + }); } catch (Exception ex) { ess.showError(teleportOwner.getSource(), ex, "\\ teleport"); diff --git a/Essentials/src/com/earth2me/essentials/TimedTeleport.java b/Essentials/src/com/earth2me/essentials/TimedTeleport.java index c3e959f5821..6b8a31ec42b 100644 --- a/Essentials/src/com/earth2me/essentials/TimedTeleport.java +++ b/Essentials/src/com/earth2me/essentials/TimedTeleport.java @@ -98,19 +98,17 @@ public void run() { cancelTimer(false); teleportUser.sendMessage(tl("teleportationCommencing")); - try { - if (timer_chargeFor != null) { - timer_chargeFor.isAffordableFor(teleportOwner); - } - if (timer_respawn) { - teleport.respawnNow(teleportUser, timer_cause); - } else { - teleport.now(teleportUser, timer_teleportTarget, timer_cause); - } - if (timer_chargeFor != null) { - timer_chargeFor.charge(teleportOwner); - } - } catch (Exception ignored) {} + if (timer_chargeFor != null) { + timer_chargeFor.isAffordableFor(teleportOwner); + } + if (timer_respawn) { + teleport.respawnNow(teleportUser, timer_cause); + } else { + teleport.now(teleportUser, timer_teleportTarget, timer_cause); + } + if (timer_chargeFor != null) { + timer_chargeFor.charge(teleportOwner); + } } catch (Exception ex) { ess.showError(teleportOwner.getSource(), ex, "\\ teleport"); From e22a03c8535f3719a1a0ba06a2d4a1a3b9439ebe Mon Sep 17 00:00:00 2001 From: JRoy Date: Thu, 11 Jun 2020 17:56:05 -0400 Subject: [PATCH 34/36] Improve messages around beds for /home --- .../com/earth2me/essentials/commands/Commandhome.java | 11 ++++++++++- Essentials/src/messages.properties | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java index 9a64a235edc..32e6fa1852a 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java @@ -41,9 +41,18 @@ public void run(final Server server, final User user, final String commandLabel, } try { if ("bed".equalsIgnoreCase(homeName) && user.isAuthorized("essentials.home.bed")) { + if (!player.getBase().isOnline()) { + throw new Exception(tl("bedOffline")); + } PaperLib.getBedSpawnLocationAsync(player.getBase(), true).thenAccept(location -> { + CompletableFuture future = getNewExceptionFuture(user.getSource(), commandLabel); if (location != null) { - user.getAsyncTeleport().teleport(location, charge, TeleportCause.COMMAND, getNewExceptionFuture(user.getSource(), commandLabel)); + future.thenAccept(success -> { + if (success) { + user.sendMessage(tl("teleportHome", "bed")); + } + }); + user.getAsyncTeleport().teleport(location, charge, TeleportCause.COMMAND, future); } else { showError(user.getBase(), new Exception(tl("bedMissing")), commandLabel); } diff --git a/Essentials/src/messages.properties b/Essentials/src/messages.properties index 20e714221d9..b32c44b1930 100644 --- a/Essentials/src/messages.properties +++ b/Essentials/src/messages.properties @@ -40,6 +40,7 @@ banJoin=You are banned from this server. Reason: {0} bed=\u00a7obed\u00a7r bedMissing=\u00a74Your bed is either unset, missing or blocked. bedNull=\u00a7mbed\u00a7r +bedOffline=\u00a74Cannot teleport to the beds of offline users. bedSet=\u00a76Bed spawn set\! bigTreeFailure=\u00a74Big tree generation failure. Try again on grass or dirt. bigTreeSuccess=\u00a76Big tree spawned. From 9efa1147d3daab7223bec584a54b61f8f268501d Mon Sep 17 00:00:00 2001 From: JRoy Date: Thu, 11 Jun 2020 18:19:37 -0400 Subject: [PATCH 35/36] Fix #3274 Allow unsafe locations for different worlds + spectator mode --- Essentials/src/com/earth2me/essentials/utils/LocationUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java index 2683e1e7d7f..b74539efa76 100644 --- a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java @@ -153,7 +153,7 @@ public static Location getSafeDestination(final IUser user, final Location loc) } public static Location getSafeDestination(final IEssentials ess, final IUser user, final Location loc) throws Exception { - if (user.getBase().isOnline() && loc.getWorld().equals(user.getBase().getWorld()) && (user.getBase().getGameMode() == GameMode.CREATIVE || user.isGodModeEnabled()) && user.getBase().getAllowFlight()) { + if (user.getBase().isOnline() && (user.getBase().getGameMode() == GameMode.CREATIVE || user.getBase().getGameMode() == GameMode.SPECTATOR || user.isGodModeEnabled()) && user.getBase().getAllowFlight()) { if (shouldFly(loc)) { user.getBase().setFlying(true); } From 7ad5bb6c6d0a258f79ba255706e5ee3aaf06a6f7 Mon Sep 17 00:00:00 2001 From: JRoy Date: Tue, 16 Jun 2020 17:00:11 -0400 Subject: [PATCH 36/36] Fix fly safety operators --- Essentials/src/com/earth2me/essentials/utils/LocationUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java index b74539efa76..5a81b2c69d7 100644 --- a/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/LocationUtil.java @@ -153,7 +153,7 @@ public static Location getSafeDestination(final IUser user, final Location loc) } public static Location getSafeDestination(final IEssentials ess, final IUser user, final Location loc) throws Exception { - if (user.getBase().isOnline() && (user.getBase().getGameMode() == GameMode.CREATIVE || user.getBase().getGameMode() == GameMode.SPECTATOR || user.isGodModeEnabled()) && user.getBase().getAllowFlight()) { + if (user.getBase().isOnline() && (user.getBase().getGameMode() == GameMode.CREATIVE || user.getBase().getGameMode() == GameMode.SPECTATOR || user.isGodModeEnabled()) || user.getBase().getAllowFlight()) { if (shouldFly(loc)) { user.getBase().setFlying(true); }