Add command ip and permissions.

This commit is contained in:
md_5 2012-10-15 14:14:08 +11:00
parent 7415afdbfd
commit 86b081ddfd
10 changed files with 117 additions and 4 deletions

View File

@ -15,6 +15,7 @@ import java.util.logging.Level;
import static net.md_5.bungee.Logger.$;
import net.md_5.bungee.command.Command;
import net.md_5.bungee.command.CommandEnd;
import net.md_5.bungee.command.CommandIP;
import net.md_5.bungee.command.CommandList;
import net.md_5.bungee.command.CommandSender;
import net.md_5.bungee.command.CommandServer;
@ -71,6 +72,7 @@ public class BungeeCord {
commandMap.put("end", new CommandEnd());
commandMap.put("glist", new CommandList());
commandMap.put("server", new CommandServer());
commandMap.put("ip", new CommandIP());
}
/**
@ -107,7 +109,7 @@ public class BungeeCord {
String[] split = commandLine.trim().split(" ");
String commandName = split[0].toLowerCase();
Command command = commandMap.get(commandName);
if (command != null) {
if (command != null && !config.disabledCommands.contains(commandName)) {
String[] args = Arrays.copyOfRange(split, 1, split.length);
try {
command.execute(sender, args);

View File

@ -14,6 +14,8 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static net.md_5.bungee.Logger.$;
import net.md_5.bungee.command.CommandSender;
import net.md_5.bungee.command.ConsoleCommandSender;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
@ -95,6 +97,14 @@ public class Configuration {
add("mbaxter");
}
};
/**
* Commands which will be blocked completely.
*/
public List<String> disabledCommands = new ArrayList<String>() {
{
add("glist");
}
};
/**
* Maximum number of lines to log before old ones are removed.
*/
@ -225,4 +235,20 @@ public class Configuration {
save(reconnect, reconnectLocations);
$().info("Saved reconnect locations to " + reconnect);
}
/**
* Get the highest permission a player has.
*
* @param sender to get permissions of
* @return their permission
*/
public Permission getPermission(CommandSender sender) {
Permission permission = Permission.DEFAULT;
if (admins.contains(sender.getName()) || sender instanceof ConsoleCommandSender) {
permission = Permission.ADMIN;
} else if (moderators.contains(sender.getName())) {
permission = Permission.MODERATOR;
}
return permission;
}
}

View File

@ -0,0 +1,17 @@
package net.md_5.bungee;
public enum Permission {
/**
* Can access all commands.
*/
ADMIN,
/**
* Can access commands which do not affect everyone.
*/
MODERATOR,
/**
* Can access other commands.
*/
DEFAULT;
}

View File

@ -2,8 +2,10 @@ package net.md_5.bungee;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import net.md_5.bungee.command.CommandSender;
@ -86,6 +88,10 @@ public class UserConnection extends GenericConnection implements CommandSender {
}
}
public SocketAddress getAddress() {
return socket.getRemoteSocketAddress();
}
private void destroySelf(String reason) {
if (BungeeCord.instance.isRunning) {
BungeeCord.instance.connections.remove(username);
@ -102,6 +108,11 @@ public class UserConnection extends GenericConnection implements CommandSender {
packetQueue.add(new Packet3Chat(message));
}
@Override
public String getName() {
return username;
}
private class UpstreamBridge extends Thread {
public UpstreamBridge() {

View File

@ -1,5 +1,8 @@
package net.md_5.bungee.command;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.Permission;
/**
* Class which represents a proxy command. The {@link #execute(net.md_5.bungee.command.CommandSender, java.lang.String[])
* } method will be called to dispatch the command.
@ -14,4 +17,8 @@ public abstract class Command {
* the original command.
*/
public abstract void execute(CommandSender sender, String[] args);
public Permission getPermission(CommandSender sender) {
return BungeeCord.instance.config.getPermission(sender);
}
}

View File

@ -2,6 +2,7 @@ package net.md_5.bungee.command;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.ChatColor;
import net.md_5.bungee.Permission;
/**
* Command to terminate the proxy instance. May only be used by the console.
@ -10,9 +11,10 @@ public class CommandEnd extends Command {
@Override
public void execute(CommandSender sender, String[] args) {
if (!(sender instanceof ConsoleCommandSender)) {
sender.sendMessage(ChatColor.RED + "Only the console can use this command");
if (getPermission(sender) != Permission.ADMIN) {
sender.sendMessage(ChatColor.RED + "You do not have permission to use this command");
} else {
BungeeCord.instance.stop();
}
BungeeCord.instance.stop();
}
}

View File

@ -0,0 +1,27 @@
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;
public class CommandIP extends Command {
@Override
public void execute(CommandSender sender, String[] args) {
if (getPermission(sender) != Permission.ADMIN) {
sender.sendMessage(ChatColor.RED + "You do not have permission to use this command");
return;
}
if (args.length < 1) {
sender.sendMessage(ChatColor.RED + "Please follow this command by a user name");
return;
}
UserConnection user = BungeeCord.instance.connections.get(args[0]);
if (user == null) {
sender.sendMessage(ChatColor.RED + "That user is not online");
} else {
sender.sendMessage(ChatColor.BLUE + "IP of " + args[0] + " is " + user.getAddress());
}
}
}

View File

@ -16,8 +16,17 @@ public class CommandList extends Command {
Collection<UserConnection> connections = BungeeCord.instance.connections.values();
for (UserConnection con : connections) {
switch (getPermission(con)) {
case ADMIN:
users.append(ChatColor.RED);
break;
case MODERATOR:
users.append(ChatColor.GREEN);
break;
}
users.append(con.username);
users.append(", ");
users.append(ChatColor.RESET);
}
users.setLength(users.length() - 2);

View File

@ -8,4 +8,11 @@ public interface CommandSender {
* @param message the message to send
*/
public abstract void sendMessage(String message);
/**
* Get the senders name or CONSOLE for console.
*
* @return the friendly name of the player.
*/
public abstract String getName();
}

View File

@ -13,4 +13,9 @@ public class ConsoleCommandSender implements CommandSender {
public void sendMessage(String message) {
System.out.println(ChatColor.stripColor(message));
}
@Override
public String getName() {
return "CONSOLE";
}
}