下面列出了怎么用org.bukkit.command.CommandMap的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);
}
}
/**
* 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;
}
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;
}
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;
}
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);
}
}
@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);
}
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);
}
}
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;
}
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 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);
}
}
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);
}
}
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) {
}
}
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;
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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;
}
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;
}
@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;
}
/**
* 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);
}
/**
* 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);
}