From 023a376e5b946d869aebc06d842981dbff383ebe Mon Sep 17 00:00:00 2001 From: LlmDl Date: Tue, 12 Nov 2024 10:01:51 -0600 Subject: [PATCH] - Fix another issue with the proximity-to-nation test. --- Towny/pom.xml | 2 +- .../bukkit/towny/utils/ProximityUtil.java | 33 +++++++++++++++---- Towny/src/main/resources/ChangeLog.txt | 4 ++- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/Towny/pom.xml b/Towny/pom.xml index 50801769b0..a9a4b0c248 100644 --- a/Towny/pom.xml +++ b/Towny/pom.xml @@ -13,7 +13,7 @@ towny jar - 0.100.4.12 + 0.100.4.13 diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/utils/ProximityUtil.java b/Towny/src/main/java/com/palmergames/bukkit/towny/utils/ProximityUtil.java index aadd4ccbd5..af06fe529e 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/utils/ProximityUtil.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/utils/ProximityUtil.java @@ -1,6 +1,7 @@ package com.palmergames.bukkit.towny.utils; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -241,11 +242,29 @@ public static void testTownProximityToNation(Town town, Nation nation) throws To throw new TownyException(Translatable.of("msg_err_nation_homeblock_in_another_world")); } - if (isTownTooFarFromNation(town, capital, nation.getTowns())) { + List townsClosestToFarthest = sortTownsClosestToFarthest(nation); + if (isTownTooFarFromNation(town, capital, townsClosestToFarthest)) { throw new TownyException(Translatable.of("msg_err_town_not_close_enough_to_nation", town.getName())); } } + private static List sortTownsClosestToFarthest(Nation nation) { + List sortedTowns = nation.getTowns().stream() + .sorted(Comparator.comparingInt(t-> getDistanceFromCapital(t, nation))) + .collect(Collectors.toList()); + return sortedTowns; + } + + private static int getDistanceFromCapital(Town town, Nation nation) { + TownBlock capitalHomeblock = nation.getCapital().getHomeBlockOrNull(); + TownBlock townHomeblock = town.getHomeBlockOrNull(); + if (capitalHomeblock == null || townHomeblock == null) + return Integer.MAX_VALUE; + if (!capitalHomeblock.getWorld().equals(townHomeblock.getWorld())) + return Integer.MAX_VALUE; + return (int) MathUtil.distance(capitalHomeblock.getCoord(), townHomeblock.getCoord()); + } + public static List gatherOutOfRangeTowns(Nation nation) { return gatherOutOfRangeTowns(nation, nation.getCapital()); } @@ -261,7 +280,7 @@ public static List gatherOutOfRangeTowns(Nation nation, Town capital) { return removedTowns; final WorldCoord capitalCoord = capitalHomeBlock.getWorldCoord(); - List townsToCheck = nation.getTowns(); + List townsToCheck = sortTownsClosestToFarthest(nation); List localTownsToKeep = new ArrayList<>(); townsToCheck.remove(capital); localTownsToKeep.add(capital); @@ -294,7 +313,7 @@ private static List getListOfInRangeTownsFromList(List townsToCheck, continue; // Check that the town missing is not missing a homeblock, and that the // homeblocks are in the same world, and the distance between. - if (isTownCloseEnoughToNation(town, capital, townsToCheck, validTowns)) + if (isTownCloseEnoughToNation(town, capital, townsToCheck, validTowns)) allowedTowns.add(town); } return allowedTowns; @@ -319,9 +338,11 @@ private static boolean closeEnoughToOtherNationTowns(Town town, Town newCapital, return false; // Try to find at least one town in the nation which is close enough to this town. - return townsToCheck.stream() - .filter(t -> !t.equals(town) && !t.isCapital() && validTowns.contains(t)) - .anyMatch(t -> closeEnoughToTown(town, t, maxDistanceFromOtherTowns)); + for (Town validTown : validTowns) { + if (closeEnoughToTown(validTown, town, maxDistanceFromOtherTowns)) + return true; + } + return false; } public static void removeOutOfRangeTowns(Nation nation) { diff --git a/Towny/src/main/resources/ChangeLog.txt b/Towny/src/main/resources/ChangeLog.txt index 2ca62f7aae..f076759746 100644 --- a/Towny/src/main/resources/ChangeLog.txt +++ b/Towny/src/main/resources/ChangeLog.txt @@ -10132,4 +10132,6 @@ v0.92.0.11: - Fix the proximity-to-nation test not removing too-far-away towns in every situation. - Closes #7668. - Add message to the plot forsale notification, shown when a player has no claimed townblocks, advising them how to claim a plot. - - New Language: Hungarian. \ No newline at end of file + - New Language: Hungarian. +0.100.4.13: + - Fix another issue with the proximity-to-nation test. \ No newline at end of file