From 2671590ad201f039be690c6e5e5c72500546a439 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 5 Jul 2019 12:53:17 -0400 Subject: [PATCH] Clean up LoginPluginResponse and use DeferredByteBufHolder too --- .../backend/LoginSessionHandler.java | 18 +++-------- .../protocol/packet/LoginPluginResponse.java | 31 ++++++++++--------- 2 files changed, 22 insertions(+), 27 deletions(-) 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 14b7ad22c..b2535931e 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 @@ -1,6 +1,5 @@ package com.velocitypowered.proxy.connection.backend; -import com.velocitypowered.api.proxy.ConnectionRequestBuilder; import com.velocitypowered.api.util.GameProfile; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.config.PlayerInfoForwarding; @@ -56,23 +55,16 @@ public class LoginSessionHandler implements MinecraftSessionHandler { MinecraftConnection mc = serverConn.ensureConnected(); VelocityConfiguration configuration = server.getConfiguration(); if (configuration.getPlayerInfoForwardingMode() == PlayerInfoForwarding.MODERN && packet - .getChannel() - .equals(VelocityConstants.VELOCITY_IP_FORWARDING_CHANNEL)) { - LoginPluginResponse response = new LoginPluginResponse(); - response.setSuccess(true); - response.setId(packet.getId()); - response.setData(createForwardingData(configuration.getForwardingSecret(), + .getChannel().equals(VelocityConstants.VELOCITY_IP_FORWARDING_CHANNEL)) { + ByteBuf forwardingData = createForwardingData(configuration.getForwardingSecret(), serverConn.getPlayer().getRemoteAddress().getHostString(), - serverConn.getPlayer().getGameProfile())); + serverConn.getPlayer().getGameProfile()); + LoginPluginResponse response = new LoginPluginResponse(packet.getId(), true, forwardingData); mc.write(response); informationForwarded = true; } else { // Don't understand - LoginPluginResponse response = new LoginPluginResponse(); - response.setSuccess(false); - response.setId(packet.getId()); - response.setData(Unpooled.EMPTY_BUFFER); - mc.write(response); + mc.write(new LoginPluginResponse(packet.getId(), false, Unpooled.EMPTY_BUFFER)); } return true; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginResponse.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginResponse.java index 159482cd8..5321c0eea 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginResponse.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/LoginPluginResponse.java @@ -4,14 +4,25 @@ import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolUtils; +import com.velocitypowered.proxy.protocol.util.DeferredByteBufHolder; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; -public class LoginPluginResponse implements MinecraftPacket { +public class LoginPluginResponse extends DeferredByteBufHolder implements MinecraftPacket { private int id; private boolean success; - private ByteBuf data = Unpooled.EMPTY_BUFFER; + + public LoginPluginResponse() { + super(Unpooled.EMPTY_BUFFER); + } + + public LoginPluginResponse(int id, boolean success, @MonotonicNonNull ByteBuf buf) { + super(buf); + this.id = id; + this.success = success; + } public int getId() { return id; @@ -29,20 +40,12 @@ public class LoginPluginResponse implements MinecraftPacket { this.success = success; } - public ByteBuf getData() { - return data; - } - - public void setData(ByteBuf data) { - this.data = data; - } - @Override public String toString() { return "LoginPluginResponse{" + "id=" + id + ", success=" + success - + ", data=" + data + + ", data=" + super.toString() + '}'; } @@ -51,9 +54,9 @@ public class LoginPluginResponse implements MinecraftPacket { this.id = ProtocolUtils.readVarInt(buf); this.success = buf.readBoolean(); if (buf.isReadable()) { - this.data = buf.readSlice(buf.readableBytes()); + this.replace(buf.readSlice(buf.readableBytes())); } else { - this.data = Unpooled.EMPTY_BUFFER; + this.replace(Unpooled.EMPTY_BUFFER); } } @@ -61,7 +64,7 @@ public class LoginPluginResponse implements MinecraftPacket { public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { ProtocolUtils.writeVarInt(buf, id); buf.writeBoolean(success); - buf.writeBytes(data); + buf.writeBytes(content()); } @Override