-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathServerboundRegisterPacket.java
More file actions
76 lines (61 loc) · 2.6 KB
/
ServerboundRegisterPacket.java
File metadata and controls
76 lines (61 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package net.hypixel.modapi.packet.impl.serverbound;
import net.hypixel.modapi.packet.PacketRegistry;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Notifies the remote server what versions of event packets we want to receive.
* <p>
* You should not use this packet manually, instead, use {@link net.hypixel.modapi.HypixelModAPI#subscribeToEventPacket(Class)} to subscribe to event packets.
*/
@ApiStatus.Internal
public class ServerboundRegisterPacket extends ServerboundVersionedPacket {
private static final int MAX_IDENTIFIER_LENGTH = 20;
private static final int MAX_IDENTIFIERS = 5;
private static final int CURRENT_VERSION = 1;
private Map<String, Integer> subscribedEvents;
public ServerboundRegisterPacket(PacketRegistry registry, Set<String> subscribedEventIdentifiers) {
super(CURRENT_VERSION);
this.subscribedEvents = registry.getEventVersions(subscribedEventIdentifiers);
if (subscribedEvents.size() > MAX_IDENTIFIERS) {
throw new IllegalArgumentException("subscribedEvents cannot contain more than " + MAX_IDENTIFIERS + " identifiers");
}
}
public ServerboundRegisterPacket(PacketSerializer serializer) {
super(serializer);
}
@Override
protected boolean read(PacketSerializer serializer) {
super.read(serializer);
int size = serializer.readVarInt();
if (size > MAX_IDENTIFIERS) {
throw new IllegalArgumentException("subscribedEvents cannot contain more than " + MAX_IDENTIFIERS + " identifiers");
}
this.subscribedEvents = new HashMap<>(size);
for (int i = 0; i < size; i++) {
subscribedEvents.put(serializer.readString(MAX_IDENTIFIER_LENGTH), serializer.readVarInt());
}
return true;
}
@Override
public void write(PacketSerializer serializer) {
super.write(serializer);
serializer.writeVarInt(subscribedEvents.size());
for (Map.Entry<String, Integer> entry : subscribedEvents.entrySet()) {
serializer.writeString(entry.getKey(), MAX_IDENTIFIER_LENGTH);
serializer.writeVarInt(entry.getValue());
}
}
public Map<String, Integer> getSubscribedEvents() {
return Collections.unmodifiableMap(subscribedEvents);
}
@Override
public String toString() {
return "ServerboundRegisterPacket{" +
"subscribedEvents=" + subscribedEvents +
"} " + super.toString();
}
}