From 952feb83f95092454fa99edf34b6e7dd23e89987 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Mon, 20 Jul 2020 17:35:29 +0100 Subject: [PATCH] Teleport async and send message on fail to fix issues with world changes --- .../randomteleport/RandomTeleport.java | 35 ++++++++++++------- .../src/main/resources/languages/lang.en.yml | 1 + 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java index 4327120..e2950d2 100644 --- a/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java +++ b/randomteleport-plugin/src/main/java/de/themoep/randomteleport/RandomTeleport.java @@ -37,6 +37,7 @@ import de.themoep.randomteleport.searcher.validators.LocationValidator; import de.themoep.randomteleport.searcher.validators.ProtectionValidator; import de.themoep.randomteleport.searcher.validators.WorldborderValidator; import de.themoep.utils.lang.bukkit.LanguageManager; +import io.papermc.lib.PaperLib; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -374,24 +375,34 @@ public class RandomTeleport extends JavaPlugin implements RandomTeleportAPI { targetLoc.setX(targetLoc.getBlockX() + 0.5); targetLoc.setY(targetLoc.getY() + 0.1); targetLoc.setZ(targetLoc.getBlockZ() + 0.5); - e.teleport(targetLoc); - sendMessage(e, "teleport", - "worldname", targetLoc.getWorld().getName(), - "x", String.valueOf(targetLoc.getBlockX()), - "y", String.valueOf(targetLoc.getBlockY()), - "z", String.valueOf(targetLoc.getBlockZ()) - ); - if (searcher.getOptions().containsKey("spawnpoint") && e instanceof Player) { - if (((Player) e).getBedSpawnLocation() == null || "force".equalsIgnoreCase(searcher.getOptions().get("spawnpoint"))){ - ((Player) e).setBedSpawnLocation(targetLoc, true); - sendMessage(e, "setspawnpoint", + PaperLib.teleportAsync(e, targetLoc).thenAccept(success -> { + if (success) { + sendMessage(e, "teleport", + "worldname", targetLoc.getWorld().getName(), + "x", String.valueOf(targetLoc.getBlockX()), + "y", String.valueOf(targetLoc.getBlockY()), + "z", String.valueOf(targetLoc.getBlockZ()) + ); + if (searcher.getOptions().containsKey("spawnpoint") && e instanceof Player) { + if (((Player) e).getBedSpawnLocation() == null || "force".equalsIgnoreCase(searcher.getOptions().get("spawnpoint"))) { + ((Player) e).setBedSpawnLocation(targetLoc, true); + sendMessage(e, "setspawnpoint", + "worldname", targetLoc.getWorld().getName(), + "x", String.valueOf(targetLoc.getBlockX()), + "y", String.valueOf(targetLoc.getBlockY()), + "z", String.valueOf(targetLoc.getBlockZ()) + ); + } + } + } else { + sendMessage(e, "error.teleport", "worldname", targetLoc.getWorld().getName(), "x", String.valueOf(targetLoc.getBlockX()), "y", String.valueOf(targetLoc.getBlockY()), "z", String.valueOf(targetLoc.getBlockZ()) ); } - } + }); }); return true; }).exceptionally(ex -> { diff --git a/randomteleport-plugin/src/main/resources/languages/lang.en.yml b/randomteleport-plugin/src/main/resources/languages/lang.en.yml index 5830e83..3bbd926 100644 --- a/randomteleport-plugin/src/main/resources/languages/lang.en.yml +++ b/randomteleport-plugin/src/main/resources/languages/lang.en.yml @@ -11,6 +11,7 @@ sign: use: "&cYou don't have permission to use the preset &6{preset}&c! &o({perm})" error: location: "&4Error: &cRandomTeleport could not find a safe location!" + teleport: "&4Error: &cRandomTeleport could not teleport you to X: {x} Y: {y} Z: {z}!" cooldown: "&cYou have to wait {cooldown_text} before using this RandomTeleport again!" parse-error: "&cError while parsing option &f{option}&c with value &f{value}&c: {error}" not-found: "&cCould not find &f{what}&c!"