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
75 lines (62 loc) · 3.15 KB
/
Copy pathHypixelModAPI.java
File metadata and controls
75 lines (62 loc) · 3.15 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
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.PacketRegistry;
import net.hypixel.modapi.packet.impl.clientbound.ClientboundLocationPacket;
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPartyInfoPacket;
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPingPacket;
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPlayerInfoPacket;
import net.hypixel.modapi.packet.impl.serverbound.ServerboundLocationPacket;
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.serializer.PacketSerializer;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
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 HypixelModAPI() {
registry.registerPacketType("hypixel:ping",
ClientboundPingPacket.class, ClientboundPingPacket::new,
ServerboundPingPacket.class, ServerboundPingPacket::new);
registry.registerPacketType("hypixel:location",
ClientboundLocationPacket.class, ClientboundLocationPacket::new,
ServerboundLocationPacket.class, ServerboundLocationPacket::new);
registry.registerPacketType("hypixel:party_info",
ClientboundPartyInfoPacket.class, ClientboundPartyInfoPacket::new,
ServerboundPartyInfoPacket.class, ServerboundPartyInfoPacket::new);
registry.registerPacketType("hypixel:player_info",
ClientboundPlayerInfoPacket.class, ClientboundPlayerInfoPacket::new,
ServerboundPlayerInfoPacket.class, ServerboundPlayerInfoPacket::new);
}
public PacketRegistry getRegistry() {
return registry;
}
public void registerHandler(ClientboundPacketHandler handler) {
handlers.add(handler);
}
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);
for (ClientboundPacketHandler handler : handlers) {
packet.handle(handler);
}
}
}