Skip to content

Commit

Permalink
Beginning steps to making proper Outpost objects.
Browse files Browse the repository at this point in the history
Remove old outpost db stuff from sql source.
Start on replacing old town methods using outpostSpawns with new stuff.
Add support for moving outpost spawns, setting outposts with /plot set
outpost.
Add a maybe-working legacy outpost converter.
Make the legacy converter twice as likely to succeed.
Lots of testing completed, legacy conversion working well.

/plot set outpost spawn & /plot set outpost taken care of.

TODO: Change Plot Perm Hud to reduce lines to 15.
Better logic and message on claim denial.
  • Loading branch information
LlmDl committed Nov 6, 2024
1 parent 7fbd0d8 commit 91c31a8
Show file tree
Hide file tree
Showing 23 changed files with 791 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public static StatusScreen getStatus(TownBlock townBlock, Player player) {
if (townBlock.hasDistrict())
screen.addComponentOf("district", colourKey(translator.of("status_district_name_and_size", townBlock.getDistrict().getName(), townBlock.getDistrict().getTownBlocks().size())));

if (townBlock.hasOutpostObject())
screen.addComponentOf("outpost", colourKey(translator.of("status_outpost_name_and_size", townBlock.getOutpost().getName(), townBlock.getOutpost().getNumTownBlocks())));

if (townBlock.hasPlotObjectGroup())
screen.addComponentOf("plotgroup", colourKey(translator.of("status_plot_group_name_and_size", townBlock.getPlotObjectGroup().getName(), townBlock.getPlotObjectGroup().getTownBlocks().size())));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.palmergames.bukkit.towny.confirmations.Confirmation;
import com.palmergames.bukkit.towny.invites.Invite;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Outpost;
import com.palmergames.bukkit.towny.object.PlotGroup;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
Expand All @@ -26,7 +27,6 @@
import org.apache.logging.log4j.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -588,7 +588,7 @@ public static void sendOutpostList(Player player, Town town, int page, int total
Translator translator = Translator.locale(player);
int outpostsCount = town.getAllOutpostSpawns().size();
int iMax = Math.min(page * 10, outpostsCount);
List<Location> outposts = town.getAllOutpostSpawns();
List<Outpost> outpostObjects = town.getOutposts();

TextComponent[] outpostsFormatted;

Expand All @@ -599,19 +599,19 @@ public static void sendOutpostList(Player player, Town town, int page, int total
}

for (int i = (page - 1) * 10; i < iMax; i++) {
Location outpost = outposts.get(i);
TownBlock tb = TownyAPI.getInstance().getTownBlock(outpost);
if (tb == null)
Outpost outpost = outpostObjects.get(i);
TownBlock tb = outpost.getSpawnTownBlock();
if (outpost.getSpawn() == null || tb == null)
continue;
String name = !tb.hasPlotObjectGroup() ? tb.getName() : tb.getPlotObjectGroup().getName();
String name = outpost.getName();
TextComponent dash = Component.text(" - ", NamedTextColor.DARK_GRAY);
TextComponent line = Component.text(Integer.toString(i + 1), NamedTextColor.GOLD)
.clickEvent(ClickEvent.runCommand("/towny:town outpost " + (i + 1)))
.append(dash);

TextComponent outpostName = Component.text(name, NamedTextColor.GREEN);
TextComponent worldName = Component.text(Optional.ofNullable(outpost.getWorld()).map(w -> w.getName()).orElse("null"), NamedTextColor.BLUE);
TextComponent coords = Component.text("(" + outpost.getBlockX() + "," + outpost.getBlockZ()+ ")", NamedTextColor.BLUE);
TextComponent worldName = Component.text(Optional.ofNullable(outpost.getSpawn().world()).map(w -> w.getName()).orElse("null"), NamedTextColor.BLUE);
TextComponent coords = Component.text("(" + outpost.getSpawn().blockX() + "," + outpost.getSpawn().blockZ()+ ")", NamedTextColor.BLUE);

if (!name.equalsIgnoreCase("")) {
line = line.append(outpostName).append(dash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException;
import com.palmergames.bukkit.towny.object.District;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Outpost;
import com.palmergames.bukkit.towny.object.PlotGroup;
import com.palmergames.bukkit.towny.object.Position;
import com.palmergames.bukkit.towny.object.Resident;
Expand Down Expand Up @@ -91,6 +92,7 @@ public class TownyUniverse {
private final Map<String, String> replacementNamesMap = new ConcurrentHashMap<>();
private final Map<UUID, PlotGroup> plotGroupUUIDMap = new ConcurrentHashMap<>();
private final Map<UUID, District> districtUUIDMap = new ConcurrentHashMap<>();
private final Map<UUID, Outpost> outpostUUIDMap = new ConcurrentHashMap<>();

private final Map<WorldCoord, TownyMapData> wildernessMapDataMap = new ConcurrentHashMap<WorldCoord, TownyMapData>();
private final String rootFolder;
Expand Down Expand Up @@ -134,6 +136,8 @@ public void clearAllObjects() {
spawnPoints.clear();
jailUUIDMap.clear();
plotGroupUUIDMap.clear();
districtUUIDMap.clear();
outpostUUIDMap.clear();
wildernessMapDataMap.clear();
replacementNamesMap.clear();
}
Expand Down Expand Up @@ -958,6 +962,58 @@ public District getDistrict(UUID districtID) {
return districtUUIDMap.get(districtID);
}


/*
* Outpost Stuff.
*/

/**
* Used in loading only.
*
* @param uuid UUID to assign to the Outpost.
*/
public void newOutpostInternal(UUID uuid) {
Outpost outpost = new Outpost(uuid, null);
registerOutpost(outpost);
}

public void registerOutpost(Outpost outpost) {
outpostUUIDMap.put(outpost.getUUID(), outpost);
}

public void unregisterOutpost(UUID uuid) {
Outpost outpost = outpostUUIDMap.get(uuid);
if (outpost == null)
return;
outpost.getTown().removeOutpost(outpost);
outpostUUIDMap.remove(uuid);
}

/**
* Get all the outposts from all towns
* Returns a collection that does not reflect any outpost additions/removals
*
* @return collection of Outpost
*/
public Collection<Outpost> getOutposts() {
return new ArrayList<>(outpostUUIDMap.values());
}

public Set<UUID> getOutpostUUIDs() {
return outpostUUIDMap.keySet();
}

/**
* Gets the outpost from the town name and the outpost UUID
*
* @param outpostID UUID of the outpost
* @return Outpost if found, null if none found.
*/
@Nullable
public Outpost getOutpost(UUID outpostID) {
return outpostUUIDMap.get(outpostID);
}

/*
* Metadata Stuff
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ protected MenuBuilder load() {
protected MenuBuilder load() {
return new MenuBuilder("town claim")
.add("", Translatable.of("msg_block_claim"))
.add("outpost", Translatable.of("mayor_help_3"))
.add("outpost [name]", Translatable.of("mayor_help_3"))
.add("[auto]", Translatable.of("mayor_help_5"))
.add("[circle/rect] [radius]", Translatable.of("mayor_help_4"))
.add("[circle/rect] auto", Translatable.of("mayor_help_5"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
import com.palmergames.bukkit.towny.huds.HUDManager;
import com.palmergames.bukkit.towny.object.Coord;
import com.palmergames.bukkit.towny.object.District;
import com.palmergames.bukkit.towny.object.Outpost;
import com.palmergames.bukkit.towny.object.PermissionData;
import com.palmergames.bukkit.towny.object.PlotGroup;
import com.palmergames.bukkit.towny.object.Position;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.SpawnPointLocation;
import com.palmergames.bukkit.towny.object.Town;
Expand Down Expand Up @@ -735,14 +737,19 @@ public void parsePlotSetOutpost(Player player, Resident resident, TownBlock town
if (!townBlock.isOutpost())
throw new TownyException(Translatable.of("msg_err_location_is_not_within_an_outpost_plot"));

town.addOutpostSpawn(player.getLocation());
Outpost outpost = townBlock.getOutpost();
outpost.setSpawn(Position.ofLocation(player.getLocation()));
outpost.save();
TownyMessaging.sendMsg(player, Translatable.of("msg_set_outpost_spawn"));
return;
}

TownyWorld townyWorld = townBlock.getWorld();
Coord key = Coord.parseCoord(player.getLocation());

if (townBlock.hasOutpostObject())
throw new TownyException("msg_err_plot_already_part_of_outpost_group");

// Throws a TownyException with message if outpost should not be set.
OutpostUtil.OutpostTests(town, resident, townyWorld, key, resident.isAdmin(), true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
import com.palmergames.bukkit.towny.object.comparators.ComparatorType;
import com.palmergames.bukkit.towny.object.economy.Account;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Outpost;
import com.palmergames.bukkit.towny.object.OutpostWorldCoord;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.SpawnType;
import com.palmergames.bukkit.towny.object.Town;
Expand Down Expand Up @@ -3384,6 +3386,10 @@ private static List<WorldCoord> getTownClaimSelectionOrThrow(Player player, Stri
if (split.length == 1 && split[0].equalsIgnoreCase("outpost")) {
if (!TownySettings.isAllowingOutposts())
throw new TownyException(Translatable.of("msg_outpost_disable"));

String name = StringMgmt.join(StringMgmt.remFirstArg(split), " ");
if (name.isEmpty())
throw new TownyException("You must specify a name for this outpost.");

checkPermOrThrow(player, PermissionNodes.TOWNY_COMMAND_TOWN_CLAIM_OUTPOST.getNode());

Expand All @@ -3396,7 +3402,7 @@ private static List<WorldCoord> getTownClaimSelectionOrThrow(Player player, Stri

// Select a single WorldCoord using the AreaSelectionUtil.
selection = new ArrayList<>();
selection.add(playerWorldCoord);
selection.add(new OutpostWorldCoord(name, playerWorldCoord));

} else if (split.length == 1 && "fill".equalsIgnoreCase(split[0])) {
checkPermOrThrow(player, PermissionNodes.TOWNY_COMMAND_TOWN_CLAIM_FILL.getNode());
Expand Down Expand Up @@ -3476,6 +3482,17 @@ private static void vetTownAllowedTheseClaims(Town town, boolean outpost, List<W
// attached to a claimed plot.
if (!outpost && !isEdgeBlock(town, selection) && !town.getTownBlocks().isEmpty())
throw new TownyException(Translatable.of("msg_err_not_attached_edge"));

// Prevent an outpost's landmass growing too large.
Optional<Outpost> outpostOptional = selection.get(0).getCardinallyAdjacentWorldCoords(false).stream()
.filter(wc -> wc.hasTownBlock() && wc.getTownBlockOrNull().hasOutpostObject())
.map(wc -> wc.getTownBlockOrNull().getOutpost())
.findFirst();
if (outpostOptional.isPresent()) {
Outpost outpostObject = outpostOptional.get();
if (outpostObject.getNumTownBlocks() >= town.getMaxAllowedOutpostLandmass())
throw new TownyException(String.format("Your town is unable to make an outpost larger than %s.", town.getMaxAllowedOutpostLandmass()));
}
}

private static void fireTownPreClaimEventOrThrow(Player player, Town town, boolean outpost, List<WorldCoord> selection) throws TownyException {
Expand Down
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
18 changes: 15 additions & 3 deletions Towny/src/main/java/com/palmergames/bukkit/towny/db/SQLSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public static void initTables(Connection cntx) {
initTable(cntx, TownyDBTableType.DISTRICT);
updateTable(cntx, TownyDBTableType.DISTRICT, getDistrictColumns());

initTable(cntx, TownyDBTableType.OUTPOST);
updateTable(cntx, TownyDBTableType.OUTPOST, getOutpostColumns());

initTable(cntx, TownyDBTableType.JAIL);
updateTable(cntx, TownyDBTableType.JAIL, getJailsColumns());

Expand Down Expand Up @@ -81,7 +84,7 @@ private static String fetchTableSchema(TownyDBTableType tableType) {
case TOWNBLOCK -> fetchCreateTownBlocksStatement();
case JAIL -> fetchCreateUUIDStatement(tableType);
case PLOTGROUP -> fetchCreatePlotGroupStatement(tableType);
case DISTRICT -> fetchCreateUUIDStatement(tableType);
case DISTRICT, OUTPOST -> fetchCreateUUIDStatement(tableType);
case COOLDOWN -> fetchCreateCooldownsStatement(tableType);
case WORLD -> fetchCreateWorldStatemnt(tableType);
case HIBERNATED_RESIDENT -> fetchCreateUUIDStatement(tableType);
Expand Down Expand Up @@ -172,6 +175,14 @@ private static List<String> getDistrictColumns() {
return columns;
}

private static List<String> getOutpostColumns() {
List<String> columns = new ArrayList<>();
columns.add("`outpostName` mediumtext NOT NULL");
columns.add("`spawn` mediumtext NOT NULL");
columns.add("`metadata` text DEFAULT NULL");
return columns;
}

private static List<String> getResidentColumns(){
List<String> columns = new ArrayList<>();
columns.add("`town` mediumtext");
Expand Down Expand Up @@ -229,7 +240,6 @@ private static List<String> getTownColumns() {
columns.add("`allowedToWar` bool NOT NULL DEFAULT '1'");
columns.add("`homeblock` mediumtext NOT NULL");
columns.add("`spawn` mediumtext NOT NULL");
columns.add("`outpostSpawns` mediumtext DEFAULT NULL");
columns.add("`jailSpawns` mediumtext DEFAULT NULL");
columns.add("`outlaws` mediumtext DEFAULT NULL");
columns.add("`uuid` VARCHAR(36) DEFAULT NULL");
Expand Down Expand Up @@ -338,13 +348,13 @@ private static List<String> getTownBlockColumns() {
columns.add("`resident` mediumtext");
columns.add("`type` TINYINT NOT NULL DEFAULT '0'");
columns.add("`typeName` mediumtext");
columns.add("`outpost` bool NOT NULL DEFAULT '0'");
columns.add("`permissions` mediumtext NOT NULL");
columns.add("`locked` bool NOT NULL DEFAULT '0'");
columns.add("`changed` bool NOT NULL DEFAULT '0'");
columns.add("`metadata` text DEFAULT NULL");
columns.add("`groupID` VARCHAR(36) DEFAULT NULL");
columns.add("`districtID` VARCHAR(36) DEFAULT NULL");
columns.add("`outpostID` VARCHAR(36) DEFAULT NULL");
columns.add("`claimedAt` BIGINT NOT NULL");
columns.add("`trustedResidents` mediumtext DEFAULT NULL");
columns.add("`customPermissionData` mediumtext DEFAULT NULL");
Expand Down Expand Up @@ -381,6 +391,8 @@ public static void cleanup(Connection connection) {
cleanups.add(ColumnUpdate.update("TOWNS", "jailSpawns"));
cleanups.add(ColumnUpdate.update("WORLDS", "disableplayertrample"));
cleanups.add(ColumnUpdate.update("TOWNS", "assistants"));
cleanups.add(ColumnUpdate.update("TOWNBLOCKS", "outpost"));
cleanups.add(ColumnUpdate.update("TOWNS", "outpostSpawns"));

for (ColumnUpdate update : cleanups)
dropColumn(connection, update.table(), update.column());
Expand Down
Loading

0 comments on commit 91c31a8

Please sign in to comment.