4
4
import net .hypixel .modapi .error .ModAPIException ;
5
5
import net .hypixel .modapi .handler .ClientboundPacketHandler ;
6
6
import net .hypixel .modapi .packet .ClientboundHypixelPacket ;
7
+ import net .hypixel .modapi .packet .EventPacket ;
7
8
import net .hypixel .modapi .packet .HypixelPacket ;
8
9
import net .hypixel .modapi .packet .PacketRegistry ;
9
10
import 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 .*;
14
14
import net .hypixel .modapi .serializer .PacketSerializer ;
15
15
16
- import java .util .List ;
16
+ import java .util .*;
17
+ import java .util .concurrent .ConcurrentHashMap ;
17
18
import java .util .concurrent .CopyOnWriteArrayList ;
18
- import java .util .function .Consumer ;
19
+ import java .util .function .Predicate ;
19
20
20
21
public class HypixelModAPI {
21
22
private static final HypixelModAPI INSTANCE = new HypixelModAPI ();
@@ -26,25 +27,54 @@ public static HypixelModAPI getInstance() {
26
27
27
28
private final PacketRegistry registry = new PacketRegistry ();
28
29
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 ;
30
33
31
34
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
+
32
45
registry .define ("hypixel:ping" )
33
46
.clientbound (ClientboundPingPacket .class , ClientboundPingPacket ::new )
34
47
.serverbound (ServerboundPingPacket .class , ServerboundPingPacket ::new )
35
48
.register ();
36
- registry .define ("hypixel:location" )
37
- .clientbound (ClientboundLocationPacket .class , ClientboundLocationPacket ::new )
38
- .serverbound (ServerboundLocationPacket .class , ServerboundLocationPacket ::new )
39
- .register ();
49
+
40
50
registry .define ("hypixel:party_info" )
41
51
.clientbound (ClientboundPartyInfoPacket .class , ClientboundPartyInfoPacket ::new )
42
52
.serverbound (ServerboundPartyInfoPacket .class , ServerboundPartyInfoPacket ::new )
43
53
.register ();
54
+
44
55
registry .define ("hypixel:player_info" )
45
56
.clientbound (ClientboundPlayerInfoPacket .class , ClientboundPlayerInfoPacket ::new )
46
57
.serverbound (ServerboundPlayerInfoPacket .class , ServerboundPlayerInfoPacket ::new )
47
58
.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
+ });
48
78
}
49
79
50
80
public PacketRegistry getRegistry () {
@@ -55,6 +85,24 @@ public void registerHandler(ClientboundPacketHandler handler) {
55
85
handlers .add (handler );
56
86
}
57
87
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
+
58
106
public void handle (String identifier , PacketSerializer serializer ) {
59
107
if (handlers .isEmpty ()) {
60
108
return ;
@@ -85,17 +133,21 @@ public void handle(ClientboundHypixelPacket packet) {
85
133
}
86
134
}
87
135
88
- public void setPacketSender (Consumer <HypixelPacket > packetSender ) {
136
+ public void setPacketSender (Predicate <HypixelPacket > packetSender ) {
89
137
if (this .packetSender != null ) {
90
138
throw new IllegalArgumentException ("Packet sender already set" );
91
139
}
92
140
this .packetSender = packetSender ;
93
141
}
94
142
95
- public void sendPacket (HypixelPacket packet ) {
143
+ /**
144
+ * @return whether the packet was sent successfully
145
+ */
146
+ public boolean sendPacket (HypixelPacket packet ) {
96
147
if (packetSender == null ) {
97
148
throw new IllegalStateException ("Packet sender not set" );
98
149
}
99
- packetSender .accept (packet );
150
+
151
+ return packetSender .test (packet );
100
152
}
101
153
}
0 commit comments