Search in sources :

Example 1 with Command

use of org.bukkit.command.Command in project TotalFreedomMod by TotalFreedom.

the class Muter method onPlayerCommandPreprocess.

@EventHandler(priority = EventPriority.LOW)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
    Player player = event.getPlayer();
    FPlayer fPlayer = plugin.pl.getPlayer(event.getPlayer());
    // Block commands if player is muted
    if (!fPlayer.isMuted()) {
        return;
    }
    String message = event.getMessage();
    if (plugin.al.isAdmin(player)) {
        fPlayer.setMuted(false);
        return;
    }
    String cmdName = message.split(" ")[0].toLowerCase();
    if (cmdName.startsWith("/")) {
        cmdName = cmdName.substring(1);
    }
    Command command = server.getPluginCommand(cmdName);
    if (command != null) {
        cmdName = command.getName().toLowerCase();
    }
    if (MUTE_COMMANDS.contains(cmdName)) {
        player.sendMessage(ChatColor.RED + "That command is blocked while you are muted.");
        event.setCancelled(true);
        return;
    }
    // TODO: Should this go here?
    if (ConfigEntry.ENABLE_PREPROCESS_LOG.getBoolean()) {
        FLog.info(String.format("[PREPROCESS_COMMAND] %s(%s): %s", player.getName(), ChatColor.stripColor(player.getDisplayName()), message), true);
    }
}
Also used : FPlayer(me.totalfreedom.totalfreedommod.player.FPlayer) FPlayer(me.totalfreedom.totalfreedommod.player.FPlayer) Player(org.bukkit.entity.Player) Command(org.bukkit.command.Command) EventHandler(org.bukkit.event.EventHandler)

Example 2 with Command

use of org.bukkit.command.Command in project TotalFreedomMod by TotalFreedom.

the class Module_help method getBody.

@Override
public String getBody() {
    final CommandMap map = CommandReflection.getCommandMap();
    if (map == null || !(map instanceof SimpleCommandMap)) {
        return paragraph("Error loading commands.");
    }
    final StringBuilder responseBody = new StringBuilder().append(heading("Command Help", 1)).append(paragraph("This page is an automatically generated listing of all plugin commands that are currently live on the server. " + "Please note that it does not include vanilla server commands."));
    final Collection<Command> knownCommands = ((SimpleCommandMap) map).getCommands();
    final Map<String, List<Command>> commandsByPlugin = new HashMap<>();
    for (Command command : knownCommands) {
        String pluginName = "Bukkit";
        if (command instanceof PluginIdentifiableCommand) {
            pluginName = ((PluginIdentifiableCommand) command).getPlugin().getName();
        }
        List<Command> pluginCommands = commandsByPlugin.get(pluginName);
        if (pluginCommands == null) {
            pluginCommands = Lists.newArrayList();
            commandsByPlugin.put(pluginName, pluginCommands);
        }
        pluginCommands.add(command);
    }
    final Iterator<Map.Entry<String, List<Command>>> it = commandsByPlugin.entrySet().iterator();
    while (it.hasNext()) {
        final Map.Entry<String, List<Command>> entry = it.next();
        final String pluginName = entry.getKey();
        final List<Command> commands = entry.getValue();
        Collections.sort(commands, new CommandComparator());
        responseBody.append(heading(pluginName, 2)).append("<ul>\r\n");
        Displayable lastTfmCommandLevel = null;
        for (Command command : commands) {
            if (!TotalFreedomMod.pluginName.equals(pluginName)) {
                responseBody.append(buildDescription(command));
                continue;
            }
            Displayable tfmCommandLevel = FreedomCommand.getFrom(command).getPerms().level();
            if (lastTfmCommandLevel == null || lastTfmCommandLevel != tfmCommandLevel) {
                responseBody.append("</ul>\r\n").append(heading(tfmCommandLevel.getName(), 3)).append("<ul>\r\n");
            }
            lastTfmCommandLevel = tfmCommandLevel;
            responseBody.append(buildDescription(command));
        }
        responseBody.append("</ul>\r\n");
    }
    return responseBody.toString();
}
Also used : Displayable(me.totalfreedom.totalfreedommod.rank.Displayable) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) HashMap(java.util.HashMap) PluginIdentifiableCommand(org.bukkit.command.PluginIdentifiableCommand) PluginIdentifiableCommand(org.bukkit.command.PluginIdentifiableCommand) FreedomCommand(me.totalfreedom.totalfreedommod.command.FreedomCommand) Command(org.bukkit.command.Command) List(java.util.List) HashMap(java.util.HashMap) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Map(java.util.Map) CommandMap(org.bukkit.command.CommandMap) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) CommandMap(org.bukkit.command.CommandMap)

Example 3 with Command

use of org.bukkit.command.Command in project SpaciousLib by anhcraft.

the class SPlugin method unloadPlugin.

public static boolean unloadPlugin(Plugin plugin) throws NoSuchFieldException, IllegalAccessException, IOException {
    String name = plugin.getName();
    PluginManager pluginManager = Bukkit.getPluginManager();
    SimpleCommandMap commandMap = null;
    List<Plugin> plugins = null;
    Map<String, Plugin> names = null;
    Map<String, Command> commands = null;
    disablePlugin(plugin);
    if (pluginManager != null) {
        Field pluginsField = Bukkit.getPluginManager().getClass().getDeclaredField("plugins");
        Field lookupNamesField = Bukkit.getPluginManager().getClass().getDeclaredField("lookupNames");
        Field commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
        Field commandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
        commandsField.setAccessible(true);
        pluginsField.setAccessible(true);
        commandMapField.setAccessible(true);
        lookupNamesField.setAccessible(true);
        commandMap = (SimpleCommandMap) commandMapField.get(pluginManager);
        commands = (Map<String, Command>) commandsField.get(commandMap);
        plugins = (List<Plugin>) pluginsField.get(pluginManager);
        names = (Map<String, Plugin>) lookupNamesField.get(pluginManager);
    }
    if (plugins != null && plugins.contains(plugin)) {
        plugins.remove(plugin);
    }
    if (names != null && names.containsKey(name)) {
        names.remove(name);
    }
    if (commandMap != null) {
        for (Iterator<Map.Entry<String, Command>> it = commands.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, Command> entry = it.next();
            if (entry.getValue() instanceof PluginCommand) {
                PluginCommand c = (PluginCommand) entry.getValue();
                if (c.getPlugin() == plugin) {
                    c.unregister(commandMap);
                    it.remove();
                }
            }
        }
    }
    ClassLoader cl = plugin.getClass().getClassLoader();
    if (cl instanceof URLClassLoader) {
        ((URLClassLoader) cl).close();
    }
    System.gc();
    return true;
}
Also used : SimpleCommandMap(org.bukkit.command.SimpleCommandMap) PluginManager(org.bukkit.plugin.PluginManager) Field(java.lang.reflect.Field) PluginCommand(org.bukkit.command.PluginCommand) Command(org.bukkit.command.Command) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Plugin(org.bukkit.plugin.Plugin) PluginCommand(org.bukkit.command.PluginCommand)

Example 4 with Command

use of org.bukkit.command.Command in project SpaciousLib by anhcraft.

the class CommandManager method unregister.

/**
 * Unregisters a specific plugin command
 * @param plugin the plugin which owned that command
 * @param command the command
 */
public static void unregister(JavaPlugin plugin, PluginCommand command) {
    try {
        Class<?> craftServerClass = Class.forName("org.bukkit.craftbukkit." + GameVersion.getVersion().toString() + ".CraftServer");
        Object craftServer = craftServerClass.cast(Bukkit.getServer());
        Field commandMapField = craftServerClass.getDeclaredField("commandMap");
        commandMapField.setAccessible(true);
        SimpleCommandMap commandMap = (SimpleCommandMap) commandMapField.get(craftServer);
        Field knownCommandsField = commandMap.getClass().getDeclaredField("knownCommands");
        knownCommandsField.setAccessible(true);
        Map<String, Command> knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
        knownCommands.remove(plugin.getName() + ":" + command.getName());
        for (String alias : command.getAliases()) {
            alias = plugin.getName() + ":" + alias;
            if (knownCommands.containsKey(alias) && knownCommands.get(alias).toString().contains(command.getName())) {
                knownCommands.remove(alias);
            }
        }
        knownCommandsField.set(commandMap, knownCommands);
        commandMapField.set(craftServer, commandMap);
        Field simplePluginManagerField = craftServerClass.getDeclaredField("pluginManager");
        simplePluginManagerField.setAccessible(true);
        SimplePluginManager simplePluginManager = (SimplePluginManager) simplePluginManagerField.get(craftServer);
        Field commandMapPluginManagerField = SimplePluginManager.class.getDeclaredField("commandMap");
        commandMapPluginManagerField.setAccessible(true);
        SimpleCommandMap commandPluginManagerMap = (SimpleCommandMap) commandMapPluginManagerField.get(simplePluginManager);
        Field knownCommandsPluginManagerField = commandPluginManagerMap.getClass().getDeclaredField("knownCommands");
        knownCommandsPluginManagerField.setAccessible(true);
        Map<String, Command> knownCommandsPluginManager = (Map<String, Command>) knownCommandsPluginManagerField.get(commandPluginManagerMap);
        knownCommandsPluginManager.remove(plugin.getName() + ":" + command.getName());
        for (String alias : command.getAliases()) {
            alias = plugin.getName() + ":" + alias;
            if (knownCommandsPluginManager.containsKey(alias) && knownCommandsPluginManager.get(alias).toString().contains(command.getName())) {
                knownCommandsPluginManager.remove(alias);
            }
        }
        knownCommandsPluginManagerField.set(commandPluginManagerMap, knownCommandsPluginManager);
        commandMapPluginManagerField.set(simplePluginManager, commandPluginManagerMap);
        simplePluginManagerField.set(craftServer, simplePluginManager);
    } catch (IllegalAccessException | NoSuchFieldException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Field(java.lang.reflect.Field) Command(org.bukkit.command.Command) PluginCommand(org.bukkit.command.PluginCommand) SimplePluginManager(org.bukkit.plugin.SimplePluginManager) Map(java.util.Map) SimpleCommandMap(org.bukkit.command.SimpleCommandMap)

Example 5 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)

Aggregations

Command (org.bukkit.command.Command)29 SimpleCommandMap (org.bukkit.command.SimpleCommandMap)10 CommandMap (org.bukkit.command.CommandMap)8 PluginCommand (org.bukkit.command.PluginCommand)7 Field (java.lang.reflect.Field)6 Player (org.bukkit.entity.Player)6 Map (java.util.Map)5 PluginIdentifiableCommand (org.bukkit.command.PluginIdentifiableCommand)5 HashMap (java.util.HashMap)4 Plugin (org.bukkit.plugin.Plugin)4 List (java.util.List)3 CommandSender (org.bukkit.command.CommandSender)3 EventHandler (org.bukkit.event.EventHandler)3 JavaPlugin (org.bukkit.plugin.java.JavaPlugin)3 Method (java.lang.reflect.Method)2 URLClassLoader (java.net.URLClassLoader)2 HashSet (java.util.HashSet)2 Entry (java.util.Map.Entry)2 Server (org.bukkit.Server)2 PluginManager (org.bukkit.plugin.PluginManager)2