Search in sources :

Example 1 with PluginCommand

use of org.bukkit.command.PluginCommand 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 2 with PluginCommand

use of org.bukkit.command.PluginCommand in project Essentials by drtshock.

the class EssentialsPlayerListener method onPlayerCommandPreprocess.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) {
    final Player player = event.getPlayer();
    final String cmd = event.getMessage().toLowerCase(Locale.ENGLISH).split(" ")[0].replace("/", "").toLowerCase(Locale.ENGLISH);
    if (ess.getUser(player).isMuted() && (ess.getSettings().getMuteCommands().contains(cmd) || ess.getSettings().getMuteCommands().contains("*"))) {
        event.setCancelled(true);
        player.sendMessage(tl("voiceSilenced"));
        LOGGER.info(tl("mutedUserSpeaks", player.getName()));
        return;
    }
    PluginCommand pluginCommand = ess.getServer().getPluginCommand(cmd);
    if (ess.getSettings().getSocialSpyCommands().contains(cmd) || ess.getSettings().getSocialSpyCommands().contains("*")) {
        if (pluginCommand == null || (!pluginCommand.getName().equals("msg") && !pluginCommand.getName().equals("r"))) {
            // /msg and /r are handled in SimpleMessageRecipient
            User user = ess.getUser(player);
            if (!user.isAuthorized("essentials.chat.spy.exempt")) {
                for (User spyer : ess.getOnlineUsers()) {
                    if (spyer.isSocialSpyEnabled() && !player.equals(spyer.getBase())) {
                        spyer.sendMessage(tl("socialSpyPrefix") + player.getDisplayName() + ": " + event.getMessage());
                    }
                }
            }
        }
    }
    // whether to broadcast the updated activity
    boolean broadcast = true;
    // Only modified when the command is afk
    boolean update = true;
    if (pluginCommand != null) {
        // Switch case for commands that shouldn't broadcast afk activity.
        switch(pluginCommand.getName()) {
            case "afk":
                update = false;
            case "vanish":
                broadcast = false;
        }
    }
    final User user = ess.getUser(player);
    if (update) {
        user.updateActivity(broadcast);
    }
    if (ess.getSettings().isCommandCooldownsEnabled() && pluginCommand != null && !user.isAuthorized("essentials.commandcooldowns.bypass")) {
        int argStartIndex = event.getMessage().indexOf(" ");
        String args = // No arguments present 
        argStartIndex == -1 ? // No arguments present 
        "" : // arguments start at argStartIndex; substring from there.
        " " + event.getMessage().substring(argStartIndex);
        String fullCommand = pluginCommand.getName() + args;
        // Used to determine whether a user already has an existing cooldown
        // If so, no need to check for (and write) new ones.
        boolean cooldownFound = false;
        // Iterate over a copy of getCommandCooldowns in case of concurrent modifications
        for (Entry<Pattern, Long> entry : new HashMap<>(user.getCommandCooldowns()).entrySet()) {
            // Remove any expired cooldowns
            if (entry.getValue() <= System.currentTimeMillis()) {
                user.clearCommandCooldown(entry.getKey());
            // Don't break in case there are other command cooldowns left to clear.
            } else if (entry.getKey().matcher(fullCommand).matches()) {
                // User's current cooldown hasn't expired, inform and terminate cooldown code.
                if (entry.getValue() > System.currentTimeMillis()) {
                    String commandCooldownTime = DateUtil.formatDateDiff(entry.getValue());
                    user.sendMessage(tl("commandCooldown", commandCooldownTime));
                    cooldownFound = true;
                    event.setCancelled(true);
                    break;
                }
            }
        }
        if (!cooldownFound) {
            Entry<Pattern, Long> cooldownEntry = ess.getSettings().getCommandCooldownEntry(fullCommand);
            if (cooldownEntry != null) {
                if (ess.getSettings().isDebug()) {
                    ess.getLogger().info("Applying " + cooldownEntry.getValue() + "ms cooldown on /" + fullCommand + " for" + user.getName() + ".");
                }
                Date expiry = new Date(System.currentTimeMillis() + cooldownEntry.getValue());
                user.addCommandCooldown(cooldownEntry.getKey(), expiry, ess.getSettings().isCommandCooldownPersistent(fullCommand));
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Player(org.bukkit.entity.Player) Date(java.util.Date) PluginCommand(org.bukkit.command.PluginCommand) EventHandler(org.bukkit.event.EventHandler)

Example 3 with PluginCommand

use of org.bukkit.command.PluginCommand in project Essentials by drtshock.

the class AlternativeCommandsHandler method removePlugin.

public void removePlugin(final Plugin plugin) {
    final Iterator<Map.Entry<String, List<PluginCommand>>> iterator = altcommands.entrySet().iterator();
    while (iterator.hasNext()) {
        final Map.Entry<String, List<PluginCommand>> entry = iterator.next();
        final Iterator<PluginCommand> pcIterator = entry.getValue().iterator();
        while (pcIterator.hasNext()) {
            final PluginCommand pc = pcIterator.next();
            if (pc.getPlugin() == null || pc.getPlugin().equals(plugin)) {
                pcIterator.remove();
            }
        }
        if (entry.getValue().isEmpty()) {
            iterator.remove();
        }
    }
}
Also used : PluginCommand(org.bukkit.command.PluginCommand)

Example 4 with PluginCommand

use of org.bukkit.command.PluginCommand in project DiscordBot by LXGaming.

the class CommandManager method registerCommand.

public void registerCommand(String... aliases) {
    PluginCommand pluginCommand = getCommand(aliases[0], DiscordBot.getInstance());
    pluginCommand.setAliases(Arrays.asList(aliases));
    getCommandMap().register(DiscordBot.getInstance().getDescription().getName(), pluginCommand);
}
Also used : PluginCommand(org.bukkit.command.PluginCommand)

Example 5 with PluginCommand

use of org.bukkit.command.PluginCommand 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)

Aggregations

PluginCommand (org.bukkit.command.PluginCommand)7 Command (org.bukkit.command.Command)3 Field (java.lang.reflect.Field)2 SimpleCommandMap (org.bukkit.command.SimpleCommandMap)2 URLClassLoader (java.net.URLClassLoader)1 Date (java.util.Date)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 Plugin (org.bukkit.plugin.Plugin)1 PluginManager (org.bukkit.plugin.PluginManager)1 SimplePluginManager (org.bukkit.plugin.SimplePluginManager)1