use of com.palmergames.bukkit.towny.object.Nation in project Towny by ElgarL.
the class TownyFlatFileSource method saveNationList.
@Override
public boolean saveNationList() {
List<String> list = new ArrayList<String>();
for (Nation nation : getNations()) {
list.add(nation.getName());
}
/*
* Make sure we only save in async
*/
this.queryQueue.add(new FlatFile_Task(list, rootFolder + dataFolder + FileMgmt.fileSeparator() + "nations.txt"));
return true;
}
use of com.palmergames.bukkit.towny.object.Nation in project Towny by ElgarL.
the class TownyFlatFileSource method loadNation.
@Override
public boolean loadNation(Nation nation) {
String line = "";
String[] tokens;
String path = getNationFilename(nation);
File fileResident = new File(path);
if (fileResident.exists() && fileResident.isFile()) {
try {
KeyValueFile kvFile = new KeyValueFile(path);
line = kvFile.get("towns");
if (line != null) {
tokens = line.split(",");
for (String token : tokens) {
if (!token.isEmpty()) {
TownyMessaging.sendDebugMsg("Nation Fetching Town: " + token);
Town town = getTown(token);
if (town != null)
nation.addTown(town);
}
}
}
line = kvFile.get("capital");
if (line != null)
nation.setCapital(getTown(line));
// line = kvFile.get("assistants");
// if (line != null) {
// tokens = line.split(",");
// for (String token : tokens) {
// if (!token.isEmpty()) {
// Resident assistant = getResident(token);
// if (assistant != null)
// nation.addAssistant(assistant);
// }
// }
// }
line = kvFile.get("tag");
if (line != null)
try {
nation.setTag(line);
} catch (TownyException e) {
nation.setTag("");
}
line = kvFile.get("allies");
if (line != null) {
tokens = line.split(",");
for (String token : tokens) {
if (!token.isEmpty()) {
Nation friend = getNation(token);
if (friend != null)
//("ally", friend);
nation.addAlly(friend);
}
}
}
line = kvFile.get("enemies");
if (line != null) {
tokens = line.split(",");
for (String token : tokens) {
if (!token.isEmpty()) {
Nation enemy = getNation(token);
if (enemy != null)
//("enemy", enemy);
nation.addEnemy(enemy);
}
}
}
line = kvFile.get("taxes");
if (line != null)
try {
nation.setTaxes(Double.parseDouble(line));
} catch (Exception e) {
nation.setTaxes(0.0);
}
line = kvFile.get("neutral");
if (line != null)
try {
nation.setNeutral(Boolean.parseBoolean(line));
} catch (Exception e) {
}
} catch (Exception e) {
TownyMessaging.sendErrorMsg("Loading Error: Exception while reading nation file " + nation.getName());
return false;
}
return true;
} else
return false;
}
use of com.palmergames.bukkit.towny.object.Nation in project Towny by ElgarL.
the class DailyTimerTask method collectNationCosts.
/**
* Collect upkeep due from all nations.
*
* @throws EconomyException
*/
public void collectNationCosts() throws EconomyException {
List<Nation> nations = new ArrayList<Nation>(TownyUniverse.getDataSource().getNations());
ListIterator<Nation> nationItr = nations.listIterator();
Nation nation = null;
while (nationItr.hasNext()) {
nation = nationItr.next();
/*
* Only charge upkeep for this nation if it really still exists.
* We are running in an Async thread so MUST verify all objects.
*/
if (TownyUniverse.getDataSource().hasNation(nation.getName())) {
double upkeep = TownySettings.getNationUpkeepCost(nation);
if (upkeep > 0) {
if (!nation.pay(TownySettings.getNationUpkeepCost(nation), "Nation Upkeep")) {
TownyUniverse.getDataSource().removeNation(nation);
TownyMessaging.sendGlobalMessage(nation.getName() + TownySettings.getLangString("msg_bankrupt_nation"));
}
if (nation.isNeutral()) {
if (!nation.pay(TownySettings.getNationNeutralityCost(), "Nation Neutrality Upkeep")) {
try {
nation.setNeutral(false);
} catch (TownyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TownyUniverse.getDataSource().saveNation(nation);
TownyMessaging.sendNationMessage(nation, TownySettings.getLangString("msg_nation_not_neutral"));
}
}
} else if (upkeep < 0) {
nation.pay(upkeep, "Negative Nation Upkeep");
}
}
}
universe.setChangedNotify(UPKEEP_NATION);
}
use of com.palmergames.bukkit.towny.object.Nation in project Towny by ElgarL.
the class TownyPlayerListener method onPlayerBedEnter.
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerBedEnter(PlayerBedEnterEvent event) {
if (!TownySettings.getBedUse())
return;
boolean isOwner = false;
boolean isInnPlot = false;
try {
Resident resident = TownyUniverse.getDataSource().getResident(event.getPlayer().getName());
WorldCoord worldCoord = new WorldCoord(event.getPlayer().getWorld().getName(), Coord.parseCoord(event.getBed().getLocation()));
TownBlock townblock = worldCoord.getTownBlock();
isOwner = townblock.isOwner(resident);
isInnPlot = townblock.getType() == TownBlockType.INN;
if (resident.hasNation() && townblock.getTown().hasNation()) {
Nation residentNation = resident.getTown().getNation();
Nation townblockNation = townblock.getTown().getNation();
if (townblockNation.hasEnemy(residentNation)) {
event.setCancelled(true);
TownyMessaging.sendErrorMsg(event.getPlayer(), "You cannot sleep in an enemy's Inn.");
return;
}
}
} catch (NotRegisteredException e) {
// Wilderness as it error'd getting a townblock.
}
if (!isOwner && !isInnPlot) {
event.setCancelled(true);
TownyMessaging.sendErrorMsg(event.getPlayer(), "You do not own the land this bed occupies and it is not an Inn plot.");
}
}
use of com.palmergames.bukkit.towny.object.Nation in project Towny by ElgarL.
the class TownyAsciiMap method generateAndSend.
public static void generateAndSend(Towny plugin, Player player, int lineHeight) {
// Collect Sample Data
boolean hasTown = false;
Resident resident;
try {
resident = TownyUniverse.getDataSource().getResident(player.getName());
if (resident.hasTown())
hasTown = true;
} catch (TownyException x) {
TownyMessaging.sendErrorMsg(player, x.getMessage());
return;
}
TownyWorld world;
try {
world = TownyUniverse.getDataSource().getWorld(player.getWorld().getName());
} catch (NotRegisteredException e1) {
TownyMessaging.sendErrorMsg(player, "You are not in a registered world.");
return;
}
if (!world.isUsingTowny()) {
TownyMessaging.sendErrorMsg(player, "This world is not using towny.");
return;
}
Coord pos = Coord.parseCoord(plugin.getCache(player).getLastLocation());
// Generate Map
int halfLineHeight = lineHeight / 2;
String[][] townyMap = new String[lineWidth][lineHeight];
int x, y = 0;
for (int tby = pos.getX() + (lineWidth - halfLineWidth - 1); tby >= pos.getX() - halfLineWidth; tby--) {
x = 0;
for (int tbx = pos.getZ() - halfLineHeight; tbx <= pos.getZ() + (lineHeight - halfLineHeight - 1); tbx++) {
try {
TownBlock townblock = world.getTownBlock(tby, tbx);
//TODO: possibly claim outside of towns
if (!townblock.hasTown())
throw new TownyException();
if (x == halfLineHeight && y == halfLineWidth)
// location
townyMap[y][x] = Colors.Gold;
else if (hasTown) {
if (resident.getTown() == townblock.getTown()) {
// own town
townyMap[y][x] = Colors.LightGreen;
try {
if (resident == townblock.getResident())
//own plot
townyMap[y][x] = Colors.Yellow;
} catch (NotRegisteredException e) {
}
} else if (resident.hasNation()) {
if (resident.getTown().getNation().hasTown(townblock.getTown()))
// towns
townyMap[y][x] = Colors.Green;
else if (townblock.getTown().hasNation()) {
Nation nation = resident.getTown().getNation();
if (nation.hasAlly(townblock.getTown().getNation()))
townyMap[y][x] = Colors.Green;
else if (nation.hasEnemy(townblock.getTown().getNation()))
// towns
townyMap[y][x] = Colors.Red;
else
townyMap[y][x] = Colors.White;
} else
townyMap[y][x] = Colors.White;
} else
townyMap[y][x] = Colors.White;
} else
townyMap[y][x] = Colors.White;
// Registered town block
if (townblock.getPlotPrice() != -1) {
// override the colour if it's a shop plot for sale
if (townblock.getType().equals(TownBlockType.COMMERCIAL))
townyMap[y][x] = Colors.Blue;
townyMap[y][x] += "$";
} else if (townblock.isHomeBlock())
townyMap[y][x] += "H";
else
townyMap[y][x] += townblock.getType().getAsciiMapKey();
} catch (TownyException e) {
if (x == halfLineHeight && y == halfLineWidth)
townyMap[y][x] = Colors.Gold;
else
townyMap[y][x] = Colors.Gray;
// Unregistered town block
townyMap[y][x] += "-";
}
x++;
}
y++;
}
String[] compass = generateCompass(player);
// Output
player.sendMessage(ChatTools.formatTitle("Towny Map " + Colors.White + "(" + pos.toString() + ")"));
String line;
int lineCount = 0;
// Variables have been rotated to fit N/S/E/W properly
for (int my = 0; my < lineHeight; my++) {
line = compass[0];
if (lineCount < compass.length)
line = compass[lineCount];
for (int mx = lineWidth - 1; mx >= 0; mx--) line += townyMap[mx][my];
if (lineCount < help.length)
line += help[lineCount];
player.sendMessage(line);
lineCount++;
}
// Current town block data
try {
TownBlock townblock = world.getTownBlock(pos);
TownyMessaging.sendMsg(player, ("Town: " + (townblock.hasTown() ? townblock.getTown().getName() : "None") + " : " + "Owner: " + (townblock.hasResident() ? townblock.getResident().getName() : "None")));
} catch (TownyException e) {
//plugin.sendErrorMsg(player, e.getError());
// Send a blank line instead of an error, to keep the map position tidy.
player.sendMessage("");
}
}
Aggregations