Search in sources :

Example 36 with TownyException

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

the class PlotClaim method run.

@Override
public void run() {
    int claimed = 0;
    if (player != null)
        TownyMessaging.sendMsg(player, "Processing " + ((claim) ? "Plot Claim..." : "Plot unclaim..."));
    if (selection != null) {
        for (WorldCoord worldCoord : selection) {
            // Make sure this is a valid world (mainly when unclaiming).
            try {
                this.world = worldCoord.getTownyWorld();
            } catch (NotRegisteredException e) {
                TownyMessaging.sendMsg(player, TownySettings.getLangString("msg_err_not_configured"));
                continue;
            }
            try {
                if (claim) {
                    if (residentClaim(worldCoord))
                        claimed++;
                } else {
                    residentUnclaim(worldCoord);
                }
            } catch (EconomyException e) {
                /*
					 * Can't pay, but try the rest as we may be
					 * re-possessing and claiming for personal plots.
					 */
                TownyMessaging.sendErrorMsg(player, e.getError());
            } catch (TownyException x) {
                TownyMessaging.sendErrorMsg(player, x.getMessage());
            }
        }
    } else if (!claim) {
        residentUnclaimAll();
    }
    if (player != null) {
        if (claim) {
            if ((selection != null) && (selection.size() > 0) && (claimed > 0))
                TownyMessaging.sendMsg(player, TownySettings.getLangString("msg_claimed") + ((selection.size() > 5) ? "Total TownBlocks: " + selection.size() : Arrays.toString(selection.toArray(new WorldCoord[0]))));
            else
                TownyMessaging.sendMsg(player, TownySettings.getLangString("msg_not_claimed_1"));
        } else if (selection != null)
            TownyMessaging.sendMsg(player, TownySettings.getLangString("msg_unclaimed") + ((selection.size() > 5) ? "Total TownBlocks: " + selection.size() : Arrays.toString(selection.toArray(new WorldCoord[0]))));
        else
            TownyMessaging.sendMsg(player, TownySettings.getLangString("msg_unclaimed"));
    }
    TownyUniverse.getDataSource().saveResident(resident);
    plugin.resetCache();
}
Also used : EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 37 with TownyException

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

the class TownClaim method townUnclaim.

private void townUnclaim(final Town town, final WorldCoord worldCoord, boolean force) throws TownyException {
    try {
        final TownBlock townBlock = worldCoord.getTownBlock();
        if (town != townBlock.getTown() && !force)
            throw new TownyException(TownySettings.getLangString("msg_area_not_own"));
        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

            @Override
            public void run() {
                TownyUniverse.getDataSource().removeTownBlock(townBlock);
                // Raise an event to signal the unclaim
                BukkitTools.getPluginManager().callEvent(new TownUnclaimEvent(town, worldCoord));
            }
        }, 1);
    } catch (NotRegisteredException e) {
        throw new TownyException(TownySettings.getLangString("msg_not_claimed_1"));
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) TownUnclaimEvent(com.palmergames.bukkit.towny.event.TownUnclaimEvent) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 38 with TownyException

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

the class TownClaim method run.

@Override
public void run() {
    List<TownyWorld> worlds = new ArrayList<TownyWorld>();
    List<Town> towns = new ArrayList<Town>();
    TownyWorld world;
    if (player != null)
        TownyMessaging.sendMsg(player, "Processing " + ((claim) ? "Town Claim..." : "Town unclaim..."));
    if (selection != null) {
        for (WorldCoord worldCoord : selection) {
            try {
                world = worldCoord.getTownyWorld();
                if (!worlds.contains(world))
                    worlds.add(world);
                if (claim) {
                    // Claim
                    townClaim(town, worldCoord, outpost);
                    // Reset so we only flag the first plot as an outpost.
                    outpost = false;
                } else {
                    // Unclaim
                    this.town = worldCoord.getTownBlock().getTown();
                    townUnclaim(town, worldCoord, forced);
                }
                // Mark this town as modified for saving.
                if (!towns.contains(town))
                    towns.add(town);
            } catch (NotRegisteredException e) {
                // Invalid world
                TownyMessaging.sendMsg(player, TownySettings.getLangString("msg_err_not_configured"));
            } catch (TownyException x) {
                TownyMessaging.sendErrorMsg(player, x.getMessage());
            }
        }
    } else if (!claim) {
        if (town == null) {
            TownyMessaging.sendMsg(player, "Nothing to unclaim!");
            return;
        }
        townUnclaimAll(town);
    }
    if (!towns.isEmpty())
        for (Town test : towns) TownyUniverse.getDataSource().saveTown(test);
    if (!worlds.isEmpty())
        for (TownyWorld test : worlds) TownyUniverse.getDataSource().saveWorld(test);
    plugin.resetCache();
    if (player != null) {
        if (claim) {
            TownyMessaging.sendMsg(player, String.format(TownySettings.getLangString("msg_annexed_area"), (selection.size() > 5) ? "Total TownBlocks: " + selection.size() : Arrays.toString(selection.toArray(new WorldCoord[0]))));
            if (town.getWorld().isUsingPlotManagementRevert())
                TownyMessaging.sendMsg(player, TownySettings.getLangString("msg_wait_locked"));
        } else if (forced) {
            TownyMessaging.sendMsg(player, String.format(TownySettings.getLangString("msg_admin_unclaim_area"), (selection.size() > 5) ? "Total TownBlocks: " + selection.size() : Arrays.toString(selection.toArray(new WorldCoord[0]))));
            if ((town != null) && (town.getWorld().isUsingPlotManagementRevert()))
                TownyMessaging.sendMsg(player, TownySettings.getLangString("msg_wait_locked"));
        }
    }
}
Also used : WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Town(com.palmergames.bukkit.towny.object.Town) ArrayList(java.util.ArrayList) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 39 with TownyException

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

the class TownyWar method checkIfTownHasMinOnlineForWar.

public static void checkIfTownHasMinOnlineForWar(Town town) throws TownyException {
    int requiredOnline = TownyWarConfig.getMinPlayersOnlineInTownForWar();
    int onlinePlayerCount = TownyUniverse.getOnlinePlayers(town).size();
    if (onlinePlayerCount < requiredOnline)
        throw new TownyException(String.format(TownySettings.getLangString("msg_err_enemy_war_require_online"), requiredOnline, town.getFormattedName()));
}
Also used : TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 40 with TownyException

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

the class TownyWar method callAttackCellEvent.

public static boolean callAttackCellEvent(Towny plugin, Player player, Block block, WorldCoord worldCoord) throws TownyException {
    int topY = block.getWorld().getHighestBlockYAt(block.getX(), block.getZ()) - 1;
    if (block.getY() < topY)
        throw new TownyException(TownySettings.getLangString("msg_err_enemy_war_must_be_placed_above_ground"));
    TownyUniverse universe = plugin.getTownyUniverse();
    Resident attackingResident;
    Town landOwnerTown, attackingTown;
    Nation landOwnerNation, attackingNation;
    TownBlock townBlock;
    try {
        attackingResident = TownyUniverse.getDataSource().getResident(player.getName());
        attackingTown = attackingResident.getTown();
        attackingNation = attackingTown.getNation();
    } catch (NotRegisteredException e) {
        throw new TownyException(TownySettings.getLangString("msg_err_dont_belong_nation"));
    }
    try {
        landOwnerTown = worldCoord.getTownBlock().getTown();
        townBlock = worldCoord.getTownBlock();
        landOwnerNation = landOwnerTown.getNation();
    } catch (NotRegisteredException e) {
        throw new TownyException(TownySettings.getLangString("msg_err_enemy_war_not_part_of_nation"));
    }
    // Check Neutrality
    if (landOwnerNation.isNeutral())
        throw new TownyException(String.format(TownySettings.getLangString("msg_err_enemy_war_is_neutral"), landOwnerNation.getFormattedName()));
    if (!TownyUniverse.getPermissionSource().isTownyAdmin(player) && attackingNation.isNeutral())
        throw new TownyException(String.format(TownySettings.getLangString("msg_err_enemy_war_is_neutral"), attackingNation.getFormattedName()));
    // Check Minimum Players Online
    checkIfTownHasMinOnlineForWar(landOwnerTown);
    checkIfNationHasMinOnlineForWar(landOwnerNation);
    checkIfTownHasMinOnlineForWar(attackingTown);
    checkIfNationHasMinOnlineForWar(attackingNation);
    // Check that attack takes place on the edge of a town
    if (TownyWarConfig.isAttackingBordersOnly() && !AreaSelectionUtil.isOnEdgeOfOwnership(landOwnerTown, worldCoord))
        throw new TownyException(TownySettings.getLangString("msg_err_enemy_war_not_on_edge_of_town"));
    // Check that the user can pay for the warflag + fines from losing/winning.
    double costToPlaceWarFlag = TownyWarConfig.getCostToPlaceWarFlag();
    if (TownySettings.isUsingEconomy()) {
        try {
            double requiredAmount = costToPlaceWarFlag;
            double balance = attackingResident.getHoldingBalance();
            // Check that the user can pay for the warflag.
            if (balance < costToPlaceWarFlag)
                throw new TownyException(String.format(TownySettings.getLangString("msg_err_insuficient_funds_warflag"), TownyEconomyHandler.getFormattedBalance(costToPlaceWarFlag)));
            // Check that the user can pay the fines from losing/winning all future warflags.
            int activeFlagCount = getNumActiveFlags(attackingResident.getName());
            double defendedAttackCost = TownyWarConfig.getDefendedAttackReward() * (activeFlagCount + 1);
            double attackWinCost = 0;
            double amount;
            amount = TownyWarConfig.getWonHomeblockReward();
            double homeBlockFine = amount < 0 ? -amount : 0;
            amount = TownyWarConfig.getWonTownblockReward();
            double townBlockFine = amount < 0 ? -amount : 0;
            // Error would be caught when actually taking the money when the plot has been won.
            if (townBlock.isHomeBlock())
                attackWinCost = homeBlockFine + activeFlagCount * townBlockFine;
            else
                attackWinCost = (activeFlagCount + 1) * townBlockFine;
            if (defendedAttackCost > 0 && attackWinCost > 0) {
                // There could be a fine
                String reason;
                double cost;
                if (defendedAttackCost > attackWinCost) {
                    // Worst case scenario that all attacks are defended.
                    requiredAmount += defendedAttackCost;
                    cost = defendedAttackCost;
                    reason = TownySettings.getLangString("name_defended_attack");
                } else {
                    // Worst case scenario that all attacks go through, but is forced to pay a rebuilding fine.
                    requiredAmount += attackWinCost;
                    cost = attackWinCost;
                    reason = TownySettings.getLangString("name_rebuilding");
                }
                // Check if player can pay in worst case scenario.
                if (balance < requiredAmount)
                    throw new TownyException(String.format(TownySettings.getLangString("msg_err_insuficient_funds_future"), TownyEconomyHandler.getFormattedBalance(cost), String.format("%d %s", activeFlagCount + 1, reason + "(s)")));
            }
        } catch (EconomyException e) {
            throw new TownyException(e.getError());
        }
    }
    // Call Event (and make sure an attack isn't already under way)
    CellAttackEvent cellAttackEvent = new CellAttackEvent(plugin, player, block);
    plugin.getServer().getPluginManager().callEvent(cellAttackEvent);
    if (cellAttackEvent.isCancelled()) {
        if (cellAttackEvent.hasReason())
            throw new TownyException(cellAttackEvent.getReason());
        else
            return false;
    }
    // Pay for war flag
    if (TownySettings.isUsingEconomy()) {
        // Skip payment + message if no cost.
        if (costToPlaceWarFlag > 0) {
            try {
                attackingResident.pay(costToPlaceWarFlag, "War - WarFlag Cost");
                TownyMessaging.sendResidentMessage(attackingResident, String.format(TownySettings.getLangString("msg_enemy_war_purchased_warflag"), TownyEconomyHandler.getFormattedBalance(costToPlaceWarFlag)));
            } catch (EconomyException e) {
                e.printStackTrace();
            }
        }
    }
    // Set yourself as target's enemy so they can retaliate.
    if (!landOwnerNation.hasEnemy(attackingNation)) {
        landOwnerNation.addEnemy(attackingNation);
        plugin.getTownyUniverse();
        TownyUniverse.getDataSource().saveNation(landOwnerNation);
    }
    // Update Cache
    universe.addWarZone(worldCoord);
    plugin.updateCache(worldCoord);
    TownyMessaging.sendGlobalMessage(String.format(TownySettings.getLangString("msg_enemy_war_area_under_attack"), landOwnerTown.getFormattedName(), worldCoord.toString(), attackingResident.getFormattedName()));
    return true;
}
Also used : EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) CellAttackEvent(com.palmergames.bukkit.towny.war.flagwar.events.CellAttackEvent) 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