下面列出了怎么用org.bukkit.command.SimpleCommandMap的API类实例代码及写法,或者点击链接到github查看源代码。
@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);
}
}
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);
}
}
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;
}
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);
}
}
}
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;
}
@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));
}
public SimplePluginManager(Server instance, SimpleCommandMap commandMap) {
server = instance;
this.commandMap = commandMap;
defaultPerms.put(true, new HashSet<Permission>());
defaultPerms.put(false, new HashSet<Permission>());
}
/**
* 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);
}
}
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();
}
}
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);
}
}
@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
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();
}
}
@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
@Override
public SimpleCommandMap getSimpleCommandMap() {
return ((CraftServer) Bukkit.getServer()).getCommandMap();
}
public SimpleCommandMap getCommandMap() {
return commandMap;
}
public static SimpleCommandMap getCommandMap() {
prepareReflection();
return (SimpleCommandMap) invokeMethod(Bukkit.getServer(), _getCommandMap);
}
/**
* Returns the Server's internal (OBC) CommandMap
* @return A SimpleCommandMap from the OBC server
*/
SimpleCommandMap getSimpleCommandMap();