diff --git a/pom.xml b/pom.xml index 2ae5b22..e8fa558 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cn.lunadeer EssentialsD - 1.18.13 + 1.18.16 jar EssentialsD diff --git a/src/main/java/cn/lunadeer/essentialsd/TeleportManager.java b/src/main/java/cn/lunadeer/essentialsd/TeleportManager.java index e29eb87..a8e49f2 100644 --- a/src/main/java/cn/lunadeer/essentialsd/TeleportManager.java +++ b/src/main/java/cn/lunadeer/essentialsd/TeleportManager.java @@ -7,7 +7,9 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.format.TextColor; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent; @@ -137,10 +139,10 @@ public class TeleportManager { public void rtp(Player player) { int radius = EssentialsD.config.getTpRtpRadius(); World world = player.getWorld(); - int x = (int) (Math.random() * radius * 2) - radius; - int z = (int) (Math.random() * radius * 2) - radius; + int x = (int) (Math.random() * radius * 2) - radius + (int) player.getLocation().getX(); + int z = (int) (Math.random() * radius * 2) - radius + (int) player.getLocation().getZ(); XLogger.debug("RTP: " + x + " " + z); - Location location = new Location(world, x, player.getY(), z); + Location location = new Location(world, x + 0.5, player.getY(), z + 0.5); if (EssentialsD.config.getTpDelay() > 0) { Notification.info(player, "将在 " + EssentialsD.config.getTpDelay() + " 秒后传送到随机位置"); } @@ -159,15 +161,33 @@ public class TeleportManager { public void doTeleportSafely(Player player, Location location) { location.getWorld().getChunkAtAsyncUrgently(location).thenAccept((chunk) -> { updateLastTpLocation(player); - for (double y = location.y(); y < 256; y++) { - Location up1 = location.getBlock().getRelative(BlockFace.UP).getLocation(); - Location up2 = up1.getBlock().getRelative(BlockFace.UP).getLocation(); - if (up1.getBlock().isPassable() && up2.getBlock().isPassable()) { - break; - } else { - location.setY(y + 1); + int max_attempts = 512; + while (location.getBlock().isPassable()) { + location.setY(location.getY() - 1); + max_attempts--; + if (max_attempts <= 0) { + Notification.error(player, "传送目的地不安全,已取消传送"); + return; } } + Block up1 = location.getBlock().getRelative(BlockFace.UP); + Block up2 = up1.getRelative(BlockFace.UP); + max_attempts = 512; + while (!(up1.isPassable() && !up1.isLiquid()) || !(up2.isPassable() && !up2.isLiquid())) { + location.setY(location.getY() + 1); + up1 = location.getBlock().getRelative(BlockFace.UP); + up2 = up1.getRelative(BlockFace.UP); + max_attempts--; + if (max_attempts <= 0) { + Notification.error(player, "传送目的地不安全,已取消传送"); + return; + } + } + location.setY(location.getY() + 1); + if (location.getBlock().getRelative(BlockFace.DOWN).getType() == Material.LAVA) { + Notification.error(player, "传送目的地不安全,已取消传送"); + return; + } player.teleportAsync(location, PlayerTeleportEvent.TeleportCause.PLUGIN); }); }