diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/CoreClass.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/CoreClass.java index 13557658..5cca224b 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/CoreClass.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/CoreClass.java @@ -12,8 +12,9 @@ import io.github.niestrat99.advancedteleport.listeners.spigot.SpigotSignChangeListener; import io.github.niestrat99.advancedteleport.listeners.spigot.SpigotSignOpenListener; import io.github.niestrat99.advancedteleport.managers.*; +import io.github.niestrat99.advancedteleport.rtp.RTPManager; import io.github.niestrat99.advancedteleport.sql.*; -import io.github.niestrat99.advancedteleport.utilities.RandomTPAlgorithms; +import io.github.niestrat99.advancedteleport.rtp.RandomTPAlgorithms; import io.github.slimjar.app.builder.InjectingApplicationBuilder; import io.github.slimjar.logging.ProcessLogger; import io.papermc.lib.PaperLib; diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/AdvancedTeleportAPI.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/AdvancedTeleportAPI.java index b7e1b398..f0d4b46b 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/AdvancedTeleportAPI.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/api/AdvancedTeleportAPI.java @@ -9,13 +9,13 @@ import io.github.niestrat99.advancedteleport.api.events.warps.WarpPostCreateEvent; import io.github.niestrat99.advancedteleport.config.MainConfig; import io.github.niestrat99.advancedteleport.managers.NamedLocationManager; -import io.github.niestrat99.advancedteleport.managers.RTPManager; +import io.github.niestrat99.advancedteleport.rtp.RTPManager; import io.github.niestrat99.advancedteleport.managers.SignManager; import io.github.niestrat99.advancedteleport.sql.MetadataSQLManager; import io.github.niestrat99.advancedteleport.sql.SpawnSQLManager; import io.github.niestrat99.advancedteleport.sql.WarpSQLManager; -import io.github.niestrat99.advancedteleport.utilities.RandomTPAlgorithms; +import io.github.niestrat99.advancedteleport.rtp.RandomTPAlgorithms; import io.papermc.lib.PaperLib; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ClearCacheCommand.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ClearCacheCommand.java index 9dc5cf02..a6cce147 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ClearCacheCommand.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ClearCacheCommand.java @@ -3,7 +3,7 @@ import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.commands.SubATCommand; import io.github.niestrat99.advancedteleport.config.CustomMessages; -import io.github.niestrat99.advancedteleport.managers.RTPManager; +import io.github.niestrat99.advancedteleport.rtp.RTPManager; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ReloadCommand.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ReloadCommand.java index f3f7c6f9..bc035c33 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ReloadCommand.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/core/ReloadCommand.java @@ -8,7 +8,7 @@ import io.github.niestrat99.advancedteleport.config.MainConfig; import io.github.niestrat99.advancedteleport.managers.CommandManager; import io.github.niestrat99.advancedteleport.managers.CooldownManager; -import io.github.niestrat99.advancedteleport.utilities.RandomCoords; +import io.github.niestrat99.advancedteleport.rtp.RTPManager; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -44,9 +44,9 @@ public boolean onCommand( ex.printStackTrace(); } } - RandomCoords.reload(); CooldownManager.init(); CommandManager.registerCommands(); + RTPManager.checkLocationsInBorder(); CustomMessages.sendMessage(sender, "Info.reloadedConfig"); return true; } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/MainConfig.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/MainConfig.java index 2b379c95..b74f5764 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/MainConfig.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/MainConfig.java @@ -68,8 +68,8 @@ public final class MainConfig extends ATConfig { public ConfigOption X; public ConfigOption Z; public ConfigOption RAPID_RESPONSE; - public ConfigOption USE_VANILLA_BORDER; - public ConfigOption USE_PLUGIN_BORDERS; + public ConfigOption SYNC_VANILLA_BORDER; + public ConfigOption SYNC_PLUGIN_BORDERS; public ConfigOption PROTECT_CLAIM_LOCATIONS; public ConfigOption PREPARED_LOCATIONS_LIMIT; public ConfigOption> IGNORE_WORLD_GENS; @@ -555,12 +555,13 @@ To use multiple worlds, use a comma (,). IMPORTANT NOTE - this feature only works on the Paper server type and any of its forks. It is not considered safe to use on Spigot or Bukkit.\ """); addDefault( - "use-vanilla-border", + "sync-vanilla-border", false, - "Whether the plugin should use the Vanilla world border as a viable " - + "option for managing /tpr boundaries."); + "Whether the plugin should sync its /tpr boundaries with the Vanilla world border." + + "\nThis does not need to be enabled to respect a smaller Vanilla world border - the plugin" + + "will automatically adjust to smaller world borders if required."); addDefault( - "use-plugin-borders", + "sync-plugin-borders", true, "Whether the plugin should use plugin world borders for managing /tpr " + "boundaries.\n" @@ -991,6 +992,9 @@ public void moveToNew() { moveTo( "permissions.allow-admin-perms-as-defaults", "allow-admin-permissions-as-default-perms"); + + moveTo("use-vanilla-border", "sync-vanilla-border"); + moveTo("use-plugin-borders", "sync-plugin-borders"); } @Override @@ -1074,8 +1078,8 @@ public void postSave() { X = new ConfigOption<>("x"); Z = new ConfigOption<>("z"); RAPID_RESPONSE = new ConfigOption<>("use-rapid-response"); - USE_VANILLA_BORDER = new ConfigOption<>("use-vanilla-border"); - USE_PLUGIN_BORDERS = new ConfigOption<>("use-plugin-borders"); + SYNC_VANILLA_BORDER = new ConfigOption<>("sync-vanilla-border"); + SYNC_PLUGIN_BORDERS = new ConfigOption<>("sync-plugin-borders"); PROTECT_CLAIM_LOCATIONS = new ConfigOption<>("protect-claim-locations"); PREPARED_LOCATIONS_LIMIT = new ConfigOption<>("prepared-locations-limit"); IGNORE_WORLD_GENS = new ConfigOption<>("ignore-world-generators"); diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/hooks/BorderPlugin.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/hooks/BorderPlugin.java index 1a250b33..53021521 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/hooks/BorderPlugin.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/hooks/BorderPlugin.java @@ -29,7 +29,7 @@ protected BorderPlugin(@Nullable final String pluginName) { * @return true if the plugin is enabled and there is a viable world border, false if not. */ public boolean canUse(@NotNull final World world) { - return MainConfig.get().USE_PLUGIN_BORDERS.get() && this.pluginUsable(); + return MainConfig.get().SYNC_PLUGIN_BORDERS.get() && this.pluginUsable(); } /** diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/hooks/borders/VanillaBorderHook.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/hooks/borders/VanillaBorderHook.java deleted file mode 100644 index 78f91c01..00000000 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/hooks/borders/VanillaBorderHook.java +++ /dev/null @@ -1,55 +0,0 @@ -package io.github.niestrat99.advancedteleport.hooks.borders; - -import io.github.niestrat99.advancedteleport.config.MainConfig; -import io.github.niestrat99.advancedteleport.hooks.BorderPlugin; - -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.plugin.Plugin; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; - -public final class VanillaBorderHook extends BorderPlugin { - - public VanillaBorderHook() { - super(null); - } - - @Override - @Contract(pure = true) - public boolean canUse(@NotNull final World world) { - return MainConfig.get().USE_VANILLA_BORDER.get() - && world.getWorldBorder().getSize() != 59999968 - && world.getWorldBorder().getSize() != (float) 5.9999968E7; - } - - @Override - @Contract(pure = true) - public double getMinX(@NotNull final World world) { - return getCentre(world).getX() - world.getWorldBorder().getSize() / 2; - } - - @Override - @Contract(pure = true) - public double getMinZ(@NotNull final World world) { - return getCentre(world).getZ() - world.getWorldBorder().getSize() / 2; - } - - @Override - @Contract(pure = true) - public double getMaxX(@NotNull final World world) { - return getCentre(world).getX() + world.getWorldBorder().getSize() / 2; - } - - @Override - @Contract(pure = true) - public double getMaxZ(@NotNull final World world) { - return getCentre(world).getZ() + world.getWorldBorder().getSize() / 2; - } - - @Override - @Contract(pure = true) - public @NotNull Location getCentre(@NotNull final World world) { - return world.getWorldBorder().getCenter(); - } -} diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/listeners/WorldLoadListener.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/listeners/WorldLoadListener.java index 9f379ccd..91c07251 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/listeners/WorldLoadListener.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/listeners/WorldLoadListener.java @@ -1,9 +1,10 @@ package io.github.niestrat99.advancedteleport.listeners; import io.github.niestrat99.advancedteleport.config.MainConfig; -import io.github.niestrat99.advancedteleport.managers.RTPManager; +import io.github.niestrat99.advancedteleport.rtp.RTPManager; import io.papermc.lib.PaperLib; +import io.papermc.paper.event.world.border.WorldBorderBoundsChangeEvent; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.world.WorldLoadEvent; @@ -17,7 +18,13 @@ public void onWorldLoad(WorldLoadEvent event) { RTPManager.loadWorldData(event.getWorld()); } + @EventHandler public void onWorldUnload(WorldUnloadEvent event) { RTPManager.unloadWorldData(event.getWorld()); } + + @EventHandler(ignoreCancelled = true) + public void onWorldBorderChange(WorldBorderBoundsChangeEvent event) { + RTPManager.checkLocationsInBorder(); + } } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/PluginHookManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/PluginHookManager.java index 63bb2497..18168a71 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/PluginHookManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/PluginHookManager.java @@ -7,7 +7,6 @@ import io.github.niestrat99.advancedteleport.hooks.MapPlugin; import io.github.niestrat99.advancedteleport.hooks.PluginHook; import io.github.niestrat99.advancedteleport.hooks.borders.ChunkyBorderHook; -import io.github.niestrat99.advancedteleport.hooks.borders.VanillaBorderHook; import io.github.niestrat99.advancedteleport.hooks.borders.WorldBorderHook; import io.github.niestrat99.advancedteleport.hooks.claims.GriefPreventionClaimHook; import io.github.niestrat99.advancedteleport.hooks.claims.LandsClaimHook; @@ -16,6 +15,7 @@ import io.github.niestrat99.advancedteleport.hooks.maps.DynmapHook; import io.github.niestrat99.advancedteleport.hooks.maps.SquaremapHook; import io.github.niestrat99.advancedteleport.hooks.particles.PlayerParticlesHook; +import io.github.niestrat99.advancedteleport.rtp.RandomTPBorders; import io.github.niestrat99.advancedteleport.sql.HomeSQLManager; import io.github.niestrat99.advancedteleport.sql.SpawnSQLManager; import io.github.niestrat99.advancedteleport.sql.WarpSQLManager; @@ -52,7 +52,6 @@ public void init() { // World border Plugins loadPlugin("worldborder", WorldBorderHook.class); loadPlugin("chunkyborder", ChunkyBorderHook.class); - loadPlugin("vanilla", VanillaBorderHook.class); // Particle plugins loadPlugin("playerparticles", PlayerParticlesHook.class); @@ -127,19 +126,26 @@ private void loadPlugin( } @Contract(pure = true) - public double[] getRandomCoords(@NotNull final World world) { - return getPluginHooks(BorderPlugin.class, true) - .filter(plugin -> plugin.canUse(world)) - .findFirst() - .map( - hook -> - new double[] { - hook.getMinX(world), - hook.getMaxX(world), - hook.getMinZ(world), - hook.getMaxZ(world) - }) - .orElse(null); + public RandomTPBorders getBorders(@NotNull final World world) { + + RandomTPBorders border = null; + for (BorderPlugin hook : getPluginHooks(BorderPlugin.class, true).toList()) { + if (!hook.canUse(world)) continue; + RandomTPBorders otherBorder = new RandomTPBorders( + hook.getMinX(world), + hook.getMaxX(world), + hook.getMinZ(world), + hook.getMaxZ(world) + ); + + if (border == null) { + border = otherBorder; + } else { + border = border.minimal(otherBorder); + } + } + + return border; } @Contract(pure = true) diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/TeleportTrackingManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/TeleportTrackingManager.java index 86215d9f..4d955470 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/TeleportTrackingManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/TeleportTrackingManager.java @@ -8,6 +8,7 @@ import io.github.niestrat99.advancedteleport.api.Spawn; import io.github.niestrat99.advancedteleport.config.CustomMessages; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.rtp.RTPManager; import io.github.niestrat99.advancedteleport.utilities.ConditionChecker; import io.github.thatsmusic99.configurationmaster.api.ConfigSection; diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/rtp/CoordinateGenerator.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/rtp/CoordinateGenerator.java new file mode 100644 index 00000000..b23da477 --- /dev/null +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/rtp/CoordinateGenerator.java @@ -0,0 +1,130 @@ +package io.github.niestrat99.advancedteleport.rtp; + +import io.github.niestrat99.advancedteleport.CoreClass; +import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.managers.PluginHookManager; +import io.github.thatsmusic99.configurationmaster.api.ConfigSection; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.WorldBorder; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Random; + +public class CoordinateGenerator { + + private static final Random random = new Random(); + + public static double getRandomCoords(double min, double max) { + return random.nextInt((int) Math.round(max - min) + 1) + min; + } + + public static @Nullable Location getRandCoords( + World world, RandomTPBorders borders, int y, int attempt) { + if (attempt++ > 15) { + return null; + } + + Location loc = + new Location( + world, + getRandomCoords(borders.minX(), borders.maxX()), + y, + getRandomCoords(borders.minZ(), borders.maxZ())); + if (PluginHookManager.get() + .isClaimed( + loc)) { // Should look into a limiter, so we don't get stuck in a loop + // somehow + return getRandCoords(world, borders, y, attempt); + } + return loc; + } + + public static @Nullable Location generateCoords(World world) { + + int y = world.getEnvironment() == World.Environment.NETHER ? 0 : 255; + return getRandCoords(world, getBorders(world), y, 0); + } + + public static @NotNull RandomTPBorders getBorders(final @NotNull World world) { + RandomTPBorders vanillaBorder = getVanillaWorldBorders(world); + RandomTPBorders pluginBorders = PluginHookManager.get().getBorders(world); + + RandomTPBorders borders; + if (MainConfig.get().SYNC_VANILLA_BORDER.get()) { + borders = vanillaBorder; + } else if (MainConfig.get().SYNC_PLUGIN_BORDERS.get()) { + borders = pluginBorders; + } else { + + ConfigSection x = MainConfig.get().X.get(); + ConfigSection z = MainConfig.get().Z.get(); + + String xStr = + x.contains(world.getName()) + ? x.getString(world.getName()) + : x.getString("default"); + String zStr = + x.contains(world.getName()) + ? z.getString(world.getName()) + : z.getString("default"); + + double[] coordsDouble = new double[4]; + + if (xStr != null || zStr != null) { + String[] xSplit = + xStr != null + ? xStr.split(";") + : zStr.split( + ";"); // Use the Z coord if X isn't present for + // some reason + setArray(coordsDouble, xSplit, 1, 0); + + String[] zSplit = + zStr != null + ? zStr.split(";") + : xStr.split( + ";"); // Use the X coord if Z isn't present for + // some reason + setArray(coordsDouble, zSplit, 3, 2); + + borders = new RandomTPBorders( + Math.min(coordsDouble[0], coordsDouble[1]), + Math.max(coordsDouble[0], coordsDouble[1]), + Math.min(coordsDouble[2], coordsDouble[3]), + Math.max(coordsDouble[2], coordsDouble[3])); + } else { + CoreClass.getInstance().getLogger().warning("X and Z /rtp boundaries for " + world.getName() + " are " + + "not set up properly. A range of -5000 to 5000 will be used."); + borders = new RandomTPBorders(-5000, 5000, -5000, 5000); + } + } + + // Minimise the border so it's smaller + borders = borders.minimal(vanillaBorder); + borders = borders.minimal(pluginBorders); + + return borders; + } + + private static RandomTPBorders getVanillaWorldBorders(final @NotNull World world) { + WorldBorder border = world.getWorldBorder(); + return new RandomTPBorders( + Math.ceil(border.getCenter().getX() - border.getSize() / 2), + Math.floor(border.getCenter().getX() + border.getSize() / 2), + Math.ceil(border.getCenter().getZ() - border.getSize() / 2), + Math.floor(border.getCenter().getZ() + border.getSize() / 2)); + } + + private static void setArray(double[] array, String[] strArray, int c1, int c2) { + if (strArray.length > 1) { + array[c1] = Integer.parseInt(strArray[0]); + array[c2] = Integer.parseInt(strArray[1]); + } else { + array[c1] = Integer.parseInt(strArray[0]); + array[c2] = Integer.parseInt(String.format("-%s", strArray[0])); + } + } +} diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/RTPManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/rtp/RTPManager.java similarity index 81% rename from AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/RTPManager.java rename to AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/rtp/RTPManager.java index 48826fa7..8d3c6c45 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/RTPManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/rtp/RTPManager.java @@ -1,10 +1,10 @@ -package io.github.niestrat99.advancedteleport.managers; +package io.github.niestrat99.advancedteleport.rtp; import com.google.common.collect.Sets; import io.github.niestrat99.advancedteleport.CoreClass; import io.github.niestrat99.advancedteleport.config.MainConfig; -import io.github.niestrat99.advancedteleport.utilities.RandomCoords; +import io.github.niestrat99.advancedteleport.managers.PluginHookManager; import io.papermc.lib.PaperLib; import org.bukkit.Bukkit; @@ -12,6 +12,7 @@ import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.BufferedReader; @@ -29,7 +30,6 @@ public class RTPManager { private static HashMap> locQueue; public static void init() { - locQueue = new HashMap<>(); if (!PaperLib.isPaper()) { CoreClass.debug("Server is not using Paper, cannot initialise RTPManager."); return; @@ -45,6 +45,8 @@ public static void init() { "Preparing random teleportation locations. " + "If your server performance or memory suffers, please set `use-rapid-response` to false in the config.yml file."); + locQueue = new HashMap<>(); + try { getPreviousLocations(); } catch (IOException e) { @@ -75,10 +77,54 @@ public static CompletableFuture getNextAvailableLocation(World world) if (queue == null || queue.isEmpty()) { return addLocation(world, true, 0); } else { - return CompletableFuture.completedFuture(queue.poll()); + Location loc = queue.poll(); + return isLocationInBorders(loc) ? CompletableFuture.completedFuture(queue.poll()) + : addLocation(world, true, 0); } } + /** + * Used to check if certain coordinates still lay within the borders. + */ + public static void checkLocationsInBorder() { + + if (!isInitialised()) return; + + // Go through each world and location + for (UUID worldUUID : locQueue.keySet()) { + + Queue pendingRandomLocs = locQueue.get(worldUUID); + Iterator pendingRandomLocsIt = pendingRandomLocs.iterator(); + while (pendingRandomLocsIt.hasNext()) { // bruh i'm gonna hit a concurrent modification exception silly billy + Location location = pendingRandomLocsIt.next(); + + // Replace the location if it's now outside the borders + if (!isLocationInBorders(location)) { + pendingRandomLocs.remove(location); + CoreClass.debug("Location " + CoreClass.getShortLocation(location) + " now sits outside of the " + + "world's borders, picking a new one..."); + + addLocation(location.getWorld(), false, 0) + .thenAccept( + newLoc -> { + if (newLoc == null) return; + pendingRandomLocs.add(newLoc); + locQueue.put(location.getWorld().getUID(), pendingRandomLocs); + }); + } + } + } + } + + private static boolean isLocationInBorders(final @NotNull Location location) { + + RandomTPBorders borders = CoordinateGenerator.getBorders(location.getWorld()); + + // Check the coordinates + return location.getX() > borders.minX() && location.getX() < borders.maxX() + && location.getZ() > borders.minZ() && location.getZ() < borders.maxZ(); + } + public static Location getLocationUrgently(World world) { Queue queue = locQueue.get(world.getUID()); addLocation(world, false, 0) @@ -91,7 +137,8 @@ public static Location getLocationUrgently(World world) { if (queue == null || queue.isEmpty()) { return null; } else { - return queue.remove(); + Location loc = queue.poll(); + return isLocationInBorders(loc) ? loc : null; } } @@ -112,14 +159,16 @@ public static Location getLocationUrgently(World world) { && locQueue.get(world.getUID()).size() > MainConfig.get().PREPARED_LOCATIONS_LIMIT.get()) { Location loc = locQueue.get(world.getUID()).poll(); - if (!PluginHookManager.get().isClaimed(loc)) { - CoreClass.debug("Area is not claimed - returning as valid location"); - return CompletableFuture.completedFuture(loc); + if (loc != null) { + if (!PluginHookManager.get().isClaimed(loc)) { + CoreClass.debug("Area is not claimed - returning as valid location"); + return CompletableFuture.completedFuture(loc); + } } } // Generate the coordinates. - Location location = RandomCoords.generateCoords(world); + Location location = CoordinateGenerator.generateCoords(world); if (location == null) { return CompletableFuture.completedFuture(null); diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/RandomTPAlgorithms.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/rtp/RandomTPAlgorithms.java similarity index 94% rename from AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/RandomTPAlgorithms.java rename to AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/rtp/RandomTPAlgorithms.java index 8b945add..fb5a63a6 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/RandomTPAlgorithms.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/rtp/RandomTPAlgorithms.java @@ -1,7 +1,8 @@ -package io.github.niestrat99.advancedteleport.utilities; +package io.github.niestrat99.advancedteleport.rtp; import io.github.niestrat99.advancedteleport.config.MainConfig; +import io.github.niestrat99.advancedteleport.utilities.ConditionChecker; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -23,13 +24,13 @@ public static void init() { (player, world) -> { // Generate random coordinates - Location location = RandomCoords.generateCoords(world); + Location location = CoordinateGenerator.generateCoords(world); // Whilst the location is too far away... while (ConditionChecker.canTeleport( player.getLocation(), location, "tpr", player) != null) { - location = RandomCoords.generateCoords(world); + location = CoordinateGenerator.generateCoords(world); } // Set the Y coordinate to 128 diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/rtp/RandomTPBorders.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/rtp/RandomTPBorders.java new file mode 100644 index 00000000..90e7b4fa --- /dev/null +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/rtp/RandomTPBorders.java @@ -0,0 +1,13 @@ +package io.github.niestrat99.advancedteleport.rtp; + +import org.jetbrains.annotations.NotNull; + +public record RandomTPBorders(double minX, double maxX, double minZ, double maxZ) { + + public RandomTPBorders minimal(final @NotNull RandomTPBorders otherBorder) { + return new RandomTPBorders(Math.max(this.minX, otherBorder.minX), + Math.min(this.maxX, otherBorder.maxX), + Math.max(this.minZ, otherBorder.minZ), + Math.min(this.maxZ, otherBorder.maxZ)); + } +} diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/RandomCoords.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/RandomCoords.java deleted file mode 100644 index 3d755113..00000000 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/utilities/RandomCoords.java +++ /dev/null @@ -1,107 +0,0 @@ -package io.github.niestrat99.advancedteleport.utilities; - -import io.github.niestrat99.advancedteleport.config.MainConfig; -import io.github.niestrat99.advancedteleport.managers.PluginHookManager; -import io.github.thatsmusic99.configurationmaster.api.ConfigSection; - -import org.bukkit.Location; -import org.bukkit.World; -import org.jetbrains.annotations.Nullable; - -import java.util.HashMap; -import java.util.Map; -import java.util.Random; - -public class RandomCoords { - - private static final Map coordCache = new HashMap<>(); - - private static final Random random = new Random(); - - public static double getRandomCoords(double min, double max) { - return random.nextInt((int) Math.round(max - min) + 1) + min; - } - - public static @Nullable Location getRandCoords( - World world, double[] coords, int y, int attempt) { - if (attempt++ > 15) { - return null; - } - - Location loc = - new Location( - world, - getRandomCoords(coords[0], coords[1]), - y, - getRandomCoords(coords[2], coords[3])); - if (PluginHookManager.get() - .isClaimed( - loc)) { // Should look into a limiter, so we don't get stuck in a loop - // somehow - return getRandCoords(world, coords, y, attempt); - } - return loc; - } - - public static @Nullable Location generateCoords(World world) { - double[] coords = PluginHookManager.get().getRandomCoords(world); - if (coords == null) { - coordCache.computeIfAbsent( - world.getName(), - k -> { - ConfigSection x = MainConfig.get().X.get(); - ConfigSection z = MainConfig.get().Z.get(); - - String xStr = - x.contains(world.getName()) - ? x.getString(world.getName()) - : x.getString("default"); - String zStr = - x.contains(world.getName()) - ? z.getString(world.getName()) - : z.getString("default"); - - double[] coordsDouble = new double[4]; - - if (xStr != null || zStr != null) { - String[] xSplit = - xStr != null - ? xStr.split(";") - : zStr.split( - ";"); // Use the Z coord if X isn't present for - // some reason - setArray(coordsDouble, xSplit, 1, 0); - - String[] zSplit = - zStr != null - ? zStr.split(";") - : xStr.split( - ";"); // Use the X coord if Z isn't present for - // some reason - setArray(coordsDouble, zSplit, 3, 2); - } - - return coordsDouble; - }); - - coords = coordCache.get(world.getName()); - } - - int y = world.getEnvironment() == World.Environment.NETHER ? 0 : 255; - return getRandCoords(world, coords, y, 0); - } - - private static void setArray(double[] array, String[] strArray, int c1, int c2) { - if (strArray.length > 1) { - array[c1] = Integer.parseInt(strArray[0]); - array[c2] = Integer.parseInt(strArray[1]); - } else { - array[c1] = Integer.parseInt(strArray[0]); - array[c2] = Integer.parseInt(String.format("-%s", strArray[0])); - } - } - - public static void reload() { - coordCache.clear(); - } -}