Skip to content

Commit

Permalink
TownBlocks_Update Commit #3
Browse files Browse the repository at this point in the history
Continuing on with removing TownBlockLists, ending TownBlocks being
tracked per-TownyWorld, adding TownBlocks being tracked per-Town.

- Add TownyAPI.isWilderness(WorldCoord worldCoord) method.
- Flesh out methods in TownyUniverse for maintaining the global
townblocks hashmap.
- Remove all traces of saveTownBlockList() and saveTownBlockList(Town
town).
- Remove extra townblock removal methods from TownyDatabaseHandler
(removeOneOfManyTownBlocks(), removeManyTownBlocks())
- Remove deprecated methods used to update ancient Towny databases that
still stored TownBlocks in the town and resident files.
  - (utilLoadTownBlocks(), utilLoadTownBlockTypeData(),
utilSaveTownBlocks()
- Replaced the Town's List<TownBlock> with ConcurrentHashMap<WorldCoord,
TownBlock>.
- Removed TownyWorld's ConcurrentHashMap<Coord, TownBlock> townBlocks,
with all methods forwarding to TownyUniverse or otherwise handling
things.
  • Loading branch information
LlmDl committed Mar 25, 2020
1 parent 9e20007 commit 963c6da
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 411 deletions.
16 changes: 16 additions & 0 deletions src/com/palmergames/bukkit/towny/TownyAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ public boolean isWilderness(Location location) {
}
}

/**
* Check if the specified {@link Location} is in the wilderness.
*
* @param location {@link Location} to test widlerness for.
* @return true if the {@link Location} is in the wilderness, false otherwise.
*/
public boolean isWilderness(WorldCoord worldCoord) {

try {
return worldCoord.getTownBlock().getTown() == null;
} catch (NotRegisteredException e) {
// Must be wilderness
return true;
}
}

/**
* Returns value of usingTowny for the given world.
*
Expand Down
102 changes: 94 additions & 8 deletions src/com/palmergames/bukkit/towny/TownyUniverse.java
Original file line number Diff line number Diff line change
Expand Up @@ -498,29 +498,115 @@ public void removeGroup(PlotGroup group) {
public HashMap<String, CustomDataField> getRegisteredMetadata() {
return registeredMetadata;
}

/**
* How to get a TownBlock for now.
*
* @param worldCoord we are testing for a townblock.
* @return townblock if it exists, otherwise null.
* @throws NotRegisteredException
*/
public TownBlock getTownBlock(WorldCoord worldCoord) throws NotRegisteredException {
if (hasTownBlock(worldCoord))
return townBlocks.get(worldCoord);
else
throw new NotRegisteredException();
}

/**
* How to get all of the townblocks in a server for now.
*
* Only used twice:
* 1. In Daily Timer task for outputting of stats.
* 2. In /towny universe output.
* TODO: That TownBlockCache
* @return ConcurrentHashMap<WorldCoord, TownBlock> of all TownBlocks.
*/
public ConcurrentHashMap<WorldCoord, TownBlock> getAllTownBlocks() {
return getTownBlocks();
}

/**
* Get Universe-wide ConcurrentHashMap of WorldCoords and their TownBlocks.
* Populated at load time from townblocks folder's files.
*
*
* @return townblocks hashmap read from townblock files.
*/
public ConcurrentHashMap<WorldCoord, TownBlock> getTownBlocks() {
return townBlocks;
}

/**
* Should not be accessed, only used for loading.
*/
public void newTownBlock(int x, int z, TownyWorld world) {

newTownBlock(new Coord(x, z), world);
}

/**
* Should not be accessed, only used for loading.
*/
public TownBlock newTownBlock(Coord key, TownyWorld world) {
if (hasTownBlock(key))
if (hasTownBlock(new WorldCoord(world.getName(), key)))
return null;
TownBlock newTownBlock = new TownBlock(key.getX(), key.getZ(), world);
townBlocks.put(new WorldCoord(world.getName(), key.getX(), key.getZ()), new TownBlock(key.getX(), key.getZ(), world));
return newTownBlock;
}

public boolean hasTownBlock(Coord key) {
/**
* Does this WorldCoord have a TownBlock?
* @param key - the coord for which we want to know if there is a townblock.
* @return true if Coord is a townblock
*/
public boolean hasTownBlock(WorldCoord worldCoord) {

return townBlocks.containsKey(key);
return townBlocks.containsKey(worldCoord);
}

/**
* Should not be accessed, only used for loading.
* @return townblocks hashmap read from townblock files.
*/
public ConcurrentHashMap<WorldCoord, TownBlock> getTownBlocks() {
return townBlocks;
* Remove one townblock from the TownyUniverse townblock map.
* @param townBlock to remove.
*/
public void removeTownBlock(TownBlock townBlock) {

if (hasTownBlock(townBlock.getWorldCoord())) {

try {
if (townBlock.hasResident())
townBlock.getResident().removeTownBlock(townBlock);
} catch (NotRegisteredException e) {
}
try {
if (townBlock.hasTown())
townBlock.getTown().removeTownBlock(townBlock);
} catch (NotRegisteredException e) {
}

removeTownBlock(townBlock.getWorldCoord());
}
}

/**
* Remove a list of TownBlocks from the TownyUniverse townblock map.
* @param townBlocks to remove.
*/
public void removeTownBlocks(List<TownBlock> townBlocks) {

for (TownBlock townBlock : new ArrayList<>(townBlocks))
removeTownBlock(townBlock);
}

/**
* Removes a townblock at the given worldCoord from the TownyUniverse townblock map.
* @param worldCoord to remove.
*/
private void removeTownBlock(WorldCoord worldCoord) {

townBlocks.remove(worldCoord);
}


}
1 change: 0 additions & 1 deletion src/com/palmergames/bukkit/towny/command/TownCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2445,7 +2445,6 @@ public static Town newTown(TownyWorld world, String name, Resident resident, Coo
townyUniverse.getDataSource().saveWorld(world);

townyUniverse.getDataSource().saveTownList();
townyUniverse.getDataSource().saveTownBlockList(town);

// Reset cache permissions for anyone in this TownBlock
plugin.updateCache(townBlock.getWorldCoord());
Expand Down
2 changes: 1 addition & 1 deletion src/com/palmergames/bukkit/towny/command/TownyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ public List<String> getUniverseStats() {
output.add("\u00A74#\u00A7c####\u00A74#\u00A7c####\u00A74# \u00A73By: \u00A7bChris H (Shade)/ElgarL/LlmDl");
output.add("\u00A70-\u00A74#\u00A7c#######\u00A74#\u00A70-");
output.add("\u00A70--\u00A74##\u00A7c###\u00A74##\u00A70-- " + "\u00A73Residents: \u00A7b" + townyUniverse.getDataSource().getResidents().size() + Colors.Gray + " | " + "\u00A73Towns: \u00A7b" + townyUniverse.getDataSource().getTowns().size() + Colors.Gray + " | " + "\u00A73Nations: \u00A7b" + townyUniverse.getDataSource().getNations().size());
output.add("\u00A70----\u00A74#\u00A7c#\u00A74#\u00A70---- " + "\u00A73Worlds: \u00A7b" + townyUniverse.getDataSource().getWorlds().size() + Colors.Gray + " | " + "\u00A73TownBlocks: \u00A7b" + townyUniverse.getDataSource().getAllTownBlocks().size());
output.add("\u00A70----\u00A74#\u00A7c#\u00A74#\u00A70---- " + "\u00A73Worlds: \u00A7b" + townyUniverse.getDataSource().getWorlds().size() + Colors.Gray + " | " + "\u00A73TownBlocks: \u00A7b" + townyUniverse.getAllTownBlocks().size());
output.add("\u00A70-----\u00A74#\u00A70----- ");
Plugin test = Bukkit.getServer().getPluginManager().getPlugin("TownyChat");
if (test != null){
Expand Down
10 changes: 5 additions & 5 deletions src/com/palmergames/bukkit/towny/db/TownyDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean loadAll() {

public boolean saveAll() {

return saveWorldList() && saveNationList() && saveTownList() && savePlotGroupList() && saveResidentList() && saveTownBlockList() && saveWorlds() && saveNations() && saveTowns() && saveResidents() && savePlotGroups() && saveAllTownBlocks() && saveRegenList() && saveSnapshotList();
return saveWorldList() && saveNationList() && saveTownList() && savePlotGroupList() && saveResidentList() && saveWorlds() && saveNations() && saveTowns() && saveResidents() && savePlotGroups() && saveAllTownBlocks() && saveRegenList() && saveSnapshotList();
}

public boolean saveAllWorlds() {
Expand Down Expand Up @@ -103,10 +103,10 @@ public boolean saveQueues() {
abstract public boolean loadPlotGroupList();

abstract public boolean loadPlotGroups();

abstract public boolean saveTownBlockList();

abstract public boolean saveTownBlockList(Town town);
//
// abstract public boolean saveTownBlockList();
//
// abstract public boolean saveTownBlockList(Town town);

abstract public boolean saveResidentList();

Expand Down
50 changes: 2 additions & 48 deletions src/com/palmergames/bukkit/towny/db/TownyDatabaseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,44 +331,6 @@ public void removeResident(Resident resident) {
BukkitTools.getPluginManager().callEvent(new DeletePlayerEvent(resident.getName()));
}

public void removeOneOfManyTownBlocks(TownBlock townBlock, Town town) {

TownPreUnclaimEvent event = new TownPreUnclaimEvent(townBlock);
BukkitTools.getPluginManager().callEvent(event);

if (event.isCancelled())
return;

Resident resident = null;
try {
resident = townBlock.getResident();
} catch (NotRegisteredException ignored) {
}

TownyWorld world = townBlock.getWorld();
WorldCoord coord = townBlock.getWorldCoord();

if (world.isUsingPlotManagementDelete())
TownyRegenAPI.addDeleteTownBlockIdQueue(coord);

// Move the plot to be restored
if (world.isUsingPlotManagementRevert()) {
PlotBlockData plotData = TownyRegenAPI.getPlotChunkSnapshot(townBlock);
if (plotData != null && !plotData.getBlockList().isEmpty()) {
TownyRegenAPI.addPlotChunk(plotData, true);
}
}

if (resident != null)
saveResident(resident);

town.removeTownBlock(townBlock);
deleteTownBlock(townBlock);

// Raise an event to signal the unclaim
BukkitTools.getPluginManager().callEvent(new TownUnclaimEvent(town, coord));
}

@Override
public void removeTownBlock(TownBlock townBlock) {

Expand All @@ -392,8 +354,6 @@ public void removeTownBlock(TownBlock townBlock) {
town.removeTownBlock(townBlock);
deleteTownBlock(townBlock);

saveTownBlockList(town);

// if (resident != null) - Removed in 0.95.2.5, residents don't store townblocks in them.
// saveResident(resident);

Expand All @@ -420,13 +380,6 @@ public void removeTownBlocks(Town town) {
for (TownBlock townBlock : new ArrayList<>(town.getTownBlocks()))
removeTownBlock(townBlock);
}

public void removeManyTownBlocks(Town town) {

for (TownBlock townBlock : new ArrayList<>(town.getTownBlocks()))
removeOneOfManyTownBlocks(townBlock, town);
saveTownBlockList(town);
}

@Override
public List<TownBlock> getAllTownBlocks() {
Expand Down Expand Up @@ -582,7 +535,7 @@ public void removeTown(Town town) {
if (preEvent.isCancelled())
return;

removeManyTownBlocks(town);
removeTownBlocks(town);
//removeTownBlocks(town);

List<Resident> toSave = new ArrayList<>(town.getResidents());
Expand Down Expand Up @@ -1192,4 +1145,5 @@ public void mergeNation(Nation succumbingNation, Nation prevailingNation) throws
lock.unlock();
}
}

}
Loading

0 comments on commit 963c6da

Please sign in to comment.