From 70e10c382e91312aceb11d4798359dff36a64669 Mon Sep 17 00:00:00 2001 From: md_5 Date: Fri, 26 Apr 2013 17:13:00 +1000 Subject: [PATCH] Properly interface ServerInfo class. --- .../md_5/bungee/api/config/ServerInfo.java | 97 +++++-------------- .../net/md_5/bungee/BungeeServerInfo.java | 54 ++++++++++- .../net/md_5/bungee/ServerConnection.java | 3 +- .../java/net/md_5/bungee/ServerConnector.java | 2 +- .../java/net/md_5/bungee/UserConnection.java | 5 +- .../net/md_5/bungee/netty/PipelineUtils.java | 4 +- 6 files changed, 81 insertions(+), 84 deletions(-) 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 b474bb52..d9741760 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 @@ -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 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 getPlayers() - { - return Collections.unmodifiableCollection( players ); - } + Collection 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 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 callback); } 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 51e2fded..3d747d15 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeServerInfo.java @@ -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 players = new ArrayList<>(); + @Getter + private final boolean restricted; @Getter private final Queue 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 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 diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java index c74b124b..815762ef 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java @@ -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 diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java index 1e9b5aaa..6d2a7730 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -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 diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index 66d1cb8b..d5b3f0a2 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -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!" ); diff --git a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java index be42aa95..1b7ecdc7 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java @@ -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 LISTENER = new AttributeKey<>( "ListerInfo" ); public static final AttributeKey USER = new AttributeKey<>( "User" ); - public static final AttributeKey TARGET = new AttributeKey<>( "Target" ); + public static final AttributeKey TARGET = new AttributeKey<>( "Target" ); public static final ChannelInitializer SERVER_CHILD = new ChannelInitializer() { @Override