forked from HypixelDev/ModAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHypixelModAPI.java
More file actions
160 lines (132 loc) · 6.02 KB
/
Copy pathHypixelModAPI.java
File metadata and controls
160 lines (132 loc) · 6.02 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package net.hypixel.modapi;
import net.hypixel.modapi.error.ErrorReason;
import net.hypixel.modapi.error.ModAPIException;
import net.hypixel.modapi.handler.ClientboundPacketHandler;
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
import net.hypixel.modapi.packet.EventPacket;
import net.hypixel.modapi.packet.HypixelPacket;
import net.hypixel.modapi.packet.PacketRegistry;
import net.hypixel.modapi.packet.impl.clientbound.*;
import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket;
import net.hypixel.modapi.packet.impl.serverbound.ServerboundPartyInfoPacket;
import net.hypixel.modapi.packet.impl.serverbound.ServerboundPingPacket;
import net.hypixel.modapi.packet.impl.serverbound.ServerboundPlayerInfoPacket;
import net.hypixel.modapi.packet.impl.serverbound.ServerboundRegisterPacket;
import net.hypixel.modapi.serializer.PacketSerializer;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Predicate;
public class HypixelModAPI {
private static final HypixelModAPI INSTANCE = new HypixelModAPI();
public static HypixelModAPI getInstance() {
return INSTANCE;
}
private final PacketRegistry registry = new PacketRegistry();
private final List<ClientboundPacketHandler> handlers = new CopyOnWriteArrayList<>();
private final Set<String> subscribedEvents = ConcurrentHashMap.newKeySet();
private Set<String> lastSubscribedEvents = Collections.emptySet();
private Predicate<HypixelPacket> packetSender = null;
private HypixelModAPI() {
registerHypixelPackets();
registerEventPackets();
registerDefaultHandler();
}
private void registerHypixelPackets() {
registry.define("hypixel:hello")
.clientbound(ClientboundHelloPacket.class, ClientboundHelloPacket::new)
.register();
registry.define("hypixel:ping")
.clientbound(ClientboundPingPacket.class, ClientboundPingPacket::new)
.serverbound(ServerboundPingPacket.class, ServerboundPingPacket::new)
.register();
registry.define("hypixel:party_info")
.clientbound(ClientboundPartyInfoPacket.class, ClientboundPartyInfoPacket::new)
.serverbound(ServerboundPartyInfoPacket.class, ServerboundPartyInfoPacket::new)
.register();
registry.define("hypixel:player_info")
.clientbound(ClientboundPlayerInfoPacket.class, ClientboundPlayerInfoPacket::new)
.serverbound(ServerboundPlayerInfoPacket.class, ServerboundPlayerInfoPacket::new)
.register();
registry.define("hypixel:register")
.serverbound(ServerboundRegisterPacket.class, ServerboundRegisterPacket::new)
.register();
}
private void registerEventPackets() {
registry.define("hyevent:location")
.clientBoundEvent(ClientboundLocationPacket.CURRENT_VERSION, ClientboundLocationPacket.class, ClientboundLocationPacket::new)
.register();
}
private void registerDefaultHandler() {
registerHandler(new ClientboundPacketHandler() {
@Override
public void onHelloEvent(ClientboundHelloPacket packet) {
sendRegisterPacket(true);
}
});
}
public PacketRegistry getRegistry() {
return registry;
}
public void registerHandler(ClientboundPacketHandler handler) {
handlers.add(handler);
}
public void subscribeToEventPacket(Class<? extends EventPacket> packet) {
if (subscribedEvents.add(getRegistry().getIdentifier(packet))) {
sendRegisterPacket(false);
}
}
private void sendRegisterPacket(boolean alwaysSendIfNotEmpty) {
if (packetSender == null) {
// Allow registering events before the mod has fully initialized
return;
}
if (lastSubscribedEvents.equals(subscribedEvents) && !(alwaysSendIfNotEmpty && !subscribedEvents.isEmpty())) {
return;
}
Set<String> lastSubscribedEvents = new HashSet<>(subscribedEvents);
Map<String, Integer> versionsMap = getRegistry().getEventVersions(lastSubscribedEvents);
if (sendPacket(new ServerboundRegisterPacket(versionsMap))) {
this.lastSubscribedEvents = lastSubscribedEvents;
}
}
public void handle(String identifier, PacketSerializer serializer) {
if (handlers.isEmpty()) {
return;
}
if (!registry.isRegistered(identifier)) {
return;
}
// All responses contain a boolean of if the response is a success, if not then a further var int is included to identify the error
if (!serializer.readBoolean()) {
ErrorReason reason = ErrorReason.getById(serializer.readVarInt());
throw new ModAPIException(identifier, reason);
}
ClientboundHypixelPacket packet = registry.createClientboundPacket(identifier, serializer);
if (packet instanceof ClientboundVersionedPacket && !((ClientboundVersionedPacket) packet).isExpectedVersion()) {
// Ignore packets that don't match our expected version, these could be received due to other mods requesting them
return;
}
handle(packet);
}
public void handle(ClientboundHypixelPacket packet) {
for (ClientboundPacketHandler handler : handlers) {
packet.handle(handler);
}
}
public void setPacketSender(Predicate<HypixelPacket> packetSender) {
if (this.packetSender != null) {
throw new IllegalArgumentException("Packet sender already set");
}
this.packetSender = packetSender;
}
/**
* @return whether the packet was sent successfully
*/
public boolean sendPacket(HypixelPacket packet) {
if (packetSender == null) {
throw new IllegalStateException("Packet sender not set");
}
return packetSender.test(packet);
}
}