Skip to content

Commit

Permalink
23w31a part 2 electric boogaloo
Browse files Browse the repository at this point in the history
Fixed tests, updated the login cycle and some misc touch-ups
  • Loading branch information
basaigh committed Aug 13, 2023
1 parent b29b1d0 commit c33fba4
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.zip.GZIPInputStream;

public class MinecraftProtocolTest {
Expand All @@ -69,7 +70,7 @@ public static void main(String[] args) {
server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, (ServerInfoBuilder) session ->
new ServerStatusInfo(
new VersionInfo(MinecraftCodec.CODEC.getMinecraftVersion(), MinecraftCodec.CODEC.getProtocolVersion()),
new PlayerInfo(100, 0, new GameProfile[0]),
new PlayerInfo(100, 0, new ArrayList<>()),
Component.text("Hello world!"),
null,
false
Expand All @@ -80,22 +81,21 @@ public static void main(String[] args) {
session.send(new ClientboundLoginPacket(
0,
false,
GameMode.SURVIVAL,
GameMode.SURVIVAL,
1,
new String[]{"minecraft:world"},
loadNetworkCodec(),
"minecraft:overworld",
"minecraft:world",
100,
0,
16,
16,
false,
false,
"minecraft:overworld",
"minecraft:world",
100,
GameMode.SURVIVAL,
GameMode.SURVIVAL,
false,
false,
null
null,
100
))
);

Expand Down Expand Up @@ -158,7 +158,7 @@ private static void status() {
+ ", " + info.getVersionInfo().getProtocolVersion());
System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers()
+ " / " + info.getPlayerInfo().getMaxPlayers());
System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers()));
System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers().toArray()));
System.out.println("Description: " + info.getDescription());
System.out.println("Icon: " + info.getIconPng());
});
Expand Down Expand Up @@ -205,7 +205,7 @@ private static void login() {
@Override
public void packetReceived(Session session, Packet packet) {
if (packet instanceof ClientboundLoginPacket) {
session.send(new ServerboundChatPacket("Hello, this is a test of MCProtocolLib.", Instant.now().toEpochMilli(), 0, new byte[0], false, new ArrayList<>(), null));
session.send(new ServerboundChatPacket("Hello, this is a test of MCProtocolLib.", Instant.now().toEpochMilli(), 0L, null, 0, new BitSet()));
} else if (packet instanceof ClientboundSystemChatPacket) {
Component message = ((ClientboundSystemChatPacket) packet).getContent();
System.out.println("Received Message: " + message);
Expand All @@ -224,14 +224,4 @@ public void disconnected(DisconnectedEvent event) {

client.connect();
}

private static CompoundTag loadNetworkCodec() {
try (InputStream inputStream = MinecraftProtocolTest.class.getClassLoader().getResourceAsStream("network_codec.nbt");
DataInputStream stream = new DataInputStream(new GZIPInputStream(inputStream))) {
return (CompoundTag) NBTIO.readTag((DataInput) stream);
} catch (IOException e) {
e.printStackTrace();
throw new AssertionError("Unable to load network codec.");
}
}
}
Binary file removed example/src/main/resources/network_codec.nbt
Binary file not shown.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.steveice10</groupId>
<artifactId>mcprotocollib</artifactId>
<version>1.20.2-SNAPSHOT</version>
<version>1.20.2-1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>MCProtocolLib</name>
Expand Down Expand Up @@ -82,9 +82,9 @@

<dependencies>
<dependency>
<groupId>com.github.GeyserMC</groupId>
<groupId>com.github.steveice10</groupId> <!-- Local repository groupId, will need to be reverted -->
<artifactId>opennbt</artifactId>
<version>1.4</version>
<version>1.6-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.github.steveice10.mc.protocol.data.status.handler.ServerInfoHandler;
import com.github.steveice10.mc.protocol.data.status.handler.ServerPingTimeHandler;
import com.github.steveice10.mc.protocol.packet.configuration.clientbound.ClientboundFinishConfigurationPacket;
import com.github.steveice10.mc.protocol.packet.configuration.serverbound.ServerboundFinishConfigurationPacket;
import com.github.steveice10.mc.protocol.packet.handshake.serverbound.ClientIntentionPacket;
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundDisconnectPacket;
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundKeepAlivePacket;
Expand All @@ -23,6 +24,7 @@
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLoginDisconnectPacket;
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundHelloPacket;
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundKeyPacket;
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundLoginAcknowledgedPacket;
import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundPongResponsePacket;
import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundStatusResponsePacket;
import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundPingRequestPacket;
Expand All @@ -33,6 +35,7 @@
import com.github.steveice10.packetlib.packet.Packet;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.SneakyThrows;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
Expand All @@ -45,6 +48,7 @@
public class ClientListener extends SessionAdapter {
private final @NonNull ProtocolState targetState;

@SneakyThrows
@Override
public void packetReceived(Session session, Packet packet) {
MinecraftProtocol protocol = (MinecraftProtocol) session.getPacketProtocol();
Expand Down Expand Up @@ -85,6 +89,7 @@ public void packetReceived(Session session, Packet packet) {
session.send(new ServerboundKeyPacket(helloPacket.getPublicKey(), key, helloPacket.getChallenge()));
session.enableEncryption(protocol.enableEncryption(key));
} else if (packet instanceof ClientboundGameProfilePacket) {
session.send(new ServerboundLoginAcknowledgedPacket());
protocol.setState(ProtocolState.CONFIGURATION);
} else if (packet instanceof ClientboundLoginDisconnectPacket) {
session.disconnect(((ClientboundLoginDisconnectPacket) packet).getReason());
Expand Down Expand Up @@ -119,7 +124,7 @@ public void packetReceived(Session session, Packet packet) {
}
} else if (protocol.getState() == ProtocolState.CONFIGURATION) {
if (packet instanceof ClientboundFinishConfigurationPacket) {
protocol.setState(ProtocolState.GAME);
session.send(new ServerboundFinishConfigurationPacket());
}
}
}
Expand All @@ -137,6 +142,8 @@ public void packetSent(Session session, Packet packet) {
} else {
session.send(new ServerboundStatusRequestPacket());
}
} else if (packet instanceof ServerboundFinishConfigurationPacket) {
((MinecraftProtocol) session.getPacketProtocol()).setState(ProtocolState.GAME);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public MinecraftProtocol(PacketCodec codec) {
* @param username Username to use.
*/
public MinecraftProtocol(@NonNull String username) {
this(new GameProfile((UUID) null, username), null);
this(new GameProfile(UUID.randomUUID(), username), null);
}

/**
Expand All @@ -95,7 +95,7 @@ public MinecraftProtocol(@NonNull String username) {
* @param username Username to use.
*/
public MinecraftProtocol(@NonNull PacketCodec codec, @NonNull String username) {
this(codec, new GameProfile((UUID) null, username), null);
this(codec, new GameProfile(UUID.randomUUID(), username), null);
}

/**
Expand Down
41 changes: 31 additions & 10 deletions src/main/java/com/github/steveice10/mc/protocol/ServerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.github.steveice10.mc.protocol.data.status.VersionInfo;
import com.github.steveice10.mc.protocol.data.status.handler.ServerInfoBuilder;
import com.github.steveice10.mc.protocol.packet.configuration.clientbound.ClientboundFinishConfigurationPacket;
import com.github.steveice10.mc.protocol.packet.configuration.clientbound.ClientboundRegistryDataPacket;
import com.github.steveice10.mc.protocol.packet.configuration.serverbound.ServerboundFinishConfigurationPacket;
import com.github.steveice10.mc.protocol.packet.handshake.serverbound.ClientIntentionPacket;
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundDisconnectPacket;
Expand All @@ -21,10 +22,13 @@
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLoginDisconnectPacket;
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundHelloPacket;
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundKeyPacket;
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundLoginAcknowledgedPacket;
import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundPongResponsePacket;
import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundStatusResponsePacket;
import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundPingRequestPacket;
import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundStatusRequestPacket;
import com.github.steveice10.opennbt.NBTIO;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.ConnectedEvent;
import com.github.steveice10.packetlib.event.session.DisconnectingEvent;
Expand All @@ -33,6 +37,10 @@
import net.kyori.adventure.text.Component;

import javax.crypto.SecretKey;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
Expand All @@ -41,6 +49,7 @@
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;
import java.util.zip.GZIPInputStream;

/**
* Handles initial login and status requests for servers.
Expand Down Expand Up @@ -121,6 +130,10 @@ public void packetReceived(Session session, Packet packet) {
SecretKey key = keyPacket.getSecretKey(privateKey);
session.enableEncryption(protocol.enableEncryption(key));
new Thread(new UserAuthTask(session, key)).start();
} else if (packet instanceof ServerboundLoginAcknowledgedPacket) {
((MinecraftProtocol) session.getPacketProtocol()).setState(ProtocolState.CONFIGURATION);
session.send(new ClientboundRegistryDataPacket(loadNetworkCodec()));
session.send(new ClientboundFinishConfigurationPacket());
}
} else if (protocol.getState() == ProtocolState.STATUS) {
if (packet instanceof ServerboundStatusRequestPacket) {
Expand Down Expand Up @@ -152,6 +165,14 @@ public void packetReceived(Session session, Packet packet) {
} else if (protocol.getState() == ProtocolState.CONFIGURATION) {
if (packet instanceof ServerboundFinishConfigurationPacket) {
protocol.setState(ProtocolState.GAME);
ServerLoginHandler handler = session.getFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY);
if (handler != null) {
handler.loggedIn(session);
}

if (session.getFlag(MinecraftConstants.AUTOMATIC_KEEP_ALIVE_MANAGEMENT, true)) {
new Thread(new KeepAliveTask(session)).start();
}
}
}
}
Expand All @@ -161,16 +182,6 @@ public void packetSent(Session session, Packet packet) {
if (packet instanceof ClientboundLoginCompressionPacket) {
session.setCompressionThreshold(((ClientboundLoginCompressionPacket) packet).getThreshold(), true);
session.send(new ClientboundGameProfilePacket((GameProfile) session.getFlag(MinecraftConstants.PROFILE_KEY)));
} else if (packet instanceof ClientboundGameProfilePacket) {
((MinecraftProtocol) session.getPacketProtocol()).setState(ProtocolState.GAME);
ServerLoginHandler handler = session.getFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY);
if (handler != null) {
handler.loggedIn(session);
}

if (session.getFlag(MinecraftConstants.AUTOMATIC_KEEP_ALIVE_MANAGEMENT, true)) {
new Thread(new KeepAliveTask(session)).start();
}
}
}

Expand Down Expand Up @@ -241,4 +252,14 @@ public void run() {
}
}
}

public static CompoundTag loadNetworkCodec() {
try (InputStream inputStream = ServerListener.class.getClassLoader().getResourceAsStream("networkCodec.nbt");
DataInputStream stream = new DataInputStream(new GZIPInputStream(inputStream))) {
return (CompoundTag) NBTIO.readTag((DataInput) stream);
} catch (IOException e) {
e.printStackTrace();
throw new AssertionError("Unable to load network codec.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ public int read() {
return expected.cast(tag);
}

public CompoundTag readAnyTag(ByteBuf buf) throws IOException {
return readAnyTag(buf, CompoundTag.class);
}

@Nullable
public <T extends Tag> T readAnyTag(ByteBuf buf, Class<T> expected) throws IOException {
Tag tag = NBTIO.readAnyTag(new InputStream() {
@Override
public int read() {
return buf.readUnsignedByte();
}
});

if (tag == null) {
return null;
}

if (tag.getClass() != expected) {
throw new IllegalArgumentException("Expected tag of type " + expected.getName() + " but got " + tag.getClass().getName());
}

return expected.cast(tag);
}

public CompoundTag readTagLE(ByteBuf buf) throws IOException {
return readTagLE(buf, CompoundTag.class);
}
Expand Down Expand Up @@ -236,6 +260,15 @@ public void write(int b) throws IOException {
}, tag);
}

public <T extends Tag> void writeAnyTag(ByteBuf buf, T tag) throws IOException {
NBTIO.writeAnyTag(new OutputStream() {
@Override
public void write(int b) throws IOException {
buf.writeByte(b);
}
}, tag);
}

public <T extends Tag> void writeTagLE(ByteBuf buf, T tag) throws IOException {
NBTIO.writeTag(new OutputStream() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public class ClientboundRegistryDataPacket implements MinecraftPacket {
private final CompoundTag registry;

public ClientboundRegistryDataPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException {
this.registry = helper.readTag(in);
this.registry = helper.readAnyTag(in);
}

@Override
public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException {
helper.writeTag(out, this.registry);
helper.writeAnyTag(out, this.registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class ClientboundLoginPacket implements MinecraftPacket {
private final int entityId;
private final boolean hardcore;
private final @NonNull String[] worldNames;
private final @NonNull CompoundTag registry;
private final int maxPlayers;
private final int viewDistance;
private final int simulationDistance;
Expand All @@ -45,7 +44,6 @@ public ClientboundLoginPacket(ByteBuf in, MinecraftCodecHelper helper) throws IO
for (int i = 0; i < worldCount; i++) {
this.worldNames[i] = helper.readString(in);
}
this.registry = helper.readTag(in);
this.maxPlayers = helper.readVarInt(in);
this.viewDistance = helper.readVarInt(in);
this.simulationDistance = helper.readVarInt(in);
Expand Down Expand Up @@ -74,7 +72,6 @@ public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOExcepti
for (String worldName : this.worldNames) {
helper.writeString(out, worldName);
}
helper.writeTag(out, this.registry);
helper.writeVarInt(out, this.maxPlayers);
helper.writeVarInt(out, this.viewDistance);
helper.writeVarInt(out, this.simulationDistance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ServerboundCustomQueryAnswerPacket implements MinecraftPacket {
private final byte[] data;

public ServerboundCustomQueryAnswerPacket(int transactionId) {
this(transactionId, null);
this(transactionId, new byte[0]);
}

public ServerboundCustomQueryAnswerPacket(ByteBuf in, MinecraftCodecHelper helper) {
Expand Down
Loading

0 comments on commit c33fba4

Please sign in to comment.