2
2
3
3
import com .mojang .logging .LogUtils ;
4
4
import net .fabricmc .api .ClientModInitializer ;
5
+ import net .fabricmc .fabric .api .client .networking .v1 .ClientConfigurationNetworking ;
5
6
import net .fabricmc .fabric .api .client .networking .v1 .ClientPlayNetworking ;
6
7
import net .fabricmc .fabric .api .networking .v1 .PayloadTypeRegistry ;
7
8
import net .hypixel .modapi .HypixelModAPI ;
@@ -50,14 +51,20 @@ public static void reloadRegistrations() {
50
51
51
52
private static void registerPacketSender () {
52
53
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 ;
56
59
}
57
60
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
+ }
61
68
});
62
69
}
63
70
@@ -68,30 +75,40 @@ private static void registerClientbound(String identifier) {
68
75
PayloadTypeRegistry .playS2C ().register (clientboundId , codec );
69
76
PayloadTypeRegistry .configurationS2C ().register (clientboundId , codec );
70
77
71
- // Also register the global receiver for handling incoming packets
78
+ // Also register the global receiver for handling incoming packets during PLAY and CONFIGURATION
72
79
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 );
83
82
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 );
89
87
});
90
88
} catch (IllegalArgumentException ignored ) {
91
89
// Ignored as this is fired when we reload the registrations and the packet is already registered
92
90
}
93
91
}
94
92
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
+
95
112
private static void registerServerbound (String identifier ) {
96
113
try {
97
114
CustomPayload .Id <ServerboundHypixelPayload > serverboundId = CustomPayload .id (identifier );
0 commit comments