From fea3642550eef493ea5bf4112d3dac06389583e6 Mon Sep 17 00:00:00 2001 From: md_5 Date: Tue, 12 Mar 2013 12:13:23 +1100 Subject: [PATCH] Add #183 - restricted servers --- .../java/net/md_5/bungee/api/ProxyServer.java | 3 ++- .../md_5/bungee/api/config/ServerInfo.java | 16 ++++++++++++++ .../main/java/net/md_5/bungee/BungeeCord.java | 4 ++-- .../net/md_5/bungee/BungeeServerInfo.java | 4 ++-- .../md_5/bungee/command/CommandServer.java | 21 ++++++++++++++----- .../net/md_5/bungee/config/YamlConfig.java | 3 ++- 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/api/src/main/java/net/md_5/bungee/api/ProxyServer.java b/api/src/main/java/net/md_5/bungee/api/ProxyServer.java index a9d05477..069cfd66 100644 --- a/api/src/main/java/net/md_5/bungee/api/ProxyServer.java +++ b/api/src/main/java/net/md_5/bungee/api/ProxyServer.java @@ -208,9 +208,10 @@ public abstract class ProxyServer * * @param name name of the server * @param address connectable Minecraft address + port of the server + * @param restricted whether the server info restricted property will be set * @return the constructed instance */ - public abstract ServerInfo constructServerInfo(String name, InetSocketAddress address); + public abstract ServerInfo constructServerInfo(String name, InetSocketAddress address, boolean restricted); /** * Returns the console overlord for this proxy. Being the console, this diff --git a/api/src/main/java/net/md_5/bungee/api/config/ServerInfo.java b/api/src/main/java/net/md_5/bungee/api/config/ServerInfo.java index dd645678..4c6bdced 100644 --- a/api/src/main/java/net/md_5/bungee/api/config/ServerInfo.java +++ b/api/src/main/java/net/md_5/bungee/api/config/ServerInfo.java @@ -31,6 +31,10 @@ public abstract class ServerInfo * Players connected to a server defined by these properties. */ private final Collection players = new ArrayList<>(); + /** + * If set to true, users will need special permissions to view this server. + */ + private final boolean restricted; /** * Add a player to the internal set of this server. @@ -79,4 +83,16 @@ public abstract class ServerInfo * @param callback the callback to call when the count has been retrieved. */ public abstract void ping(Callback callback); + + /** + * Whether the player can access this server. It will only return false when + * the player has no permission and this server is restricted. + * + * @param player the player to check access for + * @return whether access is granted to this server + */ + public boolean canAccess(ProxiedPlayer player) + { + return restricted && player.hasPermission( "bungeecord.server." + name ); + } } diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java index 6b50c506..39d11c95 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java @@ -368,9 +368,9 @@ public class BungeeCord extends ProxyServer } @Override - public ServerInfo constructServerInfo(String name, InetSocketAddress address) + public ServerInfo constructServerInfo(String name, InetSocketAddress address, boolean restricted) { - return new BungeeServerInfo( name, address ); + return new BungeeServerInfo( name, address, restricted ); } @Override diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java b/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java index 8f5ca6bb..7a73272a 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java @@ -26,9 +26,9 @@ public class BungeeServerInfo extends ServerInfo @Getter private final Queue packetQueue = new ConcurrentLinkedQueue<>(); - public BungeeServerInfo(String name, InetSocketAddress address) + public BungeeServerInfo(String name, InetSocketAddress address, boolean restricted) { - super( name, address ); + super( name, address, restricted ); } @Override diff --git a/proxy/src/main/java/net/md_5/bungee/command/CommandServer.java b/proxy/src/main/java/net/md_5/bungee/command/CommandServer.java index 64cdb977..f2a70956 100644 --- a/proxy/src/main/java/net/md_5/bungee/command/CommandServer.java +++ b/proxy/src/main/java/net/md_5/bungee/command/CommandServer.java @@ -1,9 +1,11 @@ package net.md_5.bungee.command; +import java.util.Collection; import java.util.Map; import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Command; @@ -27,16 +29,22 @@ public class CommandServer extends Command return; } ProxiedPlayer player = (ProxiedPlayer) sender; - Map servers = BungeeCord.getInstance().config.getServers(); + Map servers = ProxyServer.getInstance().getServers(); if ( args.length == 0 ) { StringBuilder serverList = new StringBuilder(); - for ( String server : servers.keySet() ) + for ( ServerInfo server : servers.values() ) { - serverList.append( server ); - serverList.append( ", " ); + if ( server.canAccess( player ) ) + { + serverList.append( server ); + serverList.append( ", " ); + } + } + if ( serverList.length() != 0 ) + { + serverList.setLength( serverList.length() - 2 ); } - serverList.setLength( serverList.length() - 2 ); player.sendMessage( ChatColor.GOLD + "You may connect to the following servers at this time: " + serverList.toString() ); } else { @@ -47,6 +55,9 @@ public class CommandServer extends Command } else if ( server.equals( player.getServer().getInfo() ) ) { player.sendMessage( ChatColor.RED + "You are already on this server." ); + } else if ( !server.canAccess( player ) ) + { + player.sendMessage( ChatColor.RED + "You don't have permission to access this server" ); } else { player.connect( server ); diff --git a/proxy/src/main/java/net/md_5/bungee/config/YamlConfig.java b/proxy/src/main/java/net/md_5/bungee/config/YamlConfig.java index ded9f70d..1c3aa55f 100644 --- a/proxy/src/main/java/net/md_5/bungee/config/YamlConfig.java +++ b/proxy/src/main/java/net/md_5/bungee/config/YamlConfig.java @@ -151,8 +151,9 @@ public class YamlConfig implements ConfigurationAdapter Map val = entry.getValue(); String name = entry.getKey(); String addr = get( "address", "localhost:25565", val ); + boolean restricted = get( "restricted", false ); InetSocketAddress address = Util.getAddr( addr ); - ServerInfo info = ProxyServer.getInstance().constructServerInfo( name, address ); + ServerInfo info = ProxyServer.getInstance().constructServerInfo( name, address, restricted ); ret.put( name, info ); }