From 80434fd2dda1789491000977a089d92ecef54830 Mon Sep 17 00:00:00 2001 From: Aubri Seebauer Date: Wed, 5 Apr 2023 20:02:49 -0400 Subject: [PATCH] Java 17 --- pom.xml | 9 +- .../townyrangelimit/TownyRangeLimit.java | 448 +++++++++--------- 2 files changed, 241 insertions(+), 216 deletions(-) diff --git a/pom.xml b/pom.xml index a7723d8..fa31b78 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,8 @@ TownyRangeLimit - 1.8 + 17 + 17 UTF-8 @@ -69,15 +70,15 @@ - io.papermc.paper - paper-api + org.spigotmc + spigot-api 1.19-R0.1-SNAPSHOT provided com.github.LlmDl Towny - 0.98.6.0 + 0.98.3.3 provided diff --git a/src/main/java/org/aubrithehuman/townyrangelimit/TownyRangeLimit.java b/src/main/java/org/aubrithehuman/townyrangelimit/TownyRangeLimit.java index f6445fb..abf62d0 100644 --- a/src/main/java/org/aubrithehuman/townyrangelimit/TownyRangeLimit.java +++ b/src/main/java/org/aubrithehuman/townyrangelimit/TownyRangeLimit.java @@ -2,8 +2,9 @@ package org.aubrithehuman.townyrangelimit; import com.palmergames.bukkit.towny.TownyAPI; import com.palmergames.bukkit.towny.TownySettings; -import com.palmergames.bukkit.towny.exceptions.TownyException; import com.palmergames.bukkit.towny.object.*; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -12,6 +13,7 @@ import org.bukkit.plugin.java.JavaPlugin; import java.util.ArrayList; import java.util.List; +import java.util.logging.Level; public final class TownyRangeLimit extends JavaPlugin implements Listener { @@ -40,10 +42,10 @@ public final class TownyRangeLimit extends JavaPlugin implements Listener { Nation nation2 = TownyAPI.getInstance().getNation(args[2]); if(nation1 == null || nation2 == null) return; - int dist = shortestDistance(nation1, nation2); + int dist = shortestDistance(nation1, nation2, event.getPlayer()); if (dist > this.getConfig().getInt("townblockRangeMax")) { - event.getPlayer().sendMessage("§e[Towny]" + "§c" + nation2.getName() + " is too far away from any town in your nation. Must be within " + (this.getConfig().getInt("townblockRangeMax") * TownySettings.getTownBlockSize()) + " blocks of the nearest town in either nation!"); + event.getPlayer().sendMessage("§e[Towny] " + "§c" + nation2.getName() + " is too far away from any town in your nation. Must be within " + (this.getConfig().getInt("townblockRangeMax") * TownySettings.getTownBlockSize()) + " blocks of the nearest town in either nation!"); event.setCancelled(true); } } @@ -58,10 +60,10 @@ public final class TownyRangeLimit extends JavaPlugin implements Listener { if(r == null) return; Nation nation = r.getNationOrNull(); - int dist = shortestDistance(town, nation); + int dist = shortestDistance(town, nation, event.getPlayer()); if (dist > this.getConfig().getInt("townblockRangeMax")) { - event.getPlayer().sendMessage("§e[Towny]" + "§c" + town.getName() + " is too far away from any town in your nation. Must be within " + (this.getConfig().getInt("townblockRangeMax") * TownySettings.getTownBlockSize()) + " blocks of the nearest town in your nation!"); + event.getPlayer().sendMessage("§e[Towny] " + "§c" + town.getName() + " is too far away from any town in your nation. Must be within " + (this.getConfig().getInt("townblockRangeMax") * TownySettings.getTownBlockSize()) + " blocks of the nearest town in your nation!"); event.setCancelled(true); } } @@ -75,10 +77,10 @@ public final class TownyRangeLimit extends JavaPlugin implements Listener { if(r == null) return; Town town = r.getTownOrNull(); - int dist = shortestDistance(town, nation); + int dist = shortestDistance(town, nation, event.getPlayer()); if (dist > this.getConfig().getInt("townblockRangeMax")) { - event.getPlayer().sendMessage("§e[Towny]" + "§c" + nation.getName() + " is too far away from any town in your town. Must be within " + (this.getConfig().getInt("townblockRangeMax") * TownySettings.getTownBlockSize()) + " blocks of the nearest town in that nation!"); + event.getPlayer().sendMessage("§e[Towny] " + "§c" + nation.getName() + " is too far away from any town in your town. Must be within " + (this.getConfig().getInt("townblockRangeMax") * TownySettings.getTownBlockSize()) + " blocks of the nearest town in that nation!"); event.setCancelled(true); } } @@ -96,10 +98,10 @@ public final class TownyRangeLimit extends JavaPlugin implements Listener { if(town1 == null || town2 == null) return; - int dist = shortestDistance(town1, town2); + int dist = shortestDistance(town1, town2, event.getPlayer()); if (dist > this.getConfig().getInt("townblockRangeMax")) { - event.getPlayer().sendMessage("§e[Towny]" + "§c" + town2.getName() + " is too far away from your town. Must be within " + (this.getConfig().getInt("townblockRangeMax") * TownySettings.getTownBlockSize()) + " blocks of the nearest chunk in your town!"); + event.getPlayer().sendMessage("§e[Towny] " + "§c" + town2.getName() + " is too far away from your town. Must be within " + (this.getConfig().getInt("townblockRangeMax") * TownySettings.getTownBlockSize()) + " blocks of the nearest chunk in your town!"); event.setCancelled(true); } } @@ -114,44 +116,116 @@ public final class TownyRangeLimit extends JavaPlugin implements Listener { * @param town2 town1 * @return shortest distance */ - private static int shortestDistance(Town town1, Town town2) { + private static int shortestDistance(Town town1, Town town2, Player p) { //max possible range (60M*sqrt2) // 16 is block size int dist = 84852814 / 16; if (town1 != null && town2 != null) { - try { - ArrayList clusterInvited = getCluster(town1.getHomeBlock().getWorldCoord()); - ArrayList clusterInviter = getCluster(town2.getHomeBlock().getWorldCoord()); - WorldCoord invitedHomeblock = town1.getHomeBlock().getWorldCoord(); - WorldCoord inviterHomeblock = town2.getHomeBlock().getWorldCoord(); + TownBlock townHomeBlock1 = town1.getHomeBlockOrNull(); + TownBlock townHomeBlock2 = town2.getHomeBlockOrNull(); + if(townHomeBlock1 == null) { + Bukkit.getLogger().log(Level.SEVERE, "[TownyRangeLimit] " + town1.getName() + " has not set a home block! Town must have a homeblock! Failing automatically."); + p.sendMessage("[TownyRangeLimit] " + town1.getName() + " has not set a home block! Town must have a homeblock! Failing automatically."); + return dist; + } + if(townHomeBlock2 == null) { + Bukkit.getLogger().log(Level.SEVERE, "[TownyRangeLimit] " + town2.getName() + " has not set a home block! Town must have a homeblock! Failing automatically."); + p.sendMessage("[TownyRangeLimit] " + town2.getName() + " has not set a home block! Town must have a homeblock! Failing automatically."); + return dist; + } + ArrayList clusterInvited = getCluster(townHomeBlock1.getWorldCoord()); + ArrayList clusterInviter = getCluster(townHomeBlock2.getWorldCoord()); + WorldCoord invitedHomeblock = townHomeBlock1.getWorldCoord(); + WorldCoord inviterHomeblock = townHomeBlock2.getWorldCoord(); - //find nearest block to inviter homeblock - WorldCoord nearestInviter = null; - int distInviter = 84852814 / 16; - for (WorldCoord coord1 : clusterInvited) { - int distX = inviterHomeblock.getX() - coord1.getX(); - int distZ = inviterHomeblock.getZ() - coord1.getZ(); - if (distX < 0) distX *= -1; - if (distZ < 0) distZ *= -1; - //pythagorus my boi - int dist1 = (int) Math.sqrt((distX * distX) + (distZ * distZ)); + //find nearest block to inviter homeblock + WorldCoord nearestInviter = null; + int distInviter = 84852814 / 16; + for (WorldCoord coord1 : clusterInvited) { + int distX = inviterHomeblock.getX() - coord1.getX(); + int distZ = inviterHomeblock.getZ() - coord1.getZ(); + if (distX < 0) distX *= -1; + if (distZ < 0) distZ *= -1; + //pythagorus my boi + int dist1 = (int) Math.sqrt((distX * distX) + (distZ * distZ)); - //if we find a new min, save it - if (dist1 <= distInviter) { - distInviter = dist1; - nearestInviter = coord1; - } + //if we find a new min, save it + if (dist1 <= distInviter) { + distInviter = dist1; + nearestInviter = coord1; + } + } + + //if we didn't find anything for some reason? IDK its worth putting + if(nearestInviter == null) { + return dist; + } + + //find nearest block to the found block + WorldCoord nearestInvited = nearestInviter; + int distInvited = 84852814 / 16; + for (WorldCoord coord2 : clusterInviter) { + int distX = invitedHomeblock.getX() - coord2.getX(); + int distZ = invitedHomeblock.getZ() - coord2.getZ(); + if (distX < 0) distX *= -1; + if (distZ < 0) distZ *= -1; + //pythagorus my boi + int dist2 = (int) Math.sqrt((distX * distX) + (distZ * distZ)); + + //if we find a new min, save it + if (dist2 <= distInvited) { + distInvited = dist2; + nearestInvited = coord2; } + } - //if we didn't find anything for some reason? IDK its worth putting - if(nearestInviter == null) { + dist = (int) Math.sqrt(((nearestInviter.getX() - nearestInvited.getX()) * (nearestInviter.getX() - nearestInvited.getX())) + ((nearestInviter.getZ() - nearestInvited.getZ()) * (nearestInviter.getZ() - nearestInvited.getZ()))); + + } + return dist; + } + + /** + * Finds the shortest distance (in townblocks) between a town and a nation + * @param town town + * @param nation nation + * @return shortest distance + */ + private static int shortestDistance(Town town, Nation nation, Player p) { + //max possible range (60M*sqrt2) + // 16 is block size + int dist = 84852814 / 16; + if (town != null && nation != null) { + TownBlock townHomeBlockM = town.getHomeBlockOrNull(); + if(townHomeBlockM == null) { + Bukkit.getLogger().log(Level.SEVERE, "[TownyRangeLimit] " + town.getName() + " has not set a home block! Town must have a homeblock! Failing automatically."); + p.sendMessage("[TownyRangeLimit] " + town.getName() + " has not set a home block! Town must have a homeblock! Failing automatically."); + return dist; + } + ArrayList clusterInvited = getCluster(townHomeBlockM.getWorldCoord()); + WorldCoord invitedHomeblock = townHomeBlockM.getWorldCoord(); + + //Grab all the towns in the nation + List nation1Towns = nation.getTowns(); + ArrayList> clustersInviter = new ArrayList<>(); + ArrayList inviterHomeblocks = new ArrayList<>(); + for (Town t : nation1Towns) { + TownBlock townHomeBlock = t.getHomeBlockOrNull(); + if(townHomeBlock == null) { + Bukkit.getLogger().log(Level.SEVERE, "[TownyRangeLimit] " + t.getName() + " has not set a home block! Town must have a homeblock! Failing automatically."); + p.sendMessage("[TownyRangeLimit] " + t.getName() + " has not set a home block! Town must have a homeblock! Failing automatically."); return dist; } + clustersInviter.add(getCluster(townHomeBlock.getWorldCoord())); + //grab homeblocks + inviterHomeblocks.add(townHomeBlock.getWorldCoord()); + } - //find nearest block to the found block - WorldCoord nearestInvited = nearestInviter; - int distInvited = 84852814 / 16; - for (WorldCoord coord2 : clusterInviter) { + //find nearest block to invited homeblock + WorldCoord nearestInvited = null; + int distInvited = 84852814 / 16; + for (ArrayList cluster : clustersInviter) { + for (WorldCoord coord2 : cluster) { int distX = invitedHomeblock.getX() - coord2.getX(); int distZ = invitedHomeblock.getZ() - coord2.getZ(); if (distX < 0) distX *= -1; @@ -163,92 +237,36 @@ public final class TownyRangeLimit extends JavaPlugin implements Listener { if (dist2 <= distInvited) { distInvited = dist2; nearestInvited = coord2; + } } - - dist = (int) Math.sqrt(((nearestInviter.getX() - nearestInvited.getX()) * (nearestInviter.getX() - nearestInvited.getX())) + ((nearestInviter.getZ() - nearestInvited.getZ()) * (nearestInviter.getZ() - nearestInvited.getZ()))); - - } catch (TownyException e) { - e.printStackTrace(); } - } - return dist; - } - /** - * Finds the shortest distance (in townblocks) between a town and a nation - * @param town town - * @param nation nation - * @return shortest distance - */ - private static int shortestDistance(Town town, Nation nation) { - //max possible range (60M*sqrt2) - // 16 is block size - int dist = 84852814 / 16; - if (town != null && nation != null) { - try { - ArrayList clusterInvited = getCluster(town.getHomeBlock().getWorldCoord()); - WorldCoord invitedHomeblock = town.getHomeBlock().getWorldCoord(); - - //Grab all the towns in the nation - List nation1Towns = nation.getTowns(); - ArrayList> clustersInviter = new ArrayList<>(); - ArrayList inviterHomeblocks = new ArrayList<>(); - for (Town t : nation1Towns) { - clustersInviter.add(getCluster(nation.getCapital().getHomeBlock().getWorldCoord())); - //grab homeblocks - inviterHomeblocks.add(t.getHomeBlock().getWorldCoord()); - } - - //find nearest block to invited homeblock - WorldCoord nearestInvited = null; - int distInvited = 84852814 / 16; - for (ArrayList cluster : clustersInviter) { - for (WorldCoord coord2 : cluster) { - int distX = invitedHomeblock.getX() - coord2.getX(); - int distZ = invitedHomeblock.getZ() - coord2.getZ(); - if (distX < 0) distX *= -1; - if (distZ < 0) distZ *= -1; - //pythagorus my boi - int dist2 = (int) Math.sqrt((distX * distX) + (distZ * distZ)); - - //if we find a new min, save it - if (dist2 <= distInvited) { - distInvited = dist2; - nearestInvited = coord2; - - } - } - } - - //if we didn't find anything for some reason? IDK its worth putting - if(nearestInvited == null) { - return dist; - } - - //find nearest block to found block - WorldCoord nearestInviter = invitedHomeblock; - int distInviter = 84852814 / 16; - for (WorldCoord coord1 : clusterInvited) { - int distX = nearestInvited.getX() - coord1.getX(); - int distZ = nearestInvited.getZ() - coord1.getZ(); - if (distX < 0) distX *= -1; - if (distZ < 0) distZ *= -1; - //pythagorus my boi - int dist1 = (int) Math.sqrt((distX * distX) + (distZ * distZ)); - - //if we find a new min, save it - if (dist1 <= distInviter) { - distInviter = dist1; - nearestInviter = coord1; - } - } - - dist = (int) Math.sqrt(((nearestInviter.getX() - nearestInvited.getX()) * (nearestInviter.getX() - nearestInvited.getX())) + ((nearestInviter.getZ() - nearestInvited.getZ()) * (nearestInviter.getZ() - nearestInvited.getZ()))); - - } catch (TownyException e) { - e.printStackTrace(); + //if we didn't find anything for some reason? IDK its worth putting + if(nearestInvited == null) { + return dist; } + + //find nearest block to found block + WorldCoord nearestInviter = invitedHomeblock; + int distInviter = 84852814 / 16; + for (WorldCoord coord1 : clusterInvited) { + int distX = nearestInvited.getX() - coord1.getX(); + int distZ = nearestInvited.getZ() - coord1.getZ(); + if (distX < 0) distX *= -1; + if (distZ < 0) distZ *= -1; + //pythagorus my boi + int dist1 = (int) Math.sqrt((distX * distX) + (distZ * distZ)); + + //if we find a new min, save it + if (dist1 <= distInviter) { + distInviter = dist1; + nearestInviter = coord1; + } + } + + dist = (int) Math.sqrt(((nearestInviter.getX() - nearestInvited.getX()) * (nearestInviter.getX() - nearestInvited.getX())) + ((nearestInviter.getZ() - nearestInvited.getZ()) * (nearestInviter.getZ() - nearestInvited.getZ()))); + } return dist; } @@ -260,108 +278,114 @@ public final class TownyRangeLimit extends JavaPlugin implements Listener { * @return shortest distance */ - private static int shortestDistance(Nation nation1, Nation nation2) { + private static int shortestDistance(Nation nation1, Nation nation2, Player p) { //max possible range (60M*sqrt2) // 16 is block size int dist = 84852814 / 16; if (nation1 != null && nation2 != null) { - try { - - //Grab all the towns in the nation - List nation1Towns = nation1.getTowns(); - ArrayList> clustersInviter = new ArrayList<>(); - ArrayList inviterHomeblocks = new ArrayList<>(); - for (Town t : nation1Towns) { - clustersInviter.add(getCluster(nation1.getCapital().getHomeBlock().getWorldCoord())); - inviterHomeblocks.add(t.getHomeBlock().getWorldCoord()); - + //Grab all the towns in the nation + List nation1Towns = nation1.getTowns(); + ArrayList> clustersInviter = new ArrayList<>(); + ArrayList inviterHomeblocks = new ArrayList<>(); + for (Town t : nation1Towns) { + TownBlock townHomeBlock = t.getHomeBlockOrNull(); + if(townHomeBlock == null) { + Bukkit.getLogger().log(Level.SEVERE, "[TownyRangeLimit] " + t.getName() + " has not set a home block! Town must have a homeblock! Failing automatically."); + p.sendMessage("[TownyRangeLimit] " + t.getName() + " has not set a home block! Town must have a homeblock! Failing automatically."); + return dist; } + clustersInviter.add(getCluster(townHomeBlock.getWorldCoord())); + inviterHomeblocks.add(townHomeBlock.getWorldCoord()); - //Grab all the towns in the nation - List nation2Towns = nation2.getTowns(); - ArrayList> clustersInvited = new ArrayList<>(); - ArrayList invitedHomeblocks = new ArrayList<>(); - for (Town t : nation2Towns) { - clustersInvited.add(getCluster(nation2.getCapital().getHomeBlock().getWorldCoord())); - invitedHomeblocks.add(t.getHomeBlock().getWorldCoord()); - - } - - //This may take forever :skull: at like multiple 900 size towns it takes like 1mil iterations????? - //IDK how tf else to check this - //find nearest block to invited homeblock - - WorldCoord nearestInvited1 = null; - int distInvited1 = 84852814 / 16; - - for (WorldCoord homeblock1 : invitedHomeblocks) { - WorldCoord nearestInvited2 = null; - int distInvited2 = 84852814 / 16; - for (ArrayList cluster2 : clustersInviter) { - for (WorldCoord coord2 : cluster2) { - int distX = homeblock1.getX() - coord2.getX(); - int distZ = homeblock1.getZ() - coord2.getZ(); - if (distX < 0) distX *= -1; - if (distZ < 0) distZ *= -1; - //pythagorus my boi - int dist2 = (int) Math.sqrt((distX * distX) + (distZ * distZ)); - - //if we find a new min, save it - if (dist2 <= distInvited2) { - distInvited2 = dist2; - nearestInvited2 = coord2; - - } - } - } - //compare to other homeblock searches - if (distInvited2 <= distInvited1) { - distInvited1 = distInvited2; - nearestInvited1 = nearestInvited2; - - } - } - - - //repeat in the other direction - WorldCoord nearestInviter1 = null; - int distInviter1 = 84852814 / 16; - - for (WorldCoord homeblock1 : inviterHomeblocks) { - WorldCoord nearestInviter2 = null; - int distInviter2 = 84852814 / 16; - for (ArrayList cluster2 : clustersInvited) { - for (WorldCoord coord2 : cluster2) { - int distX = homeblock1.getX() - coord2.getX(); - int distZ = homeblock1.getZ() - coord2.getZ(); - if (distX < 0) distX *= -1; - if (distZ < 0) distZ *= -1; - //pythagorus my boi - int dist2 = (int) Math.sqrt((distX * distX) + (distZ * distZ)); - - //if we find a new min, save it - if (dist2 <= distInviter2) { - distInviter2 = dist2; - nearestInviter2 = coord2; - - } - } - } - //compare to other homeblock searches - if (distInviter2 <= distInviter1) { - distInviter1 = distInviter2; - nearestInviter1 = nearestInviter2; - - } - } - - if(nearestInviter1 == null || nearestInvited1 == null) throw new NullPointerException(); - - dist = (int) Math.sqrt(((nearestInviter1.getX() - nearestInvited1.getX()) * (nearestInviter1.getX() - nearestInvited1.getX())) + ((nearestInviter1.getZ() - nearestInvited1.getZ()) * (nearestInviter1.getZ() - nearestInvited1.getZ()))); - - } catch (TownyException e) { - e.printStackTrace(); } + + //Grab all the towns in the nation + List nation2Towns = nation2.getTowns(); + ArrayList> clustersInvited = new ArrayList<>(); + ArrayList invitedHomeblocks = new ArrayList<>(); + for (Town t : nation2Towns) { + TownBlock townHomeBlock = t.getHomeBlockOrNull(); + if(townHomeBlock == null) { + Bukkit.getLogger().log(Level.SEVERE, "[TownyRangeLimit] " + t.getName() + " has not set a home block! Town must have a homeblock! Failing automatically."); + p.sendMessage("[TownyRangeLimit] " + t.getName() + " has not set a home block! Town must have a homeblock! Failing automatically."); + return dist; + } + clustersInvited.add(getCluster(townHomeBlock.getWorldCoord())); + invitedHomeblocks.add(townHomeBlock.getWorldCoord()); + } + + //This may take forever :skull: at like multiple 900 size towns it takes like 1mil iterations????? + //IDK how tf else to check this + //find nearest block to invited homeblock + + WorldCoord nearestInvited1 = null; + int distInvited1 = 84852814 / 16; + + for (WorldCoord homeblock1 : invitedHomeblocks) { + WorldCoord nearestInvited2 = null; + int distInvited2 = 84852814 / 16; + for (ArrayList cluster2 : clustersInviter) { + for (WorldCoord coord2 : cluster2) { + int distX = homeblock1.getX() - coord2.getX(); + int distZ = homeblock1.getZ() - coord2.getZ(); + if (distX < 0) distX *= -1; + if (distZ < 0) distZ *= -1; + //pythagorus my boi + int dist2 = (int) Math.sqrt((distX * distX) + (distZ * distZ)); + + //if we find a new min, save it + if (dist2 <= distInvited2) { + distInvited2 = dist2; + nearestInvited2 = coord2; + + } + } + } + //compare to other homeblock searches + if (distInvited2 <= distInvited1) { + distInvited1 = distInvited2; + nearestInvited1 = nearestInvited2; + + } + } + + + //repeat in the other direction + WorldCoord nearestInviter1 = null; + int distInviter1 = 84852814 / 16; + + for (WorldCoord homeblock1 : inviterHomeblocks) { + WorldCoord nearestInviter2 = null; + int distInviter2 = 84852814 / 16; + for (ArrayList cluster2 : clustersInvited) { + for (WorldCoord coord2 : cluster2) { + int distX = homeblock1.getX() - coord2.getX(); + int distZ = homeblock1.getZ() - coord2.getZ(); + if (distX < 0) distX *= -1; + if (distZ < 0) distZ *= -1; + //pythagorus my boi + int dist2 = (int) Math.sqrt((distX * distX) + (distZ * distZ)); + + //if we find a new min, save it + if (dist2 <= distInviter2) { + distInviter2 = dist2; + nearestInviter2 = coord2; + + } + } + } + //compare to other homeblock searches + if (distInviter2 <= distInviter1) { + distInviter1 = distInviter2; + nearestInviter1 = nearestInviter2; + + } + } + + if(nearestInviter1 == null || nearestInvited1 == null) throw new NullPointerException(); + + dist = (int) Math.sqrt(((nearestInviter1.getX() - nearestInvited1.getX()) * (nearestInviter1.getX() - nearestInvited1.getX())) + ((nearestInviter1.getZ() - nearestInvited1.getZ()) * (nearestInviter1.getZ() - nearestInvited1.getZ()))); + } return dist; }