13
0
geforkt von Mirrors/Velocity

Enough of the Login protocol to display a message upon connect

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-07-24 15:50:07 -04:00
Ursprung 286be4987a
Commit 0c2ca969f7
7 geänderte Dateien mit 113 neuen und 7 gelöschten Zeilen

Datei anzeigen

@ -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;

Datei anzeigen

@ -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);

Datei anzeigen

@ -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);
}
}

Datei anzeigen

@ -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);
}
}

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -55,6 +55,5 @@ public class MinecraftClientSessionHandler extends ChannelInboundHandlerAdapter
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}

Datei anzeigen

@ -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);
}
}