diff --git a/src/main/java/io/minimum/minecraft/velocity/protocol/ProtocolUtils.java b/src/main/java/io/minimum/minecraft/velocity/protocol/ProtocolUtils.java index 2ce7c5297..b96be4691 100644 --- a/src/main/java/io/minimum/minecraft/velocity/protocol/ProtocolUtils.java +++ b/src/main/java/io/minimum/minecraft/velocity/protocol/ProtocolUtils.java @@ -6,7 +6,7 @@ import io.netty.buffer.ByteBuf; import java.nio.charset.StandardCharsets; public enum ProtocolUtils { ; - private static final int DEFAULT_MAX_STRING_SIZE = 1024 * 1024; // 1MB + private static final int DEFAULT_MAX_STRING_SIZE = 65536; // 64KiB public static int readVarInt(ByteBuf buf) { int numRead = 0; diff --git a/src/main/java/io/minimum/minecraft/velocity/protocol/StateRegistry.java b/src/main/java/io/minimum/minecraft/velocity/protocol/StateRegistry.java index efad44037..b1ebabe5c 100644 --- a/src/main/java/io/minimum/minecraft/velocity/protocol/StateRegistry.java +++ b/src/main/java/io/minimum/minecraft/velocity/protocol/StateRegistry.java @@ -1,9 +1,6 @@ package io.minimum.minecraft.velocity.protocol; -import io.minimum.minecraft.velocity.protocol.packets.Handshake; -import io.minimum.minecraft.velocity.protocol.packets.Ping; -import io.minimum.minecraft.velocity.protocol.packets.StatusRequest; -import io.minimum.minecraft.velocity.protocol.packets.StatusResponse; +import io.minimum.minecraft.velocity.protocol.packets.*; import java.util.HashMap; import java.util.Map; @@ -23,6 +20,15 @@ public enum StateRegistry { TO_CLIENT.register(0x00, StatusResponse.class, StatusResponse::new); TO_CLIENT.register(0x01, Ping.class, Ping::new); } + }, + PLAY { + + }, + LOGIN { + { + TO_SERVER.register(0x00, ServerLogin.class, ServerLogin::new); + TO_CLIENT.register(0x00, Disconnect.class, Disconnect::new); + } }; public final ProtocolMappings TO_CLIENT = new ProtocolMappings(ProtocolConstants.Direction.TO_CLIENT, this); diff --git a/src/main/java/io/minimum/minecraft/velocity/protocol/packets/Disconnect.java b/src/main/java/io/minimum/minecraft/velocity/protocol/packets/Disconnect.java new file mode 100644 index 000000000..c00dcfb12 --- /dev/null +++ b/src/main/java/io/minimum/minecraft/velocity/protocol/packets/Disconnect.java @@ -0,0 +1,35 @@ +package io.minimum.minecraft.velocity.protocol.packets; + +import io.minimum.minecraft.velocity.protocol.MinecraftPacket; +import io.minimum.minecraft.velocity.protocol.ProtocolConstants; +import io.minimum.minecraft.velocity.protocol.ProtocolUtils; +import io.netty.buffer.ByteBuf; + +public class Disconnect implements MinecraftPacket { + private String reason; + + public String getReason() { + return reason; + } + + public void setReason(String reason) { + this.reason = reason; + } + + @Override + public String toString() { + return "Disconnect{" + + "reason='" + reason + '\'' + + '}'; + } + + @Override + public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + reason = ProtocolUtils.readString(buf); + } + + @Override + public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + ProtocolUtils.writeString(buf, reason); + } +} diff --git a/src/main/java/io/minimum/minecraft/velocity/protocol/packets/ServerLogin.java b/src/main/java/io/minimum/minecraft/velocity/protocol/packets/ServerLogin.java new file mode 100644 index 000000000..3efc1f803 --- /dev/null +++ b/src/main/java/io/minimum/minecraft/velocity/protocol/packets/ServerLogin.java @@ -0,0 +1,35 @@ +package io.minimum.minecraft.velocity.protocol.packets; + +import io.minimum.minecraft.velocity.protocol.MinecraftPacket; +import io.minimum.minecraft.velocity.protocol.ProtocolConstants; +import io.minimum.minecraft.velocity.protocol.ProtocolUtils; +import io.netty.buffer.ByteBuf; + +public class ServerLogin implements MinecraftPacket { + private String username; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + @Override + public String toString() { + return "ServerLogin{" + + "username='" + username + '\'' + + '}'; + } + + @Override + public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + username = ProtocolUtils.readString(buf, 16); + } + + @Override + public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + ProtocolUtils.writeString(buf, username); + } +} diff --git a/src/main/java/io/minimum/minecraft/velocity/proxy/InboundMinecraftConnection.java b/src/main/java/io/minimum/minecraft/velocity/proxy/InboundMinecraftConnection.java index d79b0d001..c7a4493fa 100644 --- a/src/main/java/io/minimum/minecraft/velocity/proxy/InboundMinecraftConnection.java +++ b/src/main/java/io/minimum/minecraft/velocity/proxy/InboundMinecraftConnection.java @@ -6,6 +6,7 @@ import io.minimum.minecraft.velocity.protocol.netty.MinecraftDecoder; import io.minimum.minecraft.velocity.protocol.netty.MinecraftEncoder; import io.minimum.minecraft.velocity.protocol.packets.Handshake; import io.minimum.minecraft.velocity.proxy.handler.HandshakeSessionHandler; +import io.minimum.minecraft.velocity.proxy.handler.LoginSessionHandler; import io.minimum.minecraft.velocity.proxy.handler.StatusSessionHandler; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; @@ -66,7 +67,9 @@ public class InboundMinecraftConnection { this.sessionHandler = new StatusSessionHandler(this); break; default: - throw new UnsupportedOperationException("Unsupported next protocol state " + handshake.getNextStatus()); + this.setStatus(StateRegistry.LOGIN); + this.sessionHandler = new LoginSessionHandler(this); + break; } } diff --git a/src/main/java/io/minimum/minecraft/velocity/proxy/MinecraftClientSessionHandler.java b/src/main/java/io/minimum/minecraft/velocity/proxy/MinecraftClientSessionHandler.java index fe8253b4e..414f4808c 100644 --- a/src/main/java/io/minimum/minecraft/velocity/proxy/MinecraftClientSessionHandler.java +++ b/src/main/java/io/minimum/minecraft/velocity/proxy/MinecraftClientSessionHandler.java @@ -55,6 +55,5 @@ public class MinecraftClientSessionHandler extends ChannelInboundHandlerAdapter @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); - ctx.close(); } } diff --git a/src/main/java/io/minimum/minecraft/velocity/proxy/handler/LoginSessionHandler.java b/src/main/java/io/minimum/minecraft/velocity/proxy/handler/LoginSessionHandler.java new file mode 100644 index 000000000..57b4aa1f2 --- /dev/null +++ b/src/main/java/io/minimum/minecraft/velocity/proxy/handler/LoginSessionHandler.java @@ -0,0 +1,28 @@ +package io.minimum.minecraft.velocity.proxy.handler; + +import com.google.common.base.Preconditions; +import io.minimum.minecraft.velocity.protocol.MinecraftPacket; +import io.minimum.minecraft.velocity.protocol.packets.Disconnect; +import io.minimum.minecraft.velocity.protocol.packets.ServerLogin; +import io.minimum.minecraft.velocity.proxy.InboundMinecraftConnection; +import io.minimum.minecraft.velocity.proxy.MinecraftSessionHandler; +import net.kyori.text.TextComponent; +import net.kyori.text.serializer.ComponentSerializers; + +public class LoginSessionHandler implements MinecraftSessionHandler { + private final InboundMinecraftConnection connection; + + public LoginSessionHandler(InboundMinecraftConnection connection) { + this.connection = Preconditions.checkNotNull(connection, "connection"); + } + + @Override + public void handle(MinecraftPacket packet) { + Preconditions.checkArgument(packet instanceof ServerLogin, "Expected a ServerLogin packet, not " + packet.getClass().getName()); + + // Disconnect with test message + Disconnect disconnect = new Disconnect(); + disconnect.setReason(ComponentSerializers.JSON.serialize(TextComponent.of("Hi there!"))); + connection.closeWith(disconnect); + } +}