Search in sources :

Example 1 with CellUnderAttack

use of com.palmergames.bukkit.towny.war.flagwar.CellUnderAttack in project Towny by ElgarL.

the class TownyWarCustomListener method onCellAttackEvent.

@EventHandler(priority = EventPriority.LOWEST)
public void onCellAttackEvent(CellAttackEvent event) {
    try {
        CellUnderAttack cell = event.getData();
        TownyWar.registerAttack(cell);
    } catch (Exception e) {
        event.setCancelled(true);
        event.setReason(e.getMessage());
    }
}
Also used : CellUnderAttack(com.palmergames.bukkit.towny.war.flagwar.CellUnderAttack) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EventHandler(org.bukkit.event.EventHandler)

Example 2 with CellUnderAttack

use of com.palmergames.bukkit.towny.war.flagwar.CellUnderAttack 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 3 with CellUnderAttack

use of com.palmergames.bukkit.towny.war.flagwar.CellUnderAttack in project Towny by ElgarL.

the class TownyWarCustomListener method onCellAttackCanceledEvent.

@EventHandler(priority = EventPriority.LOWEST)
public void onCellAttackCanceledEvent(CellAttackCanceledEvent event) {
    CellUnderAttack cell = event.getCell();
    TownyUniverse universe = plugin.getTownyUniverse();
    WorldCoord worldCoord = new WorldCoord(cell.getWorldName(), cell.getX(), cell.getZ());
    universe.removeWarZone(worldCoord);
    plugin.updateCache(worldCoord);
    System.out.println(cell.getCellString());
}
Also used : CellUnderAttack(com.palmergames.bukkit.towny.war.flagwar.CellUnderAttack) EventHandler(org.bukkit.event.EventHandler)

Example 4 with CellUnderAttack

use of com.palmergames.bukkit.towny.war.flagwar.CellUnderAttack 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

CellUnderAttack (com.palmergames.bukkit.towny.war.flagwar.CellUnderAttack)4 EventHandler (org.bukkit.event.EventHandler)4 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)3 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)3 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)3 TownClaim (com.palmergames.bukkit.towny.tasks.TownClaim)1 ArrayList (java.util.ArrayList)1 Player (org.bukkit.entity.Player)1