44import net .hypixel .modapi .error .ModAPIException ;
55import net .hypixel .modapi .handler .ClientboundPacketHandler ;
66import net .hypixel .modapi .packet .ClientboundHypixelPacket ;
7+ import net .hypixel .modapi .packet .EventPacket ;
78import net .hypixel .modapi .packet .HypixelPacket ;
89import net .hypixel .modapi .packet .PacketRegistry ;
910import net .hypixel .modapi .packet .impl .clientbound .*;
10- import net .hypixel .modapi .packet .impl .serverbound .ServerboundLocationPacket ;
11- import net .hypixel .modapi .packet .impl .serverbound .ServerboundPartyInfoPacket ;
12- import net .hypixel .modapi .packet .impl .serverbound .ServerboundPingPacket ;
13- import net .hypixel .modapi .packet .impl .serverbound .ServerboundPlayerInfoPacket ;
11+ import net .hypixel .modapi .packet .impl .clientbound .ClientboundHelloPacket ;
12+ import net .hypixel .modapi .packet .impl .clientbound .event .ClientboundLocationPacket ;
13+ import net .hypixel .modapi .packet .impl .serverbound .*;
1414import net .hypixel .modapi .serializer .PacketSerializer ;
1515
16- import java .util .List ;
16+ import java .util .*;
17+ import java .util .concurrent .ConcurrentHashMap ;
1718import java .util .concurrent .CopyOnWriteArrayList ;
18- import java .util .function .Consumer ;
19+ import java .util .function .Predicate ;
1920
2021public class HypixelModAPI {
2122 private static final HypixelModAPI INSTANCE = new HypixelModAPI ();
@@ -26,25 +27,54 @@ public static HypixelModAPI getInstance() {
2627
2728 private final PacketRegistry registry = new PacketRegistry ();
2829 private final List <ClientboundPacketHandler > handlers = new CopyOnWriteArrayList <>();
29- private Consumer <HypixelPacket > packetSender = null ;
30+ private final Set <String > subscribedEvents = ConcurrentHashMap .newKeySet ();
31+ private Set <String > lastSubscribedEvents = Collections .emptySet ();
32+ private Predicate <HypixelPacket > packetSender = null ;
3033
3134 private HypixelModAPI () {
35+ registerHypixelPackets ();
36+ registerEventPackets ();
37+ registerDefaultHandler ();
38+ }
39+
40+ private void registerHypixelPackets () {
41+ registry .define ("hypixel:hello" )
42+ .clientbound (ClientboundHelloPacket .class , ClientboundHelloPacket ::new )
43+ .register ();
44+
3245 registry .define ("hypixel:ping" )
3346 .clientbound (ClientboundPingPacket .class , ClientboundPingPacket ::new )
3447 .serverbound (ServerboundPingPacket .class , ServerboundPingPacket ::new )
3548 .register ();
36- registry .define ("hypixel:location" )
37- .clientbound (ClientboundLocationPacket .class , ClientboundLocationPacket ::new )
38- .serverbound (ServerboundLocationPacket .class , ServerboundLocationPacket ::new )
39- .register ();
49+
4050 registry .define ("hypixel:party_info" )
4151 .clientbound (ClientboundPartyInfoPacket .class , ClientboundPartyInfoPacket ::new )
4252 .serverbound (ServerboundPartyInfoPacket .class , ServerboundPartyInfoPacket ::new )
4353 .register ();
54+
4455 registry .define ("hypixel:player_info" )
4556 .clientbound (ClientboundPlayerInfoPacket .class , ClientboundPlayerInfoPacket ::new )
4657 .serverbound (ServerboundPlayerInfoPacket .class , ServerboundPlayerInfoPacket ::new )
4758 .register ();
59+
60+ registry .define ("hypixel:register" )
61+ .serverbound (ServerboundRegisterPacket .class , ServerboundRegisterPacket ::new )
62+ .register ();
63+ }
64+
65+ private void registerEventPackets () {
66+ registry .define ("hyevent:location" )
67+ .clientBoundEvent (ClientboundLocationPacket .CURRENT_VERSION , ClientboundLocationPacket .class , ClientboundLocationPacket ::new )
68+ .register ();
69+ }
70+
71+ private void registerDefaultHandler () {
72+ registerHandler (new ClientboundPacketHandler () {
73+ @ Override
74+ public void onHelloEvent (ClientboundHelloPacket packet ) {
75+ sendRegisterPacket (true );
76+ }
77+ });
4878 }
4979
5080 public PacketRegistry getRegistry () {
@@ -55,6 +85,24 @@ public void registerHandler(ClientboundPacketHandler handler) {
5585 handlers .add (handler );
5686 }
5787
88+ public void subscribeToEventPacket (Class <? extends EventPacket > packet ) {
89+ if (subscribedEvents .add (getRegistry ().getIdentifier (packet ))) {
90+ sendRegisterPacket (false );
91+ }
92+ }
93+
94+ private void sendRegisterPacket (boolean alwaysSendIfNotEmpty ) {
95+ if (lastSubscribedEvents .equals (subscribedEvents ) && !(alwaysSendIfNotEmpty && !subscribedEvents .isEmpty ())) {
96+ return ;
97+ }
98+
99+ Set <String > lastSubscribedEvents = new HashSet <>(subscribedEvents );
100+ Map <String , Integer > versionsMap = getRegistry ().getEventVersions (lastSubscribedEvents );
101+ if (sendPacket (new ServerboundRegisterPacket (versionsMap ))) {
102+ this .lastSubscribedEvents = lastSubscribedEvents ;
103+ }
104+ }
105+
58106 public void handle (String identifier , PacketSerializer serializer ) {
59107 if (handlers .isEmpty ()) {
60108 return ;
@@ -85,17 +133,21 @@ public void handle(ClientboundHypixelPacket packet) {
85133 }
86134 }
87135
88- public void setPacketSender (Consumer <HypixelPacket > packetSender ) {
136+ public void setPacketSender (Predicate <HypixelPacket > packetSender ) {
89137 if (this .packetSender != null ) {
90138 throw new IllegalArgumentException ("Packet sender already set" );
91139 }
92140 this .packetSender = packetSender ;
93141 }
94142
95- public void sendPacket (HypixelPacket packet ) {
143+ /**
144+ * @return whether the packet was sent successfully
145+ */
146+ public boolean sendPacket (HypixelPacket packet ) {
96147 if (packetSender == null ) {
97148 throw new IllegalStateException ("Packet sender not set" );
98149 }
99- packetSender .accept (packet );
150+
151+ return packetSender .test (packet );
100152 }
101153}
0 commit comments