1616import net .hypixel .modapi .packet .impl .serverbound .ServerboundRegisterPacket ;
1717import net .hypixel .modapi .serializer .PacketSerializer ;
1818import org .jetbrains .annotations .ApiStatus ;
19+ import org .jetbrains .annotations .Nullable ;
1920
2021import java .util .*;
2122import java .util .concurrent .ConcurrentHashMap ;
2223import java .util .concurrent .CopyOnWriteArrayList ;
23- import java .util .function .Predicate ;
2424
2525public class HypixelModAPI {
2626 private static final HypixelModAPI INSTANCE = new HypixelModAPI ();
@@ -33,12 +33,12 @@ public static HypixelModAPI getInstance() {
3333 private final Map <String , Collection <RegisteredHandlerImpl <?>>> handlers = new ConcurrentHashMap <>();
3434 private final Set <String > subscribedEvents = ConcurrentHashMap .newKeySet ();
3535 private Set <String > lastSubscribedEvents = Collections .emptySet ();
36- private Predicate <HypixelPacket > packetSender = null ;
36+ @ Nullable
37+ private HypixelModAPIImplementation modImplementation ;
3738
3839 private HypixelModAPI () {
3940 registerHypixelPackets ();
4041 registerEventPackets ();
41- registerDefaultHandler ();
4242 }
4343
4444 private void registerHypixelPackets () {
@@ -77,8 +77,8 @@ private void registerDefaultHandler() {
7777 }
7878
7979 private void sendRegisterPacket (boolean alwaysSendIfNotEmpty ) {
80- if (packetSender == null ) {
81- // Allow registering events before the mod has fully initialized
80+ if (modImplementation == null || ! modImplementation . isConnectedToHypixel () ) {
81+ // Allow registering events when not connected to Hypixel
8282 return ;
8383 }
8484
@@ -144,11 +144,18 @@ public void handleError(String identifier, ErrorReason reason) {
144144 }
145145
146146 @ ApiStatus .Internal
147- public void setPacketSender (Predicate <HypixelPacket > packetSender ) {
148- if (this .packetSender != null ) {
149- throw new IllegalArgumentException ("Packet sender already set" );
147+ public void setModImplementation (HypixelModAPIImplementation modImplementation ) {
148+ if (this .modImplementation != null ) {
149+ throw new IllegalStateException ("Mod implementation already set" );
150+ }
151+
152+ if (modImplementation == null ) {
153+ throw new NullPointerException ("modImplementation cannot be null" );
150154 }
151- this .packetSender = packetSender ;
155+
156+ this .modImplementation = modImplementation ;
157+ this .modImplementation .onInit ();
158+ registerDefaultHandler ();
152159 }
153160
154161 /**
@@ -184,11 +191,11 @@ public void subscribeToEventPacket(Class<? extends EventPacket> packet) {
184191 * @return whether the packet was sent successfully
185192 */
186193 public boolean sendPacket (HypixelPacket packet ) {
187- if (packetSender == null ) {
188- throw new IllegalStateException ("Packet sender not set" );
194+ if (modImplementation == null ) {
195+ throw new IllegalStateException ("Mod implementation not set" );
189196 }
190197
191- return packetSender . test (packet );
198+ return modImplementation . sendPacket (packet );
192199 }
193200
194201 private static class RegisteredHandlerImpl <T extends ClientboundHypixelPacket > implements RegisteredHandler <T > {
0 commit comments