2012-01-05 21:38:23 +00:00
|
|
|
// $Id$
|
2011-12-25 06:34:53 +00:00
|
|
|
/*
|
|
|
|
* WorldEdit
|
2012-01-05 21:38:23 +00:00
|
|
|
* Copyright (C) 2011 sk89q <http://www.sk89q.com> and contributors
|
2011-12-25 06:34:53 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.bukkit.util;
|
|
|
|
|
2011-12-25 23:36:23 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2011-12-25 06:34:53 +00:00
|
|
|
import com.sk89q.minecraft.util.commands.Command;
|
|
|
|
import com.sk89q.util.ReflectionUtil;
|
|
|
|
import org.bukkit.Bukkit;
|
2011-12-29 21:59:48 +00:00
|
|
|
import org.bukkit.command.CommandExecutor;
|
2011-12-25 06:34:53 +00:00
|
|
|
import org.bukkit.command.CommandMap;
|
|
|
|
import org.bukkit.command.SimpleCommandMap;
|
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author zml2008
|
|
|
|
*/
|
|
|
|
public class CommandRegistration {
|
|
|
|
private final Plugin plugin;
|
2011-12-29 21:59:48 +00:00
|
|
|
private final CommandExecutor executor;
|
2011-12-25 06:34:53 +00:00
|
|
|
private CommandMap fallbackCommands;
|
|
|
|
|
|
|
|
public CommandRegistration(Plugin plugin) {
|
2011-12-29 21:59:48 +00:00
|
|
|
this(plugin, plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
public CommandRegistration(Plugin plugin, CommandExecutor executor) {
|
2011-12-25 06:34:53 +00:00
|
|
|
this.plugin = plugin;
|
2011-12-29 21:59:48 +00:00
|
|
|
this.executor = executor;
|
2011-12-25 06:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean registerAll(List<Command> registered) {
|
|
|
|
CommandMap commandMap = getCommandMap();
|
|
|
|
if (registered == null || commandMap == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (Command command : registered) {
|
2011-12-29 21:59:48 +00:00
|
|
|
commandMap.register(plugin.getDescription().getName(),
|
2012-01-11 06:30:07 +00:00
|
|
|
new DynamicPluginCommand(command.aliases(), command.desc(), "/" + command.aliases()[0] + " " + command.usage(), executor));
|
2011-12-25 06:34:53 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-29 21:59:48 +00:00
|
|
|
public CommandMap getCommandMap() {
|
2011-12-25 06:34:53 +00:00
|
|
|
CommandMap commandMap = ReflectionUtil.getField(plugin.getServer().getPluginManager(), "commandMap");
|
|
|
|
if (commandMap == null) {
|
|
|
|
if (fallbackCommands != null) {
|
|
|
|
commandMap = fallbackCommands;
|
|
|
|
} else {
|
2011-12-27 00:41:21 +00:00
|
|
|
Bukkit.getServer().getLogger().severe(plugin.getDescription().getName() +
|
2011-12-25 06:34:53 +00:00
|
|
|
": Could not retrieve server CommandMap, using fallback instead! Please report to http://redmine.sk89q.com");
|
|
|
|
fallbackCommands = commandMap = new SimpleCommandMap(Bukkit.getServer());
|
2012-01-18 16:50:07 +00:00
|
|
|
Bukkit.getServer().getPluginManager().registerEvents(new FallbackRegistrationListener(fallbackCommands), plugin);
|
2011-12-25 06:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return commandMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean unregisterCommands() {
|
|
|
|
CommandMap commandMap = getCommandMap();
|
|
|
|
List<String> toRemove = new ArrayList<String>();
|
|
|
|
Map<String, org.bukkit.command.Command> knownCommands = ReflectionUtil.getField(commandMap, "knownCommands");
|
|
|
|
Set<String> aliases = ReflectionUtil.getField(commandMap, "aliases");
|
|
|
|
if (knownCommands == null || aliases == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (Iterator<org.bukkit.command.Command> i = knownCommands.values().iterator(); i.hasNext();) {
|
|
|
|
org.bukkit.command.Command cmd = i.next();
|
2011-12-29 21:59:48 +00:00
|
|
|
if (cmd instanceof DynamicPluginCommand && ((DynamicPluginCommand) cmd).getOwner().equals(plugin)) {
|
2011-12-25 06:34:53 +00:00
|
|
|
i.remove();
|
|
|
|
for (String alias : cmd.getAliases()) {
|
|
|
|
org.bukkit.command.Command aliasCmd = knownCommands.get(alias);
|
|
|
|
if (cmd.equals(aliasCmd)) {
|
|
|
|
aliases.remove(alias);
|
|
|
|
toRemove.add(alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (String string : toRemove) {
|
|
|
|
knownCommands.remove(string);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|