diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownyAdminCommand.java b/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownyAdminCommand.java index 12e7a8a5cf..fef7518132 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownyAdminCommand.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownyAdminCommand.java @@ -1055,14 +1055,10 @@ private void parseAdminCheckOutpostsCommand(CommandSender sender, @Nullable Town int removed = 0; for (Town town : towns) { for (Location loc : town.getAllOutpostSpawns()) { - boolean save = false; if (TownyAPI.getInstance().isWilderness(loc) || !TownyAPI.getInstance().getTown(loc).getUUID().equals(town.getUUID())) { - town.removeOutpostSpawn(loc); - save = true; + town.removeOutpost(loc); removed++; } - if (save) - town.save(); } } TownyMessaging.sendMsg(sender, Translatable.of("msg_removed_x_invalid_outpost_spawns", removed)); diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/db/TownySQLSource.java b/Towny/src/main/java/com/palmergames/bukkit/towny/db/TownySQLSource.java index 6ecaf6d543..64acb4929a 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/db/TownySQLSource.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/db/TownySQLSource.java @@ -2011,6 +2011,7 @@ public boolean loadTownBlocks() { UUID outpostID = UUID.fromString(line.trim()); Outpost outpost = universe.getOutpost(outpostID); if (outpost != null) { + outpost.addTownblock(townBlock); townBlock.setOutpostObject(outpost); } } catch (Exception ignored) { 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 74bfe64a0b..207a6c52f6 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 @@ -1,6 +1,5 @@ package com.palmergames.bukkit.towny.object; -import com.google.common.collect.Lists; import com.palmergames.bukkit.towny.Towny; import com.palmergames.bukkit.towny.TownyAPI; import com.palmergames.bukkit.towny.TownyEconomyHandler; @@ -80,7 +79,6 @@ public class Town extends Government implements TownBlockOwner { private final Set trustedResidents = new HashSet<>(); private final Map trustedTowns = new LinkedHashMap<>(); private List outposts = new ArrayList<>(); - private final List outpostSpawns = new ArrayList<>(); private List jails = null; private HashMap plotGroups = null; private TownBlockTypeCache plotTypeCache = new TownBlockTypeCache(); @@ -950,38 +948,15 @@ public void setOutpostSpawns(List outpostSpawns) { } public void addOutpostSpawn(Location location) { - addOutpostSpawn(Position.ofLocation(location)); - } - - /** - * Add or update an outpost spawn for a town. - * Saves the TownBlock if it is not already an Outpost. - * Saves the Town when finished. - * - * @param position Position to set an outpost's spawn point - */ - public void addOutpostSpawn(Position position) { - TownBlock townBlock = position.worldCoord().getTownBlockOrNull(); - if (townBlock == null || !this.equals(townBlock.getTownOrNull())) - return; - - // Remove any potential previous outpost spawn at this location (when run via /t set outpost.) - removeOutpostSpawn(position.worldCoord()); - - // Set the TownBlock to be an outpost. - if (!townBlock.isOutpost()) { - townBlock.setOutpost(true); - townBlock.save(); - } + TownBlock townBlock = TownyAPI.getInstance().getTownBlock(location); + Outpost outpost = townBlock.hasOutpost() + ? townBlock.getOutpost() + : new Outpost(UUID.randomUUID(), townBlock.getName() != "" ? townBlock.getName() : String.valueOf(outposts.size() + 1)); - // Add to the towns' outpost list. - outpostSpawns.add(position); - - // Add a SpawnPoint so a particle effect is displayed. - TownyUniverse.getInstance().addSpawnPoint(new SpawnPoint(spawn, SpawnPointType.OUTPOST_SPAWN)); - - // Save the town. - this.save(); + outpost.setSpawn(Position.ofLocation(location)); + if (outpost.getNumTownBlock() == 0) + outpost.addTownblock(townBlock); + outpost.save(); } /** @@ -1007,38 +982,26 @@ public boolean hasOutpostSpawn() { return getMaxOutpostSpawn() == 0; } - // Used because (perhaps) some mysql databases do not properly save a townblock's outpost flag. - private boolean isAnOutpost(Coord coord) { - return new ArrayList<>(outpostSpawns).stream().anyMatch(spawn -> spawn.worldCoord().equals(coord)); - } - /** * Get an unmodifiable List of all outpost spawns. * * @return List of outpostSpawns */ public List getAllOutpostSpawns() { - return Collections.unmodifiableList(Lists.transform(this.outpostSpawns, Position::asLocation)); + return outposts.stream().map(Outpost::getSpawn).map(Position::asLocation).collect(Collectors.toUnmodifiableList()); } /** * @return Similar to {@link #getAllOutpostSpawns()}, but with positions. */ public Collection getOutpostSpawns() { - return Collections.unmodifiableList(this.outpostSpawns); - } - - public void removeOutpostSpawn(Coord coord) { - new ArrayList<>(getAllOutpostSpawns()).stream() - .filter(spawn -> Coord.parseCoord(spawn).equals(coord)) - .forEach(spawn -> { - removeOutpostSpawn(spawn); - TownyUniverse.getInstance().removeSpawnPoint(spawn); - }); + return outposts.stream().map(Outpost::getSpawn).collect(Collectors.toUnmodifiableList()); } - public void removeOutpostSpawn(Location loc) { - outpostSpawns.remove(Position.ofLocation(loc)); + public void removeOutpost(Location loc) { + Optional optOutpost = outposts.stream().filter(o -> o.getTownblocks().contains(TownyAPI.getInstance().getTownBlock(loc))).findFirst(); + if (optOutpost.isPresent()) + removeOutpost(optOutpost.get()); } public List getOutpostNames() { diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/TownClaim.java b/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/TownClaim.java index 1f96efc19c..5278805709 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/TownClaim.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/TownClaim.java @@ -234,7 +234,9 @@ private void townClaim(WorldCoord worldCoord) throws TownyException { if (outpost) { Outpost outpostObject = new Outpost(UUID.randomUUID(), ((OutpostWorldCoord) worldCoord).getName()); outpostObject.setSpawn(Position.ofLocation(outpostLocation)); + outpostObject.addTownblock(townBlock); town.addOutpost(outpostObject); + outpostObject.save(); } if (!alreadyClaimed)