From 9ff6aeddcd21788189e4a9a2a127b6356a8fd251 Mon Sep 17 00:00:00 2001 From: Aubri Seebauer Date: Sun, 2 Apr 2023 16:07:38 -0400 Subject: [PATCH] V1 --- pom.xml | 5 + .../townyrangelimit/TownyRangeLimit.java | 423 +++++++++++++++++- src/main/resources/config.yml | 2 + 3 files changed, 426 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/config.yml diff --git a/pom.xml b/pom.xml index 9901f57..9bd575e 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,10 @@ sonatype https://oss.sonatype.org/content/groups/public/ + + jitpack.io + https://jitpack.io + @@ -74,6 +78,7 @@ com.github.LlmDl Towny 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 32aa2d6..5c6ffb6 100644 --- a/src/main/java/org/aubrithehuman/townyrangelimit/TownyRangeLimit.java +++ b/src/main/java/org/aubrithehuman/townyrangelimit/TownyRangeLimit.java @@ -1,17 +1,432 @@ 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.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.plugin.java.JavaPlugin; -public final class TownyRangeLimit extends JavaPlugin { +import java.util.ArrayList; +import java.util.List; + +public final class TownyRangeLimit extends JavaPlugin implements Listener { @Override public void onEnable() { - // Plugin startup logic - + this.saveDefaultConfig(); + getServer().getPluginManager().registerEvents(this, this); + this.getLogger().info("TownyRangeLimit loaded with limit of: " + this.getConfig().getInt("townblockRangeMax") + " townblocks"); } @Override public void onDisable() { - // Plugin shutdown logic } + + @EventHandler(priority = EventPriority.HIGHEST) + public void doCheck(PlayerCommandPreprocessEvent event) { + String[] args = event.getMessage().split(" "); + if(args.length >= 2) { + if (args[0].equalsIgnoreCase("/n") || args[0].equalsIgnoreCase("/nat") || args[0].equalsIgnoreCase("/nation")) { + //n merge + if (args[1].equalsIgnoreCase("merge")) { + if (!args[2].equalsIgnoreCase("deny") && !args[2].equalsIgnoreCase("accept")) { + Resident r = TownyAPI.getInstance().getResident(event.getPlayer()); + if(r == null) return; + Nation nation1 = r.getNationOrNull(); + Nation nation2 = TownyAPI.getInstance().getNation(args[2]); + + if(nation1 == null || nation2 == null) return; + int dist = shortestDistance(nation1, nation2); + + 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.setCancelled(true); + } + } + } + //n invite + else if (args[1].equalsIgnoreCase("invite")) { + if (!args[2].equalsIgnoreCase("deny") && !args[1].equalsIgnoreCase("accept")) { + TownyWorld tw = WorldCoord.parseWorldCoord(event.getPlayer()).getTownyWorld(); + if(tw == null) return; + Town town = tw.getTowns().get(args[2]); + Resident r = TownyAPI.getInstance().getResident(event.getPlayer()); + if(r == null) return; + Nation nation = r.getNationOrNull(); + + int dist = shortestDistance(town, nation); + + 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.setCancelled(true); + } + } + } + //n join (public) + else if (args[1].equalsIgnoreCase("join")) { + Nation nation = TownyAPI.getInstance().getNation(args[2]); + if(nation == null) return; + if(nation.isOpen()) { + Resident r = TownyAPI.getInstance().getResident(event.getPlayer()); + if(r == null) return; + Town town = r.getTownOrNull(); + + int dist = shortestDistance(town, nation); + + 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.setCancelled(true); + } + } + } + } + // t merge (wierd case lol) + else if (args[0].equalsIgnoreCase("/t") || args[0].equalsIgnoreCase("/town")) { + if (args[1].equalsIgnoreCase("merge")) { + if (!args[2].equalsIgnoreCase("deny") && !args[2].equalsIgnoreCase("accept")) { + this.getLogger().info(args[1]); + Resident r = TownyAPI.getInstance().getResident(event.getPlayer()); + if(r == null) return; + Town town1 = r.getTownOrNull(); + Town town2 = TownyAPI.getInstance().getTown(args[2]); + + if(town1 == null || town2 == null) return; + + int dist = shortestDistance(town1, town2); + + 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.setCancelled(true); + } + } + } + } + } + } + + /** + * Finds the shortest distance (in townblocks) between two towns + * @param town1 + * @param town2 + * @return + */ + private static int shortestDistance(Town town1, Town town2) { + //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(); + + //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 didnt 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; + } + } + + 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 + * @param nation + * @return + */ + 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 of 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 didnt 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(); + } + } + return dist; + } + + /** + * Finds the shortest distance (in townblocks) between two nations + * @param nation1 + * @param nation2 + * @return + */ + + private static int shortestDistance(Nation nation1, Nation nation2) { + //max possible range (60M*sqrt2) + // 16 is block size + int dist = 84852814 / 16; + if (nation1 != null && nation2 != null) { + try { + + //Grab all of 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 of 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(); + } + } + return dist; + } + + /** + * + * @param worldCoord + * @return + * @author Isaac + */ + private static ArrayList getAdjCells(WorldCoord worldCoord) { + ArrayList ReturnList = new ArrayList(); + + int[][] XZarray = new int[][]{ + {-1, 0}, + {1, 0}, + {0, -1}, + {0, 1} + }; // Array that contains relative orthogonal shifts from origin + for ( int[] pair: XZarray) { + // Constructs new WorldCoord for comparison + WorldCoord tCoord = new WorldCoord(worldCoord.getWorldName(), worldCoord.getX() + pair[0], worldCoord.getZ() + pair[1]); + if (tCoord.getTownOrNull() != null && tCoord.getTownOrNull() == worldCoord.getTownOrNull()) { + // If in town, and in same town, adds to return list + ReturnList.add(tCoord); + } + } + return ReturnList; + } + + /** Gets all adjacently connected townblocks + * @param chunkCoord - WorldCoord to check at + * @return List of WorldCoords + * @author Isaac + */ + public static ArrayList getCluster(WorldCoord chunkCoord){ + // WCoordList is the returning array, SearchList is the to-search list. + ArrayList WCoordList = new ArrayList(); + ArrayList SearchList = new ArrayList(); + SearchList.add(chunkCoord); // Adds 1st chunk to list + + // Iterates through SearchList, to create a full list of every adjacent cell. + while (SearchList.size() > 0) { + // Gets WorldCoord + WorldCoord toSearch = SearchList.get(0); + // Gets adjacent cells + ArrayList adjCells = getAdjCells(toSearch); + for (WorldCoord cell : adjCells) { + // If in final list, ignore. + if (WCoordList.contains(cell)) { + continue; + // If in to-search list, ignore + } else if (SearchList.contains(cell)) { + continue; + // Otherwise, add to search-list. + } else { + SearchList.add(cell); + } + } + // Removes from search list and adds to finished list. After checking all adjacent chunks. + SearchList.remove(toSearch); + WCoordList.add(toSearch); + } + + return WCoordList; // Returns list + } + } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..4a07959 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,2 @@ +# Townblock max range when merging, joining, or inviting +townblockRangeMax: 64 \ No newline at end of file