diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/object/Town.java b/Towny/src/main/java/com/palmergames/bukkit/towny/object/Town.java index 5e43b8f801a..6aef7c48b82 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/object/Town.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/object/Town.java @@ -165,6 +165,16 @@ public boolean equals(Object other) { public Collection getTownBlocks() { return Collections.unmodifiableCollection(townBlocks.values()); } + + public Collection getTownBlocksInWorld(@NotNull TownyWorld world) { + List list = new ArrayList<>(); + for (Map.Entry entry : townBlocks.entrySet()) { + if (world.getName().equals(entry.getKey().getWorldName())) { + list.add(entry.getValue()); + } + } + return Collections.unmodifiableCollection(list); + } public int getNumTownBlocks() { return getTownBlocks().size(); 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 a52c6303a56..a71da07c72f 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 @@ -104,7 +104,7 @@ public static void testAdjacentClaimsRulesOrThrow(WorldCoord townBlockToClaim, T } public static void testAdjacentClaimsRulesOrThrow(WorldCoord townBlockToClaim, Town town, boolean outpost, int minAdjacentBlocks) throws TownyException { - if (!outpost && minAdjacentBlocks > 0 && townHasClaimedEnoughLandToBeRestrictedByAdjacentClaims(town, minAdjacentBlocks)) { + if (!outpost && minAdjacentBlocks > 0 && townHasClaimedEnoughLandToBeRestrictedByAdjacentClaims(town, minAdjacentBlocks, townBlockToClaim)) { // Only consider the first worldCoord, larger selection-claims will automatically "bubble" anyways. int numAdjacent = numAdjacentTownOwnedTownBlocks(town, townBlockToClaim); // The number of adjacent TBs is not enough and there is not a nearby outpost. @@ -113,11 +113,13 @@ public static void testAdjacentClaimsRulesOrThrow(WorldCoord townBlockToClaim, T } } - private static boolean townHasClaimedEnoughLandToBeRestrictedByAdjacentClaims(Town town, int minAdjacentBlocks) { - if (minAdjacentBlocks == 3 && town.getTownBlocks().size() < 5) + private static boolean townHasClaimedEnoughLandToBeRestrictedByAdjacentClaims(Town town, int minAdjacentBlocks, WorldCoord worldCoord) { + TownyWorld world = worldCoord.getTownyWorld(); + int claimedTownBlocks = world != null ? town.getTownBlocksInWorld(world).size() : town.getNumTownBlocks(); + if (minAdjacentBlocks == 3 && claimedTownBlocks < 5) // Special rule that makes sure a town can claim a fifth plot after claiming a 2x2 square. return false; - return town.getTownBlocks().size() > minAdjacentBlocks; + return claimedTownBlocks > minAdjacentBlocks; } private static int numAdjacentTownOwnedTownBlocks(Town town, WorldCoord worldCoord) { @@ -181,7 +183,7 @@ public static void testAdjacentUnclaimsRulesOrThrow(WorldCoord townBlockToUnclai public static void testAdjacentUnclaimsRulesOrThrow(WorldCoord townBlockToUnclaim, Town town, int minAdjacentBlocks) throws TownyException { // Prevent unclaiming land that would reduce the number of adjacent claims of neighbouring plots below the threshold. - if (minAdjacentBlocks > 0 && townHasClaimedEnoughLandToBeRestrictedByAdjacentClaims(town, minAdjacentBlocks)) { + if (minAdjacentBlocks > 0 && townHasClaimedEnoughLandToBeRestrictedByAdjacentClaims(town, minAdjacentBlocks, townBlockToUnclaim)) { // Outposts cannot be unclaimed when there are any connected townblocks. if (townBlockToUnclaim.getTownBlockOrNull().isOutpost() && numAdjacentTownOwnedTownBlocks(town, townBlockToUnclaim) > 0) @@ -213,7 +215,7 @@ public static void testAdjacentUnclaimsRulesOrThrow(WorldCoord townBlockToUnclai */ public static void testAdjacentAddDistrictRulesOrThrow(WorldCoord townBlockToClaim, Town town, District district, int minAdjacentBlocks) throws TownyException { - if (minAdjacentBlocks > 0 && townHasClaimedEnoughLandToBeRestrictedByAdjacentClaims(town, minAdjacentBlocks)) { + if (minAdjacentBlocks > 0 && townHasClaimedEnoughLandToBeRestrictedByAdjacentClaims(town, minAdjacentBlocks, townBlockToClaim)) { int numAdjacent = numAdjacentDistrictTownBlocks(town, district, townBlockToClaim); // The number of adjacement TBs with the same District is not enough. if (numAdjacent < minAdjacentBlocks) @@ -231,7 +233,7 @@ private static int numAdjacentDistrictTownBlocks(Town town, District district, W public static void testAdjacentRemoveDistrictRulesOrThrow(WorldCoord districtCoordBeingRemoved, Town town, District district, int minAdjacentBlocks) throws TownyException { // Prevent removing parts of Districts that would cause a district to split into two sections. - if (minAdjacentBlocks > 0 && townHasClaimedEnoughLandToBeRestrictedByAdjacentClaims(town, minAdjacentBlocks)) { + if (minAdjacentBlocks > 0 && townHasClaimedEnoughLandToBeRestrictedByAdjacentClaims(town, minAdjacentBlocks, districtCoordBeingRemoved)) { List allAdjacentDistrictWorldCoords = getAdjacentDistrictWorldCoords(town, district, districtCoordBeingRemoved, false); int districtPlots = allAdjacentDistrictWorldCoords.size();