Skip to content

Commit

Permalink
Add support for moving outpost spawns, setting outposts with /plot set
Browse files Browse the repository at this point in the history
outpost.
  • Loading branch information
LlmDl committed Nov 4, 2024
1 parent 71e6a17 commit 2957759
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
65 changes: 14 additions & 51 deletions Towny/src/main/java/com/palmergames/bukkit/towny/object/Town.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -80,7 +79,6 @@ public class Town extends Government implements TownBlockOwner {
private final Set<Resident> trustedResidents = new HashSet<>();
private final Map<UUID, Town> trustedTowns = new LinkedHashMap<>();
private List<Outpost> outposts = new ArrayList<>();
private final List<Position> outpostSpawns = new ArrayList<>();
private List<Jail> jails = null;
private HashMap<String, PlotGroup> plotGroups = null;
private TownBlockTypeCache plotTypeCache = new TownBlockTypeCache();
Expand Down Expand Up @@ -950,38 +948,15 @@ public void setOutpostSpawns(List<Location> 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();
}

/**
Expand All @@ -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<Location> 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<Position> 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<Outpost> optOutpost = outposts.stream().filter(o -> o.getTownblocks().contains(TownyAPI.getInstance().getTownBlock(loc))).findFirst();
if (optOutpost.isPresent())
removeOutpost(optOutpost.get());
}

public List<String> getOutpostNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2957759

Please sign in to comment.