Skip to content

Commit

Permalink
Branch will remove TownyWorlds saving a list of towns. (#3799)
Browse files Browse the repository at this point in the history
Branch will remove TownyWorlds saving a list of towns.

Towns have changed from town.getWorld() to town.getHomeblockWorld(), with getWorld() becoming deprecated and returning getHomeblockWorld().

Some instances of town.getWorld() are now correctly using townblock.getWorld() or something similar when it is called for.

Stop getHomeblockWorld() from returning null when towns have claimed 0 townblocks.

Towny.setWorldFlags() from startup sequence is gone.

SQL now drops old towns columns and saves/loads worlds without towns like flatfile.

Swap out the List version of towns in TownyWorld for a HashMap, for faster loading of TownBlocks

Remove unneeded loop.

Fix up for loops.

Fix pvp disabled worlds still be affected with recent change to CombatUtil#preventPVP.
  • Loading branch information
LlmDl committed Mar 25, 2020
1 parent 8a87147 commit 29947ad
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 120 deletions.
30 changes: 0 additions & 30 deletions src/com/palmergames/bukkit/towny/Towny.java
Expand Up @@ -33,8 +33,6 @@
import com.palmergames.bukkit.towny.object.Coord;
import com.palmergames.bukkit.towny.object.PlayerCache;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyWorld;
import com.palmergames.bukkit.towny.object.WorldCoord;
import com.palmergames.bukkit.towny.permissions.BukkitPermSource;
import com.palmergames.bukkit.towny.permissions.GroupManagerSource;
Expand Down Expand Up @@ -193,32 +191,6 @@ public void onEnable() {
}
}

public void setWorldFlags() {
TownyUniverse universe = TownyUniverse.getInstance();
for (Town town : universe.getDataSource().getTowns()) {

if (town.getWorld() == null) {
LOGGER.warn("[Towny Error] Detected an error with the world files. Attempting to repair");
if (town.hasHomeBlock())
try {
TownyWorld world = town.getHomeBlock().getWorld();
if (!world.hasTown(town)) {
world.addTown(town);
universe.getDataSource().saveTown(town);
universe.getDataSource().saveWorld(world);
}
} catch (TownyException e) {
// Error fetching homeblock
LOGGER.warn("[Towny Error] Failed get world data for: " + town.getName());
}
else {
LOGGER.warn("[Towny Error] No Homeblock - Failed to detect world for: " + town.getName());
}
}
}

}

@Override
public void onDisable() {

Expand Down Expand Up @@ -271,8 +243,6 @@ public boolean load() {

checkPlugins();

setWorldFlags();

// make sure the timers are stopped for a reset
TownyTimerHandler.toggleTownyRepeatingTimer(false);
TownyTimerHandler.toggleDailyTimer(false);
Expand Down
4 changes: 2 additions & 2 deletions src/com/palmergames/bukkit/towny/TownyFormatter.java
Expand Up @@ -316,7 +316,7 @@ public static List<String> getStatus(Town town) {

TownyWorld world;
try {
world = town.getWorld();
world = town.getHomeblockWorld();
} catch (NullPointerException e) {
// Some towns can have no homeblock, causing getWorld() to return null.
// We're going to supplant the first TownyWorld so that the forceexpl/forcefire/forcepvp tests below do not have trouble.
Expand All @@ -327,7 +327,7 @@ public static List<String> getStatus(Town town) {

// ___[ Raccoon City (PvP) (Open) ]___
String title = town.getFormattedName();
title += ((!town.isAdminDisabledPVP()) && ((town.isPVP() || town.getWorld().isForcePVP())) ? TownySettings.getLangString("status_title_pvp") : "");
title += ((!town.isAdminDisabledPVP()) && ((town.isPVP() || town.getHomeblockWorld().isForcePVP())) ? TownySettings.getLangString("status_title_pvp") : "");
title += (town.isOpen() ? TownySettings.getLangString("status_title_open") : "");
out.add(ChatTools.formatTitle(title));

Expand Down
10 changes: 5 additions & 5 deletions src/com/palmergames/bukkit/towny/command/PlotCommand.java
Expand Up @@ -1223,26 +1223,26 @@ public void plotGroupToggle(Player player, PlotGroup plotGroup, String[] split)
private void toggleTest(Player player, TownBlock townBlock, String split) throws TownyException {

// Make sure we are allowed to set these permissions.
Town town = townBlock.getTown();

split = split.toLowerCase();

if (split.contains("mobs")) {
if (town.getWorld().isForceTownMobs())
if (townBlock.getWorld().isForceTownMobs())
throw new TownyException(TownySettings.getLangString("msg_world_mobs"));
}

if (split.contains("fire")) {
if (town.getWorld().isForceFire())
if (townBlock.getWorld().isForceFire())
throw new TownyException(TownySettings.getLangString("msg_world_fire"));
}

if (split.contains("explosion")) {
if (town.getWorld().isForceExpl())
if (townBlock.getWorld().isForceExpl())
throw new TownyException(TownySettings.getLangString("msg_world_expl"));
}

if (split.contains("pvp")) {
if (town.getWorld().isForcePVP())
if (townBlock.getWorld().isForcePVP())
throw new TownyException(TownySettings.getLangString("msg_world_pvp"));
}
if ((split.contains("pvp")) || (split.trim().equalsIgnoreCase("off"))) {
Expand Down
12 changes: 6 additions & 6 deletions src/com/palmergames/bukkit/towny/command/TownCommand.java
Expand Up @@ -1617,22 +1617,22 @@ private static void toggleTest(Player player, Town town, String split) throws To
split = split.toLowerCase();

if (split.contains("mobs")) {
if (town.getWorld().isForceTownMobs())
if (town.getHomeblockWorld().isForceTownMobs())
throw new TownyException(TownySettings.getLangString("msg_world_mobs"));
}

if (split.contains("fire")) {
if (town.getWorld().isForceFire())
if (town.getHomeblockWorld().isForceFire())
throw new TownyException(TownySettings.getLangString("msg_world_fire"));
}

if (split.contains("explosion")) {
if (town.getWorld().isForceExpl())
if (town.getHomeblockWorld().isForceExpl())
throw new TownyException(TownySettings.getLangString("msg_world_expl"));
}

if (split.contains("pvp")) {
if (town.getWorld().isForcePVP())
if (town.getHomeblockWorld().isForcePVP())
throw new TownyException(TownySettings.getLangString("msg_world_pvp"));
}
}
Expand Down Expand Up @@ -2155,7 +2155,7 @@ else if (split[1].equalsIgnoreCase("clear")) {
throw new TownyException(TownySettings.getLangString("msg_too_far"));

townBlock = townyUniverse.getDataSource().getWorld(player.getWorld().getName()).getTownBlock(coord);
oldWorld = town.getWorld();
oldWorld = town.getHomeblockWorld();
town.setHomeBlock(townBlock);
town.setSpawn(player.getLocation());

Expand Down Expand Up @@ -2233,7 +2233,7 @@ else if (split[1].equalsIgnoreCase("clear")) {
// If the town (homeblock) has moved worlds we need to update the
// world files.
if (oldWorld != null) {
townyUniverse.getDataSource().saveWorld(town.getWorld());
townyUniverse.getDataSource().saveWorld(town.getHomeblockWorld());
townyUniverse.getDataSource().saveWorld(oldWorld);
}
}
Expand Down
Expand Up @@ -780,7 +780,7 @@ public void parseAdminResidentCommand(String[] split) throws TownyException {
final String town = resident.getJailTown();
final int index = resident.getJailSpawn();
try {
final Location loc = Bukkit.getWorld(townyUniverse.getDataSource().getTownWorld(town).getName()).getSpawnLocation();
final Location loc = Bukkit.getWorld(TownyAPI.getInstance().getDataSource().getTown(town).getHomeblockWorld().getName()).getSpawnLocation();

// Use teleport warmup
jailedPlayer.sendMessage(String.format(TownySettings.getLangString("msg_town_spawn_warmup"), TownySettings.getTeleportWarmupTime()));
Expand Down
22 changes: 19 additions & 3 deletions src/com/palmergames/bukkit/towny/db/SQL_Schema.java
Expand Up @@ -27,7 +27,6 @@ private static String getWORLDS() {

private static List<String> getWorldColumns() {
List<String> columns = new ArrayList<>();
columns.add("`towns` mediumtext NOT NULL");
columns.add("`claimable` bool NOT NULL DEFAULT '0'");
columns.add("`pvp` bool NOT NULL DEFAULT '0'");
columns.add("`forcepvp` bool NOT NULL DEFAULT '0'");
Expand Down Expand Up @@ -439,9 +438,7 @@ public static void initTables(Connection cntx, String db_name) {
*
* @param cntx - Connection.
* @param db_name - Name of database.
* @deprecated - This method no longer does anything do to being empty.
*/
@Deprecated
public static void cleanup(Connection cntx, String db_name) {

/*
Expand All @@ -465,5 +462,24 @@ public static void cleanup(Connection cntx, String db_name) {
// TownyMessaging.sendErrorMsg("Error updating table RESIDENTS :" + ee.getMessage());
//
// }

/*
* Update WORLDS
*/
String world_update;

try {
world_update = "ALTER TABLE `" + db_name + "`.`" + tb_prefix + "WORLDS` " + "DROP COLUMN `towns`";

Statement s = cntx.createStatement();
s.executeUpdate(world_update);

TownyMessaging.sendDebugMsg("Table WORLDS is updated!");

} catch (SQLException ee) {
if (ee.getErrorCode() != 1060)
TownyMessaging.sendErrorMsg("Error updating table WORLDS :" + ee.getMessage());

}
}
}
9 changes: 5 additions & 4 deletions src/com/palmergames/bukkit/towny/db/TownyDatabaseHandler.java
Expand Up @@ -283,7 +283,8 @@ public TownyWorld getTownWorld(String townName) {
return world;
}

return null;
// If this has failed the Town has no land claimed at all but should be given a world regardless.
return universe.getDataSource().getWorlds().get(0);
}

@Override
Expand Down Expand Up @@ -587,7 +588,7 @@ public void removeTown(Town town) {
//removeTownBlocks(town);

List<Resident> toSave = new ArrayList<>(town.getResidents());
TownyWorld townyWorld = town.getWorld();
TownyWorld townyWorld = town.getHomeblockWorld();

try {
if (town.hasNation()) {
Expand Down Expand Up @@ -810,7 +811,7 @@ public void renameTown(Town town, String newName) throws AlreadyRegisteredExcept
isCapital = town.isCapital();
}

TownyWorld world = town.getWorld();
TownyWorld world = town.getHomeblockWorld();
world.removeTown(town);
/*
* Tidy up old files.
Expand Down Expand Up @@ -875,7 +876,7 @@ public void renameTown(Town town, String newName) throws AlreadyRegisteredExcept
saveTown(town);
saveTownList();
savePlotGroupList();
saveWorld(town.getWorld());
saveWorld(town.getHomeblockWorld());

if (nation != null) {
saveNation(nation);
Expand Down
24 changes: 3 additions & 21 deletions src/com/palmergames/bukkit/towny/db/TownyFlatFileSource.java
Expand Up @@ -1162,7 +1162,6 @@ public boolean loadNation(Nation nation) {
public boolean loadWorld(TownyWorld world) {

String line = "";
String[] tokens;
String path = getWorldFilename(world);

// create the world file if it doesn't exist
Expand All @@ -1176,21 +1175,6 @@ public boolean loadWorld(TownyWorld world) {
try {
HashMap<String, String> keys = loadFileIntoHashMap(fileWorld);

line = keys.get("towns");
if (line != null) {
tokens = line.split(",");
for (String token : tokens) {
if (!token.isEmpty()) {
TownyMessaging.sendDebugMsg("World Fetching Town: " + token);
Town town = getTown(token);
if (town != null) {
town.setWorld(world);
//world.addTown(town); not needed as it's handled in the Town object
}
}
}
}

line = keys.get("claimable");
if (line != null)
try {
Expand Down Expand Up @@ -1521,6 +1505,9 @@ public boolean loadTownBlocks() {
try {
Town town = getTown(line.trim());
townBlock.setTown(town);
TownyWorld townyWorld = townBlock.getWorld();
if (townyWorld != null && !townyWorld.hasTown(town))
townyWorld.addTown(town);
} catch (Exception ignored) {
}

Expand Down Expand Up @@ -2021,11 +2008,6 @@ public boolean saveWorld(TownyWorld world) {

List<String> list = new ArrayList<>();

// Towns
list.add("towns=" + StringMgmt.join(world.getTowns(), ","));

list.add("");

// PvP
list.add("pvp=" + world.isPVP());
// Force PvP
Expand Down
21 changes: 3 additions & 18 deletions src/com/palmergames/bukkit/towny/db/TownySQLSource.java
Expand Up @@ -1164,7 +1164,6 @@ public boolean loadWorld(TownyWorld world) {
String line;
boolean result;
long resultLong;
String[] tokens;
TownyMessaging.sendDebugMsg("Loading world " + world.getName());
if (!getContext())
return false;
Expand All @@ -1174,20 +1173,6 @@ public boolean loadWorld(TownyWorld world) {
String search;

while (rs.next()) {
line = rs.getString("towns");
if (line != null) {
search = (line.contains("#")) ? "#" : ",";
tokens = line.split(search);
for (String token : tokens) {
if (!token.isEmpty()) {
Town town = getTown(token);
if (town != null) {
town.setWorld(world);
}
}
}
}

result = rs.getBoolean("claimable");
try {
world.setClaimable(result);
Expand Down Expand Up @@ -1474,6 +1459,9 @@ public boolean loadTownBlocks() {
try {
Town town = getTown(line.trim());
townBlock.setTown(town);
TownyWorld townyWorld = townBlock.getWorld();
if (townyWorld != null && !townyWorld.hasTown(town))
townyWorld.addTown(town);
} catch (Exception ignored) {
}

Expand Down Expand Up @@ -1734,8 +1722,6 @@ public synchronized boolean saveWorld(TownyWorld world) {

nat_hm.put("name", world.getName());

// Towns
nat_hm.put("towns", StringMgmt.join(world.getTowns(), "#"));
// PvP
nat_hm.put("pvp", world.isPVP());
// Force PvP
Expand Down Expand Up @@ -2224,7 +2210,6 @@ public boolean loadPlotGroups() {
return true;
}

@SuppressWarnings("deprecation")
@Override
public boolean cleanup() {

Expand Down
4 changes: 2 additions & 2 deletions src/com/palmergames/bukkit/towny/object/Nation.java
Expand Up @@ -718,7 +718,7 @@ public EconomyAccount getAccount() {
World world;

if (hasCapital() && getCapital().hasWorld()) {
world = BukkitTools.getWorld(getCapital().getWorld().getName());
world = BukkitTools.getWorld(getCapital().getHomeblockWorld().getName());
} else {
world = BukkitTools.getWorlds().get(0);
}
Expand Down Expand Up @@ -748,7 +748,7 @@ public boolean isAlliedWith(Nation nation) {
@Deprecated
public World getBukkitWorld() {
if (hasCapital() && getCapital().hasWorld()) {
return BukkitTools.getWorld(getCapital().getWorld().getName());
return BukkitTools.getWorld(getCapital().getHomeblockWorld().getName());
} else {
return BukkitTools.getWorlds().get(0);
}
Expand Down

0 comments on commit 29947ad

Please sign in to comment.