Search in sources :

Example 1 with TownyPermissionChange

use of com.palmergames.bukkit.towny.object.TownyPermissionChange in project Towny by TownyAdvanced.

the class PlotCommand method handlePlotGroupCommand.

private boolean handlePlotGroupCommand(String[] split, Player player) throws TownyException {
    TownyPermissionSource permSource = TownyUniverse.getInstance().getPermissionSource();
    Resident resident = getResidentOrThrow(player.getUniqueId());
    TownBlock townBlock = TownyAPI.getInstance().getTownBlock(player);
    if (townBlock == null)
        throw new TownyException(Translatable.of("msg_not_claimed_1"));
    Town town = townBlock.getTownOrNull();
    if (town == null)
        throw new TownyException(Translatable.of("msg_not_claimed_1"));
    // Test we are allowed to work on this plot
    // If this fails it will trigger a TownyException.
    plotTestOwner(resident, townBlock);
    if (split.length <= 0 || split[0].equalsIgnoreCase("?")) {
        HelpMenu.PLOT_GROUP_HELP.send(player);
        if (townBlock.hasPlotObjectGroup())
            TownyMessaging.sendMsg(player, Translatable.of("status_plot_group_name_and_size", townBlock.getPlotObjectGroup().getName(), townBlock.getPlotObjectGroup().getTownBlocks().size()));
        return true;
    }
    if (split[0].equalsIgnoreCase("add") || split[0].equalsIgnoreCase("new") || split[0].equalsIgnoreCase("create")) {
        if (!permSource.testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_GROUP_ADD.getNode()))
            throw new TownyException(Translatable.of("msg_err_command_disable"));
        if (split.length == 1) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_you_must_specify_a_group_name"));
            return false;
        }
        if (split.length == 2) {
            String plotGroupName = NameValidation.filterName(split[1]);
            plotGroupName = NameValidation.filterCommas(plotGroupName);
            if (town.hasPlotGroupName(plotGroupName)) {
                TownBlockType groupType = town.getPlotObjectGroupFromName(plotGroupName).getTownBlockType();
                if (townBlock.getType() != groupType)
                    throw new TownyException(Translatable.of("msg_err_this_townblock_doesnt_match_the_groups_type", groupType.getName()));
            }
            if (townBlock.hasPlotObjectGroup()) {
                // Already has a PlotGroup and it is the same name being used to re-add.
                if (townBlock.getPlotObjectGroup().getName().equalsIgnoreCase(plotGroupName)) {
                    TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_this_plot_is_already_part_of_the_plot_group_x", plotGroupName));
                    return false;
                }
                final String name = plotGroupName;
                // Already has a PlotGroup, ask if they want to transfer from one group to another.
                Confirmation.runOnAccept(() -> {
                    PlotGroup oldGroup = townBlock.getPlotObjectGroup();
                    oldGroup.removeTownBlock(townBlock);
                    if (oldGroup.getTownBlocks().isEmpty()) {
                        String oldName = oldGroup.getName();
                        town.removePlotGroup(oldGroup);
                        TownyUniverse.getInstance().getDataSource().removePlotGroup(oldGroup);
                        TownyMessaging.sendMsg(player, Translatable.of("msg_plotgroup_deleted", oldName));
                    } else
                        oldGroup.save();
                    createOrAddOnToPlotGroup(townBlock, town, name);
                    TownyMessaging.sendMsg(player, Translatable.of("msg_townblock_transferred_from_x_to_x_group", oldGroup.getName(), townBlock.getPlotObjectGroup().getName()));
                }).setTitle(Translatable.of("msg_plot_group_already_exists_did_you_want_to_transfer", townBlock.getPlotObjectGroup().getName(), split[1])).sendTo(player);
            } else {
                // Create a brand new plot group.
                createOrAddOnToPlotGroup(townBlock, town, plotGroupName);
                TownyMessaging.sendMsg(player, Translatable.of("msg_plot_was_put_into_group_x", townBlock.getX(), townBlock.getZ(), townBlock.getPlotObjectGroup().getName()));
            }
        } else {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_plot_group_name_required"));
            return false;
        }
    } else if (split[0].equalsIgnoreCase("delete")) {
        if (!permSource.testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_GROUP_DELETE.getNode()))
            throw new TownyException(Translatable.of("msg_err_command_disable"));
        if (!townBlock.hasPlotObjectGroup()) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_plot_not_associated_with_a_group"));
            return false;
        }
        Confirmation.runOnAccept(() -> {
            PlotGroup group = townBlock.getPlotObjectGroup();
            String name = group.getName();
            town.removePlotGroup(group);
            TownyUniverse.getInstance().getDataSource().removePlotGroup(group);
            TownyMessaging.sendMsg(player, Translatable.of("msg_plotgroup_deleted", name));
        }).sendTo(player);
    } else if (split[0].equalsIgnoreCase("remove")) {
        if (!permSource.testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_GROUP_REMOVE.getNode()))
            throw new TownyException(Translatable.of("msg_err_command_disable"));
        if (!townBlock.hasPlotObjectGroup()) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_plot_not_associated_with_a_group"));
            return false;
        }
        PlotGroup group = townBlock.getPlotObjectGroup();
        String name = group.getName();
        // Remove the plot from the group.
        group.removeTownBlock(townBlock);
        // Detach group from townblock.
        townBlock.removePlotObjectGroup();
        // Save
        townBlock.save();
        TownyMessaging.sendMsg(player, Translatable.of("msg_plot_was_removed_from_group_x", townBlock.getX(), townBlock.getZ(), name));
        if (group.getTownBlocks().isEmpty()) {
            town.removePlotGroup(group);
            TownyUniverse.getInstance().getDataSource().removePlotGroup(group);
            TownyMessaging.sendMsg(player, Translatable.of("msg_plotgroup_empty_deleted", name));
        }
    } else if (split[0].equalsIgnoreCase("rename")) {
        if (!permSource.testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_GROUP_RENAME.getNode()))
            throw new TownyException(Translatable.of("msg_err_command_disable"));
        if (!townBlock.hasPlotObjectGroup()) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_plot_not_associated_with_a_group"));
            return false;
        }
        if (split.length == 1) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_plot_group_name_required"));
            return false;
        }
        String newName = split[1];
        String oldName = townBlock.getPlotObjectGroup().getName();
        // Change name;
        TownyUniverse.getInstance().getDataSource().renameGroup(townBlock.getPlotObjectGroup(), newName);
        TownyMessaging.sendMsg(player, Translatable.of("msg_plot_renamed_from_x_to_y", oldName, newName));
    } else if (split[0].equalsIgnoreCase("forsale") || split[0].equalsIgnoreCase("fs")) {
        if (!permSource.testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_GROUP_FORSALE.getNode()))
            throw new TownyException(Translatable.of("msg_err_command_disable"));
        // This means the player wants to fs the plot group they are in.
        PlotGroup group = townBlock.getPlotObjectGroup();
        if (group == null) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_plot_not_associated_with_a_group"));
            return false;
        }
        if (split.length < 2) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_plot_group_specify_price"));
            return false;
        }
        int price = 0;
        try {
            price = Integer.parseInt(split[1]);
        } catch (NumberFormatException e) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_error_must_be_num"));
            return false;
        }
        if (price < 0) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_negative_money"));
            return false;
        }
        group.setPrice(price);
        // Save
        group.save();
        TownyMessaging.sendPrefixedTownMessage(town, Translatable.of("msg_player_put_group_up_for_sale", player.getName(), group.getName(), TownyEconomyHandler.getFormattedBalance(group.getPrice())));
    } else if (split[0].equalsIgnoreCase("notforsale") || split[0].equalsIgnoreCase("nfs")) {
        if (!permSource.testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_GROUP_NOTFORSALE.getNode()))
            throw new TownyException(Translatable.of("msg_err_command_disable"));
        // This means the player wants to nfs the plot group they are in.
        PlotGroup group = townBlock.getPlotObjectGroup();
        if (group == null) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_plot_not_associated_with_a_group"));
            return false;
        }
        group.setPrice(-1);
        // Save
        group.save();
        TownyMessaging.sendPrefixedTownMessage(town, Translatable.of("msg_player_made_group_not_for_sale", player.getName(), group.getName()));
    } else if (split[0].equalsIgnoreCase("toggle")) {
        if (!permSource.testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_GROUP_TOGGLE.getNode()))
            throw new TownyException(Translatable.of("msg_err_command_disable"));
        if (townBlock.getPlotObjectGroup() == null) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_plot_not_associated_with_a_group"));
            return false;
        }
        // Create confirmation.
        PlotGroup plotGroup = townBlock.getPlotObjectGroup();
        Confirmation.runOnAccept(() -> {
            // Perform the toggle.
            new PlotCommand(Towny.getPlugin()).plotGroupToggle(player, plotGroup, StringMgmt.remArgs(split, 1));
        }).setTitle(Translatable.of("msg_plot_group_toggle_confirmation", townBlock.getPlotObjectGroup().getTownBlocks().size()).append(" " + Translatable.of("are_you_sure_you_want_to_continue"))).sendTo(player);
        return true;
    } else if (split[0].equalsIgnoreCase("set")) {
        if (!permSource.testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_GROUP_SET.getNode()))
            throw new TownyException(Translatable.of("msg_err_command_disable"));
        // Check if group is present.
        if (townBlock.getPlotObjectGroup() == null) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_plot_not_associated_with_a_group"));
            return false;
        }
        TownBlockOwner townBlockOwner = townBlock.getTownBlockOwner();
        if (split.length < 2) {
            TownyMessaging.sendMessage(player, ChatTools.formatTitle("/plot group set"));
            if (townBlockOwner instanceof Town)
                TownyMessaging.sendMessage(player, ChatTools.formatCommand("Level", "[resident/nation/ally/outsider]", "", ""));
            if (townBlockOwner instanceof Resident)
                TownyMessaging.sendMessage(player, ChatTools.formatCommand("Level", "[friend/town/ally/outsider]", "", ""));
            TownyMessaging.sendMessage(player, ChatTools.formatCommand("Type", "[build/destroy/switch/itemuse]", "", ""));
            TownyMessaging.sendMessage(player, ChatTools.formatCommand("/plot group set", "perm", "[on/off]", "Toggle all permissions"));
            TownyMessaging.sendMessage(player, ChatTools.formatCommand("/plot group set", "perm", "[level/type] [on/off]", ""));
            TownyMessaging.sendMessage(player, ChatTools.formatCommand("/plot group set", "perm", "[level] [type] [on/off]", ""));
            TownyMessaging.sendMessage(player, ChatTools.formatCommand("/plot group set", "perm", "reset", ""));
            TownyMessaging.sendMessage(player, ChatTools.formatCommand("Eg", "/plot group set perm", "friend build on", ""));
            TownyMessaging.sendMessage(player, ChatTools.formatCommand("/plot group set", "[townblocktype]", "", "Farm, Wilds, Bank, Embassy, etc."));
            return false;
        }
        if (split[1].equalsIgnoreCase("perm")) {
            // Set plot level permissions (if the plot owner) or
            // Mayor/Assistant of the town.
            PlotGroup plotGroup = townBlock.getPlotObjectGroup();
            Runnable permHandler = () -> {
                // setTownBlockPermissions returns a towny permission change object
                TownyPermissionChange permChange = PlotCommand.setTownBlockPermissions(player, townBlockOwner, townBlock, StringMgmt.remArgs(split, 2));
                // If the perm change object is not null
                if (permChange != null) {
                    plotGroup.getPermissions().change(permChange);
                    plotGroup.getTownBlocks().stream().forEach(tb -> {
                        tb.setPermissions(plotGroup.getPermissions().toString());
                        tb.setChanged(!tb.getPermissions().toString().equals(town.getPermissions().toString()));
                        tb.save();
                        // Change settings event
                        TownBlockSettingsChangedEvent event = new TownBlockSettingsChangedEvent(tb);
                        Bukkit.getServer().getPluginManager().callEvent(event);
                    });
                    plugin.resetCache();
                    TownyPermission perm = plotGroup.getPermissions();
                    TownyMessaging.sendMessage(player, Translatable.of("msg_set_perms").forLocale(player));
                    TownyMessaging.sendMessage(player, (Colors.Green + Translatable.of("status_perm").forLocale(player) + " " + ((townBlockOwner instanceof Resident) ? perm.getColourString().replace("n", "t") : perm.getColourString().replace("f", "r"))));
                    TownyMessaging.sendMessage(player, Colors.Green + Translatable.of("status_pvp").forLocale(player) + " " + (!(CombatUtil.preventPvP(townBlock.getWorld(), townBlock)) ? Colors.Red + "ON" : Colors.LightGreen + "OFF") + Colors.Green + Translatable.of("explosions").forLocale(player) + " " + ((perm.explosion) ? Colors.Red + "ON" : Colors.LightGreen + "OFF") + Colors.Green + Translatable.of("firespread").forLocale(player) + " " + ((perm.fire) ? Colors.Red + "ON" : Colors.LightGreen + "OFF") + Colors.Green + Translatable.of("mobspawns").forLocale(player) + " " + ((perm.mobs) ? Colors.Red + "ON" : Colors.LightGreen + "OFF"));
                }
            };
            // Create confirmation.
            Confirmation.runOnAccept(permHandler).setTitle(Translatable.of("msg_plot_group_set_perm_confirmation", townBlock.getPlotObjectGroup().getTownBlocks().size()).append(" " + Translatable.of("are_you_sure_you_want_to_continue"))).sendTo(player);
            return true;
        }
        /*
			 * After all other set commands are tested for we attempt to set the townblocktype.
			 */
        String plotTypeName = split[1];
        // Stop setting plot groups to Jail plot, because that would set a spawn point for each plot in the location of the player.
        if (plotTypeName.equalsIgnoreCase("jail")) {
            throw new TownyException(Translatable.of("msg_err_cannot_set_group_to_jail"));
        }
        // Handle type being reset
        if (plotTypeName.equalsIgnoreCase("reset"))
            plotTypeName = "default";
        TownBlockType type = TownBlockTypeHandler.getType(plotTypeName);
        if (type == null)
            throw new TownyException(Translatable.of("msg_err_not_block_type"));
        for (TownBlock tb : townBlock.getPlotObjectGroup().getTownBlocks()) {
            try {
                // Test we are allowed to work on this plot
                // ignore the return as we
                plotTestOwner(resident, townBlock);
            // are only checking for an
            // exception
            } catch (TownyException e) {
                TownyMessaging.sendErrorMsg(resident, e.getMessage(player));
                return false;
            }
            if (TownBlockType.ARENA.equals(type) && TownySettings.getOutsidersPreventPVPToggle()) {
                for (Player target : Bukkit.getOnlinePlayers()) {
                    if (!town.hasResident(target) && !player.getName().equals(target.getName()) && tb.getWorldCoord().equals(WorldCoord.parseWorldCoord(target)))
                        throw new TownyException(Translatable.of("msg_cant_toggle_pvp_outsider_in_plot"));
                }
            }
            // Allow for PlotPreChangeTypeEvent to trigger
            PlotPreChangeTypeEvent preEvent = new PlotPreChangeTypeEvent(type, tb, resident);
            BukkitTools.getPluginManager().callEvent(preEvent);
            // If any one of the townblocks is not allowed to be set, cancel setting all of them.
            if (preEvent.isCancelled()) {
                TownyMessaging.sendErrorMsg(player, preEvent.getCancelMessage());
                return false;
            }
        }
        int amount = townBlock.getPlotObjectGroup().getTownBlocks().size();
        double cost = type.getCost() * amount;
        try {
            // Test if we can pay first to throw an exception.
            if (cost > 0 && TownyEconomyHandler.isActive() && !resident.getAccount().canPayFromHoldings(cost))
                throw new TownyException(Translatable.of("msg_err_cannot_afford_plot_set_type_cost", type, TownyEconomyHandler.getFormattedBalance(cost)));
            // Handle payment via a confirmation to avoid suprise costs.
            if (cost > 0 && TownyEconomyHandler.isActive()) {
                Confirmation.runOnAccept(() -> {
                    if (!resident.getAccount().withdraw(cost, String.format("Plot (" + amount + ") set to %s", type))) {
                        TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_cannot_afford_plot_set_type_cost", type, TownyEconomyHandler.getFormattedBalance(cost)));
                        return;
                    }
                    TownyMessaging.sendMsg(resident, Translatable.of("msg_plot_set_cost", TownyEconomyHandler.getFormattedBalance(cost), type));
                    for (TownBlock tb : townBlock.getPlotObjectGroup().getTownBlocks()) {
                        try {
                            tb.setType(type, resident);
                        } catch (TownyException ignored) {
                        // Cannot be set to jail type as a group.
                        }
                    }
                    TownyMessaging.sendMsg(player, Translatable.of("msg_set_group_type_to_x", type));
                }).setTitle(Translatable.of("msg_confirm_purchase", TownyEconomyHandler.getFormattedBalance(cost))).sendTo(BukkitTools.getPlayerExact(resident.getName()));
            // No cost or economy so no confirmation.
            } else {
                for (TownBlock tb : townBlock.getPlotObjectGroup().getTownBlocks()) tb.setType(type, resident);
                TownyMessaging.sendMsg(player, Translatable.of("msg_set_group_type_to_x", plotTypeName));
            }
        } catch (TownyException e) {
            TownyMessaging.sendErrorMsg(resident, e.getMessage(player));
        }
    } else if (split[0].equalsIgnoreCase("trust")) {
        if (!permSource.testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_GROUP_TRUST.getNode()))
            throw new TownyException(Translatable.of("msg_err_command_disable"));
        if (split.length < 3) {
            HelpMenu.PLOT_GROUP_TRUST_HELP.send(player);
            return true;
        }
        PlotGroup group = townBlock.getPlotObjectGroup();
        if (group == null) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_plot_not_associated_with_a_group"));
            return false;
        }
        Resident trustedResident = TownyAPI.getInstance().getResident(split[2]);
        if (trustedResident == null || trustedResident.isNPC()) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_not_registered_1", split[2]));
            return false;
        }
        if (split[1].equalsIgnoreCase("add")) {
            if (group.hasTrustedResident(trustedResident)) {
                TownyMessaging.sendErrorMsg(player, Translatable.of("msg_already_trusted", trustedResident.getName(), Translatable.of("plotgroup_sing")));
                return false;
            }
            PlotTrustAddEvent event = new PlotTrustAddEvent(new ArrayList<>(group.getTownBlocks()), trustedResident, player);
            Bukkit.getPluginManager().callEvent(event);
            if (event.isCancelled()) {
                TownyMessaging.sendErrorMsg(player, event.getCancelMessage());
                return false;
            }
            group.addTrustedResident(trustedResident);
            plugin.deleteCache(trustedResident);
            TownyMessaging.sendMsg(player, Translatable.of("msg_trusted_added", trustedResident.getName(), Translatable.of("plotgroup_sing")));
            if (BukkitTools.isOnline(trustedResident.getName()) && !trustedResident.getName().equals(player.getName()))
                TownyMessaging.sendMsg(trustedResident, Translatable.of("msg_trusted_added_2", player.getName(), Translatable.of("plotgroup_sing"), group.getName()));
            return true;
        } else if (split[1].equalsIgnoreCase("remove")) {
            if (!group.hasTrustedResident(trustedResident)) {
                TownyMessaging.sendErrorMsg(player, Translatable.of("msg_not_trusted", trustedResident.getName(), Translatable.of("plotgroup_sing")));
                return false;
            }
            PlotTrustRemoveEvent event = new PlotTrustRemoveEvent(new ArrayList<>(group.getTownBlocks()), trustedResident, player);
            Bukkit.getPluginManager().callEvent(event);
            if (event.isCancelled()) {
                TownyMessaging.sendErrorMsg(player, event.getCancelMessage());
                return false;
            }
            group.removeTrustedResident(trustedResident);
            plugin.deleteCache(trustedResident);
            TownyMessaging.sendMsg(player, Translatable.of("msg_trusted_removed", trustedResident.getName(), Translatable.of("plotgroup_sing")));
            if (BukkitTools.isOnline(trustedResident.getName()) && !trustedResident.getName().equals(player.getName()))
                TownyMessaging.sendMsg(trustedResident, Translatable.of("msg_trusted_removed_2", player.getName(), Translatable.of("plotgroup_sing"), group.getName()));
            return true;
        } else {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_invalid_property", split[1]));
            return false;
        }
    } else if (split[0].equalsIgnoreCase("perm")) {
        if (!permSource.testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_GROUP_PERM.getNode()))
            throw new TownyException(Translatable.of("msg_err_command_disable"));
        if (split.length < 2) {
            HelpMenu.PLOT_GROUP_PERM_HELP.send(player);
            return true;
        }
        PlotGroup group = townBlock.getPlotObjectGroup();
        if (group == null) {
            TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_plot_not_associated_with_a_group"));
            return false;
        }
        if (split[1].equalsIgnoreCase("gui")) {
            PermissionGUIUtil.openPermissionGUI(resident, townBlock);
        } else {
            if (split.length < 3) {
                HelpMenu.PLOT_GROUP_PERM_HELP.send(player);
                return true;
            }
            Resident overrideResident = TownyAPI.getInstance().getResident(split[2]);
            if (overrideResident == null || overrideResident.isNPC()) {
                TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_not_registered_1", split[2]));
                return true;
            }
            if (split[1].equalsIgnoreCase("add")) {
                if (group.getPermissionOverrides() != null && group.getPermissionOverrides().containsKey(overrideResident)) {
                    TownyMessaging.sendErrorMsg(player, Translatable.of("msg_overrides_already_set", overrideResident.getName(), Translatable.of("plotgroup_sing")));
                    return true;
                }
                group.putPermissionOverride(overrideResident, new PermissionData(PermissionGUIUtil.getDefaultTypes(), player.getName()));
                TownyMessaging.sendMsg(player, Translatable.of("msg_overrides_added", overrideResident.getName()));
            } else if (split[1].equalsIgnoreCase("remove")) {
                if (group.getPermissionOverrides() != null && !group.getPermissionOverrides().containsKey(overrideResident)) {
                    TownyMessaging.sendErrorMsg(player, Translatable.of("msg_no_overrides_set", overrideResident.getName(), Translatable.of("plotgroup_sing")));
                    return true;
                }
                group.removePermissionOverride(overrideResident);
                TownyMessaging.sendMsg(player, Translatable.of("msg_overrides_removed", overrideResident.getName()));
            } else {
                TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_invalid_property", split[1]));
                return false;
            }
        }
    } else {
        HelpMenu.PLOT_GROUP_HELP.send(player);
        if (townBlock.hasPlotObjectGroup())
            TownyMessaging.sendMsg(player, Translatable.of("status_plot_group_name_and_size", townBlock.getPlotObjectGroup().getName(), townBlock.getPlotObjectGroup().getTownBlocks().size()));
        return true;
    }
    return false;
}
Also used : TownBlockType(com.palmergames.bukkit.towny.object.TownBlockType) TownyPermission(com.palmergames.bukkit.towny.object.TownyPermission) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) PlotTrustRemoveEvent(com.palmergames.bukkit.towny.event.plot.PlotTrustRemoveEvent) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) PlotTrustAddEvent(com.palmergames.bukkit.towny.event.plot.PlotTrustAddEvent) Town(com.palmergames.bukkit.towny.object.Town) TownBlockOwner(com.palmergames.bukkit.towny.object.TownBlockOwner) TownyPermissionChange(com.palmergames.bukkit.towny.object.TownyPermissionChange) Resident(com.palmergames.bukkit.towny.object.Resident) TownBlockSettingsChangedEvent(com.palmergames.bukkit.towny.event.TownBlockSettingsChangedEvent) TownyPermissionSource(com.palmergames.bukkit.towny.permissions.TownyPermissionSource) PlotGroup(com.palmergames.bukkit.towny.object.PlotGroup) PlotPreChangeTypeEvent(com.palmergames.bukkit.towny.event.PlotPreChangeTypeEvent) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) PermissionData(com.palmergames.bukkit.towny.object.PermissionData)

Example 2 with TownyPermissionChange

use of com.palmergames.bukkit.towny.object.TownyPermissionChange in project Towny by TownyAdvanced.

the class PlotCommand method setTownBlockPermissions.

/**
 * Returns a TownyPermissionChange object representing the change action
 *
 * @param player Player initiator
 * @param townBlockOwner Resident/Town with the targeted permissions change
 * @param townBlock Targeted town block
 * @param split Permission arguments
 * @return a TownyPermissionChange object
 */
public static TownyPermissionChange setTownBlockPermissions(Player player, TownBlockOwner townBlockOwner, TownBlock townBlock, String[] split) {
    TownyPermissionChange permChange = null;
    if (split.length == 0 || split[0].equalsIgnoreCase("?")) {
        TownyMessaging.sendMessage(player, ChatTools.formatTitle("/... set perm"));
        if (townBlockOwner instanceof Town)
            TownyMessaging.sendMessage(player, ChatTools.formatCommand("Level", "[resident/nation/ally/outsider]", "", ""));
        if (townBlockOwner instanceof Resident)
            TownyMessaging.sendMessage(player, ChatTools.formatCommand("Level", "[friend/town/ally/outsider]", "", ""));
        TownyMessaging.sendMessage(player, ChatTools.formatCommand("Type", "[build/destroy/switch/itemuse]", "", ""));
        TownyMessaging.sendMessage(player, ChatTools.formatCommand("", "set perm", "[on/off]", "Toggle all permissions"));
        TownyMessaging.sendMessage(player, ChatTools.formatCommand("", "set perm", "[level/type] [on/off]", ""));
        TownyMessaging.sendMessage(player, ChatTools.formatCommand("", "set perm", "[level] [type] [on/off]", ""));
        TownyMessaging.sendMessage(player, ChatTools.formatCommand("", "set perm", "reset", ""));
        TownyMessaging.sendMessage(player, ChatTools.formatCommand("Eg", "/plot set perm", "friend build on", ""));
        return null;
    } else {
        TownyPermission perm = townBlock.getPermissions();
        if (split.length == 1) {
            if (split[0].equalsIgnoreCase("reset")) {
                // reset this townBlock permissions (by town/resident)
                permChange = new TownyPermissionChange(TownyPermissionChange.Action.RESET, false, townBlock);
                perm.change(permChange);
                townBlock.save();
                TownyMessaging.sendMsg(player, Translatable.of("msg_set_perms_reset_single"));
                // Reset all caches as this can affect everyone.
                plugin.resetCache();
                return permChange;
            } else {
                try {
                    boolean b = StringMgmt.parseOnOff(split[0]);
                    permChange = new TownyPermissionChange(TownyPermissionChange.Action.ALL_PERMS, b);
                } catch (Exception e) {
                    TownyMessaging.sendErrorMsg(player, Translatable.of("msg_plot_set_perm_syntax_error"));
                    return null;
                }
            }
        } else if (split.length == 2) {
            boolean b;
            try {
                b = StringMgmt.parseOnOff(split[1]);
            } catch (Exception e) {
                TownyMessaging.sendErrorMsg(player, Translatable.of("msg_plot_set_perm_syntax_error"));
                return null;
            }
            if (split[0].equalsIgnoreCase("friend"))
                split[0] = "resident";
            else if (split[0].equalsIgnoreCase("town"))
                split[0] = "nation";
            else if (split[0].equalsIgnoreCase("itemuse"))
                split[0] = "item_use";
            // Check if it is a perm level first
            try {
                TownyPermission.PermLevel permLevel = TownyPermission.PermLevel.valueOf(split[0].toUpperCase());
                permChange = new TownyPermissionChange(TownyPermissionChange.Action.PERM_LEVEL, b, permLevel);
            } catch (IllegalArgumentException permLevelException) {
                // If it is not a perm level, then check if it is a action type
                try {
                    TownyPermission.ActionType actionType = TownyPermission.ActionType.valueOf(split[0].toUpperCase());
                    permChange = new TownyPermissionChange(TownyPermissionChange.Action.ACTION_TYPE, b, actionType);
                } catch (IllegalArgumentException actionTypeException) {
                    TownyMessaging.sendErrorMsg(player, Translatable.of("msg_plot_set_perm_syntax_error"));
                    return null;
                }
            }
        } else if (split.length == 3) {
            // Reset the friend to resident so the perm settings don't fail
            if (split[0].equalsIgnoreCase("friend"))
                split[0] = "resident";
            else // reset the town to nation so the perm settings don't fail
            if (split[0].equalsIgnoreCase("town"))
                split[0] = "nation";
            if (split[1].equalsIgnoreCase("itemuse"))
                split[1] = "item_use";
            TownyPermission.PermLevel permLevel;
            TownyPermission.ActionType actionType;
            try {
                permLevel = TownyPermission.PermLevel.valueOf(split[0].toUpperCase());
                actionType = TownyPermission.ActionType.valueOf(split[1].toUpperCase());
            } catch (IllegalArgumentException ignore) {
                TownyMessaging.sendErrorMsg(player, Translatable.of("msg_plot_set_perm_syntax_error"));
                return null;
            }
            try {
                boolean b = StringMgmt.parseOnOff(split[2]);
                permChange = new TownyPermissionChange(TownyPermissionChange.Action.SINGLE_PERM, b, permLevel, actionType);
            } catch (Exception e) {
                TownyMessaging.sendErrorMsg(player, Translatable.of("msg_plot_set_perm_syntax_error"));
                return null;
            }
        }
        if (permChange != null)
            perm.change(permChange);
        townBlock.setChanged(true);
        townBlock.save();
        if (!townBlock.hasPlotObjectGroup()) {
            TownyMessaging.sendMsg(player, Translatable.of("msg_set_perms"));
            TownyMessaging.sendMessage(player, Colors.Green + Translatable.of("status_perm").forLocale(player) + " " + ((townBlockOwner instanceof Resident) ? perm.getColourString().replace("n", "t") : perm.getColourString().replace("f", "r")));
            TownyMessaging.sendMessage(player, Colors.Green + Translatable.of("status_pvp").forLocale(player) + " " + ((perm.pvp) ? Colors.Red + "ON" : Colors.LightGreen + "OFF") + Colors.Green + Translatable.of("explosions").forLocale(player) + " " + ((perm.explosion) ? Colors.Red + "ON" : Colors.LightGreen + "OFF") + Colors.Green + Translatable.of("firespread").forLocale(player) + " " + ((perm.fire) ? Colors.Red + "ON" : Colors.LightGreen + "OFF") + Colors.Green + Translatable.of("mobspawns").forLocale(player) + " " + ((perm.mobs) ? Colors.Red + "ON" : Colors.LightGreen + "OFF"));
        }
        // Change settings event
        TownBlockSettingsChangedEvent event = new TownBlockSettingsChangedEvent(townBlock);
        Bukkit.getServer().getPluginManager().callEvent(event);
        // Reset all caches as this can affect everyone.
        plugin.resetCache();
        return permChange;
    }
}
Also used : TownyPermission(com.palmergames.bukkit.towny.object.TownyPermission) Town(com.palmergames.bukkit.towny.object.Town) TownyPermissionChange(com.palmergames.bukkit.towny.object.TownyPermissionChange) Resident(com.palmergames.bukkit.towny.object.Resident) TownBlockSettingsChangedEvent(com.palmergames.bukkit.towny.event.TownBlockSettingsChangedEvent) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Aggregations

TownBlockSettingsChangedEvent (com.palmergames.bukkit.towny.event.TownBlockSettingsChangedEvent)2 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)2 Resident (com.palmergames.bukkit.towny.object.Resident)2 Town (com.palmergames.bukkit.towny.object.Town)2 TownyPermission (com.palmergames.bukkit.towny.object.TownyPermission)2 TownyPermissionChange (com.palmergames.bukkit.towny.object.TownyPermissionChange)2 PlotPreChangeTypeEvent (com.palmergames.bukkit.towny.event.PlotPreChangeTypeEvent)1 PlotTrustAddEvent (com.palmergames.bukkit.towny.event.plot.PlotTrustAddEvent)1 PlotTrustRemoveEvent (com.palmergames.bukkit.towny.event.plot.PlotTrustRemoveEvent)1 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)1 PermissionData (com.palmergames.bukkit.towny.object.PermissionData)1 PlotGroup (com.palmergames.bukkit.towny.object.PlotGroup)1 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)1 TownBlockOwner (com.palmergames.bukkit.towny.object.TownBlockOwner)1 TownBlockType (com.palmergames.bukkit.towny.object.TownBlockType)1 TownyPermissionSource (com.palmergames.bukkit.towny.permissions.TownyPermissionSource)1 ArrayList (java.util.ArrayList)1 Player (org.bukkit.entity.Player)1