Skip to content

Commit

Permalink
- Fix another issue with the proximity-to-nation test.
Browse files Browse the repository at this point in the history
  • Loading branch information
LlmDl committed Nov 12, 2024
1 parent 2d0ecc5 commit 023a376
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Towny/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<artifactId>towny</artifactId>
<packaging>jar</packaging>
<version>0.100.4.12</version>
<version>0.100.4.13</version>

<licenses>
<license>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Town> 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<Town> sortTownsClosestToFarthest(Nation nation) {
List<Town> 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<Town> gatherOutOfRangeTowns(Nation nation) {
return gatherOutOfRangeTowns(nation, nation.getCapital());
}
Expand All @@ -261,7 +280,7 @@ public static List<Town> gatherOutOfRangeTowns(Nation nation, Town capital) {
return removedTowns;

final WorldCoord capitalCoord = capitalHomeBlock.getWorldCoord();
List<Town> townsToCheck = nation.getTowns();
List<Town> townsToCheck = sortTownsClosestToFarthest(nation);
List<Town> localTownsToKeep = new ArrayList<>();
townsToCheck.remove(capital);
localTownsToKeep.add(capital);
Expand Down Expand Up @@ -294,7 +313,7 @@ private static List<Town> getListOfInRangeTownsFromList(List<Town> 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;
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion Towny/src/main/resources/ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- New Language: Hungarian.
0.100.4.13:
- Fix another issue with the proximity-to-nation test.

0 comments on commit 023a376

Please sign in to comment.