Search in sources :

Example 6 with Command

use of org.bukkit.command.Command in project MassiveCore by MassiveCraft.

the class TypeStringCommand method getTabList.

@Override
public Collection<String> getTabList(CommandSender sender, String arg) {
    // Split the arg into a list of args #inception-the-movie!
    String[] args = argAsArgs(arg);
    // Tab completion of base commands
    if (args.length <= 1)
        return getKnownCommands().keySet();
    // Get command alias and subargs
    String alias = args[0];
    // Attempt using the tab completion of that command.
    Command command = getCommandSmart(alias);
    if (command == null)
        return Collections.emptySet();
    List<String> subcompletions = command.tabComplete(sender, alias, Arrays.copyOfRange(args, 1, args.length));
    String prefix = Txt.implode(Arrays.copyOfRange(args, 0, args.length - 1), " ") + " ";
    List<String> ret = new MassiveList<>();
    for (String subcompletion : subcompletions) {
        String completion = prefix + subcompletion;
        ret.add(completion);
    }
    return ret;
}
Also used : MassiveList(com.massivecraft.massivecore.collections.MassiveList) Command(org.bukkit.command.Command)

Example 7 with Command

use of org.bukkit.command.Command in project NoCheatPlus by NoCheatPlus.

the class ChatListener method onPlayerCommandPreprocess.

/**
 * We listen to PlayerCommandPreprocess events because commands can be used for spamming too.
 *
 * @param event
 *            the event
 */
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) {
    final Player player = event.getPlayer();
    // Tell TickTask to update cached permissions.
    final IPlayerData pData = DataManager.getPlayerData(player);
    final ChatConfig cc = pData.getGenericInstance(ChatConfig.class);
    // Checks that replace parts of the message (color).
    if (color.isEnabled(player, pData)) {
        event.setMessage(color.check(player, event.getMessage(), true));
    }
    // Left-trim is necessary because the server accepts leading spaces with commands.
    final String message = event.getMessage();
    final String lcMessage = StringUtil.leftTrim(message).toLowerCase();
    // TODO: Remove bukkit: etc.
    final String[] split = lcMessage.split(" ", 2);
    final String alias = split[0].substring(1);
    final Command command = CommandUtil.getCommand(alias);
    // Could as well use an array and allow null on input of SimpleCharPrefixTree.
    final List<String> messageVars = new ArrayList<String>();
    messageVars.add(lcMessage);
    // Message to run chat checks on.
    String checkMessage = message;
    if (command != null) {
        messageVars.add("/" + command.getLabel().toLowerCase() + (split.length > 1 ? (" " + split[1]) : ""));
    }
    if (alias.indexOf(":") != -1) {
        final int index = message.indexOf(":") + 1;
        if (index < message.length()) {
            checkMessage = message.substring(index);
            messageVars.add(checkMessage.toLowerCase());
        }
    }
    // Prevent commands from being used by players (e.g. /op /deop /reload).
    if (cc.consoleOnlyCheck && consoleOnlyCommands.hasAnyPrefixWords(messageVars)) {
        if (command == null || command.testPermission(player)) {
            player.sendMessage(cc.consoleOnlyMessage);
        }
        event.setCancelled(true);
        return;
    }
    // Handle as chat or command.
    if (chatCommands.hasAnyPrefixWords(messageVars)) {
        // TODO: Cut off the command ?.
        if (textChecks(player, checkMessage, cc, pData, true, false)) {
            event.setCancelled(true);
        }
    } else if (!commandExclusions.hasAnyPrefixWords(messageVars)) {
        // Treat as command.
        if (commands.isEnabled(player, pData) && commands.check(player, checkMessage, cc, pData, captcha)) {
            event.setCancelled(true);
        } else {
            // TODO: Consider always checking these?
            // Note that this checks for prefixes, not prefix words.
            final MovingConfig mcc = pData.getGenericInstance(MovingConfig.class);
            if (mcc.passableUntrackedCommandCheck && mcc.passableUntrackedCommandPrefixes.hasAnyPrefix(messageVars)) {
                if (checkUntrackedLocation(player, message, mcc, pData)) {
                    event.setCancelled(true);
                }
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) Command(org.bukkit.command.Command) ArrayList(java.util.ArrayList) IPlayerData(fr.neatmonster.nocheatplus.players.IPlayerData) MovingConfig(fr.neatmonster.nocheatplus.checks.moving.MovingConfig) EventHandler(org.bukkit.event.EventHandler)

Example 8 with Command

use of org.bukkit.command.Command in project NoCheatPlus by NoCheatPlus.

the class PermissionUtil method protectCommands.

/**
 * Set up the command protection with the given permission message.
 * @param permissionBase
 * @param ignoredCommands
 * @param invertIgnored
 * @param ops
 * @param permissionMessage
 * @return
 */
public static List<CommandProtectionEntry> protectCommands(final String permissionBase, final Collection<String> ignoredCommands, final boolean invertIgnored, final boolean ops, final String permissionMessage) {
    final Set<String> checked = new HashSet<String>();
    for (String label : ignoredCommands) {
        checked.add(CommandUtil.getCommandLabel(label, false));
    }
    final PluginManager pm = Bukkit.getPluginManager();
    Permission rootPerm = pm.getPermission(permissionBase);
    if (rootPerm == null) {
        rootPerm = new Permission(permissionBase);
        pm.addPermission(rootPerm);
    }
    final List<CommandProtectionEntry> changed = new LinkedList<CommandProtectionEntry>();
    // Apply protection based on white-list or black-list.
    for (final Command command : CommandUtil.getCommands()) {
        final String lcLabel = command.getLabel().trim().toLowerCase();
        if (checked.contains(lcLabel) || containsAnyAliases(checked, command)) {
            if (!invertIgnored) {
                continue;
            }
        } else if (invertIgnored) {
            continue;
        }
        // Set the permission for the command.
        String cmdPermName = command.getPermission();
        final boolean cmdHadPerm;
        if (cmdPermName == null) {
            // Set a permission.
            cmdPermName = permissionBase + "." + lcLabel;
            command.setPermission(cmdPermName);
            cmdHadPerm = false;
        } else {
            cmdHadPerm = true;
        }
        // Set permission default behavior.
        Permission cmdPerm = pm.getPermission(cmdPermName);
        final boolean permRegistered = cmdPerm != null;
        if (!permRegistered) {
            cmdPerm = new Permission(cmdPermName);
            if (!cmdHadPerm) {
                // NCP added the permission, allow root.
                cmdPerm.addParent(rootPerm, true);
            }
            // else: permission was present, but not registered.
            pm.addPermission(cmdPerm);
        }
        // Create change history entry.
        if (cmdHadPerm && permRegistered) {
            changed.add(new CommandProtectionEntry(command, lcLabel, cmdPermName, cmdPerm.getDefault(), command.getPermissionMessage()));
        } else {
            // (New Permission instances will not be touched on restore.)
            changed.add(new CommandProtectionEntry(command, lcLabel, null, null, command.getPermissionMessage()));
        }
        // Change
        cmdPerm.setDefault(ops ? PermissionDefault.OP : PermissionDefault.FALSE);
        command.setPermissionMessage(permissionMessage);
    }
    return changed;
}
Also used : PluginManager(org.bukkit.plugin.PluginManager) Command(org.bukkit.command.Command) Permission(org.bukkit.permissions.Permission) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 9 with Command

use of org.bukkit.command.Command in project Arcade2 by ShootGame.

the class GeneralListeners method onPlayerCommandPreprocess.

// 
// Disable some commands
// 
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
    String prefix = BukkitCommands.BUKKIT_COMMAND_PREFIX.toLowerCase();
    String message = event.getMessage();
    if (!message.startsWith(prefix)) {
        return;
    }
    String query = message.substring(1);
    if (query.isEmpty()) {
        return;
    }
    String label = query.split(" ", 2)[0].toLowerCase();
    if (label.isEmpty()) {
        return;
    }
    Command command = this.plugin.getServer().getCommandMap().getCommand(label);
    if (command == null) {
        return;
    }
    for (String disabled : DISABLED_COMMANDS) {
        for (String alias : command.getAliases()) {
            if (command.getName().equalsIgnoreCase(disabled) || alias.equalsIgnoreCase(disabled)) {
                event.setCancelled(true);
                event.getPlayer().sendMessage(String.format(COMMAND_MESSAGE, prefix + command.getName()));
                return;
            }
        }
    }
}
Also used : Command(org.bukkit.command.Command) EventHandler(org.bukkit.event.EventHandler)

Example 10 with Command

use of org.bukkit.command.Command in project Essentials by EssentialsX.

the class AlternativeCommandsHandler method addPlugin.

public final void addPlugin(final Plugin plugin) {
    if (plugin.getDescription().getMain().contains("com.earth2me.essentials")) {
        return;
    }
    final List<Command> commands = PluginCommandYamlParser.parse(plugin);
    final String pluginName = plugin.getDescription().getName().toLowerCase(Locale.ENGLISH);
    for (Command command : commands) {
        final PluginCommand pc = (PluginCommand) command;
        final List<String> labels = new ArrayList<>(pc.getAliases());
        labels.add(pc.getName());
        PluginCommand reg = ess.getServer().getPluginCommand(pluginName + ":" + pc.getName().toLowerCase(Locale.ENGLISH));
        if (reg == null) {
            reg = ess.getServer().getPluginCommand(pc.getName().toLowerCase(Locale.ENGLISH));
        }
        if (reg == null || !reg.getPlugin().equals(plugin)) {
            continue;
        }
        for (String label : labels) {
            List<PluginCommand> plugincommands = altcommands.get(label.toLowerCase(Locale.ENGLISH));
            if (plugincommands == null) {
                plugincommands = new ArrayList<>();
                altcommands.put(label.toLowerCase(Locale.ENGLISH), plugincommands);
            }
            boolean found = false;
            for (PluginCommand pc2 : plugincommands) {
                if (pc2.getPlugin().equals(plugin)) {
                    found = true;
                }
            }
            if (!found) {
                plugincommands.add(reg);
            }
        }
    }
}
Also used : Command(org.bukkit.command.Command) PluginCommand(org.bukkit.command.PluginCommand) PluginCommand(org.bukkit.command.PluginCommand)

Aggregations

Command (org.bukkit.command.Command)27 SimpleCommandMap (org.bukkit.command.SimpleCommandMap)9 CommandMap (org.bukkit.command.CommandMap)7 Field (java.lang.reflect.Field)6 PluginCommand (org.bukkit.command.PluginCommand)6 Map (java.util.Map)5 PluginIdentifiableCommand (org.bukkit.command.PluginIdentifiableCommand)5 Player (org.bukkit.entity.Player)5 HashMap (java.util.HashMap)4 EventHandler (org.bukkit.event.EventHandler)3 Plugin (org.bukkit.plugin.Plugin)3 PluginManager (org.bukkit.plugin.PluginManager)3 URLClassLoader (java.net.URLClassLoader)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 Server (org.bukkit.Server)2 CommandSender (org.bukkit.command.CommandSender)2 JavaPlugin (org.bukkit.plugin.java.JavaPlugin)2 Arguments (cat.nyaa.nyaacore.CommandReceiver.Arguments)1