55import net .fabricmc .fabric .api .client .networking .v1 .ClientConfigurationNetworking ;
66import net .fabricmc .fabric .api .client .networking .v1 .ClientPlayNetworking ;
77import net .fabricmc .fabric .api .networking .v1 .PayloadTypeRegistry ;
8+ import net .fabricmc .loader .api .FabricLoader ;
89import net .hypixel .modapi .HypixelModAPI ;
910import net .hypixel .modapi .fabric .event .HypixelModAPICallback ;
11+ import net .hypixel .modapi .fabric .event .HypixelModAPIErrorCallback ;
1012import net .hypixel .modapi .fabric .payload .ClientboundHypixelPayload ;
1113import net .hypixel .modapi .fabric .payload .ServerboundHypixelPayload ;
14+ import net .hypixel .modapi .packet .impl .clientbound .event .ClientboundLocationPacket ;
1215import net .minecraft .client .MinecraftClient ;
1316import net .minecraft .network .PacketByteBuf ;
1417import net .minecraft .network .codec .PacketCodec ;
1720
1821public class FabricModAPI implements ClientModInitializer {
1922 private static final Logger LOGGER = LogUtils .getLogger ();
23+ private static final boolean DEBUG_MODE = FabricLoader .getInstance ().isDevelopmentEnvironment () || Boolean .getBoolean ("net.hypixel.modapi.debug" );
2024
2125 @ Override
2226 public void onInitializeClient () {
2327 reloadRegistrations ();
2428 registerPacketSender ();
29+
30+ if (DEBUG_MODE ) {
31+ LOGGER .info ("Debug mode is enabled!" );
32+ registerDebug ();
33+ }
2534 }
2635
2736 /**
@@ -93,6 +102,17 @@ private static void registerClientbound(String identifier) {
93102 private static void handleIncomingPayload (String identifier , ClientboundHypixelPayload payload ) {
94103 if (!payload .isSuccess ()) {
95104 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+ }
96116 return ;
97117 }
98118
@@ -119,4 +139,15 @@ private static void registerServerbound(String identifier) {
119139 // Ignored as this is fired when we reload the registrations and the packet is already registered
120140 }
121141 }
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+ }
122153}
0 commit comments