java类org.bukkit.command.CommandMap的实例源码

CommandMapUtil.java 文件源码 项目:helper 阅读 35 收藏 0 点赞 0 评论 0
/**
 * Unregisters a CommandExecutor with the server
 *
 * @param command the command instance
 * @param <T> the command executor class type
 * @return the command executor
 */
@Nonnull
public static <T extends CommandExecutor> T unregisterCommand(@Nonnull T command) {
    CommandMap map = getCommandMap();
    try {
        //noinspection unchecked
        Map<String, Command> knownCommands = (Map<String, Command>) KNOWN_COMMANDS_FIELD.get(map);

        Iterator<Command> iterator = knownCommands.values().iterator();
        while (iterator.hasNext()) {
            Command cmd = iterator.next();
            if (cmd instanceof PluginCommand) {
                CommandExecutor executor = ((PluginCommand) cmd).getExecutor();
                if (command == executor) {
                    cmd.unregister(map);
                    iterator.remove();
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Could not unregister command", e);
    }

    return command;
}
CentralPoint.java 文件源码 项目:Lukkit 阅读 24 收藏 0 点赞 0 评论 0
@Override
public LuaValue onCalled(Varargs parameters) {
    String name = parameters.arg(1).tojstring();
    String desc = parameters.arg(2).tojstring();
    String usage = parameters.arg(3).tojstring();
    LuaValue func = parameters.arg(4);
    DynamicCommand command = new DynamicCommand(name, desc, usage, func);
    try {
        Field cmdMapField = SimplePluginManager.class.getDeclaredField("commandMap");
        cmdMapField.setAccessible(true);
        CommandMap commandMap = (CommandMap) cmdMapField.get(Bukkit.getPluginManager());
        commandMap.register(command.getName(), command);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return LuaValue.NIL;
}
CommandFramework.java 文件源码 项目:ServerCommons 阅读 28 收藏 0 点赞 0 评论 0
public CommandFramework(Plugin plugin) {
    this.plugin = plugin;

    if (plugin.getServer().getPluginManager() instanceof SimplePluginManager) {
        SimplePluginManager manager = (SimplePluginManager) plugin.getServer().getPluginManager();

        try {
            Field field = SimplePluginManager.class.getDeclaredField("commandMap");
            field.setAccessible(true);
            this.map = (CommandMap) field.get(manager);
        }
        catch (IllegalArgumentException | SecurityException | NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
CommandManager.java 文件源码 项目:EchoPet 阅读 23 收藏 0 点赞 0 评论 0
public boolean unregister() {
    CommandMap commandMap = getCommandMap();
    List<String> toRemove = new ArrayList<String>();
    Map<String, Command> knownCommands = KNOWN_COMMANDS.get(commandMap);
    if (knownCommands == null) {
        return false;
    }
    for (Iterator<Command> i = knownCommands.values().iterator(); i.hasNext(); ) {
        Command cmd = i.next();
        if (cmd instanceof DynamicPluginCommand) {
            i.remove();
            for (String alias : cmd.getAliases()) {
                Command aliasCmd = knownCommands.get(alias);
                if (cmd.equals(aliasCmd)) {
                    toRemove.add(alias);
                }
            }
        }
    }
    for (String string : toRemove) {
        knownCommands.remove(string);
    }
    return true;
}
CommandManager.java 文件源码 项目:EchoPet 阅读 24 收藏 0 点赞 0 评论 0
public CommandMap getCommandMap() {
    if (!(Bukkit.getPluginManager() instanceof SimplePluginManager)) {
        this.plugin.getLogger().warning("Seems like your server is using a custom PluginManager? Well let's try injecting our custom commands anyways...");
    }

    CommandMap map = null;

    try {
        map = SERVER_COMMAND_MAP.get(Bukkit.getPluginManager());

        if (map == null) {
            if (fallback != null) {
                return fallback;
            } else {
                fallback = map = new SimpleCommandMap(EchoPet.getPlugin().getServer());
                Bukkit.getPluginManager().registerEvents(new FallbackCommandRegistrationListener(fallback), this.plugin);
            }
        }
    } catch (Exception pie) {
        this.plugin.getLogger().warning("Failed to dynamically register the commands! Let's give it a last shot...");
        // Hmmm.... Pie...
        fallback = map = new SimpleCommandMap(EchoPet.getPlugin().getServer());
        Bukkit.getPluginManager().registerEvents(new FallbackCommandRegistrationListener(fallback), this.plugin);
    }
    return map;
}
CommandManager.java 文件源码 项目:SonarPet 阅读 24 收藏 0 点赞 0 评论 0
public boolean unregister() {
    CommandMap commandMap = getCommandMap();
    List<String> toRemove = new ArrayList<String>();
    Map<String, Command> knownCommands = KNOWN_COMMANDS.get(commandMap);
    if (knownCommands == null) {
        return false;
    }
    for (Iterator<Command> i = knownCommands.values().iterator(); i.hasNext(); ) {
        Command cmd = i.next();
        if (cmd instanceof DynamicPluginCommand) {
            i.remove();
            for (String alias : cmd.getAliases()) {
                Command aliasCmd = knownCommands.get(alias);
                if (cmd.equals(aliasCmd)) {
                    toRemove.add(alias);
                }
            }
        }
    }
    for (String string : toRemove) {
        knownCommands.remove(string);
    }
    return true;
}
CommandManager.java 文件源码 项目:SonarPet 阅读 25 收藏 0 点赞 0 评论 0
public CommandMap getCommandMap() {
    if (!(Bukkit.getPluginManager() instanceof SimplePluginManager)) {
        this.plugin.getLogger().warning("Seems like your server is using a custom PluginManager? Well let's try injecting our custom commands anyways...");
    }

    CommandMap map = null;

    try {
        map = SERVER_COMMAND_MAP.get(Bukkit.getPluginManager());

        if (map == null) {
            if (fallback != null) {
                return fallback;
            } else {
                fallback = map = new SimpleCommandMap(EchoPet.getPlugin().getServer());
                Bukkit.getPluginManager().registerEvents(new FallbackCommandRegistrationListener(fallback), this.plugin);
            }
        }
    } catch (Exception pie) {
        this.plugin.getLogger().warning("Failed to dynamically register the commands! Let's give it a last shot...");
        // Hmmm.... Pie...
        fallback = map = new SimpleCommandMap(EchoPet.getPlugin().getServer());
        Bukkit.getPluginManager().registerEvents(new FallbackCommandRegistrationListener(fallback), this.plugin);
    }
    return map;
}
PluginHelper.java 文件源码 项目:EnderChest 阅读 25 收藏 0 点赞 0 评论 0
@SneakyThrows
public static void addExecutor(Plugin plugin, Command command) {
    Field f = SimplePluginManager.class.getDeclaredField("commandMap");
    f.setAccessible(true);
    CommandMap map = (CommandMap) f.get(plugin.getServer().getPluginManager());
    Constructor<PluginCommand> init = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
    init.setAccessible(true);
    PluginCommand inject = init.newInstance(command.getName(), plugin);
    inject.setExecutor((who, __, label, input) -> command.execute(who, label, input));
    inject.setAliases(command.getAliases());
    inject.setDescription(command.getDescription());
    inject.setLabel(command.getLabel());
    inject.setName(command.getName());
    inject.setPermission(command.getPermission());
    inject.setPermissionMessage(command.getPermissionMessage());
    inject.setUsage(command.getUsage());
    map.register(plugin.getName().toLowerCase(), inject);
}
SpleefCommand.java 文件源码 项目:EpicSpleef 阅读 27 收藏 0 点赞 0 评论 0
/**
 * Unregisters the command.
 */
public void unregister() {  
    try {
        Field fMap = Command.class.getDeclaredField("commandMap");
        fMap.setAccessible(true);
        CommandMap map = (CommandMap) fMap.get(this);
        this.unregister(map);

        Field fKnownCommands = map.getClass().getDeclaredField("knownCommands");
        fKnownCommands.setAccessible(true);
        @SuppressWarnings("unchecked")
        HashMap<String, Command> knownCommands = (HashMap<String, Command>) fKnownCommands.get(map);
        for (Entry<String, Command> entry : knownCommands.entrySet()) {
            if (entry.getValue() == this) {
                knownCommands.remove(entry.getKey());
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
BukkitCommandHandler.java 文件源码 项目:Zephyr 阅读 30 收藏 0 点赞 0 评论 0
public BukkitCommandHandler(Plugin plugin) {
    cmdMap = new HashMap<String, BukkitExecutor>();
    this.plugin = plugin;

    if (plugin.getServer().getPluginManager() instanceof SimplePluginManager) {
        SimplePluginManager manager = (SimplePluginManager) plugin
                .getServer().getPluginManager();
        try {
            Field field = SimplePluginManager.class
                    .getDeclaredField("commandMap");
            field.setAccessible(true);
            map = (CommandMap) field.get(manager);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
ShopCommand.java 文件源码 项目:ShopChest 阅读 23 收藏 0 点赞 0 评论 0
private void register() {
    if (pluginCommand == null) return;

    plugin.debug("Registering command " + name);

    try {
        Field f = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
        f.setAccessible(true);

        Object commandMapObject = f.get(Bukkit.getPluginManager());
        if (commandMapObject instanceof CommandMap) {
            CommandMap commandMap = (CommandMap) commandMapObject;
            commandMap.register(plugin.getName(), pluginCommand);
        }
    } catch (NoSuchFieldException | IllegalAccessException e) {
        plugin.getLogger().severe("Failed to register command");
        plugin.debug("Failed to register plugin command");
        plugin.debug(e);
    }
}
ReflectiveCommandRegistrar.java 文件源码 项目:Chatterbox 阅读 28 收藏 0 点赞 0 评论 0
/**
 * Registers a command in the server's CommandMap.
 *
 * @param ce CommandExecutor to be registered
 * @param rc ReflectCommand the command was annotated with
 */
public void registerCommand(@NotNull final BaseCommand<? extends Plugin> ce, @NotNull final ReflectCommand rc) {
    Preconditions.checkNotNull(ce, "ce was null");
    Preconditions.checkNotNull(rc, "rc was null");
    try {
        final Constructor c = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
        c.setAccessible(true);
        final PluginCommand pc = (PluginCommand) c.newInstance(rc.name(), this.plugin);
        pc.setExecutor(ce);
        pc.setAliases(Arrays.asList(rc.aliases()));
        pc.setDescription(rc.description());
        pc.setUsage(rc.usage());
        final CommandMap cm = this.getCommandMap();
        if (cm == null) {
            this.plugin.getLogger().warning("CommandMap was null. Command " + rc.name() + " not registered.");
            return;
        }
        cm.register(this.plugin.getDescription().getName(), pc);
        this.commandHandler.addCommand(new CommandCoupling(ce, pc));
    } catch (Exception e) {
        this.plugin.getLogger().warning("Could not register command \"" + rc.name() + "\" - an error occurred: " + e.getMessage() + ".");
    }
}
PluginHelper.java 文件源码 项目:PlayerSQL 阅读 27 收藏 0 点赞 0 评论 0
@SneakyThrows
public static void addExecutor(Plugin plugin, Command command) {
    Field f = SimplePluginManager.class.getDeclaredField("commandMap");
    f.setAccessible(true);
    CommandMap map = (CommandMap) f.get(plugin.getServer().getPluginManager());
    Constructor<PluginCommand> init = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
    init.setAccessible(true);
    PluginCommand inject = init.newInstance(command.getName(), plugin);
    inject.setExecutor((who, __, label, input) -> command.execute(who, label, input));
    inject.setAliases(command.getAliases());
    inject.setDescription(command.getDescription());
    inject.setLabel(command.getLabel());
    inject.setName(command.getName());
    inject.setPermission(command.getPermission());
    inject.setPermissionMessage(command.getPermissionMessage());
    inject.setUsage(command.getUsage());
    map.register(plugin.getName().toLowerCase(), inject);
}
SpleefCommand.java 文件源码 项目:EpicSpleef 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Unregisters the command.
 */
public void unregister() {  
    try {
        Field fMap = Command.class.getDeclaredField("commandMap");
        fMap.setAccessible(true);
        CommandMap map = (CommandMap) fMap.get(this);
        this.unregister(map);

        Field fKnownCommands = map.getClass().getDeclaredField("knownCommands");
        fKnownCommands.setAccessible(true);
        @SuppressWarnings("unchecked")
        HashMap<String, Command> knownCommands = (HashMap<String, Command>) fKnownCommands.get(map);
        for (Entry<String, Command> entry : knownCommands.entrySet()) {
            if (entry.getValue() == this) {
                knownCommands.remove(entry.getKey());
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
CommandFramework.java 文件源码 项目:PartyLobby 阅读 28 收藏 0 点赞 0 评论 0
/**
 * Initializes the command framework and sets up the command maps
 * 
 * @param plugin
 */
public CommandFramework(Plugin plugin) {
    this.plugin = plugin;
    if (plugin.getServer().getPluginManager() instanceof SimplePluginManager) {
        SimplePluginManager manager = (SimplePluginManager) plugin
                .getServer().getPluginManager();
        try {
            Field field = SimplePluginManager.class
                    .getDeclaredField("commandMap");
            field.setAccessible(true);
            map = (CommandMap) field.get(manager);
        } catch (IllegalArgumentException | NoSuchFieldException
                | IllegalAccessException | SecurityException e) {
            e.printStackTrace();
        }
    }
}
TFM_CommandLoader.java 文件源码 项目:TatsuCraftMod 阅读 26 收藏 0 点赞 0 评论 0
public void unregisterCommand(Command command, CommandMap commandMap)
{
    try
    {
        command.unregister(commandMap);
        HashMap<String, Command> knownCommands = getKnownCommands(commandMap);
        if (knownCommands != null)
        {
            knownCommands.remove(command.getName());
            for (String alias : command.getAliases())
            {
                knownCommands.remove(alias);
            }
        }
    }
    catch (Exception ex)
    {
        TFM_Log.severe(ex);
    }
}
AbyssPlugin.java 文件源码 项目:Abyss 阅读 25 收藏 0 点赞 0 评论 0
private CommandMap getCommandMap() {
    final PluginManager m = getServer().getPluginManager();
    if (! (m instanceof SimplePluginManager) )
        return null;

    try {
        final Field f = SimplePluginManager.class.getDeclaredField("commandMap");
        f.setAccessible(true);

        Object map = f.get(m);
        if ( map instanceof CommandMap )
            return (CommandMap) map;

    } catch(final Exception e) { }

    return null;
}
CommandMapUtil.java 文件源码 项目:helper 阅读 28 收藏 0 点赞 0 评论 0
private static CommandMap getCommandMap() {
    try {
        return (CommandMap) COMMAND_MAP_FIELD.get(Bukkit.getServer().getPluginManager());
    } catch (Exception e) {
        throw new RuntimeException("Could not get CommandMap", e);
    }
}
PluginHelper.java 文件源码 项目:chatcolor 阅读 27 收藏 0 点赞 0 评论 0
public static void addExecutor(Plugin plugin, Command command) {
    try {
        Field f = SimplePluginManager.class.getDeclaredField("commandMap");
        f.setAccessible(true);
        CommandMap map = (CommandMap) f.get(plugin.getServer().getPluginManager());
        map.register(plugin.getName().toLowerCase(), command);
    } catch (ReflectiveOperationException e) {
        e.printStackTrace();
    }
}
Registry.java 文件源码 项目:SuperiorCraft 阅读 27 收藏 0 点赞 0 评论 0
public static void registerCommand(CustomCommand command) {
    try {
        Field f = NMSAdapter.getCraftBukkitClass("CraftServer").getDeclaredField("commandMap");
        f.setAccessible(true);
        CommandMap cmap = (CommandMap) f.get(Bukkit.getServer());
        cmap.register("", command);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
CommandManager.java 文件源码 项目:MystiCraft 阅读 31 收藏 0 点赞 0 评论 0
public CommandManager() {
    if (Bukkit.getServer().getPluginManager() instanceof SimplePluginManager) {
        SimplePluginManager manager = (SimplePluginManager) Bukkit.getServer().getPluginManager();
        try {
            Field field = SimplePluginManager.class.getDeclaredField("commandMap");
            field.setAccessible(true);
            map = (CommandMap) field.get(manager);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
SimpleCommand.java 文件源码 项目:AlphaLibary 阅读 25 收藏 0 点赞 0 评论 0
private void register() {
    try {
        Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
        f.setAccessible(true);

        CommandMap map = (CommandMap) f.get(Bukkit.getServer());
        map.register("AlphaLibary", this);
    } catch (Exception exc) {
        exc.printStackTrace();
    }
}
LoggedPluginManager.java 文件源码 项目:VoxelGamesLibv2 阅读 40 收藏 0 点赞 0 评论 0
public LoggedPluginManager(Plugin owner) {
    this.owner = owner;
    this.delegate = owner.getServer().getPluginManager();
    try {
        Field commandMapField = Bukkit.getServer().getClass().getDeclaredField("commandMap");
        commandMapField.setAccessible(true);
        commandMap = (CommandMap) commandMapField.get(Bukkit.getServer());
    } catch (ReflectiveOperationException ignored) {

    }
}
CommandManager.java 文件源码 项目:MCLibrary 阅读 36 收藏 0 点赞 0 评论 0
public CommandManager() {
    PluginManager manager = Bukkit.getPluginManager();
    try {
        Field mapField = SimplePluginManager.class.getDeclaredField("commandMap");
        mapField.setAccessible(true);
        commandMap = (CommandMap) mapField.get(manager);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
}
Commands.java 文件源码 项目:McLink 阅读 38 收藏 0 点赞 0 评论 0
public static CommandMap getCommandMap() {
    try {
        Field f = BukkitAdapter.getBukkitClass("CraftServer").getDeclaredField("commandMap");
        f.setAccessible(true);
        return (CommandMap) f.get(Bukkit.getServer());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}
OMGCommand.java 文件源码 项目:OMGPI 阅读 30 收藏 0 点赞 0 评论 0
/**
 * Create and register a new command.
 *
 * @param name       Command name used by default in /&lt;name&gt;.
 * @param permission Permission needed to execute the command.
 * @param aliases    Aliases which can be used to execute the command like /&lt;alias&gt;.
 */
public OMGCommand(String name, String permission, String... aliases) {
    this.name = name;
    this.permission = permission;
    this.aliases = aliases;
    registeredCommands.add(this);
    bukkit = new Command(name, "", "", Arrays.asList(aliases)) {
        public boolean execute(CommandSender sender, String label, String... args) {
            if (permission == null || sender.hasPermission(permission)) {
                lastargscall = args;
                if (args.length == 0) call(sender, label);
                if (args.length == 1) call(sender, label, args[0]);
                if (args.length == 2) call(sender, label, args[0], args[1]);
                if (args.length == 3) call(sender, label, args[0], args[1], args[2]);
                if (args.length == 4) call(sender, label, args[0], args[1], args[2], args[3]);
                call(sender, label, args);
            } else
                sender.sendMessage(ChatColor.DARK_AQUA + "You don't have enough permissions to execute this command.");
            return true;
        }
    };
    try {
        ((CommandMap) ReflectionUtils.getClazz(cbclasses, "CraftServer").getDeclaredMethod("getCommandMap").invoke(Bukkit.getServer())).register("omgpi", bukkit);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
OMGCommand.java 文件源码 项目:OMGPI 阅读 25 收藏 0 点赞 0 评论 0
/**
 * Unregister the command from bukkit.
 */
public void unregister() {
    try {
        bukkit.unregister((CommandMap) ReflectionUtils.getClazz(cbclasses, "CraftServer").getDeclaredMethod("getCommandMap").invoke(Bukkit.getServer()));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
CommandManager.java 文件源码 项目:Bukkit-Utilities 阅读 24 收藏 0 点赞 0 评论 0
private static CommandMap getCommandMap() {
  if (!(Bukkit.getPluginManager() instanceof SimplePluginManager)) throw new IllegalStateException("PluginManager instance is not SimplePluginManager");
  try {
    Field field = SimplePluginManager.class.getDeclaredField("commandMap");
    field.setAccessible(true);
    return (SimpleCommandMap) field.get(Bukkit.getPluginManager());
  } catch (IllegalAccessException | NoSuchFieldException excepted) {
    excepted.printStackTrace();
  }
  return null;
}
Registry.java 文件源码 项目:SuperiorCraft 阅读 44 收藏 0 点赞 0 评论 0
public static void registerCommand(CustomCommand command) {
    try {
        Field f = NMSAdapter.getCraftBukkitClass("CraftServer").getDeclaredField("commandMap");
        f.setAccessible(true);
        CommandMap cmap = (CommandMap) f.get(Bukkit.getServer());
        cmap.register("", command);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
CommandManager.java 文件源码 项目:DiscordBot 阅读 25 收藏 0 点赞 0 评论 0
private CommandMap getCommandMap() {
    if (!(Bukkit.getPluginManager() instanceof SimplePluginManager)) {
        return null;
    }

    try {
        Field field = SimplePluginManager.class.getDeclaredField("commandMap");
        field.setAccessible(true);
        return (CommandMap) field.get(Bukkit.getPluginManager());
    } catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException | SecurityException ex) {
        DiscordBot.getInstance().getLogger().severe("Exception getting commandMap!");
        ex.printStackTrace();
    }
    return null;
}


问题


面经


文章

微信
公众号

扫码关注公众号