Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.20.2 #752

Merged
merged 21 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.github.steveice10.mc.protocol.codec.MinecraftCodec;
import com.github.steveice10.mc.protocol.data.ProtocolState;
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerSpawnInfo;
import com.github.steveice10.mc.protocol.data.status.PlayerInfo;
import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
import com.github.steveice10.mc.protocol.data.status.VersionInfo;
Expand Down Expand Up @@ -46,6 +47,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 +71,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 +82,24 @@ 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,
false,
false,
null
new PlayerSpawnInfo(
"minecraft:overworld",
"minecraft:world",
100,
GameMode.SURVIVAL,
GameMode.SURVIVAL,
false,
false,
null,
100
)
))
);

Expand Down Expand Up @@ -158,7 +162,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 +209,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 +228,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</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>
<artifactId>opennbt</artifactId>
<version>1.4</version>
<version>1.6</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@
import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
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.ingame.clientbound.ClientboundDisconnectPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundKeepAlivePacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundKeepAlivePacket;
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundDisconnectPacket;
import com.github.steveice10.mc.protocol.packet.common.clientbound.ClientboundKeepAlivePacket;
import com.github.steveice10.mc.protocol.packet.common.serverbound.ServerboundKeepAlivePacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundStartConfigurationPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundConfigurationAcknowledgedPacket;
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundGameProfilePacket;
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundHelloPacket;
import com.github.steveice10.mc.protocol.packet.login.clientbound.ClientboundLoginCompressionPacket;
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 @@ -31,11 +36,11 @@
import com.github.steveice10.packetlib.packet.Packet;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.SneakyThrows;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
* Handles making initial login and status requests for clients.
Expand All @@ -44,6 +49,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 @@ -84,7 +90,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) {
protocol.setState(ProtocolState.GAME);
session.send(new ServerboundLoginAcknowledgedPacket());
} else if (packet instanceof ClientboundLoginDisconnectPacket) {
session.disconnect(((ClientboundLoginDisconnectPacket) packet).getReason());
} else if (packet instanceof ClientboundLoginCompressionPacket) {
Expand Down Expand Up @@ -113,15 +119,21 @@ public void packetReceived(Session session, Packet packet) {
session.send(new ServerboundKeepAlivePacket(((ClientboundKeepAlivePacket) packet).getPingId()));
} else if (packet instanceof ClientboundDisconnectPacket) {
session.disconnect(((ClientboundDisconnectPacket) packet).getReason());
} else if (packet instanceof ClientboundStartConfigurationPacket) {
session.send(new ServerboundConfigurationAcknowledgedPacket());
}
} else if (protocol.getState() == ProtocolState.CONFIGURATION) {
if (packet instanceof ClientboundFinishConfigurationPacket) {
session.send(new ServerboundFinishConfigurationPacket());
}
}
}

@Override
public void packetSent(Session session, Packet packet) {
MinecraftProtocol protocol = (MinecraftProtocol) session.getPacketProtocol();
if (packet instanceof ClientIntentionPacket) {
// Once the HandshakePacket has been sent, switch to the next protocol mode.
MinecraftProtocol protocol = (MinecraftProtocol) session.getPacketProtocol();
protocol.setState(this.targetState);

if (this.targetState == ProtocolState.LOGIN) {
Expand All @@ -130,6 +142,12 @@ public void packetSent(Session session, Packet packet) {
} else {
session.send(new ServerboundStatusRequestPacket());
}
} else if (packet instanceof ServerboundLoginAcknowledgedPacket) {
protocol.setState(ProtocolState.CONFIGURATION); // LOGIN -> CONFIGURATION
} else if (packet instanceof ServerboundFinishConfigurationPacket) {
protocol.setState(ProtocolState.GAME); // CONFIGURATION -> GAME
} else if (packet instanceof ServerboundConfigurationAcknowledgedPacket) {
protocol.setState(ProtocolState.CONFIGURATION); // GAME -> CONFIGURATION
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.github.steveice10.mc.protocol.codec.PacketCodec;
import com.github.steveice10.mc.protocol.codec.PacketStateCodec;
import com.github.steveice10.mc.protocol.data.ProtocolState;
import com.github.steveice10.opennbt.NBTIO;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.packetlib.Server;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.codec.PacketCodecHelper;
Expand All @@ -19,17 +21,31 @@
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.jetbrains.annotations.Nullable;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.util.Objects;
import java.util.UUID;
import java.util.zip.GZIPInputStream;

/**
* Implements the Minecraft protocol.
*/
public class MinecraftProtocol extends PacketProtocol {

/**
* The network codec sent from the server to the client during {@link ProtocolState#CONFIGURATION}.
* Lazily loaded once when {@link #newServerSession(Server, Session)} is invoked,
* if {@link #isUseDefaultListeners()} is true.
*/
@Nullable
private static CompoundTag DEFAULT_NETWORK_CODEC;

/**
* The codec used for the Minecraft protocol.
*/
Expand Down Expand Up @@ -85,7 +101,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 +111,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 Expand Up @@ -156,7 +172,11 @@ public void newServerSession(Server server, Session session) {
this.setState(ProtocolState.HANDSHAKE);

if (this.useDefaultListeners) {
session.addListener(new ServerListener());
if (DEFAULT_NETWORK_CODEC == null) {
DEFAULT_NETWORK_CODEC = loadNetworkCodec();
}

session.addListener(new ServerListener(DEFAULT_NETWORK_CODEC));
}
}

Expand Down Expand Up @@ -231,4 +251,13 @@ public Class<? extends Packet> getServerboundClass(int id) {
public PacketDefinition<?, ?> getClientboundDefinition(int id) {
return this.stateCodec.getClientboundDefinition(id);
}

public static CompoundTag loadNetworkCodec() {
try (InputStream inputStream = Objects.requireNonNull(MinecraftProtocol.class.getClassLoader().getResourceAsStream("networkCodec.nbt")) ;
DataInputStream stream = new DataInputStream(new GZIPInputStream(inputStream))) {
return (CompoundTag) NBTIO.readTag((DataInput) stream);
} catch (Exception e) {
throw new AssertionError("Unable to load network codec.", e);
}
}
}
Loading
Loading