diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java index a09132457..9740dbc6f 100644 --- a/src/main/java/net/minestom/server/MinecraftServer.java +++ b/src/main/java/net/minestom/server/MinecraftServer.java @@ -114,6 +114,7 @@ public final class MinecraftServer { private static int maxPacketSize = 30_000; // Network private static PacketListenerManager packetListenerManager; + private static PacketProcessor packetProcessor; private static NettyServer nettyServer; // In-Game Manager @@ -181,7 +182,7 @@ public final class MinecraftServer { connectionManager = new ConnectionManager(); // Networking - final PacketProcessor packetProcessor = new PacketProcessor(); + packetProcessor = new PacketProcessor(); packetListenerManager = new PacketListenerManager(); instanceManager = new InstanceManager(); @@ -434,6 +435,18 @@ public final class MinecraftServer { return connectionManager; } + /** + * Gets the object handling the client packets processing. + *
+ * Can be used if you want to convert a buffer to a client packet object. + * + * @return the packet processor + */ + public static PacketProcessor getPacketProcessor() { + checkInitStatus(packetProcessor); + return packetProcessor; + } + /** * Gets if the server is up and running. * diff --git a/src/main/java/net/minestom/server/network/PacketProcessor.java b/src/main/java/net/minestom/server/network/PacketProcessor.java index f5825e2d1..aedb33c58 100644 --- a/src/main/java/net/minestom/server/network/PacketProcessor.java +++ b/src/main/java/net/minestom/server/network/PacketProcessor.java @@ -23,6 +23,16 @@ import org.slf4j.LoggerFactory; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +/** + * Responsible for processing client packets. + *
+ * You can retrieve the different packet handlers per state (status/login/play) + * from the {@link net.minestom.server.network.packet.client.handler.ClientPacketsHandler} class. + *
+ * Packet handlers are cached here and can be retrieved with {@link #getStatusPacketsHandler()}, {@link #getLoginPacketsHandler()} + * and {@link #getPlayPacketsHandler()}. The one to use depend on the type of packet you need to retrieve (the packet id 0 does not have + * the same meaning as it is a login or play packet). + */ public final class PacketProcessor { private final static Logger LOGGER = LoggerFactory.getLogger(PacketProcessor.class); @@ -94,10 +104,22 @@ public final class PacketProcessor { return connectionPlayerConnectionMap.get(channel); } - public void removePlayerConnection(ChannelHandlerContext channel) { + public void removePlayerConnection(@NotNull ChannelHandlerContext channel) { connectionPlayerConnectionMap.remove(channel); } + public ClientStatusPacketsHandler getStatusPacketsHandler() { + return statusPacketsHandler; + } + + public ClientLoginPacketsHandler getLoginPacketsHandler() { + return loginPacketsHandler; + } + + public ClientPlayPacketsHandler getPlayPacketsHandler() { + return playPacketsHandler; + } + /** * Calls {@link Readable#read(BinaryReader)} and catch all the exceptions to be printed using the packet processor logger. * diff --git a/src/main/java/net/minestom/server/network/packet/client/handler/ClientPacketsHandler.java b/src/main/java/net/minestom/server/network/packet/client/handler/ClientPacketsHandler.java index 214f18487..a61b1b8cb 100644 --- a/src/main/java/net/minestom/server/network/packet/client/handler/ClientPacketsHandler.java +++ b/src/main/java/net/minestom/server/network/packet/client/handler/ClientPacketsHandler.java @@ -1,10 +1,19 @@ package net.minestom.server.network.packet.client.handler; import net.minestom.server.network.packet.client.ClientPacket; +import net.minestom.server.utils.binary.BinaryReader; import org.jetbrains.annotations.NotNull; import java.util.function.Supplier; +/** + * Contains registered packets and a way to instantiate them. + *
+ * Packets are register using {@link #register(int, ClientPacketSupplier)} + * (you can override a packet id even if not recommended and not officially supported) and retrieved with {@link #getPacketInstance(int)}. + *
+ * If you want to fill the packet from a buffer, consider using {@link ClientPacket#read(BinaryReader)} after getting the packet instance. + */ public class ClientPacketsHandler { // Max packet id