Skip to content

Commit

Permalink
Lots of testing completed, legacy conversion working well.
Browse files Browse the repository at this point in the history
/plot set outpost spawn & /plot set outpost taken care of.

TODO: Change Plot Perm Hud to reduce lines to 15.
  • Loading branch information
LlmDl committed Nov 4, 2024
1 parent 91d33ac commit bd4bba6
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 84 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 @@ -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 @@ -3485,12 +3485,12 @@ private static void vetTownAllowedTheseClaims(Town town, boolean outpost, List<W

// Prevent an outpost's landmass growing too large.
Optional<Outpost> outpostOptional = selection.get(0).getCardinallyAdjacentWorldCoords(false).stream()
.filter(wc -> wc.hasTownBlock() && wc.getTownBlockOrNull().hasOutpost())
.filter(wc -> wc.hasTownBlock() && wc.getTownBlockOrNull().hasOutpostObject())
.map(wc -> wc.getTownBlockOrNull().getOutpost())
.findFirst();
if (outpostOptional.isPresent()) {
Outpost outpostObject = outpostOptional.get();
if (outpostObject.getNumTownBlock() > town.getMaxAllowedOutpostLandmass())
if (outpostObject.getNumTownBlocks() > town.getMaxAllowedOutpostLandmass())
throw new TownyException("Your town is unable to make an outpost this large.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public abstract class TownyDataSource {

public boolean loadAll() {

return loadWorldList() && loadNationList() && loadTownList() && loadPlotGroupList() && loadDistrictList() && loadOutpostList() && loadJailList() && loadResidentList() && loadTownBlockList() && loadWorlds() && loadResidents() && loadTowns() && loadNations() && loadTownBlocks() && loadPlotGroups() && loadDistricts() && loadOutposts() && loadJails() && loadRegenList() && loadCooldowns();
return loadWorldList() && loadNationList() && loadTownList() && loadPlotGroupList() && loadDistrictList() && loadOutpostList() && loadJailList() && loadResidentList() && loadTownBlockList() && loadWorlds() && loadResidents() && loadTowns() && loadNations() && loadOutposts() && loadTownBlocks() && loadPlotGroups() && loadDistricts() && loadJails() && loadRegenList() && loadCooldowns();
}

public boolean saveAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public TownyFlatFileSource(Towny plugin, TownyUniverse universe) {
dataFolderPath + File.separator + "plotgroups" + File.separator + "deleted",
dataFolderPath + File.separator + "districts",
dataFolderPath + File.separator + "districts" + File.separator + "deleted",
dataFolderPath + File.separator + "outposts",
dataFolderPath + File.separator + "outposts" + File.separator + "deleted",
dataFolderPath + File.separator + "jails",
dataFolderPath + File.separator + "jails" + File.separator + "deleted"
)) {
Expand Down Expand Up @@ -1755,7 +1757,7 @@ public boolean loadOutpost(Outpost outpost) {

line = keys.get("spawn");
if (line != null) {
String[] tokens = line.split(",");
String[] tokens = line.split("#");
if (tokens.length >= 4)
try {
outpost.setSpawn(Position.deserialize(tokens));
Expand Down Expand Up @@ -1941,10 +1943,12 @@ else if (universe.getReplacementNameMap().containsKey(line.trim())) {
}

if (outpostID != null) {
Outpost outpost= universe.getOutpost(outpostID);
Outpost outpost = universe.getOutpost(outpostID);
if (outpost != null) {
outpost.addTownblock(townBlock);
townBlock.setOutpostObject(outpost);
if (outpost.getNumTownBlocks() <= 1)
townBlock.getTownOrNull().addOutpost(outpost);
} else {
townBlock.removeOutpost();
}
Expand Down Expand Up @@ -2304,7 +2308,8 @@ public boolean saveOutpost(Outpost outpost) {

try {
list.add("outpostName=" + outpost.getName());
list.add("spawn=" + String.join(",", outpost.getSpawn().serialize()));
if (outpost.getSpawn() != null)
list.add("spawn=" + String.join("#", outpost.getSpawn().serialize()));
list.add("metadata=" + serializeMetadata(outpost));
} catch (Exception e) {
plugin.getLogger().log(Level.WARNING, "An exception occurred while saving outpost " + Optional.ofNullable(outpost).map(g -> g.getUUID().toString()).orElse("null") + ": ", e);
Expand Down Expand Up @@ -2609,7 +2614,7 @@ public boolean saveTownBlock(TownBlock townBlock) {
list.add("districtID=" + districtID);

// Outpost ID
if (townBlock.hasOutpost())
if (townBlock.hasOutpostObject())
list.add("outpostID=" + townBlock.getOutpostUUID());

list.add("trustedResidents=" + StringMgmt.join(toUUIDList(townBlock.getTrustedResidents()), ","));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,8 @@ public boolean loadTownBlocks() {
if (outpost != null) {
outpost.addTownblock(townBlock);
townBlock.setOutpostObject(outpost);
if (outpost.getNumTownBlocks() <= 1)
townBlock.getTownOrNull().addOutpost(outpost);
}
} catch (Exception ignored) {
}
Expand Down Expand Up @@ -2820,7 +2822,7 @@ public synchronized boolean saveTownBlock(TownBlock townBlock) {
tb_hm.put("districtID", townBlock.getDistrict().getUUID().toString());
else
tb_hm.put("districtID", "");
if (townBlock.hasOutpost())
if (townBlock.hasOutpostObject())
tb_hm.put("outpostID", townBlock.getOutpostUUID().toString());
else
tb_hm.put("outpostID", "");
Expand Down
53 changes: 32 additions & 21 deletions Towny/src/main/java/com/palmergames/bukkit/towny/huds/PermHUD.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class PermHUD {
private static final String HUD_OBJECTIVE = "PERM_HUD_OBJ";
private static final String TEAM_PERMS_TITLE = "permsTitle";
private static final String TEAM_DISTRICT_NAME = "districtName";
private static final String TEAM_OUTPOST_NAME = "outpostName";
private static final String TEAM_PLOT_NAME = "plot_name";
private static final String TEAM_PLOT_COST = "plot_cost";
private static final String TEAM_BUILD = "build";
Expand All @@ -65,7 +66,7 @@ public static String permHudTestKey() {

public static void updatePerms(Player p, WorldCoord worldCoord) {
Translator translator = Translator.locale(p);
String districtName, plotName, build, destroy, switching, item, type, pvp, explosions, firespread, mobspawn, title;
String districtName, outpostName, plotName, build, destroy, switching, item, type, pvp, explosions, firespread, mobspawn, title;
Scoreboard board = p.getScoreboard();
// Due to tick delay (probably not confirmed), a HUD can actually be removed from the player.
// Causing board to return null, and since we don't create a new board, a NullPointerException occurs.
Expand Down Expand Up @@ -94,8 +95,11 @@ public static void updatePerms(Player p, WorldCoord worldCoord) {
title = GOLD + owner.getName() + (townBlock.hasResident() ? " (" + townBlock.getTownOrNull().getName() + ")" : "");

// District name
districtName = townBlock.hasDistrict() ? townBlock.getDistrict().getFormattedName() : "";

districtName = townBlock.hasDistrict() ? DARK_GREEN + "District: " + townBlock.getDistrict().getFormattedName() : "";

// Outpost name
outpostName = townBlock.hasOutpostObject() ? DARK_GREEN + "Outpost: " + townBlock.getOutpost().getFormattedName() : "";

// Plot Type
type = townBlock.getType().equals(TownBlockType.RESIDENTIAL) ? " " : townBlock.getType().getName();

Expand Down Expand Up @@ -125,6 +129,7 @@ public static void updatePerms(Player p, WorldCoord worldCoord) {
// Set the values to our Scoreboard's teams.
board.getObjective(HUD_OBJECTIVE).setDisplayName(HUDManager.check(title));
board.getTeam(TEAM_DISTRICT_NAME).setSuffix(districtName);
board.getTeam(TEAM_OUTPOST_NAME).setSuffix(outpostName);
board.getTeam(TEAM_PLOT_NAME).setSuffix(plotName);
board.getTeam(TEAM_PLOT_TYPE).setSuffix(type);
board.getTeam(TEAM_PLOT_COST).setSuffix(forSale);
Expand Down Expand Up @@ -170,6 +175,7 @@ private static void clearPerms (Player p) {
try {
board.getObjective(HUD_OBJECTIVE).setDisplayName(HUDManager.check(getFormattedWildernessName(p.getWorld())));
board.getTeam(TEAM_DISTRICT_NAME).setSuffix(" ");
board.getTeam(TEAM_OUTPOST_NAME).setSuffix(" ");
board.getTeam(TEAM_PLOT_NAME).setSuffix(" ");
board.getTeam(TEAM_PLOT_TYPE).setSuffix(" ");
board.getTeam(TEAM_PLOT_COST).setSuffix(" ");
Expand Down Expand Up @@ -212,7 +218,8 @@ public static void toggleOn (Player p) {

private static void initializeScoreboard(Translator translator, Scoreboard board) {
String PERM_HUD_TITLE = GOLD + "";
String districtName_entry = "";
String districtName_entry = GOLD + "";
String outpostName_entry = GOLD + "";
String plotName_entry = "";
String keyPlotType_entry = DARK_GREEN + translator.of("msg_perm_hud_plot_type");
String forSale_entry = DARK_GREEN + translator.of("msg_perm_hud_plot_for_sale") + GRAY;
Expand Down Expand Up @@ -241,6 +248,7 @@ private static void initializeScoreboard(Translator translator, Scoreboard board
obj.setDisplayName(PERM_HUD_TITLE);
//register teams
Team districtName = board.registerNewTeam(TEAM_DISTRICT_NAME);
Team outpostName = board.registerNewTeam(TEAM_OUTPOST_NAME);
Team plotName = board.registerNewTeam(TEAM_PLOT_NAME);
Team keyPlotType = board.registerNewTeam(TEAM_PLOT_TYPE);
Team forSaleTitle = board.registerNewTeam(TEAM_PLOT_COST);
Expand All @@ -263,6 +271,7 @@ private static void initializeScoreboard(Translator translator, Scoreboard board

//add each team as an entry (this sets the prefix to each line of the HUD.)
districtName.addEntry(districtName_entry);
outpostName.addEntry(outpostName_entry);
plotName.addEntry(plotName_entry);
keyPlotType.addEntry(keyPlotType_entry);
forSaleTitle.addEntry(forSale_entry);
Expand All @@ -283,24 +292,26 @@ private static void initializeScoreboard(Translator translator, Scoreboard board
keyFriend.addEntry(keyNation_entry);
keyAlly.addEntry(keyAlly_entry);

int i = 18;
//set scores for positioning
obj.getScore(districtName_entry).setScore(17);
obj.getScore(plotName_entry).setScore(16);
obj.getScore(keyPlotType_entry).setScore(15);
obj.getScore(forSale_entry).setScore(14);
obj.getScore(permsTitle_entry).setScore(13);
obj.getScore(build_entry).setScore(12);
obj.getScore(destroy_entry).setScore(11);
obj.getScore(switching_entry).setScore(10);
obj.getScore(item_entry).setScore(9);
obj.getScore(pvp_entry).setScore(8);
obj.getScore(explosions_entry).setScore(7);
obj.getScore(firespread_entry).setScore(6);
obj.getScore(mobspawn_entry).setScore(5);
obj.getScore(keyTitle_entry).setScore(4);
obj.getScore(keyResident_entry).setScore(3);
obj.getScore(keyNation_entry).setScore(2);
obj.getScore(keyAlly_entry).setScore(1);
obj.getScore(districtName_entry).setScore(i--);
obj.getScore(outpostName_entry).setScore(i--);
obj.getScore(plotName_entry).setScore(i--);
obj.getScore(keyPlotType_entry).setScore(i--);
obj.getScore(forSale_entry).setScore(i--);
obj.getScore(permsTitle_entry).setScore(i--);
obj.getScore(build_entry).setScore(i--);
obj.getScore(destroy_entry).setScore(i--);
obj.getScore(switching_entry).setScore(i--);
obj.getScore(item_entry).setScore(i--);
obj.getScore(pvp_entry).setScore(i--);
obj.getScore(explosions_entry).setScore(i--);
obj.getScore(firespread_entry).setScore(i--);
obj.getScore(mobspawn_entry).setScore(i--);
obj.getScore(keyTitle_entry).setScore(i--);
obj.getScore(keyResident_entry).setScore(i--);
obj.getScore(keyNation_entry).setScore(i--);
obj.getScore(keyAlly_entry).setScore(i--);
}

private static String prettyMoney(double price) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ public void onTownClaim(TownClaimEvent event) {
CellSurface.getCellSurface(event.getTownBlock().getWorldCoord()).runClaimingParticleOverSurfaceAtPlayer(event.getResident().getPlayer()));

// Add the outpost object to the newly claimed townblock, if it is part of an outpost's landmass.
if (!event.getTownBlock().hasOutpost()) {
if (!event.getTownBlock().hasOutpostObject()) {
Optional<Outpost> outpost = event.getTownBlock().getWorldCoord().getCardinallyAdjacentWorldCoords(false).stream()
.filter(wc -> wc.hasTownBlock() && wc.getTownBlockOrNull().hasOutpost())
.filter(wc -> wc.hasTownBlock() && wc.getTownBlockOrNull().hasOutpostObject())
.map(wc -> wc.getTownBlockOrNull().getOutpost())
.findFirst();
if (outpost.isPresent())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.palmergames.bukkit.towny.object;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import org.jetbrains.annotations.Nullable;

import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.towny.object.SpawnPoint.SpawnPointType;

public class Outpost extends ObjectGroup {
private Town town;
private Position spawn;
private Set<TownBlock> townblocks;
private Set<TownBlock> townblocks = new HashSet<>();

public Outpost(UUID id, String name) {
super(id, name);
Expand Down Expand Up @@ -40,7 +43,7 @@ public Position getSpawn() {
public void setSpawn(Position spawn) {
// Remove any previously set spawn's particles.
if (this.spawn != null)
TownyUniverse.getInstance().removeSpawnPoint(spawn.asLocation());
TownyUniverse.getInstance().removeSpawnPoint(this.spawn.asLocation());

this.spawn = spawn;
TownyUniverse.getInstance().addSpawnPoint(new SpawnPoint(spawn, SpawnPointType.OUTPOST_SPAWN));
Expand All @@ -50,7 +53,7 @@ public boolean isOutpostHomeBlock(TownBlock tb) {
return tb.getWorldCoord().equals(WorldCoord.parseWorldCoord(spawn.asLocation()));
}

public int getNumTownBlock() {
public int getNumTownBlocks() {
return townblocks.size();
}

Expand All @@ -65,10 +68,20 @@ public void addTownblock(TownBlock townblock) {
}

public void removeTownblock(TownBlock townblock) {
if (isOutpostHomeBlock(townblock))
TownyUniverse.getInstance().removeSpawnPoint(spawn.asLocation());

this.townblocks.remove(townblock);
}

public boolean hasTownBlocks() {
return townblocks.size() > 0;
}

@Nullable
public TownBlock getSpawnTownBlock() {
if (spawn == null)
return null;
return WorldCoord.parseWorldCoord(spawn.asLocation()).getTownBlockOrNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,9 @@ public static Position deserialize(@NotNull final String[] data) throws IllegalA

return new Position(world, x, y, z, pitch, yaw);
}

@Override
public String toString() {
return String.format("%s - %s, %s, %s", this.world.getName(), x, y, z);
}
}
Loading

0 comments on commit bd4bba6

Please sign in to comment.