Add #183 - restricted servers

This commit is contained in:
md_5 2013-03-12 12:13:23 +11:00
parent f02d17c979
commit fea3642550
6 changed files with 40 additions and 11 deletions

View File

@ -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

View File

@ -31,6 +31,10 @@ public abstract class ServerInfo
* Players connected to a server defined by these properties.
*/
private final Collection<ProxiedPlayer> 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<ServerPing> 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 );
}
}

View File

@ -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

View File

@ -26,9 +26,9 @@ public class BungeeServerInfo extends ServerInfo
@Getter
private final Queue<DefinedPacket> 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

View File

@ -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<String, ServerInfo> servers = BungeeCord.getInstance().config.getServers();
Map<String, ServerInfo> 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 );

View File

@ -151,8 +151,9 @@ public class YamlConfig implements ConfigurationAdapter
Map<String, Object> 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 );
}