From 63f22dc2de09bb1f27045d239d9bba3a8a881852 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Thu, 27 Sep 2018 02:35:10 -0400 Subject: [PATCH] Extra clarity for server/MinecraftConnection naming --- .../backend/BackendPlaySessionHandler.java | 70 +++++++++---------- .../backend/LoginSessionHandler.java | 38 +++++----- .../backend/VelocityServerConnection.java | 28 ++++---- .../client/ClientPlaySessionHandler.java | 38 +++++----- .../connection/client/ConnectedPlayer.java | 2 +- 5 files changed, 88 insertions(+), 88 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index 35ef63d92..ba16d0a6a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -16,38 +16,38 @@ import io.netty.buffer.ByteBuf; public class BackendPlaySessionHandler implements MinecraftSessionHandler { private final VelocityServer server; - private final VelocityServerConnection connection; + private final VelocityServerConnection serverConn; - public BackendPlaySessionHandler(VelocityServer server, VelocityServerConnection connection) { + public BackendPlaySessionHandler(VelocityServer server, VelocityServerConnection serverConn) { this.server = server; - this.connection = connection; + this.serverConn = serverConn; } @Override public void activated() { - server.getEventManager().fireAndForget(new ServerConnectedEvent(connection.getPlayer(), connection.getServer())); - connection.getServer().addPlayer(connection.getPlayer()); + server.getEventManager().fireAndForget(new ServerConnectedEvent(serverConn.getPlayer(), serverConn.getServer())); + serverConn.getServer().addPlayer(serverConn.getPlayer()); } @Override public void handle(MinecraftPacket packet) { - if (!connection.getPlayer().isActive()) { + if (!serverConn.getPlayer().isActive()) { // Connection was left open accidentally. Close it so as to avoid "You logged in from another location" // errors. - connection.disconnect(); + serverConn.disconnect(); return; } ClientPlaySessionHandler playerHandler = - (ClientPlaySessionHandler) connection.getPlayer().getConnection().getSessionHandler(); + (ClientPlaySessionHandler) serverConn.getPlayer().getConnection().getSessionHandler(); if (packet instanceof KeepAlive) { // Forward onto the player - connection.setLastPingId(((KeepAlive) packet).getRandomId()); - connection.getPlayer().getConnection().write(packet); + serverConn.setLastPingId(((KeepAlive) packet).getRandomId()); + serverConn.getPlayer().getConnection().write(packet); } else if (packet instanceof Disconnect) { Disconnect original = (Disconnect) packet; - connection.disconnect(); - connection.getPlayer().handleConnectionException(connection.getServer(), original); + serverConn.disconnect(); + serverConn.getPlayer().handleConnectionException(serverConn.getServer(), original); } else if (packet instanceof JoinGame) { playerHandler.handleBackendJoinGame((JoinGame) packet); } else if (packet instanceof BossBar) { @@ -60,7 +60,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { playerHandler.getServerBossBars().remove(bossBar.getUuid()); break; } - connection.getPlayer().getConnection().write(packet); + serverConn.getPlayer().getConnection().write(packet); } else if (packet instanceof PluginMessage) { PluginMessage pm = (PluginMessage) packet; if (!canForwardPluginMessage(pm)) { @@ -68,61 +68,61 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { } if (PluginMessageUtil.isMCBrand(pm)) { - connection.getPlayer().getConnection().write(PluginMessageUtil.rewriteMCBrand(pm)); + serverConn.getPlayer().getConnection().write(PluginMessageUtil.rewriteMCBrand(pm)); return; } - if (!connection.hasCompletedJoin() && pm.getChannel().equals(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL)) { - if (!connection.isLegacyForge()) { - connection.setLegacyForge(true); + if (!serverConn.hasCompletedJoin() && pm.getChannel().equals(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL)) { + if (!serverConn.isLegacyForge()) { + serverConn.setLegacyForge(true); // We must always reset the handshake before a modded connection is established if // we haven't done so already. - connection.getPlayer().sendLegacyForgeHandshakeResetPacket(); + serverConn.getPlayer().sendLegacyForgeHandshakeResetPacket(); } // Always forward these messages during login - connection.getPlayer().getConnection().write(pm); + serverConn.getPlayer().getConnection().write(pm); return; } ChannelIdentifier id = server.getChannelRegistrar().getFromId(pm.getChannel()); if (id == null) { - connection.getPlayer().getConnection().write(pm); + serverConn.getPlayer().getConnection().write(pm); } else { - PluginMessageEvent event = new PluginMessageEvent(connection, connection.getPlayer(), id, pm.getData()); + PluginMessageEvent event = new PluginMessageEvent(serverConn, serverConn.getPlayer(), id, pm.getData()); server.getEventManager().fire(event) .thenAcceptAsync(pme -> { if (pme.getResult().isAllowed()) { - connection.getPlayer().getConnection().write(pm); + serverConn.getPlayer().getConnection().write(pm); } - }, connection.getMinecraftConnection().getChannel().eventLoop()); + }, serverConn.getConnection().getChannel().eventLoop()); } } else if (packet instanceof TabCompleteResponse) { playerHandler.handleTabCompleteResponse((TabCompleteResponse) packet); - } else if (connection.hasCompletedJoin()) { + } else if (serverConn.hasCompletedJoin()) { // Just forward the packet on. We don't have anything to handle at this time. - connection.getPlayer().getConnection().write(packet); + serverConn.getPlayer().getConnection().write(packet); } } @Override public void handleUnknown(ByteBuf buf) { - if (!connection.getPlayer().isActive()) { + if (!serverConn.getPlayer().isActive()) { // Connection was left open accidentally. Close it so as to avoid "You logged in from another location" // errors. - connection.disconnect(); + serverConn.disconnect(); return; } - if (connection.hasCompletedJoin()) { - connection.getPlayer().getConnection().write(buf.retain()); + if (serverConn.hasCompletedJoin()) { + serverConn.getPlayer().getConnection().write(buf.retain()); } } @Override public void exception(Throwable throwable) { - connection.getPlayer().handleConnectionException(connection.getServer(), throwable); + serverConn.getPlayer().handleConnectionException(serverConn.getServer(), throwable); } public VelocityServer getServer() { @@ -131,18 +131,18 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { @Override public void disconnected() { - connection.getServer().removePlayer(connection.getPlayer()); - if (!connection.isGracefulDisconnect()) { - connection.getPlayer().handleConnectionException(connection.getServer(), Disconnect.create( + serverConn.getServer().removePlayer(serverConn.getPlayer()); + if (!serverConn.isGracefulDisconnect()) { + serverConn.getPlayer().handleConnectionException(serverConn.getServer(), Disconnect.create( ConnectionMessages.UNEXPECTED_DISCONNECT)); } } private boolean canForwardPluginMessage(PluginMessage message) { ClientPlaySessionHandler playerHandler = - (ClientPlaySessionHandler) connection.getPlayer().getConnection().getSessionHandler(); + (ClientPlaySessionHandler) serverConn.getPlayer().getConnection().getSessionHandler(); boolean isMCOrFMLMessage; - if (connection.getMinecraftConnection().getProtocolVersion() <= ProtocolConstants.MINECRAFT_1_12_2) { + if (serverConn.getConnection().getProtocolVersion() <= ProtocolConstants.MINECRAFT_1_12_2) { String channel = message.getChannel(); isMCOrFMLMessage = channel.startsWith("MC|") || channel.startsWith(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL); } else { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java index 5fb5341b2..4672ad990 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java @@ -27,12 +27,12 @@ import java.util.concurrent.CompletableFuture; public class LoginSessionHandler implements MinecraftSessionHandler { private final VelocityServer server; - private final VelocityServerConnection connection; + private final VelocityServerConnection serverConn; private boolean informationForwarded; - public LoginSessionHandler(VelocityServer server, VelocityServerConnection connection) { + public LoginSessionHandler(VelocityServer server, VelocityServerConnection serverConn) { this.server = server; - this.connection = connection; + this.serverConn = serverConn; } @Override @@ -48,9 +48,9 @@ public class LoginSessionHandler implements MinecraftSessionHandler { response.setSuccess(true); response.setId(message.getId()); response.setData(createForwardingData(configuration.getForwardingSecret(), - connection.getPlayer().getRemoteAddress().getHostString(), - connection.getPlayer().getProfile())); - connection.getMinecraftConnection().write(response); + serverConn.getPlayer().getRemoteAddress().getHostString(), + serverConn.getPlayer().getProfile())); + serverConn.getConnection().write(response); informationForwarded = true; } else { // Don't understand @@ -58,48 +58,48 @@ public class LoginSessionHandler implements MinecraftSessionHandler { response.setSuccess(false); response.setId(message.getId()); response.setData(Unpooled.EMPTY_BUFFER); - connection.getMinecraftConnection().write(response); + serverConn.getConnection().write(response); } } else if (packet instanceof Disconnect) { Disconnect disconnect = (Disconnect) packet; // Do we have an outstanding notification? If so, fulfill it. doNotify(ConnectionRequestResults.forDisconnect(disconnect)); - connection.disconnect(); + serverConn.disconnect(); } else if (packet instanceof SetCompression) { SetCompression sc = (SetCompression) packet; - connection.getMinecraftConnection().setCompressionThreshold(sc.getThreshold()); + serverConn.getConnection().setCompressionThreshold(sc.getThreshold()); } else if (packet instanceof ServerLoginSuccess) { if (server.getConfiguration().getPlayerInfoForwardingMode() == PlayerInfoForwarding.MODERN && !informationForwarded) { doNotify(ConnectionRequestResults.forDisconnect( TextComponent.of("Your server did not send a forwarding request to the proxy. Is it set up correctly?"))); - connection.disconnect(); + serverConn.disconnect(); return; } // The player has been logged on to the backend server. - connection.getMinecraftConnection().setState(StateRegistry.PLAY); - VelocityServerConnection existingConnection = connection.getPlayer().getConnectedServer(); + serverConn.getConnection().setState(StateRegistry.PLAY); + VelocityServerConnection existingConnection = serverConn.getPlayer().getConnectedServer(); if (existingConnection == null) { // Strap on the play session handler - connection.getPlayer().getConnection().setSessionHandler(new ClientPlaySessionHandler(server, connection.getPlayer())); + serverConn.getPlayer().getConnection().setSessionHandler(new ClientPlaySessionHandler(server, serverConn.getPlayer())); } else { // The previous server connection should become obsolete. // Before we remove it, if the server we are departing is modded, we must always reset the client state. if (existingConnection.isLegacyForge()) { - connection.getPlayer().sendLegacyForgeHandshakeResetPacket(); + serverConn.getPlayer().sendLegacyForgeHandshakeResetPacket(); } existingConnection.disconnect(); } doNotify(ConnectionRequestResults.SUCCESSFUL); - connection.getMinecraftConnection().setSessionHandler(new BackendPlaySessionHandler(server, connection)); - connection.getPlayer().setConnectedServer(connection); + serverConn.getConnection().setSessionHandler(new BackendPlaySessionHandler(server, serverConn)); + serverConn.getPlayer().setConnectedServer(serverConn); } } @Override public void exception(Throwable throwable) { - CompletableFuture future = connection.getMinecraftConnection().getChannel() + CompletableFuture future = serverConn.getConnection().getChannel() .attr(VelocityServerConnection.CONNECTION_NOTIFIER).getAndSet(null); if (future != null) { future.completeExceptionally(throwable); @@ -108,7 +108,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { @Override public void disconnected() { - CompletableFuture future = connection.getMinecraftConnection().getChannel() + CompletableFuture future = serverConn.getConnection().getChannel() .attr(VelocityServerConnection.CONNECTION_NOTIFIER).getAndSet(null); if (future != null) { future.completeExceptionally(new IOException("Unexpectedly disconnected from remote server")); @@ -116,7 +116,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { } private void doNotify(ConnectionRequestBuilder.Result result) { - CompletableFuture future = connection.getMinecraftConnection().getChannel() + CompletableFuture future = serverConn.getConnection().getChannel() .attr(VelocityServerConnection.CONNECTION_NOTIFIER).getAndSet(null); if (future != null) { future.complete(result); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java index 36450f1ea..60b35c1a6 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java @@ -40,7 +40,7 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, private final VelocityRegisteredServer registeredServer; private final ConnectedPlayer proxyPlayer; private final VelocityServer server; - private MinecraftConnection minecraftConnection; + private MinecraftConnection connection; private boolean legacyForge = false; private boolean hasCompletedJoin = false; private boolean gracefulDisconnect = false; @@ -78,10 +78,10 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { - minecraftConnection = future.channel().pipeline().get(MinecraftConnection.class); + connection = future.channel().pipeline().get(MinecraftConnection.class); // Kick off the connection process - minecraftConnection.setSessionHandler(new LoginSessionHandler(server, VelocityServerConnection.this)); + connection.setSessionHandler(new LoginSessionHandler(server, VelocityServerConnection.this)); startHandshake(); } else { result.completeExceptionally(future.cause()); @@ -116,25 +116,25 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, handshake.setServerAddress(registeredServer.getServerInfo().getAddress().getHostString()); } handshake.setPort(registeredServer.getServerInfo().getAddress().getPort()); - minecraftConnection.write(handshake); + connection.write(handshake); int protocolVersion = proxyPlayer.getConnection().getProtocolVersion(); - minecraftConnection.setProtocolVersion(protocolVersion); - minecraftConnection.setState(StateRegistry.LOGIN); + connection.setProtocolVersion(protocolVersion); + connection.setState(StateRegistry.LOGIN); ServerLogin login = new ServerLogin(); login.setUsername(proxyPlayer.getUsername()); - minecraftConnection.write(login); + connection.write(login); } public void writeIfJoined(PluginMessage message) { if (hasCompletedJoin) { - minecraftConnection.write(message); + connection.write(message); } } - public MinecraftConnection getMinecraftConnection() { - return minecraftConnection; + public MinecraftConnection getConnection() { + return connection; } @Override @@ -153,9 +153,9 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, } public void disconnect() { - if (minecraftConnection != null) { - minecraftConnection.close(); - minecraftConnection = null; + if (connection != null) { + connection.close(); + connection = null; gracefulDisconnect = true; } } @@ -172,7 +172,7 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, PluginMessage message = new PluginMessage(); message.setChannel(identifier.getId()); message.setData(data); - minecraftConnection.write(message); + connection.write(message); return true; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index bc0856f8c..810899956 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -69,7 +69,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { return; } player.setPing(System.currentTimeMillis() - serverConnection.getLastPingSent()); - serverConnection.getMinecraftConnection().write(packet); + serverConnection.getConnection().write(packet); serverConnection.resetLastPingId(); return; } @@ -86,7 +86,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { if (msg.startsWith("/")) { try { if (!server.getCommandManager().execute(player, msg.substring(1))) { - player.getConnectedServer().getMinecraftConnection().write(chat); + player.getConnectedServer().getConnection().write(chat); } } catch (Exception e) { logger.info("Exception occurred while running command for {}", player.getProfile().getName(), e); @@ -98,11 +98,11 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { server.getEventManager().fire(event) .thenAcceptAsync(pme -> { if (pme.getResult().equals(PlayerChatEvent.ChatResult.allowed())){ - player.getConnectedServer().getMinecraftConnection().write(chat); + player.getConnectedServer().getConnection().write(chat); } else if (pme.getResult().isAllowed() && pme.getResult().getMessage().isPresent()){ - player.getConnectedServer().getMinecraftConnection().write(Chat.createServerbound(pme.getResult().getMessage().get())); + player.getConnectedServer().getConnection().write(Chat.createServerbound(pme.getResult().getMessage().get())); } - }, player.getConnectedServer().getMinecraftConnection().getChannel().eventLoop()); + }, player.getConnectedServer().getConnection().getChannel().eventLoop()); } return; } @@ -110,7 +110,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { if (packet instanceof TabCompleteRequest) { // Record the request so that the outstanding request can be augmented later. outstandingTabComplete = (TabCompleteRequest) packet; - serverConnection.getMinecraftConnection().write(packet); + serverConnection.getConnection().write(packet); } if (packet instanceof PluginMessage) { @@ -120,7 +120,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { // If we don't want to handle this packet, just forward it on. if (serverConnection.hasCompletedJoin()) { - serverConnection.getMinecraftConnection().write(packet); + serverConnection.getConnection().write(packet); } } @@ -133,7 +133,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } if (serverConnection.hasCompletedJoin()) { - serverConnection.getMinecraftConnection().write(buf.retain()); + serverConnection.getConnection().write(buf.retain()); } } @@ -157,7 +157,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { VelocityServerConnection server = player.getConnectedServer(); if (server != null) { boolean writable = player.getConnection().getChannel().isWritable(); - server.getMinecraftConnection().getChannel().config().setAutoRead(writable); + server.getConnection().getChannel().config().setAutoRead(writable); } } @@ -215,14 +215,14 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { if (!toRegister.isEmpty()) { String channel = player.getConnection().getProtocolVersion() >= ProtocolConstants.MINECRAFT_1_13 ? "minecraft:register" : "REGISTER"; - player.getConnectedServer().getMinecraftConnection().delayedWrite(PluginMessageUtil.constructChannelsPacket( + player.getConnectedServer().getConnection().delayedWrite(PluginMessageUtil.constructChannelsPacket( channel, toRegister)); } // If we had plugin messages queued during login/FML handshake, send them now. PluginMessage pm; while ((pm = loginPluginMessages.poll()) != null) { - player.getConnectedServer().getMinecraftConnection().delayedWrite(pm); + player.getConnectedServer().getConnection().delayedWrite(pm); } // Clear any title from the previous server. @@ -230,7 +230,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { // Flush everything player.getConnection().flush(); - player.getConnectedServer().getMinecraftConnection().flush(); + player.getConnectedServer().getConnection().flush(); player.getConnectedServer().setHasCompletedJoin(true); if (player.getConnectedServer().isLegacyForge()) { // We only need to indicate we can send a reset packet if we complete a handshake, that is, @@ -268,18 +268,18 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { if (actuallyRegistered.size() > 0) { PluginMessage newRegisterPacket = PluginMessageUtil.constructChannelsPacket(packet.getChannel(), actuallyRegistered); - player.getConnectedServer().getMinecraftConnection().write(newRegisterPacket); + player.getConnectedServer().getConnection().write(newRegisterPacket); } } else if (PluginMessageUtil.isMCUnregister(packet)) { List channels = PluginMessageUtil.getChannels(packet); clientPluginMsgChannels.removeAll(channels); - player.getConnectedServer().getMinecraftConnection().write(packet); + player.getConnectedServer().getConnection().write(packet); } else if (PluginMessageUtil.isMCBrand(packet)) { - player.getConnectedServer().getMinecraftConnection().write(PluginMessageUtil.rewriteMCBrand(packet)); + player.getConnectedServer().getConnection().write(PluginMessageUtil.rewriteMCBrand(packet)); } else if (player.getConnectedServer().isLegacyForge() && !player.getConnectedServer().hasCompletedJoin()) { if (packet.getChannel().equals(VelocityConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL)) { // Always forward the FML handshake to the remote server. - player.getConnectedServer().getMinecraftConnection().write(packet); + player.getConnectedServer().getConnection().write(packet); } else { // The client is trying to send messages too early. This is primarily caused by mods, but it's further // aggravated by Velocity. To work around these issues, we will queue any non-FML handshake messages to @@ -289,15 +289,15 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } else { ChannelIdentifier id = server.getChannelRegistrar().getFromId(packet.getChannel()); if (id == null) { - player.getConnectedServer().getMinecraftConnection().write(packet); + player.getConnectedServer().getConnection().write(packet); } else { PluginMessageEvent event = new PluginMessageEvent(player, player.getConnectedServer(), id, packet.getData()); server.getEventManager().fire(event) .thenAcceptAsync(pme -> { if (pme.getResult().isAllowed()) { - player.getConnectedServer().getMinecraftConnection().write(packet); + player.getConnectedServer().getConnection().write(packet); } - }, player.getConnectedServer().getMinecraftConnection().getChannel().eventLoop()); + }, player.getConnectedServer().getConnection().getChannel().eventLoop()); } } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index cd4c55572..b7a5f8654 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -420,7 +420,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { @Override public void spoofChatInput(String input) { Preconditions.checkArgument(input.length() <= Chat.MAX_SERVERBOUND_MESSAGE_LENGTH, "input cannot be greater than " + Chat.MAX_SERVERBOUND_MESSAGE_LENGTH + " characters in length"); - connectedServer.getMinecraftConnection().write(Chat.createServerbound(input)); + connectedServer.getConnection().write(Chat.createServerbound(input)); } private class ConnectionRequestBuilderImpl implements ConnectionRequestBuilder {