Skip to content

Commit 2f05a23

Browse files
authored
Merge pull request #752 from GeyserMC/feature/1.20.2
Move to 1.20.2
2 parents 592a7fd + d4f3053 commit 2f05a23

File tree

60 files changed

+757
-501
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+757
-501
lines changed

example/src/main/java/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.github.steveice10.mc.protocol.codec.MinecraftCodec;
1212
import com.github.steveice10.mc.protocol.data.ProtocolState;
1313
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
14+
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerSpawnInfo;
1415
import com.github.steveice10.mc.protocol.data.status.PlayerInfo;
1516
import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
1617
import com.github.steveice10.mc.protocol.data.status.VersionInfo;
@@ -46,6 +47,7 @@
4647
import java.time.Instant;
4748
import java.util.ArrayList;
4849
import java.util.Arrays;
50+
import java.util.BitSet;
4951
import java.util.zip.GZIPInputStream;
5052

5153
public class MinecraftProtocolTest {
@@ -69,7 +71,7 @@ public static void main(String[] args) {
6971
server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, (ServerInfoBuilder) session ->
7072
new ServerStatusInfo(
7173
new VersionInfo(MinecraftCodec.CODEC.getMinecraftVersion(), MinecraftCodec.CODEC.getProtocolVersion()),
72-
new PlayerInfo(100, 0, new GameProfile[0]),
74+
new PlayerInfo(100, 0, new ArrayList<>()),
7375
Component.text("Hello world!"),
7476
null,
7577
false
@@ -80,22 +82,24 @@ public static void main(String[] args) {
8082
session.send(new ClientboundLoginPacket(
8183
0,
8284
false,
83-
GameMode.SURVIVAL,
84-
GameMode.SURVIVAL,
85-
1,
8685
new String[]{"minecraft:world"},
87-
loadNetworkCodec(),
88-
"minecraft:overworld",
89-
"minecraft:world",
90-
100,
9186
0,
9287
16,
9388
16,
9489
false,
9590
false,
9691
false,
97-
false,
98-
null
92+
new PlayerSpawnInfo(
93+
"minecraft:overworld",
94+
"minecraft:world",
95+
100,
96+
GameMode.SURVIVAL,
97+
GameMode.SURVIVAL,
98+
false,
99+
false,
100+
null,
101+
100
102+
)
99103
))
100104
);
101105

@@ -158,7 +162,7 @@ private static void status() {
158162
+ ", " + info.getVersionInfo().getProtocolVersion());
159163
System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers()
160164
+ " / " + info.getPlayerInfo().getMaxPlayers());
161-
System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers()));
165+
System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers().toArray()));
162166
System.out.println("Description: " + info.getDescription());
163167
System.out.println("Icon: " + info.getIconPng());
164168
});
@@ -205,7 +209,7 @@ private static void login() {
205209
@Override
206210
public void packetReceived(Session session, Packet packet) {
207211
if (packet instanceof ClientboundLoginPacket) {
208-
session.send(new ServerboundChatPacket("Hello, this is a test of MCProtocolLib.", Instant.now().toEpochMilli(), 0, new byte[0], false, new ArrayList<>(), null));
212+
session.send(new ServerboundChatPacket("Hello, this is a test of MCProtocolLib.", Instant.now().toEpochMilli(), 0L, null, 0, new BitSet()));
209213
} else if (packet instanceof ClientboundSystemChatPacket) {
210214
Component message = ((ClientboundSystemChatPacket) packet).getContent();
211215
System.out.println("Received Message: " + message);
@@ -224,14 +228,4 @@ public void disconnected(DisconnectedEvent event) {
224228

225229
client.connect();
226230
}
227-
228-
private static CompoundTag loadNetworkCodec() {
229-
try (InputStream inputStream = MinecraftProtocolTest.class.getClassLoader().getResourceAsStream("network_codec.nbt");
230-
DataInputStream stream = new DataInputStream(new GZIPInputStream(inputStream))) {
231-
return (CompoundTag) NBTIO.readTag((DataInput) stream);
232-
} catch (IOException e) {
233-
e.printStackTrace();
234-
throw new AssertionError("Unable to load network codec.");
235-
}
236-
}
237231
}
-2.64 KB
Binary file not shown.

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.steveice10</groupId>
77
<artifactId>mcprotocollib</artifactId>
8-
<version>1.20-2</version>
8+
<version>1.20.2-1-SNAPSHOT</version>
99
<packaging>jar</packaging>
1010

1111
<name>MCProtocolLib</name>
@@ -82,9 +82,9 @@
8282

8383
<dependencies>
8484
<dependency>
85-
<groupId>com.github.GeyserMC</groupId>
85+
<groupId>com.github.steveice10</groupId>
8686
<artifactId>opennbt</artifactId>
87-
<version>1.4</version>
87+
<version>1.6</version>
8888
<scope>compile</scope>
8989
</dependency>
9090
<dependency>

src/main/java/com/github/steveice10/mc/protocol/ClientListener.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@
1111
import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
1212
import com.github.steveice10.mc.protocol.data.status.handler.ServerInfoHandler;
1313
import com.github.steveice10.mc.protocol.data.status.handler.ServerPingTimeHandler;
14+
import com.github.steveice10.mc.protocol.packet.configuration.clientbound.ClientboundFinishConfigurationPacket;
15+
import com.github.steveice10.mc.protocol.packet.configuration.serverbound.ServerboundFinishConfigurationPacket;
1416
import com.github.steveice10.mc.protocol.packet.handshake.serverbound.ClientIntentionPacket;
15-
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundDisconnectPacket;
16-
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundKeepAlivePacket;
17-
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundKeepAlivePacket;
17+
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundDisconnectPacket;
18+
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundKeepAlivePacket;
19+
import com.github.steveice10.mc.protocol.packet.common.serverbound.ServerboundKeepAlivePacket;
20+
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundStartConfigurationPacket;
21+
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundConfigurationAcknowledgedPacket;
1822
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundGameProfilePacket;
1923
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundHelloPacket;
2024
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLoginCompressionPacket;
2125
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLoginDisconnectPacket;
2226
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundHelloPacket;
2327
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundKeyPacket;
28+
import com.github.steveice10.mc.protocol.packet.login.serverbound.ServerboundLoginAcknowledgedPacket;
2429
import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundPongResponsePacket;
2530
import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundStatusResponsePacket;
2631
import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundPingRequestPacket;
@@ -31,11 +36,11 @@
3136
import com.github.steveice10.packetlib.packet.Packet;
3237
import lombok.AllArgsConstructor;
3338
import lombok.NonNull;
39+
import lombok.SneakyThrows;
3440

3541
import javax.crypto.KeyGenerator;
3642
import javax.crypto.SecretKey;
3743
import java.security.NoSuchAlgorithmException;
38-
import java.util.UUID;
3944

4045
/**
4146
* Handles making initial login and status requests for clients.
@@ -44,6 +49,7 @@
4449
public class ClientListener extends SessionAdapter {
4550
private final @NonNull ProtocolState targetState;
4651

52+
@SneakyThrows
4753
@Override
4854
public void packetReceived(Session session, Packet packet) {
4955
MinecraftProtocol protocol = (MinecraftProtocol) session.getPacketProtocol();
@@ -84,7 +90,7 @@ public void packetReceived(Session session, Packet packet) {
8490
session.send(new ServerboundKeyPacket(helloPacket.getPublicKey(), key, helloPacket.getChallenge()));
8591
session.enableEncryption(protocol.enableEncryption(key));
8692
} else if (packet instanceof ClientboundGameProfilePacket) {
87-
protocol.setState(ProtocolState.GAME);
93+
session.send(new ServerboundLoginAcknowledgedPacket());
8894
} else if (packet instanceof ClientboundLoginDisconnectPacket) {
8995
session.disconnect(((ClientboundLoginDisconnectPacket) packet).getReason());
9096
} else if (packet instanceof ClientboundLoginCompressionPacket) {
@@ -113,15 +119,21 @@ public void packetReceived(Session session, Packet packet) {
113119
session.send(new ServerboundKeepAlivePacket(((ClientboundKeepAlivePacket) packet).getPingId()));
114120
} else if (packet instanceof ClientboundDisconnectPacket) {
115121
session.disconnect(((ClientboundDisconnectPacket) packet).getReason());
122+
} else if (packet instanceof ClientboundStartConfigurationPacket) {
123+
session.send(new ServerboundConfigurationAcknowledgedPacket());
124+
}
125+
} else if (protocol.getState() == ProtocolState.CONFIGURATION) {
126+
if (packet instanceof ClientboundFinishConfigurationPacket) {
127+
session.send(new ServerboundFinishConfigurationPacket());
116128
}
117129
}
118130
}
119131

120132
@Override
121133
public void packetSent(Session session, Packet packet) {
134+
MinecraftProtocol protocol = (MinecraftProtocol) session.getPacketProtocol();
122135
if (packet instanceof ClientIntentionPacket) {
123136
// Once the HandshakePacket has been sent, switch to the next protocol mode.
124-
MinecraftProtocol protocol = (MinecraftProtocol) session.getPacketProtocol();
125137
protocol.setState(this.targetState);
126138

127139
if (this.targetState == ProtocolState.LOGIN) {
@@ -130,6 +142,12 @@ public void packetSent(Session session, Packet packet) {
130142
} else {
131143
session.send(new ServerboundStatusRequestPacket());
132144
}
145+
} else if (packet instanceof ServerboundLoginAcknowledgedPacket) {
146+
protocol.setState(ProtocolState.CONFIGURATION); // LOGIN -> CONFIGURATION
147+
} else if (packet instanceof ServerboundFinishConfigurationPacket) {
148+
protocol.setState(ProtocolState.GAME); // CONFIGURATION -> GAME
149+
} else if (packet instanceof ServerboundConfigurationAcknowledgedPacket) {
150+
protocol.setState(ProtocolState.CONFIGURATION); // GAME -> CONFIGURATION
133151
}
134152
}
135153

src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.github.steveice10.mc.protocol.codec.PacketCodec;
77
import com.github.steveice10.mc.protocol.codec.PacketStateCodec;
88
import com.github.steveice10.mc.protocol.data.ProtocolState;
9+
import com.github.steveice10.opennbt.NBTIO;
10+
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
911
import com.github.steveice10.packetlib.Server;
1012
import com.github.steveice10.packetlib.Session;
1113
import com.github.steveice10.packetlib.codec.PacketCodecHelper;
@@ -19,17 +21,31 @@
1921
import lombok.Getter;
2022
import lombok.NonNull;
2123
import lombok.Setter;
24+
import org.jetbrains.annotations.Nullable;
2225

26+
import java.io.DataInput;
27+
import java.io.DataInputStream;
2328
import java.io.IOException;
29+
import java.io.InputStream;
2430
import java.security.GeneralSecurityException;
2531
import java.security.Key;
32+
import java.util.Objects;
2633
import java.util.UUID;
34+
import java.util.zip.GZIPInputStream;
2735

2836
/**
2937
* Implements the Minecraft protocol.
3038
*/
3139
public class MinecraftProtocol extends PacketProtocol {
3240

41+
/**
42+
* The network codec sent from the server to the client during {@link ProtocolState#CONFIGURATION}.
43+
* Lazily loaded once when {@link #newServerSession(Server, Session)} is invoked,
44+
* if {@link #isUseDefaultListeners()} is true.
45+
*/
46+
@Nullable
47+
private static CompoundTag DEFAULT_NETWORK_CODEC;
48+
3349
/**
3450
* The codec used for the Minecraft protocol.
3551
*/
@@ -85,7 +101,7 @@ public MinecraftProtocol(PacketCodec codec) {
85101
* @param username Username to use.
86102
*/
87103
public MinecraftProtocol(@NonNull String username) {
88-
this(new GameProfile((UUID) null, username), null);
104+
this(new GameProfile(UUID.randomUUID(), username), null);
89105
}
90106

91107
/**
@@ -95,7 +111,7 @@ public MinecraftProtocol(@NonNull String username) {
95111
* @param username Username to use.
96112
*/
97113
public MinecraftProtocol(@NonNull PacketCodec codec, @NonNull String username) {
98-
this(codec, new GameProfile((UUID) null, username), null);
114+
this(codec, new GameProfile(UUID.randomUUID(), username), null);
99115
}
100116

101117
/**
@@ -156,7 +172,11 @@ public void newServerSession(Server server, Session session) {
156172
this.setState(ProtocolState.HANDSHAKE);
157173

158174
if (this.useDefaultListeners) {
159-
session.addListener(new ServerListener());
175+
if (DEFAULT_NETWORK_CODEC == null) {
176+
DEFAULT_NETWORK_CODEC = loadNetworkCodec();
177+
}
178+
179+
session.addListener(new ServerListener(DEFAULT_NETWORK_CODEC));
160180
}
161181
}
162182

@@ -231,4 +251,13 @@ public Class<? extends Packet> getServerboundClass(int id) {
231251
public PacketDefinition<?, ?> getClientboundDefinition(int id) {
232252
return this.stateCodec.getClientboundDefinition(id);
233253
}
254+
255+
public static CompoundTag loadNetworkCodec() {
256+
try (InputStream inputStream = Objects.requireNonNull(MinecraftProtocol.class.getClassLoader().getResourceAsStream("networkCodec.nbt")) ;
257+
DataInputStream stream = new DataInputStream(new GZIPInputStream(inputStream))) {
258+
return (CompoundTag) NBTIO.readTag((DataInput) stream);
259+
} catch (Exception e) {
260+
throw new AssertionError("Unable to load network codec.", e);
261+
}
262+
}
234263
}

0 commit comments

Comments
 (0)