33import com .mojang .logging .LogUtils ;
44import net .fabricmc .api .ClientModInitializer ;
55import net .fabricmc .fabric .api .client .networking .v1 .ClientConfigurationNetworking ;
6+ import net .fabricmc .fabric .api .client .networking .v1 .ClientPlayConnectionEvents ;
67import net .fabricmc .fabric .api .client .networking .v1 .ClientPlayNetworking ;
78import net .fabricmc .fabric .api .networking .v1 .PayloadTypeRegistry ;
89import net .fabricmc .loader .api .FabricLoader ;
910import net .hypixel .modapi .HypixelModAPI ;
11+ import net .hypixel .modapi .HypixelModAPIImplementation ;
1012import net .hypixel .modapi .fabric .event .HypixelModAPICallback ;
1113import net .hypixel .modapi .fabric .event .HypixelModAPIErrorCallback ;
1214import net .hypixel .modapi .fabric .payload .ClientboundHypixelPayload ;
1315import net .hypixel .modapi .fabric .payload .ServerboundHypixelPayload ;
16+ import net .hypixel .modapi .packet .HypixelPacket ;
17+ import net .hypixel .modapi .packet .impl .clientbound .ClientboundHelloPacket ;
1418import net .hypixel .modapi .packet .impl .clientbound .event .ClientboundLocationPacket ;
1519import net .minecraft .client .MinecraftClient ;
1620import net .minecraft .network .PacketByteBuf ;
1721import net .minecraft .network .codec .PacketCodec ;
1822import net .minecraft .network .packet .CustomPayload ;
23+ import net .minecraft .test .GameTest ;
1924import net .minecraft .util .Identifier ;
25+ import org .jetbrains .annotations .ApiStatus ;
2026import org .slf4j .Logger ;
2127
22- public class FabricModAPI implements ClientModInitializer {
28+ public class FabricModAPI implements ClientModInitializer , HypixelModAPIImplementation {
2329 private static final Logger LOGGER = LogUtils .getLogger ();
2430 private static final boolean DEBUG_MODE = FabricLoader .getInstance ().isDevelopmentEnvironment () || Boolean .getBoolean ("net.hypixel.modapi.debug" );
2531
32+ private boolean onHypixel = false ;
33+
34+ @ GameTest
2635 @ Override
2736 public void onInitializeClient () {
28- reloadRegistrations ();
29- registerPacketSender ();
37+ HypixelModAPI .getInstance ().setModImplementation (this );
38+ }
39+
40+ @ Override
41+ public void onInit () {
42+ HypixelModAPI .getInstance ().createHandler (ClientboundHelloPacket .class , packet -> onHypixel = true );
43+ ClientPlayConnectionEvents .DISCONNECT .register ((handler , client ) -> onHypixel = false );
3044
3145 if (DEBUG_MODE ) {
3246 LOGGER .info ("Debug mode is enabled!" );
3347 registerDebug ();
3448 }
49+
50+ reloadRegistrations ();
51+ }
52+
53+ @ Override
54+ public boolean sendPacket (HypixelPacket packet ) {
55+ if (!isConnectedToHypixel ()) {
56+ return false ;
57+ }
58+
59+ ServerboundHypixelPayload hypixelPayload = new ServerboundHypixelPayload (packet );
60+
61+ if (MinecraftClient .getInstance ().getNetworkHandler () != null ) {
62+ ClientPlayNetworking .send (hypixelPayload );
63+ return true ;
64+ }
65+
66+ try {
67+ ClientConfigurationNetworking .send (hypixelPayload );
68+ return true ;
69+ } catch (IllegalStateException ignored ) {
70+ LOGGER .warn ("Failed to send a packet as the client is not connected to a server '{}'" , packet );
71+ return false ;
72+ }
73+ }
74+
75+ @ Override
76+ public boolean isConnectedToHypixel () {
77+ return onHypixel ;
3578 }
3679
3780 /**
3881 * Reloads the identifiers that are registered in the Hypixel Mod API and makes sure that the packets are registered.
3982 * <p>
4083 * This method is available for internal use by Hypixel to add new packets externally, and is not intended for use by other developers.
4184 */
85+ @ ApiStatus .Internal
4286 public static void reloadRegistrations () {
4387 for (String identifier : HypixelModAPI .getInstance ().getRegistry ().getClientboundIdentifiers ()) {
4488 try {
@@ -59,25 +103,6 @@ public static void reloadRegistrations() {
59103 }
60104 }
61105
62- private static void registerPacketSender () {
63- HypixelModAPI .getInstance ().setPacketSender ((packet ) -> {
64- ServerboundHypixelPayload hypixelPayload = new ServerboundHypixelPayload (packet );
65-
66- if (MinecraftClient .getInstance ().getNetworkHandler () != null ) {
67- ClientPlayNetworking .send (hypixelPayload );
68- return true ;
69- }
70-
71- try {
72- ClientConfigurationNetworking .send (hypixelPayload );
73- return true ;
74- } catch (IllegalStateException ignored ) {
75- LOGGER .warn ("Failed to send a packet as the client is not connected to a server '{}'" , packet );
76- return false ;
77- }
78- });
79- }
80-
81106 private static void registerClientbound (String identifier ) {
82107 try {
83108 CustomPayload .Id <ClientboundHypixelPayload > clientboundId = new CustomPayload .Id <>(Identifier .of (identifier ));
0 commit comments