From b67b66a2b0ed5d33d2f6c1c3e0e53fdae05ccb49 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 16 Feb 2022 21:18:25 +0000 Subject: [PATCH] Fix #143 --- pom.xml | 32 +++++---- .../huskhomes2/api/HuskHomesAPI.java | 65 ++++++++---------- .../huskhomes2/commands/SpawnCommand.java | 11 ++-- .../huskhomes2/config/Settings.java | 35 +++++++++- .../huskhomes2/listeners/PlayerListener.java | 19 +++--- .../huskhomes2/teleport/TeleportManager.java | 66 ++++++++++++++----- .../teleport/points/TeleportationPoint.java | 24 +++---- src/main/resources/config.yml | 3 +- 8 files changed, 156 insertions(+), 99 deletions(-) diff --git a/pom.xml b/pom.xml index a1d6470c..6e856526 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.William278 HuskHomes - 2.12-dev + 2.12 jar HuskHomes @@ -16,7 +16,6 @@ 16 UTF-8 - 4.1.1 @@ -31,7 +30,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.9.0 + 3.10.0 ${java.version} ${java.version} @@ -40,25 +39,29 @@ org.apache.maven.plugins maven-shade-plugin - 3.3.0-SNAPSHOT + 3.3.1-SNAPSHOT + + org.jetbrains + me.william278.huskhomes2.libraries.annotations + + + com.zaxxer + me.william278.huskhomes2.libraries.hikaricp + + + io.papermc + me.william278.huskhomes2.libraries.paperlib + org.bstats me.william278.huskhomes2.libraries.bstats - de.themoep.minedown + de.themoep me.william278.huskhomes2.libraries.minedown - - io.papermc.lib - me.william278.huskhomes2.libraries.paperlib - - - com.zaxxer.hikari - me.william278.huskhomes2.libraries.hikari - @@ -80,6 +83,7 @@ false -Xdoclint:none + UTF-8 ${project.build.directory}/javadoc-bundle-options/maven-javadoc-plugin-stale-data.txt @@ -189,7 +193,7 @@ redis.clients jedis - ${jedis.version} + 4.1.1 jar provided diff --git a/src/main/java/me/william278/huskhomes2/api/HuskHomesAPI.java b/src/main/java/me/william278/huskhomes2/api/HuskHomesAPI.java index 5d92ee2a..b8626ea1 100644 --- a/src/main/java/me/william278/huskhomes2/api/HuskHomesAPI.java +++ b/src/main/java/me/william278/huskhomes2/api/HuskHomesAPI.java @@ -1,4 +1,3 @@ - package me.william278.huskhomes2.api; import me.william278.huskhomes2.HuskHomes; @@ -13,6 +12,7 @@ import org.bukkit.entity.Player; import java.sql.Connection; import java.sql.SQLException; import java.util.List; +import java.util.Optional; import java.util.logging.Level; /** @@ -117,18 +117,6 @@ public class HuskHomesAPI { return getHomes(player).size(); } - /** - * Returns the maximum number of homes a player can set - * - * @param player the Player being checked - * @return the maximum homes a player can set - * @deprecated Use getMaxSetHomes instead (capitalized H) - */ - @Deprecated - public int getMaxSethomes(Player player) { - return getMaxSetHomes(player); - } - /** * Returns the maximum number of homes a player can set * @@ -139,18 +127,6 @@ public class HuskHomesAPI { return Home.getSetHomeLimit(player); } - /** - * Returns the maximum number of homes a player can set - * - * @param player the Player being checked - * @return the maximum homes a player can set - * @deprecated Use getFreeSetHomes instead (capitalized H) - */ - @Deprecated - public int getFreeSethomes(Player player) { - return getFreeSetHomes(player); - } - /** * Returns the number of homes a player can set for free * @@ -211,13 +187,30 @@ public class HuskHomesAPI { return getWarps().size(); } + /** + * Returns an {@link Optional} that will contain the spawn position set on the server, as a {@link TeleportationPoint}. + *

+ *

    + *
  • To check if the spawn position is present: {@code #getServerSpawnPosition().isPresent()}
  • + *
  • To get the spawn position, if it is present: {@code #getServerSpawnPosition().get()}
  • + *
  • To get a runnable, if it is present: {@code #getServerSpawnPosition().get()}
  • + *
+ * + * @return an {@link Optional} that will contain the server's {@link TeleportationPoint} spawn position if it has been set (empty otherwise) + */ + public Optional getServerSpawnPosition() { + return TeleportManager.getSpawnLocation(); + } + /** * Returns the current spawn position as a TeleportationPoint * - * @return the spawn position as a TeleportationPoint + * @return the spawn position as a {@link TeleportationPoint}; {@code null otherwise} + * @deprecated Use {@link #getServerSpawnPosition()} that returns an {@code Optional} instead */ + @Deprecated public TeleportationPoint getSpawnPosition() { - return TeleportManager.getSpawnLocation(); + return TeleportManager.getSpawnLocation().isPresent() ? TeleportManager.getSpawnLocation().get() : null; } /** @@ -237,12 +230,11 @@ public class HuskHomesAPI { } /** - * Teleport a player to a specific TeleportationPoint + * Teleport a {@link Player} to a specific {@link TeleportationPoint} * - * @param player The player to be teleported - * @param point The target teleportationPoint - * @param timed Whether to do a warmup countdown - * @see TeleportationPoint + * @param player The {@link Player} to be teleported + * @param point The target {@link TeleportationPoint} + * @param timed Whether the teleport should be timed; have a warmup countdown */ public void teleportPlayer(Player player, TeleportationPoint point, boolean timed) { Bukkit.getScheduler().runTask(huskHomes, () -> { @@ -255,12 +247,11 @@ public class HuskHomesAPI { } /** - * Teleport a player to a specific TeleportationPoint + * Teleport a {@link Player} to a specific {@link TeleportationPoint} * - * @param player The player to be teleported - * @param targetPlayerName The target player's name - * @param timed Whether to do a warmup countdown - * @see TeleportationPoint + * @param player The {@link Player} to be teleported + * @param targetPlayerName The target {@link Player}'s username + * @param timed Whether the teleport should be timed; have a warmup countdown */ public void teleportPlayer(Player player, String targetPlayerName, boolean timed) { Bukkit.getScheduler().runTask(huskHomes, () -> { diff --git a/src/main/java/me/william278/huskhomes2/commands/SpawnCommand.java b/src/main/java/me/william278/huskhomes2/commands/SpawnCommand.java index 72cf4382..8b9b026b 100644 --- a/src/main/java/me/william278/huskhomes2/commands/SpawnCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/SpawnCommand.java @@ -1,9 +1,9 @@ package me.william278.huskhomes2.commands; import me.william278.huskhomes2.HuskHomes; -import me.william278.huskhomes2.util.MessageManager; import me.william278.huskhomes2.data.DataManager; import me.william278.huskhomes2.teleport.TeleportManager; +import me.william278.huskhomes2.util.MessageManager; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.entity.Player; @@ -32,12 +32,9 @@ public class SpawnCommand extends CommandBase { return; } - // Server-based spawn teleport - if (TeleportManager.getSpawnLocation() != null) { - TeleportManager.queueTimedTeleport(p, TeleportManager.getSpawnLocation()); - } else { - MessageManager.sendMessage(p, "error_spawn_undefined"); - } + // Teleport the player to spawn if it has been set + TeleportManager.getSpawnLocation().ifPresentOrElse(teleportationPoint -> TeleportManager.queueTimedTeleport(p, teleportationPoint), + () -> MessageManager.sendMessage(p, "error_spawn_undefined")); } catch (SQLException e) { plugin.getLogger().log(Level.SEVERE, "An exception occurred returning back to spawn"); } diff --git a/src/main/java/me/william278/huskhomes2/config/Settings.java b/src/main/java/me/william278/huskhomes2/config/Settings.java index 06c54fdb..bf5dac25 100644 --- a/src/main/java/me/william278/huskhomes2/config/Settings.java +++ b/src/main/java/me/william278/huskhomes2/config/Settings.java @@ -77,6 +77,9 @@ public class Settings { // Vanished player checks private boolean checkVanishedPlayers; + // Use PaperLib if available + private boolean usePaperLibIfAvailable; + // Time and maximum home settings private int maximumHomes; private int teleportRequestExpiryTime; @@ -253,6 +256,8 @@ public class Settings { this.checkVanishedPlayers = config.getBoolean("handle_vanished_players", true); + this.usePaperLibIfAvailable = config.getBoolean("general.use_paperlib_if_available", true); + this.doSpawnCommand = config.getBoolean("spawn_command.enabled", true); this.doCrossServerSpawn = config.getBoolean("spawn_command.bungee_network_spawn.enabled", false); this.crossServerSpawnWarpName = config.getString("spawn_command.bungee_network_spawn.warp_name", "Spawn"); @@ -454,14 +459,17 @@ public class Settings { public int getWarpsPerPage() { return warpsPerPage; } + @Nullable public Sound getTeleportationCompleteSound() { return teleportationCompleteSound; } + @Nullable public Sound getTeleportWarmupSound() { return teleportWarmupSound; } + @Nullable public Sound getTeleportCancelledSound() { return teleportCancelledSound; @@ -523,9 +531,13 @@ public class Settings { return redisPassword; } - public boolean getRedisSSL() { return redisSSL; } + public boolean getRedisSSL() { + return redisSSL; + } - public boolean getDoBackCommand() { return doBackCommand; } + public boolean getDoBackCommand() { + return doBackCommand; + } public int getHikariMaximumPoolSize() { return hikariMaximumPoolSize; @@ -555,14 +567,33 @@ public class Settings { return homeLimitPermissionStacking; } + public boolean doUsePaperLibIfAvailable() { + return usePaperLibIfAvailable; + } + public DisplayStyle getWarmupDisplayStyle() { return teleportWarmupDisplayStyle; } + /** + * Used to define where messages should appear + */ public enum DisplayStyle { + /** + * In the action bar (above the hotbar) + */ ACTION_BAR, + /** + * In the title slot in the middle of the player's screen + */ TITLE, + /** + * In the subtitle slot, just below the title + */ SUBTITLE, + /** + * In chat + */ CHAT } } diff --git a/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java b/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java index e3bc19af..4e5e6fff 100644 --- a/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java +++ b/src/main/java/me/william278/huskhomes2/listeners/PlayerListener.java @@ -1,6 +1,5 @@ package me.william278.huskhomes2.listeners; -import io.papermc.lib.PaperLib; import me.william278.huskhomes2.HuskHomes; import me.william278.huskhomes2.util.MessageManager; import me.william278.huskhomes2.commands.HomeCommand; @@ -50,7 +49,7 @@ public class PlayerListener implements Listener { try (Connection connection = HuskHomes.getConnection()) { if (!DataManager.playerExists(p, connection)) { DataManager.createPlayer(p, connection); - if (TeleportManager.getSpawnLocation() != null) { + if (TeleportManager.getSpawnLocation().isPresent()) { teleportToSpawn(p); return; } @@ -84,7 +83,7 @@ public class PlayerListener implements Listener { } } else { if (HuskHomes.getSettings().doForceSpawnOnLogin()) { - if (TeleportManager.getSpawnLocation() != null) { + if (TeleportManager.getSpawnLocation().isPresent()) { teleportToSpawn(p); } } @@ -109,14 +108,12 @@ public class PlayerListener implements Listener { } private void teleportToSpawn(Player player) { - if (TeleportManager.getSpawnLocation() != null) { - Bukkit.getScheduler().runTask(plugin, () -> { - if (!player.isEmpty()) { - player.eject(); // Eject passengers before teleporting - } - PaperLib.teleportAsync(player, TeleportManager.getSpawnLocation().getLocation()); - }); - } + TeleportManager.getSpawnLocation().ifPresent(teleportationPoint -> { + final Location spawnLocation = teleportationPoint.getLocation(); + if (spawnLocation.isWorldLoaded()) { + TeleportManager.teleportLocally(player, spawnLocation, false); + } + }); } @EventHandler diff --git a/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java b/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java index a43ed6bd..4f46fb0b 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java +++ b/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java @@ -19,6 +19,7 @@ import org.bukkit.entity.Player; import java.sql.Connection; import java.sql.SQLException; import java.time.Instant; +import java.util.Optional; import java.util.logging.Level; public class TeleportManager { @@ -99,18 +100,9 @@ public class TeleportManager { if (!HuskHomes.getSettings().doBungee() || server.equals(HuskHomes.getSettings().getServerID())) { DataManager.setPlayerTeleporting(player, false, connection); DataManager.deletePlayerDestination(player.getName(), connection); - Location targetLocation = targetPoint[0].getLocation(); - Bukkit.getScheduler().runTask(plugin, () -> { - if (!player.isEmpty()) { - player.eject(); // Eject passengers before teleporting - } - PaperLib.teleportAsync(player, targetLocation).thenRun(() -> { - if (HuskHomes.getSettings().getTeleportationCompleteSound() != null) { - player.playSound(targetLocation, HuskHomes.getSettings().getTeleportationCompleteSound(), 1, 1); - } - MessageManager.sendMessage(player, "teleporting_complete"); - }); - }); + + final Location targetLocation = targetPoint[0].getLocation(); + teleportLocally(player, targetLocation, true); } else if (HuskHomes.getSettings().doBungee()) { DataManager.setPlayerDestinationLocation(player, targetPoint[0], connection); DataManager.setPlayerTeleporting(player, true, connection); @@ -127,6 +119,44 @@ public class TeleportManager { }); } + /** + * Teleport a player locally on the server via PaperLib + * + * @param player The {@link Player} to teleport + * @param targetLocation The target {@link Location} + * @param notify Whether to notify the player of teleportation completion or failure through sounds and messages + */ + public static void teleportLocally(Player player, Location targetLocation, boolean notify) { + Bukkit.getScheduler().runTask(plugin, () -> { + if (!player.isEmpty()) { + player.eject(); // Eject passengers before teleporting + } + if (!targetLocation.isWorldLoaded()) { + if (notify) { + MessageManager.sendMessage(player, "error_invalid_on_arrival"); + } + return; + } + if (HuskHomes.getSettings().doUsePaperLibIfAvailable()) { + PaperLib.teleportAsync(player, targetLocation).thenAccept(succeeded -> { + if (!succeeded) { + player.teleport(targetLocation); // Teleport normally if paperlib fails + } + }); + } else { + player.teleport(targetLocation); + } + + // Send teleportation complete sound + if (notify) { + if (HuskHomes.getSettings().getTeleportationCompleteSound() != null) { + player.playSound(targetLocation, HuskHomes.getSettings().getTeleportationCompleteSound(), 1, 1); + } + MessageManager.sendMessage(player, "teleporting_complete"); + } + }); + } + public static void queueTimedTeleport(Player player, String targetPlayer) { if (player.hasPermission("huskhomes.bypass_timer")) { teleportPlayer(player, targetPlayer); @@ -294,11 +324,15 @@ public class TeleportManager { } } - public static TeleportationPoint getSpawnLocation() { - return spawnLocation; + public static Optional getSpawnLocation() { + if (spawnLocation == null) { + return Optional.empty(); + } else { + return Optional.of(spawnLocation); + } } - public static void setSpawnLocation(TeleportationPoint spawnLocation) { - TeleportManager.spawnLocation = spawnLocation; + public static void setSpawnLocation(TeleportationPoint spawnPoint) { + spawnLocation = spawnPoint; } } diff --git a/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java b/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java index 346d66c0..1297793e 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java +++ b/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java @@ -5,10 +5,7 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; -import java.util.StringJoiner; - /** - * @author William * HuskHomes' position representation object; represents an in-game location on a server */ public class TeleportationPoint { @@ -26,8 +23,14 @@ public class TeleportationPoint { * * @param location a new Bukkit location * @param server a new Bungee server ID + * @throws IllegalStateException if the location supplied has an unloaded world */ - public void setLocation(Location location, String server) { + public void setLocation(Location location, String server) throws IllegalStateException { + if (!location.isWorldLoaded()) { + throw new IllegalStateException("The location provided has a world not loaded"); + } + assert location.getWorld() != null; + worldName = location.getWorld().getName(); x = location.getX(); y = location.getY(); @@ -44,8 +47,8 @@ public class TeleportationPoint { * @param server The Bungee server ID which the location is on */ public TeleportationPoint(Location location, String server) { - if (location.getWorld() == null) { - throw new IllegalStateException("The location provided has an invalid world"); + if (!location.isWorldLoaded()) { + throw new IllegalStateException("The location provided has a world not loaded"); } setLocation(location, server); } @@ -72,18 +75,17 @@ public class TeleportationPoint { } /** - * Get the Bukkit location from a TeleportationPoint + * Get the Bukkit {@link Location} from a TeleportationPoint * * @return the Bukkit location on the server from a TeleportationPoint * @throws IllegalStateException if the location is not valid on the server */ public Location getLocation() { World world = Bukkit.getWorld(worldName); - if (world != null) { - return new Location(world, x, y, z, yaw, pitch); - } else { - throw new IllegalStateException(worldName + " is not a valid world on the server; failed to return location"); + if (world == null) { + throw new IllegalStateException(worldName + " is not loaded on this server; failed to fetch location"); } + return new Location(world, x, y, z, yaw, pitch); } /** diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 3bd1b263..e180ad3b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -35,6 +35,7 @@ general: teleport_request_expiry_time: 60 allow_unicode_descriptions: true enable_back_command: true + use_paperlib_if_available: true sounds: teleportation_complete: 'ENTITY_ENDERMAN_TELEPORT' teleportation_warmup: 'BLOCK_NOTE_BLOCK_BANJO' @@ -94,4 +95,4 @@ economy_integration: random_teleport: 20 back: 0 handle_vanished_players: true -config_file_version: 8 \ No newline at end of file +config_file_version: 9 \ No newline at end of file