Search in sources :

Example 26 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class NationCommand method nationEnemy.

public void nationEnemy(Player player, String[] split) {
    Resident resident;
    Nation nation;
    if (split.length < 2) {
        TownyMessaging.sendErrorMsg(player, "Eg: /nation enemy [add/remove] [name]");
        return;
    }
    try {
        resident = TownyUniverse.getDataSource().getResident(player.getName());
        nation = resident.getTown().getNation();
    } catch (TownyException x) {
        TownyMessaging.sendErrorMsg(player, x.getMessage());
        return;
    }
    ArrayList<Nation> list = new ArrayList<Nation>();
    Nation enemy;
    // test add or remove
    String test = split[0];
    String[] newSplit = StringMgmt.remFirstArg(split);
    if ((test.equalsIgnoreCase("remove") || test.equalsIgnoreCase("add")) && newSplit.length > 0) {
        for (String name : newSplit) {
            try {
                enemy = TownyUniverse.getDataSource().getNation(name);
                if (nation.equals(enemy))
                    TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_own_nation_disallow"));
                else
                    list.add(enemy);
            } catch (NotRegisteredException e) {
            // Do nothing here as the name doesn't match a Nation
            }
        }
        if (!list.isEmpty())
            nationEnemy(player, nation, list, test.equalsIgnoreCase("add"));
    } else {
        TownyMessaging.sendErrorMsg(player, String.format(TownySettings.getLangString("msg_err_invalid_property"), "[add/remove]"));
    }
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) ArrayList(java.util.ArrayList) Resident(com.palmergames.bukkit.towny.object.Resident) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 27 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class TownySQLSource method loadTown.

@SuppressWarnings("deprecation")
@Override
public boolean loadTown(Town town) {
    String line;
    String[] tokens;
    TownyMessaging.sendDebugMsg("Loading town " + town.getName());
    if (!getContext())
        return false;
    try {
        Statement s = cntx.createStatement();
        ResultSet rs = s.executeQuery("SELECT * FROM " + tb_prefix + "TOWNS " + " WHERE name='" + town.getName() + "'");
        String search;
        while (rs.next()) {
            line = rs.getString("residents");
            if (line != null) {
                search = (line.contains("#")) ? "#" : ",";
                tokens = line.split(search);
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        Resident resident = getResident(token);
                        if (resident != null)
                            town.addResident(resident);
                    }
                }
            }
            town.setMayor(getResident(rs.getString("mayor")));
            // line = rs.getString("assistants");
            // if (line != null) {
            // tokens = line.split(",");
            // for (String token : tokens) {
            // if (!token.isEmpty()) {
            // Resident assistant = getResident(token);
            // if ((assistant != null) && (town.hasResident(assistant)))
            // town.addAssistant(assistant);
            // }
            // }
            // }
            town.setTownBoard(rs.getString("townBoard"));
            line = rs.getString("tag");
            if (line != null)
                try {
                    town.setTag(line);
                } catch (TownyException e) {
                    town.setTag("");
                }
            town.setPermissions(rs.getString("protectionStatus").replaceAll("#", ","));
            town.setBonusBlocks(rs.getInt("bonus"));
            town.setTaxPercentage(rs.getBoolean("taxpercent"));
            town.setTaxes(rs.getFloat("taxes"));
            town.setHasUpkeep(rs.getBoolean("hasUpkeep"));
            town.setPlotPrice(rs.getFloat("plotPrice"));
            town.setPlotTax(rs.getFloat("plotTax"));
            town.setEmbassyPlotPrice(rs.getFloat("embassyPlotPrice"));
            town.setEmbassyPlotTax(rs.getFloat("embassyPlotTax"));
            town.setCommercialPlotPrice(rs.getFloat("commercialPlotPrice"));
            town.setCommercialPlotTax(rs.getFloat("commercialPlotTax"));
            town.setOpen(rs.getBoolean("open"));
            town.setPublic(rs.getBoolean("public"));
            town.setAdminDisabledPVP(rs.getBoolean("admindisabledpvp"));
            town.setPurchasedBlocks(rs.getInt("purchased"));
            line = rs.getString("homeBlock");
            if (line != null) {
                search = (line.contains("#")) ? "#" : ",";
                tokens = line.split(search);
                if (tokens.length == 3)
                    try {
                        TownyWorld world = getWorld(tokens[0]);
                        try {
                            int x = Integer.parseInt(tokens[1]);
                            int z = Integer.parseInt(tokens[2]);
                            TownBlock homeBlock = world.getTownBlock(x, z);
                            town.forceSetHomeBlock(homeBlock);
                        } catch (NumberFormatException e) {
                            TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " homeBlock tried to load invalid location.");
                        } catch (NotRegisteredException e) {
                            TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " homeBlock tried to load invalid TownBlock.");
                        } catch (TownyException e) {
                            TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " does not have a home block.");
                        }
                    } catch (NotRegisteredException e) {
                        TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " homeBlock tried to load invalid world.");
                    }
            }
            line = rs.getString("spawn");
            if (line != null) {
                search = (line.contains("#")) ? "#" : ",";
                tokens = line.split(search);
                if (tokens.length >= 4)
                    try {
                        World world = plugin.getServerWorld(tokens[0]);
                        double x = Double.parseDouble(tokens[1]);
                        double y = Double.parseDouble(tokens[2]);
                        double z = Double.parseDouble(tokens[3]);
                        Location loc = new Location(world, x, y, z);
                        if (tokens.length == 6) {
                            loc.setPitch(Float.parseFloat(tokens[4]));
                            loc.setYaw(Float.parseFloat(tokens[5]));
                        }
                        town.forceSetSpawn(loc);
                    } catch (NumberFormatException e) {
                    } catch (NotRegisteredException e) {
                    } catch (NullPointerException e) {
                    }
                // Load outpost spawns
                line = rs.getString("outpostSpawns");
                if (line != null) {
                    String[] outposts = line.split(";");
                    for (String spawn : outposts) {
                        search = (line.contains("#")) ? "#" : ",";
                        tokens = spawn.split(search);
                        if (tokens.length >= 4)
                            try {
                                World world = plugin.getServerWorld(tokens[0]);
                                double x = Double.parseDouble(tokens[1]);
                                double y = Double.parseDouble(tokens[2]);
                                double z = Double.parseDouble(tokens[3]);
                                Location loc = new Location(world, x, y, z);
                                if (tokens.length == 6) {
                                    loc.setPitch(Float.parseFloat(tokens[4]));
                                    loc.setYaw(Float.parseFloat(tokens[5]));
                                }
                                town.forceAddOutpostSpawn(loc);
                            } catch (NumberFormatException e) {
                            } catch (NotRegisteredException e) {
                            } catch (NullPointerException e) {
                            }
                    }
                }
            }
            /*
				 * Attempt these for older databases.
				 */
            try {
                line = rs.getString("townBlocks");
                if (line != null)
                    utilLoadTownBlocks(line, town, null);
            } catch (SQLException e) {
            }
            s.close();
            return true;
        }
        s.close();
        return false;
    } catch (SQLException e) {
        TownyMessaging.sendErrorMsg("SQL: Load Town sql Error - " + e.getMessage());
    } catch (Exception e) {
        TownyMessaging.sendErrorMsg("SQL: Load Town unknown Error - ");
        e.printStackTrace();
    }
    return false;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) World(org.bukkit.World) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) 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) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) ResultSet(java.sql.ResultSet) Resident(com.palmergames.bukkit.towny.object.Resident) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) Location(org.bukkit.Location)

Example 28 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class PlayerCacheUtil method getTownBlockStatus.

/**
 * Fetch the TownBlockStatus type for this player at this WorldCoord.
 *
 * @param player
 * @param worldCoord
 * @return TownBlockStatus type.
 */
public static TownBlockStatus getTownBlockStatus(Player player, WorldCoord worldCoord) {
    try {
        if (!worldCoord.getTownyWorld().isUsingTowny())
            return TownBlockStatus.OFF_WORLD;
    } catch (NotRegisteredException ex) {
        // Not a registered world
        return TownBlockStatus.NOT_REGISTERED;
    }
    // TownyUniverse universe = plugin.getTownyUniverse();
    TownBlock townBlock;
    Town town;
    try {
        townBlock = worldCoord.getTownBlock();
        town = townBlock.getTown();
        if (townBlock.isLocked()) {
            // Push the TownBlock location to the queue for a snapshot (if it's not already in the queue).
            if (town.getWorld().isUsingPlotManagementRevert() && (TownySettings.getPlotManagementSpeed() > 0)) {
                TownyRegenAPI.addWorldCoord(townBlock.getWorldCoord());
                return TownBlockStatus.LOCKED;
            }
            townBlock.setLocked(false);
        }
    } catch (NotRegisteredException e) {
        // Unclaimed Zone switch rights
        return TownBlockStatus.UNCLAIMED_ZONE;
    }
    /*
		 * Find the resident data for this player.
		 */
    Resident resident;
    try {
        resident = TownyUniverse.getDataSource().getResident(player.getName());
    } catch (TownyException e) {
        System.out.print("Failed to fetch resident: " + player.getName());
        return TownBlockStatus.NOT_REGISTERED;
    }
    try {
        // War Time switch rights
        if (TownyUniverse.isWarTime()) {
            if (TownySettings.isAllowWarBlockGriefing()) {
                try {
                    if (!resident.getTown().getNation().isNeutral() && !town.getNation().isNeutral())
                        return TownBlockStatus.WARZONE;
                } catch (NotRegisteredException e) {
                }
            }
            // If this town is not in a nation and we are set to non neutral status during war.
            if (!TownySettings.isWarTimeTownsNeutral() && !town.hasNation())
                return TownBlockStatus.WARZONE;
        }
        // Town Owner Override
        try {
            if (// || townBlock.getTown().hasAssistant(resident))
            townBlock.getTown().isMayor(resident))
                return TownBlockStatus.TOWN_OWNER;
        } catch (NotRegisteredException e) {
        }
        // Resident Plot rights
        try {
            Resident owner = townBlock.getResident();
            if (resident == owner)
                return TownBlockStatus.PLOT_OWNER;
            else if (owner.hasFriend(resident))
                return TownBlockStatus.PLOT_FRIEND;
            else if (resident.hasTown() && CombatUtil.isAlly(owner.getTown(), resident.getTown()))
                return TownBlockStatus.PLOT_ALLY;
            else
                // Exit out and use town permissions
                throw new TownyException();
        } catch (NotRegisteredException x) {
        } catch (TownyException x) {
        }
        // Resident with no town.
        if (!resident.hasTown()) {
            if (townBlock.isWarZone()) {
                if (!TownySettings.isWarTimeTownsNeutral())
                    return TownBlockStatus.WARZONE;
                else
                    return TownBlockStatus.OUTSIDER;
            }
            throw new TownyException();
        }
        if (resident.getTown() != town) {
            // Allied destroy rights
            if (CombatUtil.isAlly(town, resident.getTown()))
                return TownBlockStatus.TOWN_ALLY;
            else if (CombatUtil.isEnemy(resident.getTown(), town)) {
                if (townBlock.isWarZone())
                    return TownBlockStatus.WARZONE;
                else
                    return TownBlockStatus.ENEMY;
            } else
                return TownBlockStatus.OUTSIDER;
        } else if (// || resident.getTown().hasAssistant(resident))
        resident.isMayor())
            return TownBlockStatus.TOWN_OWNER;
        else
            return TownBlockStatus.TOWN_RESIDENT;
    } catch (TownyException e) {
        // Outsider destroy rights
        return TownBlockStatus.OUTSIDER;
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Town(com.palmergames.bukkit.towny.object.Town) Resident(com.palmergames.bukkit.towny.object.Resident) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 29 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class War method getWinningTownScore.

public KeyValue<Town, Integer> getWinningTownScore() throws TownyException {
    KeyValueTable<Town, Integer> kvTable = new KeyValueTable<Town, Integer>(townScores);
    kvTable.sortByValue();
    kvTable.revese();
    if (kvTable.getKeyValues().size() > 0)
        return kvTable.getKeyValues().get(0);
    else
        throw new TownyException();
}
Also used : Town(com.palmergames.bukkit.towny.object.Town) KeyValueTable(com.palmergames.util.KeyValueTable) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 30 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException 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)

Aggregations

TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)70 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)37 Resident (com.palmergames.bukkit.towny.object.Resident)29 Town (com.palmergames.bukkit.towny.object.Town)17 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)16 Nation (com.palmergames.bukkit.towny.object.Nation)16 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)15 ArrayList (java.util.ArrayList)12 Player (org.bukkit.entity.Player)12 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)11 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)11 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)7 Location (org.bukkit.Location)7 IOException (java.io.IOException)6 EventHandler (org.bukkit.event.EventHandler)6 Coord (com.palmergames.bukkit.towny.object.Coord)5 InvalidNameException (javax.naming.InvalidNameException)5 BlockLocation (com.palmergames.bukkit.towny.regen.block.BlockLocation)4 EmptyNationException (com.palmergames.bukkit.towny.exceptions.EmptyNationException)3 EmptyTownException (com.palmergames.bukkit.towny.exceptions.EmptyTownException)3