use of com.palmergames.bukkit.towny.object.Nation in project Towny by ElgarL.
the class CombatUtil method canAttackEnemy.
/**
* Can resident a attack resident b?
*
* @param a
* @param b
* @return true if they can attack.
*/
public static boolean canAttackEnemy(String a, String b) {
try {
Resident residentA = TownyUniverse.getDataSource().getResident(a);
Resident residentB = TownyUniverse.getDataSource().getResident(b);
if (residentA.getTown() == residentB.getTown())
return false;
if (residentA.getTown().getNation() == residentB.getTown().getNation())
return false;
Nation nationA = residentA.getTown().getNation();
Nation nationB = residentB.getTown().getNation();
if (nationA.isNeutral() || nationB.isNeutral())
return false;
if (nationA.hasEnemy(nationB))
return true;
} catch (NotRegisteredException e) {
return false;
}
return false;
}
use of com.palmergames.bukkit.towny.object.Nation in project Towny by ElgarL.
the class War method end.
public void end() {
for (Player player : BukkitTools.getOnlinePlayers()) if (player != null)
sendStats(player);
double halfWinnings;
try {
// Transactions might leave 1 coin. (OH noez!)
halfWinnings = getWarSpoils().getHoldingBalance() / 2.0;
try {
// Again, might leave residue.
double nationWinnings = halfWinnings / warringNations.size();
for (Nation winningNation : warringNations) {
getWarSpoils().payTo(nationWinnings, winningNation, "War - Nation Winnings");
TownyMessaging.sendGlobalMessage("Winning Nation: " + winningNation.getName() + " won " + TownyEconomyHandler.getFormattedBalance(nationWinnings) + ".");
}
} catch (ArithmeticException e) {
// A war ended with 0 nations.
}
try {
KeyValue<Town, Integer> winningTownScore = getWinningTownScore();
getWarSpoils().payTo(halfWinnings, winningTownScore.key, "War - Town Winnings");
TownyMessaging.sendGlobalMessage("Highest Score: " + winningTownScore.key.getName() + " won " + TownyEconomyHandler.getFormattedBalance(halfWinnings) + " with the score " + winningTownScore.value + ".");
} catch (TownyException e) {
}
} catch (EconomyException e1) {
}
}
use of com.palmergames.bukkit.towny.object.Nation in project Towny by ElgarL.
the class War method start.
public void start() {
warTime = true;
// Seed spoils of war
try {
warSpoils.pay(TownySettings.getBaseSpoilsOfWar(), "Start of War - Base Spoils");
TownyMessaging.sendMsg("[War] Seeding spoils of war with " + TownySettings.getBaseSpoilsOfWar());
} catch (EconomyException e) {
TownyMessaging.sendErrorMsg("[War] Could not seed spoils of war.");
}
//Gather all nations at war
for (Nation nation : TownyUniverse.getDataSource().getNations()) {
if (!nation.isNeutral()) {
add(nation);
TownyMessaging.sendGlobalMessage(String.format(TownySettings.getLangString("msg_war_join_nation"), nation.getName()));
} else if (!TownySettings.isDeclaringNeutral()) {
try {
nation.setNeutral(false);
add(nation);
TownyMessaging.sendGlobalMessage(String.format(TownySettings.getLangString("msg_war_join_forced"), nation.getName()));
} catch (TownyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//warTimer.scheduleAtFixedRate(new WarTimerTask(this), 0, 1000);
int id = BukkitTools.scheduleAsyncRepeatingTask(new WarTimerTask(plugin, this), 0, TimeTools.convertToTicks(5));
if (id == -1) {
TownyMessaging.sendErrorMsg("Could not schedule war event loop.");
end();
} else
addTaskId(id);
checkEnd();
}
use of com.palmergames.bukkit.towny.object.Nation in project Towny by ElgarL.
the class TownyHModFlatFileSource method loadNation.
@Override
public boolean loadNation(Nation nation) {
System.out.println("[Towny] [hMod Conversion] Nation: " + nation.getName());
String line = "";
String[] tokens;
String path = rootFolder + dataFolder + "/nations/" + nation.getName() + ".txt";
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) {
Town town = getTown(token);
if (town != null)
nation.addTown(town);
}
}
line = kvFile.get("capital");
nation.setCapital(getTown(line));
// line = kvFile.get("assistants");
// if (line != null) {
// tokens = line.split(",");
// for (String token : tokens) {
// Resident assistant = getResident(token);
// if (assistant != null)
// nation.addAssistant(assistant);
// }
// }
line = kvFile.get("allies");
if (line != null) {
tokens = line.split(",");
for (String token : tokens) {
Nation friend = getNation(token);
if (friend != null)
nation.setAllegiance("ally", friend);
}
}
line = kvFile.get("enemies");
if (line != null) {
tokens = line.split(",");
for (String token : tokens) {
Nation enemy = getNation(token);
if (enemy != null)
nation.setAllegiance("enemy", enemy);
}
}
line = kvFile.get("taxes");
if (line != null)
try {
nation.setTaxes(Integer.parseInt(line));
} catch (Exception e) {
nation.setTaxes(0);
}
line = kvFile.get("neutral");
if (line != null)
try {
nation.setNeutral(Boolean.parseBoolean(line));
} catch (Exception e) {
}
} catch (Exception e) {
System.out.println("[Towny] Loading Error: Exception while reading nation file " + nation.getName());
e.printStackTrace();
return false;
}
return true;
} else
return false;
}
use of com.palmergames.bukkit.towny.object.Nation in project Towny by ElgarL.
the class TownySQLSource method loadNation.
@Override
public boolean loadNation(Nation nation) {
String line = "";
String[] tokens;
TownyMessaging.sendDebugMsg("Loading nation " + nation.getName());
if (!getContext())
return false;
try {
Statement s = cntx.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM " + tb_prefix + "NATIONS WHERE name='" + nation.getName() + "'");
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)
nation.addTown(town);
}
}
}
nation.setCapital(getTown(rs.getString("capital")));
// line = rs.getString("assistants");
// if (line != null) {
// tokens = line.split(",");
// for (String token : tokens) {
// if (!token.isEmpty()) {
// Resident assistant = getResident(token);
// if (assistant != null)
// nation.addAssistant(assistant);
// }
// }
// }
nation.setTag(rs.getString("tag"));
line = rs.getString("allies");
if (line != null) {
search = (line.contains("#")) ? "#" : ",";
tokens = line.split(search);
for (String token : tokens) {
if (!token.isEmpty()) {
Nation friend = getNation(token);
if (friend != null)
// ("ally", friend);
nation.addAlly(friend);
}
}
}
line = rs.getString("enemies");
if (line != null) {
search = (line.contains("#")) ? "#" : ",";
tokens = line.split(search);
for (String token : tokens) {
if (!token.isEmpty()) {
Nation enemy = getNation(token);
if (enemy != null)
// ("enemy", enemy);
nation.addEnemy(enemy);
}
}
}
nation.setTaxes(rs.getDouble("taxes"));
nation.setNeutral(rs.getBoolean("neutral"));
}
s.close();
return true;
} catch (SQLException e) {
TownyMessaging.sendErrorMsg("SQL: Load Nation sql error " + e.getMessage());
} catch (Exception e) {
TownyMessaging.sendErrorMsg("SQL: Load Nation unknown error - ");
e.printStackTrace();
}
return false;
}
Aggregations