Finish up command API, with permissions and aliases!

This commit is contained in:
md_5
2013-01-12 12:03:59 +11:00
parent f065d0099f
commit b4105f8081
15 changed files with 122 additions and 11 deletions

View File

@@ -0,0 +1,156 @@
package net.md_5.bungee.api;
import java.util.regex.Pattern;
/**
* Simplistic enumeration of all supported color values for chat.
*/
public enum ChatColor
{
/**
* Represents black.
*/
BLACK('0'),
/**
* Represents dark blue.
*/
DARK_BLUE('1'),
/**
* Represents dark green.
*/
DARK_GREEN('2'),
/**
* Represents dark blue (aqua).
*/
DARK_AQUA('3'),
/**
* Represents dark red.
*/
DARK_RED('4'),
/**
* Represents dark purple.
*/
DARK_PURPLE('5'),
/**
* Represents gold.
*/
GOLD('6'),
/**
* Represents gray.
*/
GRAY('7'),
/**
* Represents dark gray.
*/
DARK_GRAY('8'),
/**
* Represents blue.
*/
BLUE('9'),
/**
* Represents green.
*/
GREEN('a'),
/**
* Represents aqua.
*/
AQUA('b'),
/**
* Represents red.
*/
RED('c'),
/**
* Represents light purple.
*/
LIGHT_PURPLE('d'),
/**
* Represents yellow.
*/
YELLOW('e'),
/**
* Represents white.
*/
WHITE('f'),
/**
* Represents magical characters that change around randomly.
*/
MAGIC('k'),
/**
* Makes the text bold.
*/
BOLD('l'),
/**
* Makes a line appear through the text.
*/
STRIKETHROUGH('m'),
/**
* Makes the text appear underlined.
*/
UNDERLINE('n'),
/**
* Makes the text italic.
*/
ITALIC('o'),
/**
* Resets all previous chat colors or formats.
*/
RESET('r');
/**
* The special character which prefixes all chat colour codes. Use this if
* you need to dynamically convert colour codes from your custom format.
*/
public static final char COLOR_CHAR = '\u00A7';
/**
* Pattern to remove all colour codes.
*/
private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + String.valueOf(COLOR_CHAR) + "[0-9A-FK-OR]");
/**
* This colour's colour char prefixed by the {@link #COLOR_CHAR}.
*/
private final String toString;
private ChatColor(char code)
{
this.toString = new String(new char[]
{
COLOR_CHAR, code
});
}
@Override
public String toString()
{
return toString;
}
/**
* Strips the given message of all color codes
*
* @param input String to strip of color
* @return A copy of the input string, without any coloring
*/
public static String stripColor(final String input)
{
if (input == null)
{
return null;
}
return STRIP_COLOR_PATTERN.matcher(input).replaceAll("");
}
public static String translateAlternateColorCodes(char altColorChar, String textToTranslate)
{
char[] b = textToTranslate.toCharArray();
for (int i = 0; i < b.length - 1; i++)
{
if (b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i + 1]) > -1)
{
b[i] = ChatColor.COLOR_CHAR;
b[i + 1] = Character.toLowerCase(b[i + 1]);
}
}
return new String(b);
}
}

View File

@@ -0,0 +1,42 @@
package net.md_5.bungee.api.plugin;
import lombok.AccessLevel;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.CommandSender;
/**
* A command that can be executed by a {@link CommandSender}.
*/
@Data
@RequiredArgsConstructor(access = AccessLevel.NONE)
public abstract class Command
{
private final String name;
private final String permission;
private final String[] aliases;
/**
* Construct a new command.
*
* @param name primary name of this command
* @param permission the permission node required to execute this command,
* null or empty string allows it to be executed by everyone
* @param aliases aliases which map back to this command
*/
public Command(String name, String permission, String... aliases)
{
this.name = name;
this.permission = permission;
this.aliases = aliases;
}
/**
* Execute this command with the specified sender and arguments.
*
* @param sender the executor of this command
* @param args arguments used to invoke this command
*/
public abstract void execute(CommandSender sender, String[] args);
}

View File

@@ -7,12 +7,16 @@ import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.regex.Pattern;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import org.yaml.snakeyaml.Yaml;
@@ -23,9 +27,72 @@ import org.yaml.snakeyaml.Yaml;
public class PluginManager
{
private static final Pattern argsSplit = Pattern.compile(" ");
/*========================================================================*/
private final Yaml yaml = new Yaml();
private final EventBus eventBus = new EventBus();
private final Map<String, Plugin> plugins = new HashMap<>();
private final Map<String, Command> commandMap = new HashMap<>();
/**
* Register a command so that it may be executed.
*
* @param command the command to register
*/
public void registerCommand(Command command)
{
commandMap.put(command.getName(), command);
for (String alias : command.getAliases())
{
commandMap.put(alias, command);
}
}
/**
* Unregister a command so it will no longer be executed.
*
* @param command the command to unregister
*/
public void unregisterCommand(Command command)
{
commandMap.values().remove(command);
}
/**
* Execute a command if it is registered, else return false.
*
* @param sender the sender executing the command
* @param commandLine the complete command line including command name and
* arguments
* @return whether the command was handled
*/
public boolean dispatchCommand(CommandSender sender, String commandLine)
{
String[] split = argsSplit.split(commandLine);
Command command = commandMap.get(split[0]);
if (command == null)
{
return false;
}
String permission = command.getPermission();
if (permission != null && !permission.isEmpty() && !sender.hasPermission(permission))
{
sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command!");
return true;
}
String[] args = Arrays.copyOfRange(split, 1, split.length);
try
{
command.execute(sender, args);
} catch (Exception ex)
{
sender.sendMessage(ChatColor.RED + "An internal error occurred whilst executing this command, please check the console log for details.");
ProxyServer.getInstance().getLogger().log(Level.WARNING, "Error in dispatching command", ex);
}
return true;
}
/**
* Returns the {@link Plugin} objects corresponding to all loaded plugins.