Search in sources :

Example 1 with EconomyException

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

the class War method end.

public void end() {
    for (Player player : BukkitTools.getOnlinePlayers()) if (player != null)
        sendStats(player);
    double halfWinnings;
    try {
        // Transactions might leave 1 coin. (OH noez!)
        halfWinnings = getWarSpoils().getHoldingBalance() / 2.0;
        try {
            // Again, might leave residue.
            double nationWinnings = halfWinnings / warringNations.size();
            for (Nation winningNation : warringNations) {
                getWarSpoils().payTo(nationWinnings, winningNation, "War - Nation Winnings");
                TownyMessaging.sendGlobalMessage("Winning Nation: " + winningNation.getName() + " won " + TownyEconomyHandler.getFormattedBalance(nationWinnings) + ".");
            }
        } catch (ArithmeticException e) {
        // A war ended with 0 nations.
        }
        try {
            KeyValue<Town, Integer> winningTownScore = getWinningTownScore();
            getWarSpoils().payTo(halfWinnings, winningTownScore.key, "War - Town Winnings");
            TownyMessaging.sendGlobalMessage("Highest Score: " + winningTownScore.key.getName() + " won " + TownyEconomyHandler.getFormattedBalance(halfWinnings) + " with the score " + winningTownScore.value + ".");
        } catch (TownyException e) {
        }
    } catch (EconomyException e1) {
    }
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) Player(org.bukkit.entity.Player) Town(com.palmergames.bukkit.towny.object.Town) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 2 with EconomyException

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

the class War method start.

public void start() {
    warTime = true;
    // Seed spoils of war
    try {
        warSpoils.pay(TownySettings.getBaseSpoilsOfWar(), "Start of War - Base Spoils");
        TownyMessaging.sendMsg("[War] Seeding spoils of war with " + TownySettings.getBaseSpoilsOfWar());
    } catch (EconomyException e) {
        TownyMessaging.sendErrorMsg("[War] Could not seed spoils of war.");
    }
    //Gather all nations at war
    for (Nation nation : TownyUniverse.getDataSource().getNations()) {
        if (!nation.isNeutral()) {
            add(nation);
            TownyMessaging.sendGlobalMessage(String.format(TownySettings.getLangString("msg_war_join_nation"), nation.getName()));
        } else if (!TownySettings.isDeclaringNeutral()) {
            try {
                nation.setNeutral(false);
                add(nation);
                TownyMessaging.sendGlobalMessage(String.format(TownySettings.getLangString("msg_war_join_forced"), nation.getName()));
            } catch (TownyException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    //warTimer.scheduleAtFixedRate(new WarTimerTask(this), 0, 1000);
    int id = BukkitTools.scheduleAsyncRepeatingTask(new WarTimerTask(plugin, this), 0, TimeTools.convertToTicks(5));
    if (id == -1) {
        TownyMessaging.sendErrorMsg("Could not schedule war event loop.");
        end();
    } else
        addTaskId(id);
    checkEnd();
}
Also used : EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) Nation(com.palmergames.bukkit.towny.object.Nation) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 3 with EconomyException

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

the class DailyTimerTask method run.

@Override
public void run() {
    long start = System.currentTimeMillis();
    TownyMessaging.sendDebugMsg("New Day");
    // Collect taxes
    if (TownyEconomyHandler.isActive() && TownySettings.isTaxingDaily()) {
        TownyMessaging.sendGlobalMessage(String.format(TownySettings.getLangString("msg_new_day_tax")));
        try {
            TownyMessaging.sendDebugMsg("Collecting Town Taxes");
            collectTownTaxes();
            TownyMessaging.sendDebugMsg("Collecting Nation Taxes");
            collectNationTaxes();
            TownyMessaging.sendDebugMsg("Collecting Town Costs");
            collectTownCosts();
            TownyMessaging.sendDebugMsg("Collecting Nation Costs");
            collectNationCosts();
        } catch (EconomyException e) {
        } catch (TownyException e) {
            // TODO king exception
            e.printStackTrace();
        }
    } else
        TownyMessaging.sendGlobalMessage(String.format(TownySettings.getLangString("msg_new_day")));
    // Automatically delete old residents
    if (TownySettings.isDeletingOldResidents()) {
        // Run a purge in it's own thread
        new ResidentPurge(plugin, null, TownySettings.getDeleteTime() * 1000).start();
    }
    // Backups
    TownyMessaging.sendDebugMsg("Cleaning up old backups.");
    TownyUniverse.getDataSource().cleanupBackups();
    if (TownySettings.isBackingUpDaily())
        try {
            TownyMessaging.sendDebugMsg("Making backup.");
            TownyUniverse.getDataSource().backup();
        } catch (IOException e) {
            TownyMessaging.sendErrorMsg("Could not create backup.");
            e.printStackTrace();
        }
    TownyMessaging.sendDebugMsg("Finished New Day Code");
    TownyMessaging.sendDebugMsg("Universe Stats:");
    TownyMessaging.sendDebugMsg("    Residents: " + TownyUniverse.getDataSource().getResidents().size());
    TownyMessaging.sendDebugMsg("    Towns: " + TownyUniverse.getDataSource().getTowns().size());
    TownyMessaging.sendDebugMsg("    Nations: " + TownyUniverse.getDataSource().getNations().size());
    for (TownyWorld world : TownyUniverse.getDataSource().getWorlds()) TownyMessaging.sendDebugMsg("    " + world.getName() + " (townblocks): " + world.getTownBlocks().size());
    TownyMessaging.sendDebugMsg("Memory (Java Heap):");
    TownyMessaging.sendDebugMsg(String.format("%8d Mb (max)", Runtime.getRuntime().maxMemory() / 1024 / 1024));
    TownyMessaging.sendDebugMsg(String.format("%8d Mb (total)", Runtime.getRuntime().totalMemory() / 1024 / 1024));
    TownyMessaging.sendDebugMsg(String.format("%8d Mb (free)", Runtime.getRuntime().freeMemory() / 1024 / 1024));
    TownyMessaging.sendDebugMsg(String.format("%8d Mb (used=total-free)", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024));
    TownyMessaging.sendDebugMsg("newDay took " + (System.currentTimeMillis() - start) + "ms");
}
Also used : EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) IOException(java.io.IOException) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 4 with EconomyException

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

the class NationCommand method nationWithdraw.

private void nationWithdraw(Player player, int amount) {
    Resident resident;
    Nation nation;
    try {
        if (!TownySettings.geNationBankAllowWithdrawls())
            throw new TownyException(TownySettings.getLangString("msg_err_withdraw_disabled"));
        if (amount < 0)
            // TODO
            throw new TownyException(TownySettings.getLangString("msg_err_negative_money"));
        resident = TownyUniverse.getDataSource().getResident(player.getName());
        nation = resident.getTown().getNation();
        nation.withdrawFromBank(resident, amount);
        TownyMessaging.sendNationMessage(nation, String.format(TownySettings.getLangString("msg_xx_withdrew_xx"), resident.getName(), amount, "nation"));
    } catch (TownyException x) {
        TownyMessaging.sendErrorMsg(player, x.getMessage());
    } catch (EconomyException x) {
        TownyMessaging.sendErrorMsg(player, x.getMessage());
    }
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) Resident(com.palmergames.bukkit.towny.object.Resident) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 5 with EconomyException

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

the class PlotCommand method parsePlotCommand.

public boolean parsePlotCommand(Player player, String[] split) throws TownyException {
    if (split.length == 0 || split[0].equalsIgnoreCase("?")) {
        for (String line : output) player.sendMessage(line);
    } else {
        Resident resident;
        String world;
        try {
            resident = TownyUniverse.getDataSource().getResident(player.getName());
            world = player.getWorld().getName();
        //resident.getTown();
        } catch (TownyException x) {
            TownyMessaging.sendErrorMsg(player, x.getMessage());
            return true;
        }
        try {
            if (split[0].equalsIgnoreCase("claim")) {
                if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_CLAIM.getNode()))
                    throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                if (TownyUniverse.isWarTime())
                    throw new TownyException(TownySettings.getLangString("msg_war_cannot_do"));
                List<WorldCoord> selection = AreaSelectionUtil.selectWorldCoordArea(resident, new WorldCoord(world, Coord.parseCoord(player)), StringMgmt.remFirstArg(split));
                if (selection.size() > 0) {
                    double cost = 0;
                    // tally up costs.
                    for (WorldCoord worldCoord : new ArrayList<WorldCoord>(selection)) {
                        try {
                            double price = worldCoord.getTownBlock().getPlotPrice();
                            if (price > -1)
                                cost += worldCoord.getTownBlock().getPlotPrice();
                            else {
                                if (// ||
                                !worldCoord.getTownBlock().getTown().isMayor(resident))
                                    // worldCoord.getTownBlock().getTown().hasAssistant(resident))
                                    selection.remove(worldCoord);
                            }
                        } catch (NotRegisteredException e) {
                            selection.remove(worldCoord);
                        }
                    }
                    int maxPlots = TownySettings.getMaxResidentPlots(resident);
                    if (maxPlots >= 0 && resident.getTownBlocks().size() + selection.size() > maxPlots)
                        throw new TownyException(String.format(TownySettings.getLangString("msg_max_plot_own"), maxPlots));
                    if (TownySettings.isUsingEconomy() && (!resident.canPayFromHoldings(cost)))
                        throw new TownyException(String.format(TownySettings.getLangString("msg_no_funds_claim"), selection.size(), TownyEconomyHandler.getFormattedBalance(cost)));
                    // Start the claim task
                    new PlotClaim(plugin, player, resident, selection, true).start();
                } else {
                    player.sendMessage(TownySettings.getLangString("msg_err_empty_area_selection"));
                }
            } else if (split[0].equalsIgnoreCase("unclaim")) {
                if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_UNCLAIM.getNode()))
                    throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                if (TownyUniverse.isWarTime())
                    throw new TownyException(TownySettings.getLangString("msg_war_cannot_do"));
                if (split.length == 2 && split[1].equalsIgnoreCase("all")) {
                    // Start the unclaim task
                    new PlotClaim(plugin, player, resident, null, false).start();
                } else {
                    List<WorldCoord> selection = AreaSelectionUtil.selectWorldCoordArea(resident, new WorldCoord(world, Coord.parseCoord(player)), StringMgmt.remFirstArg(split));
                    selection = AreaSelectionUtil.filterOwnedBlocks(resident, selection);
                    if (selection.size() > 0) {
                        // Start the unclaim task
                        new PlotClaim(plugin, player, resident, selection, false).start();
                    } else {
                        player.sendMessage(TownySettings.getLangString("msg_err_empty_area_selection"));
                    }
                }
            } else if (split[0].equalsIgnoreCase("notforsale") || split[0].equalsIgnoreCase("nfs")) {
                if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_NOTFORSALE.getNode()))
                    throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                List<WorldCoord> selection = AreaSelectionUtil.selectWorldCoordArea(resident, new WorldCoord(world, Coord.parseCoord(player)), StringMgmt.remFirstArg(split));
                selection = AreaSelectionUtil.filterOwnedBlocks(resident.getTown(), selection);
                for (WorldCoord worldCoord : selection) {
                    setPlotForSale(resident, worldCoord, -1);
                }
            } else if (split[0].equalsIgnoreCase("forsale") || split[0].equalsIgnoreCase("fs")) {
                if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_FORSALE.getNode()))
                    throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                WorldCoord pos = new WorldCoord(world, Coord.parseCoord(player));
                double plotPrice = pos.getTownBlock().getTown().getPlotTypePrice(pos.getTownBlock().getType());
                if (split.length > 1) {
                    int areaSelectPivot = AreaSelectionUtil.getAreaSelectPivot(split);
                    List<WorldCoord> selection;
                    if (areaSelectPivot >= 0) {
                        selection = AreaSelectionUtil.selectWorldCoordArea(resident, new WorldCoord(world, Coord.parseCoord(player)), StringMgmt.subArray(split, areaSelectPivot + 1, split.length));
                        selection = AreaSelectionUtil.filterOwnedBlocks(resident.getTown(), selection);
                        if (selection.size() == 0) {
                            player.sendMessage(TownySettings.getLangString("msg_err_empty_area_selection"));
                            return true;
                        }
                    } else {
                        selection = new ArrayList<WorldCoord>();
                        selection.add(pos);
                    }
                    // Check that it's not: /plot forsale within rect 3
                    if (areaSelectPivot != 1) {
                        try {
                            // command was 'plot fs $'
                            plotPrice = Double.parseDouble(split[1]);
                            if (plotPrice < 0) {
                                TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_negative_money"));
                                return true;
                            }
                        } catch (NumberFormatException e) {
                            player.sendMessage(String.format(TownySettings.getLangString("msg_error_must_be_num")));
                            return true;
                        }
                    }
                    for (WorldCoord worldCoord : selection) {
                        if (selection.size() > 1)
                            plotPrice = worldCoord.getTownBlock().getTown().getPlotTypePrice(worldCoord.getTownBlock().getType());
                        setPlotForSale(resident, worldCoord, plotPrice);
                    }
                } else {
                    // basic 'plot fs' command
                    setPlotForSale(resident, pos, plotPrice);
                }
            } else if (split[0].equalsIgnoreCase("perm")) {
                if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_PERM.getNode()))
                    throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                TownBlock townBlock = new WorldCoord(world, Coord.parseCoord(player)).getTownBlock();
                TownyMessaging.sendMessage(player, TownyFormatter.getStatus(townBlock));
            } else if (split[0].equalsIgnoreCase("toggle")) {
                /*
					 * perm test in the plottoggle.
					 */
                TownBlock townBlock = new WorldCoord(world, Coord.parseCoord(player)).getTownBlock();
                // Test we are allowed to work on this plot
                // ignore the return as
                plotTestOwner(resident, townBlock);
                // we are only checking
                // for an exception
                plotToggle(player, new WorldCoord(world, Coord.parseCoord(player)).getTownBlock(), StringMgmt.remFirstArg(split));
            } else if (split[0].equalsIgnoreCase("set")) {
                split = StringMgmt.remFirstArg(split);
                if (split.length > 0) {
                    if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_SET.getNode(split[0].toLowerCase())))
                        throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                    if (split[0].equalsIgnoreCase("perm")) {
                        // Set plot level permissions (if the plot owner) or
                        // Mayor/Assistant of the town.
                        TownBlock townBlock = new WorldCoord(world, Coord.parseCoord(player)).getTownBlock();
                        // Test we are allowed to work on this plot
                        TownBlockOwner owner = plotTestOwner(resident, townBlock);
                        // Check we are allowed to set these perms
                        toggleTest(player, townBlock, StringMgmt.join(StringMgmt.remFirstArg(split), ""));
                        setTownBlockPermissions(player, owner, townBlock, StringMgmt.remFirstArg(split));
                        return true;
                    } else if (split[0].equalsIgnoreCase("name")) {
                        TownBlock townBlock = new WorldCoord(world, Coord.parseCoord(player)).getTownBlock();
                        // Test we are allowed to work on this plot
                        plotTestOwner(resident, townBlock);
                        if (split.length == 1) {
                            townBlock.setName("");
                            TownyMessaging.sendMsg(player, String.format("Plot name removed"));
                            TownyUniverse.getDataSource().saveTownBlock(townBlock);
                            return true;
                        }
                        // Test if the plot name contains invalid characters.
                        if (!NameValidation.isBlacklistName(split[1])) {
                            townBlock.setName(StringMgmt.join(StringMgmt.remFirstArg(split), ""));
                            //townBlock.setChanged(true);
                            TownyUniverse.getDataSource().saveTownBlock(townBlock);
                            TownyMessaging.sendMsg(player, String.format("Plot name set to [%s]", townBlock.getName()));
                        } else {
                            TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_invalid_name"));
                        }
                        return true;
                    }
                    WorldCoord worldCoord = new WorldCoord(world, Coord.parseCoord(player));
                    setPlotType(resident, worldCoord, split[0]);
                    player.sendMessage(String.format(TownySettings.getLangString("msg_plot_set_type"), split[0]));
                } else {
                    player.sendMessage(ChatTools.formatCommand("", "/plot set", "name", ""));
                    player.sendMessage(ChatTools.formatCommand("", "/plot set", "reset", ""));
                    player.sendMessage(ChatTools.formatCommand("", "/plot set", "shop|embassy|arena|wilds|spleef", ""));
                    player.sendMessage(ChatTools.formatCommand("", "/plot set perm", "?", ""));
                }
            } else if (split[0].equalsIgnoreCase("clear")) {
                if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_CLEAR.getNode()))
                    throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                TownBlock townBlock = new WorldCoord(world, Coord.parseCoord(player)).getTownBlock();
                if (townBlock != null) {
                    /**
						 * Only allow mayors or plot owners to use this command.
						 */
                    if (townBlock.hasResident()) {
                        if (!townBlock.isOwner(resident)) {
                            player.sendMessage(TownySettings.getLangString("msg_area_not_own"));
                            return true;
                        }
                    } else if (!townBlock.getTown().equals(resident.getTown())) {
                        player.sendMessage(TownySettings.getLangString("msg_area_not_own"));
                        return true;
                    }
                    for (String material : TownyUniverse.getDataSource().getWorld(world).getPlotManagementMayorDelete()) if (Material.matchMaterial(material) != null) {
                        TownyRegenAPI.deleteTownBlockMaterial(townBlock, Material.getMaterial(material));
                        player.sendMessage(String.format(TownySettings.getLangString("msg_clear_plot_material"), material));
                    } else
                        throw new TownyException(String.format(TownySettings.getLangString("msg_err_invalid_property"), material));
                    // Raise an event for the claim
                    BukkitTools.getPluginManager().callEvent(new PlotClearEvent(townBlock));
                } else {
                    // Shouldn't ever reach here as a null townBlock should
                    // be caught already in WorldCoord.
                    player.sendMessage(TownySettings.getLangString("msg_err_empty_area_selection"));
                }
            } else
                throw new TownyException(String.format(TownySettings.getLangString("msg_err_invalid_property"), split[0]));
        } catch (TownyException x) {
            TownyMessaging.sendErrorMsg(player, x.getMessage());
        } catch (EconomyException x) {
            TownyMessaging.sendErrorMsg(player, x.getMessage());
        }
    }
    return true;
}
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) PlotClaim(com.palmergames.bukkit.towny.tasks.PlotClaim) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) TownBlockOwner(com.palmergames.bukkit.towny.object.TownBlockOwner) Resident(com.palmergames.bukkit.towny.object.Resident) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) PlotClearEvent(com.palmergames.bukkit.towny.event.PlotClearEvent) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

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