Search in sources :

Example 1 with PlotClaim

use of com.palmergames.bukkit.towny.tasks.PlotClaim 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

PlotClearEvent (com.palmergames.bukkit.towny.event.PlotClearEvent)1 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)1 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)1 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)1 Resident (com.palmergames.bukkit.towny.object.Resident)1 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)1 TownBlockOwner (com.palmergames.bukkit.towny.object.TownBlockOwner)1 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)1 PlotClaim (com.palmergames.bukkit.towny.tasks.PlotClaim)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1