org.bukkit.command.SimpleCommandMap Java Examples

The following examples show how to use org.bukkit.command.SimpleCommandMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CommandManager.java    From SonarPet with GNU General Public License v3.0 7 votes vote down vote up
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;
}
 
Example #2
Source File: CommandService.java    From mcspring-boot with MIT License 6 votes vote down vote up
@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);
    }
}
 
Example #3
Source File: CommandManager.java    From EchoPet with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #4
Source File: LukkitCommand.java    From Lukkit with MIT License 6 votes vote down vote up
public void unregister() throws NoSuchFieldException, IllegalAccessException {
    Object result = getPrivateField(Bukkit.getServer().getPluginManager(), "commandMap");
    SimpleCommandMap commandMap = (SimpleCommandMap) result;
    Object knownCommandsMap;
    try {
        knownCommandsMap = getPrivateField(commandMap, "knownCommands");
    } catch (Exception ignored) {
        // Early exit as there's nothing to do
        return;
    }
    HashMap<String, Command> knownCommands = (HashMap<String, Command>) knownCommandsMap;
    knownCommands.remove(getName());
    for (String alias : getAliases()) {
        if (knownCommands.containsKey(alias) && knownCommands.get(alias).toString().contains(this.getName())) {
            knownCommands.remove(alias);
        }
    }
}
 
Example #5
Source File: ScriptCommand.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
public void register(final SimpleCommandMap commandMap, final Map<String, Command> knownCommands, final @Nullable Set<String> aliases) {
	synchronized (commandMap) {
		overriddenAliases.clear();
		overridden = knownCommands.put(label, bukkitCommand);
		if (aliases != null)
			aliases.remove(label);
		final Iterator<String> as = activeAliases.iterator();
		while (as.hasNext()) {
			final String lowerAlias = as.next().toLowerCase();
			if (knownCommands.containsKey(lowerAlias) && (aliases == null || !aliases.contains(lowerAlias))) {
				as.remove();
				continue;
			}
			overriddenAliases.put(lowerAlias, knownCommands.put(lowerAlias, bukkitCommand));
			if (aliases != null)
				aliases.add(lowerAlias);
		}
		bukkitCommand.setAliases(activeAliases);
		commandMap.register("skript", bukkitCommand);
	}
}
 
Example #6
Source File: SimpleCommandExecutor.java    From AstralEdit with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the dynamic command
 *
 * @param command command
 */
private void registerDynamicCommand(String command) {
    try {
        final Class<?> clazz = Class.forName("org.bukkit.craftbukkit.VERSION.CraftServer".replace("VERSION", getServerVersion()));
        final Object server = clazz.cast(Bukkit.getServer());
        final SimpleCommandMap map = (SimpleCommandMap) server.getClass().getDeclaredMethod("getCommandMap").invoke(server);
        map.register(command, this);
    } catch (final Exception ex) {
        AstralEditPlugin.logger().log(Level.WARNING, "Cannot register dynamic command.", ex);
    }
}
 
Example #7
Source File: CommandService.java    From mcspring-boot with MIT License 5 votes vote down vote up
@SneakyThrows
public void registerCommand(CommandSpec commandSpec) {
    val commandMapField = bukkitClass.getDeclaredField("commandMap");
    commandMapField.setAccessible(true);
    val commandMap = (SimpleCommandMap) commandMapField.get(plugin.getServer());

    commandMap.register(plugin.getName().toLowerCase(), new WrappedCommand(commandSpec, context, commandExecutor));
}
 
Example #8
Source File: SimplePluginManager.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public SimplePluginManager(Server instance, SimpleCommandMap commandMap) {
    server = instance;
    this.commandMap = commandMap;

    defaultPerms.put(true, new HashSet<Permission>());
    defaultPerms.put(false, new HashSet<Permission>());
}
 
Example #9
Source File: ShopCommand.java    From ShopChest with MIT License 5 votes vote down vote up
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);
    }
}
 
Example #10
Source File: ScriptCommand.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
public void unregister(final SimpleCommandMap commandMap, final Map<String, Command> knownCommands, final @Nullable Set<String> aliases) {
	synchronized (commandMap) {
		knownCommands.remove(label);
		knownCommands.remove("skript:" + label);
		if (aliases != null)
			aliases.removeAll(activeAliases);
		for (final String alias : activeAliases) {
			knownCommands.remove(alias);
			knownCommands.remove("skript:" + alias);
		}
		activeAliases = new ArrayList<>(this.aliases);
		bukkitCommand.unregister(commandMap);
		bukkitCommand.setAliases(this.aliases);
		if (overridden != null) {
			knownCommands.put(label, overridden);
			overridden = null;
		}
		for (final Entry<String, Command> e : overriddenAliases.entrySet()) {
			if (e.getValue() == null)
				continue;
			knownCommands.put(e.getKey(), e.getValue());
			if (aliases != null)
				aliases.add(e.getKey());
		}
		overriddenAliases.clear();
	}
}
 
Example #11
Source File: NMS_1_14_4.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
@Override
public SimpleCommandMap getSimpleCommandMap() {
    return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
 
Example #12
Source File: NMS_1_14_3.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
@Override
public SimpleCommandMap getSimpleCommandMap() {
    return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
 
Example #13
Source File: NMS_1_13_1.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
@Override
public SimpleCommandMap getSimpleCommandMap() {
    return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
 
Example #14
Source File: NMS_1_13.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
@Override
public SimpleCommandMap getSimpleCommandMap() {
    return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
 
Example #15
Source File: NMS_1_14.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
@Override
public SimpleCommandMap getSimpleCommandMap() {
	return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
 
Example #16
Source File: NMS_1_13_2.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
@Override
public SimpleCommandMap getSimpleCommandMap() {
    return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
 
Example #17
Source File: NMS_1_16_R1.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
@Override
public SimpleCommandMap getSimpleCommandMap() {
	return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
 
Example #18
Source File: CraftServer.java    From Thermos with GNU General Public License v3.0 4 votes vote down vote up
public SimpleCommandMap getCommandMap() {
    return commandMap;
}
 
Example #19
Source File: BukkitReflect.java    From NBTEditor with GNU General Public License v3.0 4 votes vote down vote up
public static SimpleCommandMap getCommandMap() {
	prepareReflection();
	return (SimpleCommandMap) invokeMethod(Bukkit.getServer(), _getCommandMap);
}
 
Example #20
Source File: CommandAPIHandler.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
protected void fixPermissions() {
	/*
	 * Makes permission checks more "Bukkit" like and less "Vanilla Minecraft" like
	 */
	try {

		// Get the command map to find registered commands
		SimpleCommandMap map = nms.getSimpleCommandMap();
		Field f = getField(SimpleCommandMap.class, "knownCommands");
		Map<String, org.bukkit.command.Command> knownCommands = (Map<String, org.bukkit.command.Command>) f
				.get(map);

		CommandAPIMain.getLog().info("Linking permissions to commands:");
		permissionsToFix.forEach((cmdName, perm) -> {

			if (perm.equals(CommandPermission.NONE)) {
				if (CommandAPIMain.getConfiguration().hasVerboseOutput()) {
					CommandAPIMain.getLog().info("NONE -> /" + cmdName);
				}
				// Set the command permission to empty string (Minecraft standard for "no
				// permission required")
				if (nms.isVanillaCommandWrapper(knownCommands.get(cmdName))) {
					knownCommands.get(cmdName).setPermission("");
				}
				if (nms.isVanillaCommandWrapper(knownCommands.get("minecraft:" + cmdName))) {
					knownCommands.get(cmdName).setPermission("");
				}
			} else {
				if (perm.getPermission() != null) {
					if (CommandAPIMain.getConfiguration().hasVerboseOutput()) {
						CommandAPIMain.getLog().info(perm.getPermission() + " -> /" + cmdName);
					}
					// Set the command permission to the (String) permission node
					if (nms.isVanillaCommandWrapper(knownCommands.get(cmdName))) {
						knownCommands.get(cmdName).setPermission(perm.getPermission());
					}
					if (nms.isVanillaCommandWrapper(knownCommands.get("minecraft:" + cmdName))) {
						knownCommands.get(cmdName).setPermission(perm.getPermission());
					}
				} else {
					// Dafaq?
				}
			}
		});
	} catch (IllegalAccessException | IllegalArgumentException e) {
		e.printStackTrace();
	}
}
 
Example #21
Source File: NMS_1_15.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
@Override
public SimpleCommandMap getSimpleCommandMap() {
	return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
 
Example #22
Source File: NMS.java    From 1.13-Command-API with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the Server's internal (OBC) CommandMap
 * @return A SimpleCommandMap from the OBC server
 */
SimpleCommandMap getSimpleCommandMap();