From 70be70d69f9797b4fd6c8f473f92514717dbad56 Mon Sep 17 00:00:00 2001 From: Connor Linfoot Date: Sat, 22 Jun 2024 21:51:36 +0100 Subject: [PATCH] improvement: add support for handling packets during configuration phase --- .../hypixel/modapi/fabric/FabricModAPI.java | 61 ++++++++++++------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/hypixel/modapi/fabric/FabricModAPI.java b/src/main/java/net/hypixel/modapi/fabric/FabricModAPI.java index 2837ba3..9d4fb89 100644 --- a/src/main/java/net/hypixel/modapi/fabric/FabricModAPI.java +++ b/src/main/java/net/hypixel/modapi/fabric/FabricModAPI.java @@ -2,6 +2,7 @@ import com.mojang.logging.LogUtils; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.hypixel.modapi.HypixelModAPI; @@ -50,14 +51,20 @@ public static void reloadRegistrations() { private static void registerPacketSender() { HypixelModAPI.getInstance().setPacketSender((packet) -> { - if (MinecraftClient.getInstance().getNetworkHandler() == null) { - // The client is not connected to a server, so we can't send the packet - return false; + ServerboundHypixelPayload hypixelPayload = new ServerboundHypixelPayload(packet); + + if (MinecraftClient.getInstance().getNetworkHandler() != null) { + ClientPlayNetworking.send(hypixelPayload); + return true; } - ServerboundHypixelPayload payload = new ServerboundHypixelPayload(packet); - ClientPlayNetworking.send(payload); - return true; + try { + ClientConfigurationNetworking.send(hypixelPayload); + return true; + } catch (IllegalStateException ignored) { + LOGGER.warn("Failed to send a packet as the client is not connected to a server '{}'", packet); + return false; + } }); } @@ -68,30 +75,40 @@ private static void registerClientbound(String identifier) { PayloadTypeRegistry.playS2C().register(clientboundId, codec); PayloadTypeRegistry.configurationS2C().register(clientboundId, codec); - // Also register the global receiver for handling incoming packets + // Also register the global receiver for handling incoming packets during PLAY and CONFIGURATION ClientPlayNetworking.registerGlobalReceiver(clientboundId, (payload, context) -> { - if (!payload.isSuccess()) { - LOGGER.warn("Received an error response for packet {}: {}", identifier, payload.getErrorReason()); - return; - } - - try { - HypixelModAPI.getInstance().handle(payload.getPacket()); - } catch (Exception e) { - LOGGER.error("An error occurred while handling packet {}", identifier, e); - } + LOGGER.debug("Received packet with identifier '{}', during PLAY", identifier); + handleIncomingPayload(identifier, payload); - try { - HypixelModAPICallback.EVENT.invoker().onPacketReceived(payload.getPacket()); - } catch (Exception e) { - LOGGER.error("An error occurred while handling packet {}", identifier, e); - } + }); + ClientConfigurationNetworking.registerGlobalReceiver(clientboundId, (payload, context) -> { + LOGGER.debug("Received packet with identifier '{}', during CONFIGURATION", identifier); + handleIncomingPayload(identifier, payload); }); } catch (IllegalArgumentException ignored) { // Ignored as this is fired when we reload the registrations and the packet is already registered } } + private static void handleIncomingPayload(String identifier, ClientboundHypixelPayload payload) { + if (!payload.isSuccess()) { + LOGGER.warn("Received an error response for packet {}: {}", identifier, payload.getErrorReason()); + return; + } + + try { + HypixelModAPI.getInstance().handle(payload.getPacket()); + } catch (Exception e) { + LOGGER.error("An error occurred while handling packet {}", identifier, e); + } + + try { + HypixelModAPICallback.EVENT.invoker().onPacketReceived(payload.getPacket()); + } catch (Exception e) { + LOGGER.error("An error occurred while handling packet {}", identifier, e); + } + } + private static void registerServerbound(String identifier) { try { CustomPayload.Id serverboundId = CustomPayload.id(identifier);