Properly interface ServerInfo class.

This commit is contained in:
md_5 2013-04-26 17:13:00 +10:00
parent 93ea108acb
commit 70e10c382e
6 changed files with 81 additions and 84 deletions

View File

@ -1,13 +1,7 @@
package net.md_5.bungee.api.config;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Synchronized;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ServerPing;
@ -16,60 +10,40 @@ import net.md_5.bungee.api.connection.ProxiedPlayer;
/**
* Class used to represent a server to connect to.
*/
@Data
@AllArgsConstructor
public abstract class ServerInfo
public interface ServerInfo
{
/**
* Name this server displays as.
* Get the name of this server.
*
* @return the configured name for this server address
*/
private final String name;
/**
* Connectable address of this server.
*/
private final InetSocketAddress address;
/**
* 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;
String getName();
/**
* Add a player to the internal set of this server.
* Gets the connectable host + port pair for this server. Implementations
* expect this to be used as the unique identifier per each instance of this
* class.
*
* @param player the player to add
* @return the IP and port pair for this server
*/
@Synchronized("players")
public void addPlayer(ProxiedPlayer player)
{
players.add( player );
}
/**
* Remove a player form the internal set of this server.
*
* @param player the player to remove
*/
@Synchronized("players")
public void removePlayer(ProxiedPlayer player)
{
players.remove( player );
}
InetSocketAddress getAddress();
/**
* Get the set of all players on this server.
*
* @return an unmodifiable collection of all players on this server
*/
@Synchronized("players")
public Collection<ProxiedPlayer> getPlayers()
{
return Collections.unmodifiableCollection( players );
}
Collection<ProxiedPlayer> getPlayers();
/**
* Whether the player can access this server. It will only return false when
* the player has no permission and this server is restricted.
*
* @param sender the player to check access for
* @return whether access is granted to this server
*/
boolean canAccess(CommandSender sender);
/**
* Send data by any available means to this server.
@ -77,39 +51,12 @@ public abstract class ServerInfo
* @param channel the channel to send this data via
* @param data the data to send
*/
public abstract void sendData(String channel, byte[] data);
void sendData(String channel, byte[] data);
/**
* Asynchronously gets the current player count on this server.
*
* @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(CommandSender player)
{
return !restricted || player.hasPermission( "bungeecord.server." + name );
}
@Override
public boolean equals(Object obj)
{
return ( obj instanceof ServerInfo ) && Objects.equals( getAddress(), ( (ServerInfo) obj ).getAddress() );
}
@Override
public int hashCode()
{
int hash = 7;
hash = 73 * hash + Objects.hashCode( getClass() );
hash = 73 * hash + Objects.hashCode( getAddress() );
return hash;
}
void ping(Callback<ServerPing> callback);
}

View File

@ -6,13 +6,21 @@ import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Synchronized;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.connection.PingHandler;
import net.md_5.bungee.netty.HandlerBoss;
@ -20,15 +28,55 @@ import net.md_5.bungee.netty.PipelineUtils;
import net.md_5.bungee.packet.DefinedPacket;
import net.md_5.bungee.packet.PacketFAPluginMessage;
public class BungeeServerInfo extends ServerInfo
@RequiredArgsConstructor
public class BungeeServerInfo implements ServerInfo
{
@Getter
private final String name;
@Getter
private final InetSocketAddress address;
private final Collection<ProxiedPlayer> players = new ArrayList<>();
@Getter
private final boolean restricted;
@Getter
private final Queue<DefinedPacket> packetQueue = new ConcurrentLinkedQueue<>();
public BungeeServerInfo(String name, InetSocketAddress address, boolean restricted)
@Synchronized("players")
public void addPlayer(ProxiedPlayer player)
{
super( name, address, restricted );
players.add( player );
}
@Synchronized("players")
public void removePlayer(ProxiedPlayer player)
{
players.remove( player );
}
@Synchronized("players")
@Override
public Collection<ProxiedPlayer> getPlayers()
{
return Collections.unmodifiableCollection( players );
}
@Override
public boolean canAccess(CommandSender player)
{
return !restricted || player.hasPermission( "bungeecord.server." + name );
}
@Override
public boolean equals(Object obj)
{
return ( obj instanceof ServerInfo ) && Objects.equals( getAddress(), ( (ServerInfo) obj ).getAddress() );
}
@Override
public int hashCode()
{
return address.hashCode();
}
@Override

View File

@ -6,7 +6,6 @@ import java.util.concurrent.TimeUnit;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.packet.Packet1Login;
import net.md_5.bungee.packet.PacketFAPluginMessage;
@ -19,7 +18,7 @@ public class ServerConnection implements Server
@Getter
private final Channel ch;
@Getter
private final ServerInfo info;
private final BungeeServerInfo info;
@Getter
private final Packet1Login loginPacket;
@Getter

View File

@ -36,7 +36,7 @@ public class ServerConnector extends PacketHandler
private final ProxyServer bungee;
private Channel ch;
private final UserConnection user;
private final ServerInfo target;
private final BungeeServerInfo target;
private State thisState = State.ENCRYPT_REQUEST;
private enum State

View File

@ -118,7 +118,10 @@ public final class UserConnection implements ProxiedPlayer
{
ServerConnectEvent event = new ServerConnectEvent( this, info );
ProxyServer.getInstance().getPluginManager().callEvent( event );
final ServerInfo target = event.getTarget(); // Update in case the event changed target
Preconditions.checkArgument( event.getTarget() instanceof BungeeServerInfo, "BungeeCord can only connect to BungeeServerInfo instances" );
final BungeeServerInfo target = (BungeeServerInfo) event.getTarget(); // Update in case the event changed target
if ( getServer() != null && Objects.equals( getServer().getInfo(), target ) )
{
sendMessage( ChatColor.RED + "Cannot connect to server you are already on!" );

View File

@ -9,12 +9,12 @@ import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.util.AttributeKey;
import java.util.concurrent.TimeUnit;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.BungeeServerInfo;
import net.md_5.bungee.ServerConnector;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.connection.InitialHandler;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ListenerInfo;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.protocol.PacketDefinitions;
public class PipelineUtils
@ -22,7 +22,7 @@ public class PipelineUtils
public static final AttributeKey<ListenerInfo> LISTENER = new AttributeKey<>( "ListerInfo" );
public static final AttributeKey<UserConnection> USER = new AttributeKey<>( "User" );
public static final AttributeKey<ServerInfo> TARGET = new AttributeKey<>( "Target" );
public static final AttributeKey<BungeeServerInfo> TARGET = new AttributeKey<>( "Target" );
public static final ChannelInitializer<Channel> SERVER_CHILD = new ChannelInitializer<Channel>()
{
@Override