Search in sources :

Example 1 with SiegeWarStartEvent

use of com.gmail.goosius.siegewar.events.SiegeWarStartEvent 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)

Aggregations

SiegeWarStartEvent (com.gmail.goosius.siegewar.events.SiegeWarStartEvent)1 Siege (com.gmail.goosius.siegewar.objects.Siege)1 Nation (com.palmergames.bukkit.towny.object.Nation)1