5
5
import net .fabricmc .fabric .api .client .networking .v1 .ClientConfigurationNetworking ;
6
6
import net .fabricmc .fabric .api .client .networking .v1 .ClientPlayNetworking ;
7
7
import net .fabricmc .fabric .api .networking .v1 .PayloadTypeRegistry ;
8
+ import net .fabricmc .loader .api .FabricLoader ;
8
9
import net .hypixel .modapi .HypixelModAPI ;
9
10
import net .hypixel .modapi .fabric .event .HypixelModAPICallback ;
11
+ import net .hypixel .modapi .fabric .event .HypixelModAPIErrorCallback ;
10
12
import net .hypixel .modapi .fabric .payload .ClientboundHypixelPayload ;
11
13
import net .hypixel .modapi .fabric .payload .ServerboundHypixelPayload ;
14
+ import net .hypixel .modapi .packet .impl .clientbound .event .ClientboundLocationPacket ;
12
15
import net .minecraft .client .MinecraftClient ;
13
16
import net .minecraft .network .PacketByteBuf ;
14
17
import net .minecraft .network .codec .PacketCodec ;
17
20
18
21
public class FabricModAPI implements ClientModInitializer {
19
22
private static final Logger LOGGER = LogUtils .getLogger ();
23
+ private static final boolean DEBUG_MODE = FabricLoader .getInstance ().isDevelopmentEnvironment () || Boolean .getBoolean ("net.hypixel.modapi.debug" );
20
24
21
25
@ Override
22
26
public void onInitializeClient () {
23
27
reloadRegistrations ();
24
28
registerPacketSender ();
29
+
30
+ if (DEBUG_MODE ) {
31
+ LOGGER .info ("Debug mode is enabled!" );
32
+ registerDebug ();
33
+ }
25
34
}
26
35
27
36
/**
@@ -93,6 +102,17 @@ private static void registerClientbound(String identifier) {
93
102
private static void handleIncomingPayload (String identifier , ClientboundHypixelPayload payload ) {
94
103
if (!payload .isSuccess ()) {
95
104
LOGGER .warn ("Received an error response for packet {}: {}" , identifier , payload .getErrorReason ());
105
+ try {
106
+ HypixelModAPI .getInstance ().handleError (identifier , payload .getErrorReason ());
107
+ } catch (Exception e ) {
108
+ LOGGER .error ("An error occurred while handling error response for packet {}" , identifier , e );
109
+ }
110
+
111
+ try {
112
+ HypixelModAPIErrorCallback .EVENT .invoker ().onError (identifier , payload .getErrorReason ());
113
+ } catch (Exception e ) {
114
+ LOGGER .error ("An error occurred while handling error response for packet {}" , identifier , e );
115
+ }
96
116
return ;
97
117
}
98
118
@@ -119,4 +139,15 @@ private static void registerServerbound(String identifier) {
119
139
// Ignored as this is fired when we reload the registrations and the packet is already registered
120
140
}
121
141
}
142
+
143
+ private static void registerDebug () {
144
+ // Register events
145
+ HypixelModAPI .getInstance ().subscribeToEventPacket (ClientboundLocationPacket .class );
146
+
147
+ HypixelModAPI .getInstance ().createHandler (ClientboundLocationPacket .class , packet -> LOGGER .info ("Received location packet {}" , packet ))
148
+ .onError (error -> LOGGER .error ("Received error response for location packet: {}" , error ));
149
+
150
+ HypixelModAPICallback .EVENT .register (packet -> LOGGER .info ("Received packet {}" , packet ));
151
+ HypixelModAPIErrorCallback .EVENT .register ((identifier , error ) -> LOGGER .error ("Received error response for packet {}: {}" , identifier , error ));
152
+ }
122
153
}
0 commit comments