Search in sources :

Example 11 with EconomyException

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

the class ResidentCommand method residentSpawn.

/**
	 * Attempt to send player to bed spawn.
	 * 
	 * @param player
	 */
public void residentSpawn(Player player) {
    boolean isTownyAdmin = TownyUniverse.getPermissionSource().isTownyAdmin(player);
    Resident resident;
    try {
        resident = TownyUniverse.getDataSource().getResident(player.getName());
        Town town;
        Location spawnLoc;
        String notAffordMSG;
        TownSpawnLevel townSpawnPermission;
        // Set target town and affiliated messages.
        town = resident.getTown();
        notAffordMSG = TownySettings.getLangString("msg_err_cant_afford_tp");
        if (TownySettings.getBedUse() && player.getBedSpawnLocation() != null) {
            spawnLoc = player.getBedSpawnLocation();
        } else {
            spawnLoc = town.getSpawn();
        }
        if (isTownyAdmin) {
            townSpawnPermission = TownSpawnLevel.ADMIN;
        } else {
            townSpawnPermission = TownSpawnLevel.TOWN_RESIDENT;
        }
        if (!isTownyAdmin) {
            // Prevent spawn travel while in disallowed zones (if
            // configured)
            List<String> disallowedZones = TownySettings.getDisallowedTownSpawnZones();
            if (!disallowedZones.isEmpty()) {
                String inTown = null;
                try {
                    Location loc = plugin.getCache(player).getLastLocation();
                    inTown = TownyUniverse.getTownName(loc);
                } catch (NullPointerException e) {
                    inTown = TownyUniverse.getTownName(player.getLocation());
                }
                if (inTown == null && disallowedZones.contains("unclaimed"))
                    throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "the Wilderness"));
                if (inTown != null && resident.hasNation() && TownyUniverse.getDataSource().getTown(inTown).hasNation()) {
                    Nation inNation = TownyUniverse.getDataSource().getTown(inTown).getNation();
                    Nation playerNation = resident.getTown().getNation();
                    if (inNation.hasEnemy(playerNation) && disallowedZones.contains("enemy"))
                        throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "Enemy areas"));
                    if (!inNation.hasAlly(playerNation) && !inNation.hasEnemy(playerNation) && disallowedZones.contains("neutral"))
                        throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "Neutral towns"));
                }
            }
        }
        double travelCost = townSpawnPermission.getCost();
        // Check if need/can pay
        if (travelCost > 0 && TownySettings.isUsingEconomy() && (resident.getHoldingBalance() < travelCost))
            throw new TownyException(notAffordMSG);
        // Used later to make sure the chunk we teleport to is loaded.
        Chunk chunk = spawnLoc.getChunk();
        // Essentials tests
        boolean UsingESS = plugin.isEssentials();
        if (UsingESS && !isTownyAdmin) {
            try {
                User user = plugin.getEssentials().getUser(player);
                if (!user.isJailed()) {
                    Teleport teleport = user.getTeleport();
                    if (!chunk.isLoaded())
                        chunk.load();
                    // Cause an essentials exception if in cooldown.
                    teleport.cooldown(true);
                    teleport.teleport(spawnLoc, null);
                }
            } catch (Exception e) {
                TownyMessaging.sendErrorMsg(player, "Error: " + e.getMessage());
                // cooldown?
                return;
            }
        }
        // travel.
        if (travelCost > 0 && TownySettings.isUsingEconomy() && resident.payTo(travelCost, town, String.format("Resident Spawn (%s)", townSpawnPermission))) {
            // +
            TownyMessaging.sendMsg(player, String.format(TownySettings.getLangString("msg_cost_spawn"), TownyEconomyHandler.getFormattedBalance(travelCost)));
        // TownyEconomyObject.getEconomyCurrency()));
        }
        // If an Admin or Essentials teleport isn't being used, use our own.
        if (isTownyAdmin) {
            if (player.getVehicle() != null)
                player.getVehicle().eject();
            if (!chunk.isLoaded())
                chunk.load();
            player.teleport(spawnLoc, TeleportCause.COMMAND);
            return;
        }
        if (!UsingESS) {
            if (TownyTimerHandler.isTeleportWarmupRunning()) {
                // Use teleport warmup
                player.sendMessage(String.format(TownySettings.getLangString("msg_town_spawn_warmup"), TownySettings.getTeleportWarmupTime()));
                plugin.getTownyUniverse().requestTeleport(player, spawnLoc, travelCost);
            } else {
                // Don't use teleport warmup
                if (player.getVehicle() != null)
                    player.getVehicle().eject();
                if (!chunk.isLoaded())
                    chunk.load();
                player.teleport(spawnLoc, TeleportCause.COMMAND);
            }
        }
    } catch (TownyException e) {
        TownyMessaging.sendErrorMsg(player, e.getMessage());
    } catch (EconomyException e) {
        TownyMessaging.sendErrorMsg(player, e.getMessage());
    }
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) TownSpawnLevel(com.palmergames.bukkit.towny.object.TownSpawnLevel) User(com.earth2me.essentials.User) Chunk(org.bukkit.Chunk) Teleport(com.earth2me.essentials.Teleport) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) Town(com.palmergames.bukkit.towny.object.Town) Resident(com.palmergames.bukkit.towny.object.Resident) Location(org.bukkit.Location)

Example 12 with EconomyException

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

the class OnPlayerLogin method warningMessage.

/**
	 * Send a warning message if the town or nation is due to be deleted.
	 * 
	 * @param resident
	 */
private void warningMessage(Resident resident) {
    if (TownyEconomyHandler.isActive() && TownySettings.isTaxingDaily()) {
        if (resident.hasTown()) {
            try {
                Town town = resident.getTown();
                if (town.hasUpkeep()) {
                    double upkeep = TownySettings.getTownUpkeepCost(town);
                    try {
                        if ((upkeep > 0) && (!town.canPayFromHoldings(upkeep))) {
                            /*
								 *  Warn that the town is due to be deleted.
								 */
                            TownyMessaging.sendMessage(resident, String.format(TownySettings.getLangString("msg_warning_delete"), town.getName()));
                        }
                    } catch (EconomyException ex) {
                    // Economy error, so ignore it and try to continue.
                    }
                }
                if (town.hasNation()) {
                    Nation nation = town.getNation();
                    double upkeep = TownySettings.getNationUpkeepCost(nation);
                    try {
                        if ((upkeep > 0) && (!nation.canPayFromHoldings(upkeep))) {
                            /*
								 *  Warn that the nation is due to be deleted.
								 */
                            TownyMessaging.sendMessage(resident, String.format(TownySettings.getLangString("msg_warning_delete"), nation.getName()));
                        }
                    } catch (EconomyException ex) {
                    // Economy error, so ignore it and try to continue.
                    }
                }
            } catch (NotRegisteredException ex) {
            // Should never reach here as we tested it beforehand.
            }
        }
    }
}
Also used : EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) Nation(com.palmergames.bukkit.towny.object.Nation) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Town(com.palmergames.bukkit.towny.object.Town)

Example 13 with EconomyException

use of com.palmergames.bukkit.towny.exceptions.EconomyException 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 14 with EconomyException

use of com.palmergames.bukkit.towny.exceptions.EconomyException 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 15 with EconomyException

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

Aggregations

EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)16 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)13 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)9 Nation (com.palmergames.bukkit.towny.object.Nation)8 Resident (com.palmergames.bukkit.towny.object.Resident)5 Town (com.palmergames.bukkit.towny.object.Town)5 ArrayList (java.util.ArrayList)3 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)2 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)2 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)2 CellUnderAttack (com.palmergames.bukkit.towny.war.flagwar.CellUnderAttack)2 InvalidNameException (javax.naming.InvalidNameException)2 Player (org.bukkit.entity.Player)2 EventHandler (org.bukkit.event.EventHandler)2 Teleport (com.earth2me.essentials.Teleport)1 User (com.earth2me.essentials.User)1 PlotClearEvent (com.palmergames.bukkit.towny.event.PlotClearEvent)1 EmptyNationException (com.palmergames.bukkit.towny.exceptions.EmptyNationException)1 TownBlockOwner (com.palmergames.bukkit.towny.object.TownBlockOwner)1 TownSpawnLevel (com.palmergames.bukkit.towny.object.TownSpawnLevel)1