Skip to content

Commit

Permalink
- Refactor town homeblock proximity tests into new ProximityUtil
Browse files Browse the repository at this point in the history
method.
  • Loading branch information
LlmDl committed Dec 29, 2023
1 parent e9ff708 commit 2352315
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 56 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.0.17</version>
<version>0.100.0.18</version>

<licenses>
<license>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2304,26 +2304,7 @@ public static void townSetHomeblock(Player player, final Town town) throws Towny
if (town.hasHomeBlock() && town.getHomeBlock().getWorldCoord().equals(townBlock.getWorldCoord()))
throw new TownyException(Translatable.of("msg_err_homeblock_already_set_here"));

if (world.hasTowns() &&
TownySettings.getMinDistanceFromTownHomeblocks() > 0 ||
TownySettings.getMaxDistanceBetweenHomeblocks() > 0 ||
TownySettings.getMinDistanceBetweenHomeblocks() > 0) {

final int distanceToNextNearestHomeblock = world.getMinDistanceFromOtherTownsHomeBlocks(Coord.parseCoord(player), town);
if (distanceToNextNearestHomeblock < TownySettings.getMinDistanceFromTownHomeblocks() ||
distanceToNextNearestHomeblock < TownySettings.getMinDistanceBetweenHomeblocks())
throw new TownyException(Translatable.of("msg_too_close2", Translatable.of("homeblock")));

if (TownySettings.getMaxDistanceBetweenHomeblocks() > 0 &&
distanceToNextNearestHomeblock > TownySettings.getMaxDistanceBetweenHomeblocks())
throw new TownyException(Translatable.of("msg_too_far"));
}

if (TownySettings.getHomeBlockMovementDistanceInTownBlocks() > 0) {
double distance = MathUtil.distance(town.getHomeBlock().getCoord(), townBlock.getCoord());
if (distance > TownySettings.getHomeBlockMovementDistanceInTownBlocks())
throw new TownyException(Translatable.of("msg_err_you_cannot_move_your_homeblock_this_far_limit_is_x_you_are_x", TownySettings.getHomeBlockMovementDistanceInTownBlocks(), Math.floor(distance)));
}
ProximityUtil.allowTownHomeBlockOrThrow(world, townBlock.getCoord(), town, false);

BukkitTools.ifCancelledThenThrow(new TownPreSetHomeBlockEvent(town, townBlock, player));

Expand Down Expand Up @@ -2584,8 +2565,7 @@ public static void newTown(Player player, String name, Resident resident, boolea
throw new TownyException("msg_err_cannot_begin_town_in_this_biome");
}

if (world.hasTowns())
testDistancesOrThrow(world, key);
ProximityUtil.allowTownHomeBlockOrThrow(world, key, null, true);

// If the town doesn't cost money to create, just make the Town.
if (noCharge || !TownyEconomyHandler.isActive()) {
Expand Down Expand Up @@ -2692,37 +2672,6 @@ public static Town newTown(TownyWorld world, String name, Resident resident, Coo
return town;
}

private static void testDistancesOrThrow(TownyWorld world, Coord key) throws TownyException {
if (TownySettings.getMinDistanceFromTownPlotblocks() > 0 || TownySettings.getNewTownMinDistanceFromTownPlots() > 0) {
int minDistance = TownySettings.getNewTownMinDistanceFromTownPlots();
if (minDistance <= 0)
minDistance = TownySettings.getMinDistanceFromTownPlotblocks();

if (world.getMinDistanceFromOtherTownsPlots(key) < minDistance)
throw new TownyException(Translatable.of("msg_too_close2", Translatable.of("townblock")));
}

if (TownySettings.getMinDistanceFromTownHomeblocks() > 0 ||
TownySettings.getMaxDistanceBetweenHomeblocks() > 0 ||
TownySettings.getMinDistanceBetweenHomeblocks() > 0 ||
TownySettings.getNewTownMinDistanceFromTownHomeblocks() > 0) {

final int distanceToNextNearestHomeblock = world.getMinDistanceFromOtherTownsHomeBlocks(key);

int minDistance = TownySettings.getNewTownMinDistanceFromTownHomeblocks();
if (minDistance <= 0)
minDistance = TownySettings.getMinDistanceFromTownHomeblocks();

if (distanceToNextNearestHomeblock < minDistance || distanceToNextNearestHomeblock < TownySettings.getMinDistanceBetweenHomeblocks())
throw new TownyException(Translatable.of("msg_too_close2", Translatable.of("homeblock")));

if (TownySettings.getMaxDistanceBetweenHomeblocks() > 0 &&
TownyUniverse.getInstance().getTowns().size() > 0 &&
distanceToNextNearestHomeblock > TownySettings.getMaxDistanceBetweenHomeblocks())
throw new TownyException(Translatable.of("msg_too_far"));
}
}

private static String filterNameOrThrow(String name) throws TownyException {
if (TownySettings.getTownAutomaticCapitalisationEnabled())
name = StringMgmt.capitalizeStrings(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ private static List<WorldCoord> selectWorldCoordAreaCircle(int maxSelectionSize,
* Returns a list containing only townblocks that can be claimed.
* Filters out townblocks too close to other towns as set in the config.
*
* @param selection - List&lt;WorldCoord&gt; of coordinates
* @param town - Town to check distance from
* @return List of {@link WorldCoord}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,69 @@
import java.util.List;
import java.util.stream.Collectors;

import org.jetbrains.annotations.Nullable;

import com.palmergames.bukkit.towny.TownyMessaging;
import com.palmergames.bukkit.towny.TownySettings;
import com.palmergames.bukkit.towny.exceptions.TownyException;
import com.palmergames.bukkit.towny.object.Coord;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownBlock;
import com.palmergames.bukkit.towny.object.TownyWorld;
import com.palmergames.bukkit.towny.object.Translatable;
import com.palmergames.bukkit.towny.object.WorldCoord;
import com.palmergames.util.MathUtil;

public class ProximityUtil {

/*
* Town Proximity Methods
*/

public static void allowTownHomeBlockOrThrow(TownyWorld world, Coord key, @Nullable Town town, boolean newTown) throws TownyException {
if (!world.hasTowns()) // No towns exist yet, we cannot be too close to any other town.
return;

if (newTown) { // Tests run only when there is a new town involved.
if (TownySettings.getMinDistanceFromTownPlotblocks() > 0 || TownySettings.getNewTownMinDistanceFromTownPlots() > 0) {
// Sometimes new towns have special min. distances from other towns.
int minDistance = TownySettings.getNewTownMinDistanceFromTownPlots();
if (minDistance <= 0)
minDistance = TownySettings.getMinDistanceFromTownPlotblocks();

// throws when a new town is being made to close to another town's land.
if (world.getMinDistanceFromOtherTownsPlots(key) < minDistance)
throw new TownyException(Translatable.of("msg_too_close2", Translatable.of("townblock")));
}
}

if (TownySettings.getMinDistanceFromTownHomeblocks() > 0 ||
TownySettings.getMaxDistanceBetweenHomeblocks() > 0 ||
TownySettings.getMinDistanceBetweenHomeblocks() > 0 ||
(newTown && TownySettings.getNewTownMinDistanceFromTownHomeblocks() > 0)) {

final int distanceToNextNearestHomeblock = world.getMinDistanceFromOtherTownsHomeBlocks(key, town);
// Sometimes new towns have special min. distances from other towns' homeblocks.
int minDistance = newTown ? TownySettings.getNewTownMinDistanceFromTownHomeblocks() : 0;
if (minDistance <= 0)
minDistance = TownySettings.getMinDistanceFromTownHomeblocks();

// throws when the town's homeblock is too close to another homeblock.
if (distanceToNextNearestHomeblock < minDistance || distanceToNextNearestHomeblock < TownySettings.getMinDistanceBetweenHomeblocks())
throw new TownyException(Translatable.of("msg_too_close2", Translatable.of("homeblock")));

// throws when the town's homeblock would be too far from other towns' homeblocks.
if (TownySettings.getMaxDistanceBetweenHomeblocks() > 0 &&
distanceToNextNearestHomeblock > TownySettings.getMaxDistanceBetweenHomeblocks())
throw new TownyException(Translatable.of("msg_too_far"));
}
}

/*
* Nation Promixity Methods
*/

public static void testTownProximityToNation(Town town, Nation nation) throws TownyException {
if (TownySettings.getNationProximityToCapital() <= 0)
return;
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 @@ -9356,4 +9356,6 @@ v0.92.0.11:
- New Config Option: claiming.biome_rules.ocean_blocking.threshold
- Default: 55
- The max amount of combined ocean biomes as a percent, that will be allowed in plots being claimed by towns.
- For example, if a townblock would be more than X percent ocean it will not be able to be claimed.
- For example, if a townblock would be more than X percent ocean it will not be able to be claimed.
0.100.0.18:
- Refactor town homeblock proximity tests into new ProximityUtil method.

0 comments on commit 2352315

Please sign in to comment.