Search in sources :

Example 1 with Siege

use of com.gmail.goosius.siegewar.objects.Siege in project SiegeWar by TownyAdvanced.

the class SiegeController method startSiege.

/**
 * Start a siege
 *
 * @param bannerBlock banner block
 * @param siegeType the siege type
 * @param targetTown the target town
 * @param attacker the attacking government
 * @param defender the defending government
 * @param townOfSiegeStarter the town of the siege starter
 * @param useWarchest true if warchest should be used
 */
public static void startSiege(Block bannerBlock, SiegeType siegeType, Town targetTown, Government attacker, Government defender, Town townOfSiegeStarter, boolean useWarchest) {
    // Create Siege
    SiegeController.newSiege(targetTown);
    Siege siege = SiegeController.getSiege(targetTown);
    // Set values in siege object
    siege.setSiegeType(siegeType);
    siege.setTown(targetTown);
    siege.setAttacker(attacker);
    siege.setDefender(defender);
    siege.setStatus(SiegeStatus.IN_PROGRESS);
    siege.setTownPlundered(false);
    siege.setTownInvaded(false);
    siege.setStartTime(System.currentTimeMillis());
    siege.setScheduledEndTime((System.currentTimeMillis() + ((long) (SiegeWarSettings.getWarSiegeMaxHoldoutTimeHours() * TimeMgmt.ONE_HOUR_IN_MILLIS))));
    siege.setActualEndTime(0);
    siege.setFlagLocation(bannerBlock.getLocation());
    SiegeController.setSiege(targetTown, true);
    SiegeController.putTownInSiegeMap(targetTown, siege);
    sendGlobalSiegeStartMessage(siege);
    // Pay into warchest
    if (useWarchest) {
        siege.setWarChestAmount(SiegeWarMoneyUtil.calculateSiegeCost(targetTown));
        if (TownyEconomyHandler.isActive()) {
            // Pay upfront cost into warchest now
            attacker.getAccount().withdraw(siege.getWarChestAmount(), "Cost of starting a siege.");
            String moneyMessage = Translation.of("msg_siege_war_attack_pay_war_chest", attacker.getName(), TownyEconomyHandler.getFormattedBalance(siege.getWarChestAmount()));
            TownyMessaging.sendPrefixedNationMessage((Nation) attacker, moneyMessage);
            if (defender instanceof Nation) {
                TownyMessaging.sendPrefixedNationMessage((Nation) defender, moneyMessage);
            } else {
                TownyMessaging.sendPrefixedTownMessage((Town) defender, moneyMessage);
            }
        }
    } else {
        siege.setWarChestAmount(0);
    }
    // Save to DB
    SiegeController.saveSiege(siege);
    // Call event
    Bukkit.getPluginManager().callEvent(new SiegeWarStartEvent(siege, townOfSiegeStarter));
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) SiegeWarStartEvent(com.gmail.goosius.siegewar.events.SiegeWarStartEvent) Siege(com.gmail.goosius.siegewar.objects.Siege)

Example 2 with Siege

use of com.gmail.goosius.siegewar.objects.Siege in project SiegeWar by TownyAdvanced.

the class SiegeController method newSiege.

public static void newSiege(Town town) {
    Siege siege = new Siege(town);
    townSiegeMap.put(town.getUUID(), siege);
    siegedTowns.add(town);
    siegedTownNames.add(town.getName());
}
Also used : Siege(com.gmail.goosius.siegewar.objects.Siege)

Example 3 with Siege

use of com.gmail.goosius.siegewar.objects.Siege in project SiegeWar by TownyAdvanced.

the class SiegeWar method cleanupBattleSession.

/**
 * Cleans up the battle session, if it did not exit properly when the plugin shut down.
 */
private void cleanupBattleSession() {
    if (siegeWarPluginError) {
        severe("SiegeWar is in safe mode. Battle Session Cleanup not attempted.");
    } else {
        // Find any sieges with unresolved battles
        List<Siege> siegesWithUnresolvedBattles = new ArrayList<>();
        for (Siege siege : SiegeController.getSieges()) {
            if (siege.getStatus() == SiegeStatus.IN_PROGRESS && (siege.getAttackerBattlePoints() > 0 || siege.getDefenderBattlePoints() > 0)) {
                siegesWithUnresolvedBattles.add(siege);
            }
        }
        // Resolve battles
        if (siegesWithUnresolvedBattles.size() > 0) {
            info(Translation.of("msg.battle.session.cleanup.starting"));
            int numBattlesUpdated = 0;
            for (Siege siege : siegesWithUnresolvedBattles) {
                siege.adjustSiegeBalance(siege.getAttackerBattlePoints() - siege.getDefenderBattlePoints());
                siege.setAttackerBattlePoints(0);
                siege.setDefenderBattlePoints(0);
                SiegeController.saveSiege(siege);
                numBattlesUpdated++;
            }
            info(Translation.of("msg.battle.session.cleanup.complete", numBattlesUpdated));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Siege(com.gmail.goosius.siegewar.objects.Siege)

Example 4 with Siege

use of com.gmail.goosius.siegewar.objects.Siege in project SiegeWar by TownyAdvanced.

the class SiegeWarBukkitEventListener method onPlayerConsume.

/*
	 * SW will prevent someone in a banner area from curing their poisoning with milk.
	 */
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerConsume(PlayerItemConsumeEvent event) {
    if (SiegeWarSettings.getWarSiegeEnabled()) {
        try {
            // Prevent milk bucket usage while attempting to gain banner control
            if (event.getItem().getType() == Material.MILK_BUCKET) {
                for (Siege siege : SiegeController.getSieges()) {
                    if (siege.getBannerControlSessions().containsKey(event.getPlayer())) {
                        event.setCancelled(true);
                        Messaging.sendErrorMsg(event.getPlayer(), Translatable.of("msg_war_siege_zone_milk_bucket_forbidden_while_attempting_banner_control"));
                    }
                }
            }
        } catch (Exception e) {
            SiegeWar.severe("Problem evaluating siege player consume event");
            e.printStackTrace();
        }
    }
}
Also used : Siege(com.gmail.goosius.siegewar.objects.Siege) EventHandler(org.bukkit.event.EventHandler)

Example 5 with Siege

use of com.gmail.goosius.siegewar.objects.Siege in project SiegeWar by TownyAdvanced.

the class CannonsListener method cannonFireEvent.

/**
 * Process a Cannon Fire Event, where a player fires a cannon
 *
 * @param event the event
 */
@EventHandler
public void cannonFireEvent(CannonFireEvent event) {
    // Generate new breach points, if appropriate
    if (!event.isCancelled() && SiegeWar.isCannonsPluginInstalled() && SiegeWarSettings.getWarSiegeEnabled() && TownyAPI.getInstance().getTownyWorld(event.getCannon().getLocation().getWorld()).isWarAllowed() && SiegeWarSettings.isWallBreachingEnabled() && SiegeWarSettings.isWallBreachingCannonsIntegrationEnabled() && SiegeWarDistanceUtil.isLocationInActiveSiegeZone(event.getCannon().getLocation())) {
        // Ensure a battle session is active
        Player gunnerPlayer = BukkitTools.getPlayer(event.getPlayer());
        if (!BattleSession.getBattleSession().isActive()) {
            Messaging.sendErrorMsg(gunnerPlayer, Translatable.of("msg_err_cannot_fire_cannon_without_battle_session"));
            event.setCancelled(true);
            // Cannon fire was cancelled
            return;
        }
        // Ensure cannon is fully in the wilderness OR the besieged town
        Siege siege = SiegeController.getSiegeAtLocation(event.getCannon().getLocation());
        for (Location cannonBlockLocation : event.getCannon().getCannonDesign().getAllCannonBlocks(event.getCannon())) {
            if (!TownyAPI.getInstance().isWilderness(cannonBlockLocation) && !TownyAPI.getInstance().getTown(cannonBlockLocation).equals(siege.getTown())) {
                Messaging.sendErrorMsg(gunnerPlayer, Translatable.of("msg_err_cannon_must_be_in_wilderness_or_besieged_town"));
                event.setCancelled(true);
                // Cannon fire was cancelled
                return;
            }
        }
        // Ensure player has perms to fire, and there are enough breach points
        if (CannonsIntegration.canPlayerUseBreachPointsByCannon(gunnerPlayer, siege)) {
            // Player can fire cannon
            return;
        } else if (CannonsIntegration.canPlayerGenerateBreachPointsByCannon(gunnerPlayer, siege)) {
            // If the player has not already fired this tick, breach points are generated.
            if (!siege.getRecentTownFriendlyCannonFirers().contains(gunnerPlayer)) {
                double wallBreachPointsIncrease = SiegeWarSettings.getWallBreachingCannonFirePointGenerationRate() * siege.getTown().getTownBlocks().size();
                siege.increaseWallBreachPointsToCap(wallBreachPointsIncrease);
                siege.addRecentTownFriendlyCannonFirer(gunnerPlayer);
            }
            // Player can fire cannon
            return;
        } else {
            Messaging.sendErrorMsg(gunnerPlayer, Translatable.of("msg_err_cannot_fire_cannon_without_perms"));
            event.setCancelled(true);
            // Cannon fire was cancelled
            return;
        }
    }
}
Also used : Player(org.bukkit.entity.Player) Siege(com.gmail.goosius.siegewar.objects.Siege) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Siege (com.gmail.goosius.siegewar.objects.Siege)22 Resident (com.palmergames.bukkit.towny.object.Resident)7 Town (com.palmergames.bukkit.towny.object.Town)7 Translator (com.palmergames.bukkit.towny.object.Translator)6 EventHandler (org.bukkit.event.EventHandler)5 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)4 Nation (com.palmergames.bukkit.towny.object.Nation)4 ArrayList (java.util.ArrayList)4 TextComponent (net.md_5.bungee.api.chat.TextComponent)3 Location (org.bukkit.Location)3 Player (org.bukkit.entity.Player)3 Material (org.bukkit.Material)2 Block (org.bukkit.block.Block)2 SiegeSide (com.gmail.goosius.siegewar.enums.SiegeSide)1 SiegeStatus (com.gmail.goosius.siegewar.enums.SiegeStatus)1 BattleSessionEndedEvent (com.gmail.goosius.siegewar.events.BattleSessionEndedEvent)1 SiegeWarStartEvent (com.gmail.goosius.siegewar.events.SiegeWarStartEvent)1 BattleSession (com.gmail.goosius.siegewar.objects.BattleSession)1 TextComponent (com.palmergames.adventure.text.TextComponent)1 TownyUniverse (com.palmergames.bukkit.towny.TownyUniverse)1