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

@ -1,4 +1,4 @@
package net.md_5.bungee;
package net.md_5.bungee.api;
import java.util.regex.Pattern;

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.

View File

@ -16,6 +16,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import static net.md_5.bungee.Logger.$;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.command.*;
import net.md_5.bungee.packet.DefinedPacket;
import net.md_5.bungee.packet.PacketFAPluginMessage;

View File

@ -15,6 +15,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import static net.md_5.bungee.Logger.$;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.command.CommandSender;
import net.md_5.bungee.command.ConsoleCommandSender;
import org.yaml.snakeyaml.DumperOptions;

View File

@ -1,12 +1,12 @@
package net.md_5.bungee;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.SecretKey;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.packet.Packet2Handshake;
import net.md_5.bungee.packet.PacketFCEncryptionResponse;
import net.md_5.bungee.packet.PacketFDEncryptionRequest;

View File

@ -1,9 +1,9 @@
package net.md_5.bungee.command;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.ChatColor;
import net.md_5.bungee.Permission;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.ChatColor;
public class CommandAlert extends Command
{

View File

@ -1,7 +1,7 @@
package net.md_5.bungee.command;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.ChatColor;
import net.md_5.bungee.api.ChatColor;
public class CommandBungee extends Command
{

View File

@ -1,8 +1,8 @@
package net.md_5.bungee.command;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.ChatColor;
import net.md_5.bungee.Permission;
import net.md_5.bungee.api.ChatColor;
/**
* Command to terminate the proxy instance. May only be used by the console.

View File

@ -1,9 +1,9 @@
package net.md_5.bungee.command;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.ChatColor;
import net.md_5.bungee.Permission;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.ChatColor;
public class CommandIP extends Command
{

View File

@ -2,8 +2,8 @@ package net.md_5.bungee.command;
import java.util.Collection;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.ChatColor;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.ChatColor;
/**
* Command to list all players connected to the proxy.

View File

@ -1,8 +1,8 @@
package net.md_5.bungee.command;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.ChatColor;
import net.md_5.bungee.Permission;
import net.md_5.bungee.api.ChatColor;
/**
* Command to set a temp copy of the motd in real-time without stopping the

View File

@ -1,8 +1,8 @@
package net.md_5.bungee.command;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.ChatColor;
import net.md_5.bungee.Permission;
import net.md_5.bungee.api.ChatColor;
public class CommandReload extends Command
{

View File

@ -2,8 +2,8 @@ package net.md_5.bungee.command;
import java.util.Collection;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.ChatColor;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.ChatColor;
/**
* Command to list and switch a player between available servers.

View File

@ -1,6 +1,6 @@
package net.md_5.bungee.command;
import net.md_5.bungee.ChatColor;
import net.md_5.bungee.api.ChatColor;
/**
* Command sender representing the proxy console.