类org.bukkit.command.CommandMap源码实例Demo

下面列出了怎么用org.bukkit.command.CommandMap的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: mcspring-boot   文件: CommandService.java
@SneakyThrows
public void unregisterCommand(CommandSpec commandSpec) {
    val commandName = commandSpec.name();
    val manager = (SimplePluginManager) plugin.getServer().getPluginManager();

    val commandMapField = SimplePluginManager.class.getDeclaredField("commandMap");
    commandMapField.setAccessible(true);
    val map = (CommandMap) commandMapField.get(manager);

    val knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
    knownCommandsField.setAccessible(true);
    val knownCommands = (Map<String, Command>) knownCommandsField.get(map);

    val command = knownCommands.get(commandName);
    if (command != null) {
        command.unregister(map);
        knownCommands.remove(commandName);
    }
}
 
源代码2 项目: helper   文件: CommandMapUtil.java
/**
 * 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;
}
 
源代码3 项目: SonarPet   文件: CommandManager.java
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;
}
 
源代码4 项目: SonarPet   文件: CommandManager.java
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;
}
 
源代码5 项目: ShopChest   文件: ShopCommand.java
private void register() {
    if (pluginCommand == null) return;

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

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

        Object commandMapObject = fCommandMap.get(Bukkit.getPluginManager());
        if (commandMapObject instanceof CommandMap) {
            CommandMap commandMap = (CommandMap) commandMapObject;
            commandMap.register(fallbackPrefix, pluginCommand);
        }
    } catch (NoSuchFieldException | IllegalAccessException e) {
        plugin.getLogger().severe("Failed to register command");
        plugin.debug("Failed to register plugin command");
        plugin.debug(e);
    }
}
 
源代码6 项目: PlayerSQL   文件: PluginHelper.java
@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);
}
 
源代码7 项目: FunnyGuilds   文件: ExecutorCaller.java
private void register() {
    try {
        Performer p = new Performer(this.overriding);
        if (this.aliases != null) {
            p.setAliases(this.aliases);
        }

        p.setPermissionMessage(FunnyGuilds.getInstance().getMessageConfiguration().permission);
        Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
        f.setAccessible(true);
        
        CommandMap cmap = (CommandMap) f.get(Bukkit.getServer());
        cmap.register(FunnyGuilds.getInstance().getPluginConfiguration().pluginName, p);
        p.setExecutor(this);
    }
    catch (Exception ex) {
        FunnyGuilds.getInstance().getPluginLogger().error("Could not register command", ex);
    }
}
 
源代码8 项目: EchoPet   文件: CommandManager.java
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;
}
 
源代码9 项目: EchoPet   文件: CommandManager.java
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;
}
 
源代码10 项目: intake   文件: BukkitIntake.java
public CommandMap getCommandMap() {
  try {
    PluginManager manager = plugin.getServer().getPluginManager();
    Field field = manager.getClass().getDeclaredField("commandMap");
    field.setAccessible(true);
    return CommandMap.class.cast(field.get(manager));
  } catch (NoSuchFieldException | IllegalAccessException error) {
    throw new IllegalStateException("Intake could not find CommandMap from server", error);
  }
}
 
源代码11 项目: helper   文件: CommandMapUtil.java
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);
    }
}
 
源代码12 项目: VoxelGamesLibv2   文件: LoggedPluginManager.java
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) {

    }
}
 
源代码13 项目: DiscordBot   文件: CommandManager.java
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;
}
 
源代码14 项目: LuckPerms   文件: CommandMapUtil.java
public static CommandMap getCommandMap(Server server) {
    try {
        return (CommandMap) COMMAND_MAP_FIELD.get(server.getPluginManager());
    } catch (Exception e) {
        throw new RuntimeException("Could not get CommandMap", e);
    }
}
 
源代码15 项目: ShopChest   文件: ShopCommand.java
public void unregister() {
    if (pluginCommand == null) return;

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

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

        Object commandMapObject = fCommandMap.get(Bukkit.getPluginManager());
        if (commandMapObject instanceof CommandMap) {
            CommandMap commandMap = (CommandMap) commandMapObject;
            pluginCommand.unregister(commandMap);

            Field fKnownCommands = SimpleCommandMap.class.getDeclaredField("knownCommands");
            fKnownCommands.setAccessible(true);

            Object knownCommandsObject = fKnownCommands.get(commandMap);
            if (knownCommandsObject instanceof Map) {
                Map<?, ?> knownCommands = (Map<?, ?>) knownCommandsObject;
                knownCommands.remove(fallbackPrefix + ":" + name);
                if (pluginCommand.equals(knownCommands.get(name))) {
                    knownCommands.remove(name);
                }
            }
        }
    } catch (NoSuchFieldException | IllegalAccessException e) {
        plugin.getLogger().severe("Failed to unregister command");
        plugin.debug("Failed to unregister plugin command");
        plugin.debug(e);
    }
}
 
源代码16 项目: HubBasics   文件: CommandFramework.java
public void register(Command command) {
    if (command.getDescription().equals("")) {
        command.setDescription("A HubBasics command.");
    }
    try {
        Field field = Bukkit.getServer().getClass().getDeclaredField("commandMap");
        if (!field.isAccessible())
            field.setAccessible(true);
        CommandMap commandMap = (CommandMap) field.get(Bukkit.getServer());
        commandMap.register(command.getName(), command);
        this.commandList.add(command);
    } catch (NoSuchFieldException | IllegalAccessException ex) {
        logger.error("Error while registering command", ex);
    }
}
 
源代码17 项目: HubBasics   文件: CommandFramework.java
public void unregister(Command command) {
    try {
        Field field = Bukkit.getServer().getClass().getDeclaredField("commandMap");
        if (!field.isAccessible())
            field.setAccessible(true);
        CommandMap commandMap = (CommandMap) field.get(Bukkit.getServer());
        command.unregister(commandMap);
        this.commandList.remove(command);
    } catch (NoSuchFieldException | IllegalAccessException ex) {
        logger.error("Error while unregistering command", ex);
    }
}
 
源代码18 项目: BungeePerms   文件: BukkitPlugin.java
private CommandMap getCommandMap()
{
    try
    {
        Field f = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
        f.setAccessible(true);
        return (CommandMap) f.get(Bukkit.getPluginManager());
    }
    catch (Exception ex)
    {
    }
    return null;
}
 
源代码19 项目: Lukkit   文件: LukkitCommand.java
public void register() throws NoSuchFieldException, IllegalAccessException {
    if (getName() == null || getDescription() == null || registered)
        return;
    final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");

    bukkitCommandMap.setAccessible(true);
    CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());

    commandMap.register(plugin.getDescription().getName(), this);
    registered = true;
}
 
源代码20 项目: BlueMap   文件: BukkitPlugin.java
@Override
public void onEnable() {
	new MetricsLite(this);
	
	//save world so the level.dat is present on new worlds
	Logger.global.logInfo("Saving all worlds once, to make sure the level.dat is present...");
	for (World world : getServer().getWorlds()) {
		world.save();
	}
	
	//register events
	getServer().getPluginManager().registerEvents(eventForwarder, this);
	
	//register commands
	try {
		final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");

		bukkitCommandMap.setAccessible(true);
		CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());

		for (BukkitCommand command : commands.getRootCommands()) {
			commandMap.register(command.getLabel(), command);
		}
	} catch(NoSuchFieldException | SecurityException | IllegalAccessException e) {
		Logger.global.logError("Failed to register commands!", e);
	}
	
	//tab completions
	getServer().getPluginManager().registerEvents(commands, this);
	
	//load bluemap
	getServer().getScheduler().runTaskAsynchronously(this, () -> {
		try {
			Logger.global.logInfo("Loading...");
			this.bluemap.load();
			if (bluemap.isLoaded()) Logger.global.logInfo("Loaded!");
		} catch (Throwable t) {
			Logger.global.logError("Failed to load!", t);
		}
	});
}
 
public FallbackCommandRegistrationListener(CommandMap commandMap) {
    this.fallback = commandMap;
}
 
public FallbackRegistrationListener(CommandMap commandRegistration) {
    this.commandRegistration = commandRegistration;
}
 
public FallbackCommandRegistrationListener(CommandMap commandMap) {
    this.fallback = commandMap;
}
 
源代码24 项目: Carbon-2   文件: Utils.java
/**
 * Registers a bukkit command without the need for a plugin.yml entry.
 *
 * @param fallbackPrefix
 * @param cmd
 */
public static void registerBukkitCommand(String fallbackPrefix, Command cmd) {
    CommandMap cmap = ReflectionUtils.getFieldValue(CraftServer.class, "commandMap", Bukkit.getServer());
    cmap.register(fallbackPrefix, cmd);
}
 
源代码25 项目: Carbon-2   文件: Utils.java
/**
 * Registers a bukkit command without the need for a plugin.yml entry.
 *
 * @param fallbackPrefix
 * @param cmd
 */
public static void registerBukkitCommand(String fallbackPrefix, Command cmd) {
    CommandMap cmap = ReflectionUtils.getFieldValue(CraftServer.class, "commandMap", Bukkit.getServer());
    cmap.register(fallbackPrefix, cmd);
}
 
 类所在包
 类方法
 同包方法