Search in sources :

Example 1 with Nation

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;
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident)

Example 2 with Nation

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) {
    }
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) Player(org.bukkit.entity.Player) Town(com.palmergames.bukkit.towny.object.Town) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 3 with Nation

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();
}
Also used : EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) Nation(com.palmergames.bukkit.towny.object.Nation) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 4 with Nation

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;
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) Town(com.palmergames.bukkit.towny.object.Town) KeyValueFile(com.palmergames.util.KeyValueFile) File(java.io.File) KeyValueFile(com.palmergames.util.KeyValueFile) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) IOException(java.io.IOException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)

Example 5 with Nation

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;
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) Town(com.palmergames.bukkit.towny.object.Town) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) SQLException(java.sql.SQLException) IOException(java.io.IOException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Aggregations

Nation (com.palmergames.bukkit.towny.object.Nation)30 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)17 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)16 Resident (com.palmergames.bukkit.towny.object.Resident)16 Town (com.palmergames.bukkit.towny.object.Town)10 ArrayList (java.util.ArrayList)10 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)9 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)8 EmptyNationException (com.palmergames.bukkit.towny.exceptions.EmptyNationException)3 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)3 IOException (java.io.IOException)3 InvalidNameException (javax.naming.InvalidNameException)3 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)2 KeyValueFile (com.palmergames.util.KeyValueFile)2 File (java.io.File)2 Player (org.bukkit.entity.Player)2 Teleport (com.earth2me.essentials.Teleport)1 User (com.earth2me.essentials.User)1 Coord (com.palmergames.bukkit.towny.object.Coord)1 TownSpawnLevel (com.palmergames.bukkit.towny.object.TownSpawnLevel)1