Search in sources :

Example 66 with TownyException

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

the class TownyPlayerListener method onPlayerRespawn.

@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerRespawn(PlayerRespawnEvent event) {
    if (plugin.isError()) {
        return;
    }
    Player player = event.getPlayer();
    TownyMessaging.sendDebugMsg("onPlayerDeath: " + player.getName());
    if (!TownySettings.isTownRespawning())
        return;
    try {
        Location respawn = plugin.getTownyUniverse().getTownSpawnLocation(player);
        // Check if only respawning in the same world as the town's spawn.
        if (TownySettings.isTownRespawningInOtherWorlds() && !player.getWorld().equals(respawn.getWorld()))
            return;
        // Bed spawn or town.
        if (TownySettings.getBedUse() && (player.getBedSpawnLocation() != null)) {
            event.setRespawnLocation(player.getBedSpawnLocation());
        } else {
            event.setRespawnLocation(respawn);
        }
    } catch (TownyException e) {
    // Town has not set respawn location. Using default.
    }
}
Also used : Player(org.bukkit.entity.Player) Location(org.bukkit.Location) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EventHandler(org.bukkit.event.EventHandler)

Example 67 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 68 with TownyException

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

the class TownyWarCustomListener method onCellDefendedEvent.

@EventHandler(priority = EventPriority.LOWEST)
public void onCellDefendedEvent(CellDefendedEvent event) {
    Player player = event.getPlayer();
    CellUnderAttack cell = event.getCell().getAttackData();
    TownyUniverse universe = plugin.getTownyUniverse();
    WorldCoord worldCoord = new WorldCoord(cell.getWorldName(), cell.getX(), cell.getZ());
    universe.removeWarZone(worldCoord);
    plugin.updateCache(worldCoord);
    String playerName;
    if (player == null) {
        playerName = "Greater Forces";
    } else {
        playerName = player.getName();
        try {
            playerName = TownyUniverse.getDataSource().getResident(player.getName()).getFormattedName();
        } catch (TownyException e) {
        }
    }
    plugin.getServer().broadcastMessage(String.format(TownySettings.getLangString("msg_enemy_war_area_defended"), playerName, cell.getCellString()));
    // Also doesn't take into account of paying as much as the attacker can afford (Eg: cost=10 and balance=9).
    if (TownySettings.isUsingEconomy()) {
        try {
            Resident attackingPlayer, defendingPlayer = null;
            attackingPlayer = TownyUniverse.getDataSource().getResident(cell.getNameOfFlagOwner());
            if (player != null) {
                try {
                    defendingPlayer = TownyUniverse.getDataSource().getResident(player.getName());
                } catch (NotRegisteredException e) {
                }
            }
            String formattedMoney = TownyEconomyHandler.getFormattedBalance(TownyWarConfig.getDefendedAttackReward());
            if (defendingPlayer == null) {
                if (attackingPlayer.pay(TownyWarConfig.getDefendedAttackReward(), "War - Attack Was Defended (Greater Forces)"))
                    try {
                        TownyMessaging.sendResidentMessage(attackingPlayer, String.format(TownySettings.getLangString("msg_enemy_war_area_defended_greater_forces"), formattedMoney));
                    } catch (TownyException e) {
                    }
            } else {
                if (attackingPlayer.payTo(TownyWarConfig.getDefendedAttackReward(), defendingPlayer, "War - Attack Was Defended")) {
                    try {
                        TownyMessaging.sendResidentMessage(attackingPlayer, String.format(TownySettings.getLangString("msg_enemy_war_area_defended_attacker"), defendingPlayer.getFormattedName(), formattedMoney));
                    } catch (TownyException e) {
                    }
                    try {
                        TownyMessaging.sendResidentMessage(defendingPlayer, String.format(TownySettings.getLangString("msg_enemy_war_area_defended_defender"), attackingPlayer.getFormattedName(), formattedMoney));
                    } catch (TownyException e) {
                    }
                }
            }
        } catch (EconomyException e) {
            e.printStackTrace();
        } catch (NotRegisteredException e) {
            e.printStackTrace();
        }
    }
}
Also used : EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) Player(org.bukkit.entity.Player) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) CellUnderAttack(com.palmergames.bukkit.towny.war.flagwar.CellUnderAttack) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EventHandler(org.bukkit.event.EventHandler)

Example 69 with TownyException

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

the class TownyWarCustomListener method onCellWonEvent.

@EventHandler(priority = EventPriority.LOWEST)
public void onCellWonEvent(CellWonEvent event) {
    CellUnderAttack cell = event.getCellAttackData();
    TownyUniverse universe = plugin.getTownyUniverse();
    try {
        Resident attackingResident = TownyUniverse.getDataSource().getResident(cell.getNameOfFlagOwner());
        Town attackingTown = attackingResident.getTown();
        Nation attackingNation = attackingTown.getNation();
        WorldCoord worldCoord = TownyWar.cellToWorldCoord(cell);
        universe.removeWarZone(worldCoord);
        TownBlock townBlock = worldCoord.getTownBlock();
        Town defendingTown = townBlock.getTown();
        // Payments
        double amount = 0;
        String moneyTranserMsg = null;
        if (TownySettings.isUsingEconomy()) {
            try {
                String reasonType;
                if (townBlock.isHomeBlock()) {
                    amount = TownyWarConfig.getWonHomeblockReward();
                    reasonType = "Homeblock";
                } else {
                    amount = TownyWarConfig.getWonTownblockReward();
                    reasonType = "Townblock";
                }
                if (amount > 0) {
                    // Defending Town -> Attacker (Pillage)
                    String reason = String.format("War - Won Enemy %s (Pillage)", reasonType);
                    amount = Math.min(amount, defendingTown.getHoldingBalance());
                    defendingTown.payTo(amount, attackingResident, reason);
                    // Message
                    moneyTranserMsg = String.format(TownySettings.getLangString("msg_enemy_war_area_won_pillage"), attackingResident.getFormattedName(), TownyEconomyHandler.getFormattedBalance(amount), defendingTown.getFormattedName());
                } else if (amount < 0) {
                    // Attacker -> Defending Town (Rebuild cost)
                    // Inverse the amount so it's positive.
                    amount = -amount;
                    String reason = String.format("War - Won Enemy %s (Rebuild Cost)", reasonType);
                    if (!attackingResident.payTo(amount, defendingTown, reason)) {
                        // Could Not Pay Defending Town the Rebuilding Cost.
                        TownyMessaging.sendGlobalMessage(String.format(TownySettings.getLangString("msg_enemy_war_area_won"), attackingResident.getFormattedName(), (attackingNation.hasTag() ? attackingNation.getTag() : attackingNation.getFormattedName()), cell.getCellString()));
                    }
                    // Message
                    moneyTranserMsg = String.format(TownySettings.getLangString("msg_enemy_war_area_won_rebuilding"), attackingResident.getFormattedName(), TownyEconomyHandler.getFormattedBalance(amount), defendingTown.getFormattedName());
                }
            } catch (EconomyException x) {
                x.printStackTrace();
            }
        }
        // Defender loses townblock
        TownyUniverse.getDataSource().removeTownBlock(townBlock);
        // Attacker Claim Automatically
        try {
            List<WorldCoord> selection = new ArrayList<WorldCoord>();
            selection.add(worldCoord);
            TownCommand.checkIfSelectionIsValid(attackingTown, selection, false, 0, false);
            new TownClaim(plugin, null, attackingTown, selection, false, true, false).start();
        } catch (TownyException te) {
        // Couldn't claim it.
        }
        // Cleanup
        plugin.updateCache(worldCoord);
        // Event Message
        TownyMessaging.sendGlobalMessage(String.format(TownySettings.getLangString("msg_enemy_war_area_won"), attackingResident.getFormattedName(), (attackingNation.hasTag() ? attackingNation.getTag() : attackingNation.getFormattedName()), cell.getCellString()));
        // Money Transfer message.
        if (TownySettings.isUsingEconomy()) {
            if (amount != 0 && moneyTranserMsg != null) {
                try {
                    TownyMessaging.sendResidentMessage(attackingResident, moneyTranserMsg);
                } catch (TownyException e) {
                }
                TownyMessaging.sendTownMessage(defendingTown, moneyTranserMsg);
            }
        }
    } catch (NotRegisteredException e) {
        e.printStackTrace();
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) ArrayList(java.util.ArrayList) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) CellUnderAttack(com.palmergames.bukkit.towny.war.flagwar.CellUnderAttack) TownClaim(com.palmergames.bukkit.towny.tasks.TownClaim) EventHandler(org.bukkit.event.EventHandler)

Example 70 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