Skip to content

Commit 48a7873

Browse files
committed
change to using functional interface for clientbound packet handling
1 parent 68c495d commit 48a7873

File tree

2 files changed

+13
-31
lines changed

2 files changed

+13
-31
lines changed

src/main/java/net/hypixel/modapi/HypixelModAPI.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static HypixelModAPI getInstance() {
2828
}
2929

3030
private final PacketRegistry registry = new PacketRegistry();
31-
private final List<ClientboundPacketHandler> handlers = new CopyOnWriteArrayList<>();
31+
private final Map<Class<? extends ClientboundHypixelPacket>, Collection<ClientboundPacketHandler<?>>> handlers = new ConcurrentHashMap<>();
3232
private final Set<String> subscribedEvents = ConcurrentHashMap.newKeySet();
3333
private Set<String> lastSubscribedEvents = Collections.emptySet();
3434
private Predicate<HypixelPacket> packetSender = null;
@@ -71,20 +71,16 @@ private void registerEventPackets() {
7171
}
7272

7373
private void registerDefaultHandler() {
74-
registerHandler(new ClientboundPacketHandler() {
75-
@Override
76-
public void onHelloEvent(ClientboundHelloPacket packet) {
77-
sendRegisterPacket(true);
78-
}
79-
});
74+
registerHandler(ClientboundHelloPacket.class, p -> sendRegisterPacket(true));
8075
}
8176

8277
public PacketRegistry getRegistry() {
8378
return registry;
8479
}
8580

86-
public void registerHandler(ClientboundPacketHandler handler) {
87-
handlers.add(handler);
81+
public <T extends ClientboundHypixelPacket> void registerHandler(Class<T> packetClass, ClientboundPacketHandler<T> handler) {
82+
if (packetClass == null || handler == null) return;
83+
handlers.computeIfAbsent(packetClass, cls -> new CopyOnWriteArrayList<>()).add(handler);
8884
}
8985

9086
public void subscribeToEventPacket(Class<? extends EventPacket> packet) {
@@ -135,7 +131,10 @@ public void handle(String identifier, PacketSerializer serializer) {
135131
}
136132

137133
public void handle(ClientboundHypixelPacket packet) {
138-
for (ClientboundPacketHandler handler : handlers) {
134+
Collection<ClientboundPacketHandler<?>> typedHandlers = handlers.get(packet.getClass());
135+
// nothing registered for this packet.
136+
if (typedHandlers == null) return;
137+
for (ClientboundPacketHandler<?> handler : typedHandlers) {
139138
packet.handle(handler);
140139
}
141140
}
Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
11
package net.hypixel.modapi.handler;
22

3-
import net.hypixel.modapi.packet.impl.clientbound.ClientboundHelloPacket;
4-
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPartyInfoPacket;
5-
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPingPacket;
6-
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPlayerInfoPacket;
7-
import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket;
3+
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
84

9-
public interface ClientboundPacketHandler {
10-
11-
default void onHelloEvent(ClientboundHelloPacket packet) {
12-
}
13-
14-
default void onPingPacket(ClientboundPingPacket packet) {
15-
}
16-
17-
default void onPartyInfoPacket(ClientboundPartyInfoPacket packet) {
18-
}
19-
20-
default void onPlayerInfoPacket(ClientboundPlayerInfoPacket packet) {
21-
}
22-
23-
default void onLocationEvent(ClientboundLocationPacket packet) {
24-
}
5+
@FunctionalInterface
6+
public interface ClientboundPacketHandler<T extends ClientboundHypixelPacket> {
7+
void handle(T packet);
258
}

0 commit comments

Comments
 (0)