3
3
import com .mojang .logging .LogUtils ;
4
4
import net .fabricmc .api .ClientModInitializer ;
5
5
import net .fabricmc .fabric .api .client .networking .v1 .ClientConfigurationNetworking ;
6
+ import net .fabricmc .fabric .api .client .networking .v1 .ClientPlayConnectionEvents ;
6
7
import net .fabricmc .fabric .api .client .networking .v1 .ClientPlayNetworking ;
7
8
import net .fabricmc .fabric .api .networking .v1 .PayloadTypeRegistry ;
8
9
import net .fabricmc .loader .api .FabricLoader ;
9
10
import net .hypixel .modapi .HypixelModAPI ;
11
+ import net .hypixel .modapi .HypixelModAPIImplementation ;
10
12
import net .hypixel .modapi .fabric .event .HypixelModAPICallback ;
11
13
import net .hypixel .modapi .fabric .event .HypixelModAPIErrorCallback ;
12
14
import net .hypixel .modapi .fabric .payload .ClientboundHypixelPayload ;
13
15
import net .hypixel .modapi .fabric .payload .ServerboundHypixelPayload ;
16
+ import net .hypixel .modapi .packet .HypixelPacket ;
17
+ import net .hypixel .modapi .packet .impl .clientbound .ClientboundHelloPacket ;
14
18
import net .hypixel .modapi .packet .impl .clientbound .event .ClientboundLocationPacket ;
15
19
import net .minecraft .client .MinecraftClient ;
16
20
import net .minecraft .network .PacketByteBuf ;
17
21
import net .minecraft .network .codec .PacketCodec ;
18
22
import net .minecraft .network .packet .CustomPayload ;
23
+ import net .minecraft .test .GameTest ;
19
24
import net .minecraft .util .Identifier ;
25
+ import org .jetbrains .annotations .ApiStatus ;
20
26
import org .slf4j .Logger ;
21
27
22
- public class FabricModAPI implements ClientModInitializer {
28
+ public class FabricModAPI implements ClientModInitializer , HypixelModAPIImplementation {
23
29
private static final Logger LOGGER = LogUtils .getLogger ();
24
30
private static final boolean DEBUG_MODE = FabricLoader .getInstance ().isDevelopmentEnvironment () || Boolean .getBoolean ("net.hypixel.modapi.debug" );
25
31
32
+ private boolean onHypixel = false ;
33
+
34
+ @ GameTest
26
35
@ Override
27
36
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 );
30
44
31
45
if (DEBUG_MODE ) {
32
46
LOGGER .info ("Debug mode is enabled!" );
33
47
registerDebug ();
34
48
}
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 ;
35
78
}
36
79
37
80
/**
38
81
* Reloads the identifiers that are registered in the Hypixel Mod API and makes sure that the packets are registered.
39
82
* <p>
40
83
* This method is available for internal use by Hypixel to add new packets externally, and is not intended for use by other developers.
41
84
*/
85
+ @ ApiStatus .Internal
42
86
public static void reloadRegistrations () {
43
87
for (String identifier : HypixelModAPI .getInstance ().getRegistry ().getClientboundIdentifiers ()) {
44
88
try {
@@ -59,25 +103,6 @@ public static void reloadRegistrations() {
59
103
}
60
104
}
61
105
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
-
81
106
private static void registerClientbound (String identifier ) {
82
107
try {
83
108
CustomPayload .Id <ClientboundHypixelPayload > clientboundId = new CustomPayload .Id <>(Identifier .of (identifier ));
0 commit comments