Skip to content

Commit 70be70d

Browse files
committed
improvement: add support for handling packets during configuration phase
1 parent 897edb2 commit 70be70d

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

src/main/java/net/hypixel/modapi/fabric/FabricModAPI.java

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.mojang.logging.LogUtils;
44
import net.fabricmc.api.ClientModInitializer;
5+
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
56
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
67
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
78
import net.hypixel.modapi.HypixelModAPI;
@@ -50,14 +51,20 @@ public static void reloadRegistrations() {
5051

5152
private static void registerPacketSender() {
5253
HypixelModAPI.getInstance().setPacketSender((packet) -> {
53-
if (MinecraftClient.getInstance().getNetworkHandler() == null) {
54-
// The client is not connected to a server, so we can't send the packet
55-
return false;
54+
ServerboundHypixelPayload hypixelPayload = new ServerboundHypixelPayload(packet);
55+
56+
if (MinecraftClient.getInstance().getNetworkHandler() != null) {
57+
ClientPlayNetworking.send(hypixelPayload);
58+
return true;
5659
}
5760

58-
ServerboundHypixelPayload payload = new ServerboundHypixelPayload(packet);
59-
ClientPlayNetworking.send(payload);
60-
return true;
61+
try {
62+
ClientConfigurationNetworking.send(hypixelPayload);
63+
return true;
64+
} catch (IllegalStateException ignored) {
65+
LOGGER.warn("Failed to send a packet as the client is not connected to a server '{}'", packet);
66+
return false;
67+
}
6168
});
6269
}
6370

@@ -68,30 +75,40 @@ private static void registerClientbound(String identifier) {
6875
PayloadTypeRegistry.playS2C().register(clientboundId, codec);
6976
PayloadTypeRegistry.configurationS2C().register(clientboundId, codec);
7077

71-
// Also register the global receiver for handling incoming packets
78+
// Also register the global receiver for handling incoming packets during PLAY and CONFIGURATION
7279
ClientPlayNetworking.registerGlobalReceiver(clientboundId, (payload, context) -> {
73-
if (!payload.isSuccess()) {
74-
LOGGER.warn("Received an error response for packet {}: {}", identifier, payload.getErrorReason());
75-
return;
76-
}
77-
78-
try {
79-
HypixelModAPI.getInstance().handle(payload.getPacket());
80-
} catch (Exception e) {
81-
LOGGER.error("An error occurred while handling packet {}", identifier, e);
82-
}
80+
LOGGER.debug("Received packet with identifier '{}', during PLAY", identifier);
81+
handleIncomingPayload(identifier, payload);
8382

84-
try {
85-
HypixelModAPICallback.EVENT.invoker().onPacketReceived(payload.getPacket());
86-
} catch (Exception e) {
87-
LOGGER.error("An error occurred while handling packet {}", identifier, e);
88-
}
83+
});
84+
ClientConfigurationNetworking.registerGlobalReceiver(clientboundId, (payload, context) -> {
85+
LOGGER.debug("Received packet with identifier '{}', during CONFIGURATION", identifier);
86+
handleIncomingPayload(identifier, payload);
8987
});
9088
} catch (IllegalArgumentException ignored) {
9189
// Ignored as this is fired when we reload the registrations and the packet is already registered
9290
}
9391
}
9492

93+
private static void handleIncomingPayload(String identifier, ClientboundHypixelPayload payload) {
94+
if (!payload.isSuccess()) {
95+
LOGGER.warn("Received an error response for packet {}: {}", identifier, payload.getErrorReason());
96+
return;
97+
}
98+
99+
try {
100+
HypixelModAPI.getInstance().handle(payload.getPacket());
101+
} catch (Exception e) {
102+
LOGGER.error("An error occurred while handling packet {}", identifier, e);
103+
}
104+
105+
try {
106+
HypixelModAPICallback.EVENT.invoker().onPacketReceived(payload.getPacket());
107+
} catch (Exception e) {
108+
LOGGER.error("An error occurred while handling packet {}", identifier, e);
109+
}
110+
}
111+
95112
private static void registerServerbound(String identifier) {
96113
try {
97114
CustomPayload.Id<ServerboundHypixelPayload> serverboundId = CustomPayload.id(identifier);

0 commit comments

Comments
 (0)