Skip to content

Commit b0f3658

Browse files
authored
improvement: Change to using functional interfaces for clientbound packet handling (#29)
1 parent 68c495d commit b0f3658

File tree

8 files changed

+16
-72
lines changed

8 files changed

+16
-72
lines changed

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

Lines changed: 12 additions & 11 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) {
@@ -134,9 +130,14 @@ public void handle(String identifier, PacketSerializer serializer) {
134130
handle(packet);
135131
}
136132

133+
@SuppressWarnings("unchecked")
137134
public void handle(ClientboundHypixelPacket packet) {
138-
for (ClientboundPacketHandler handler : handlers) {
139-
packet.handle(handler);
135+
Collection<ClientboundPacketHandler<?>> typedHandlers = handlers.get(packet.getClass());
136+
// nothing registered for this packet.
137+
if (typedHandlers == null) return;
138+
for (ClientboundPacketHandler<?> handler : typedHandlers) {
139+
// this cast is safe as we ensure its type when it is added to the handlers list in the first place.
140+
((ClientboundPacketHandler<ClientboundHypixelPacket>) handler).handle(packet);
140141
}
141142
}
142143

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
}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
package net.hypixel.modapi.packet;
22

3-
import net.hypixel.modapi.handler.ClientboundPacketHandler;
4-
53
public interface ClientboundHypixelPacket extends HypixelPacket {
6-
7-
void handle(ClientboundPacketHandler handler);
8-
94
}

src/main/java/net/hypixel/modapi/packet/impl/clientbound/ClientboundHelloPacket.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package net.hypixel.modapi.packet.impl.clientbound;
22

33
import net.hypixel.data.region.Environment;
4-
import net.hypixel.modapi.annotation.Experimental;
5-
import net.hypixel.modapi.handler.ClientboundPacketHandler;
64
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
75
import net.hypixel.modapi.serializer.PacketSerializer;
86

@@ -24,11 +22,6 @@ public ClientboundHelloPacket(PacketSerializer serializer) {
2422
serializer.discardRemaining();
2523
}
2624

27-
@Override
28-
public void handle(ClientboundPacketHandler handler) {
29-
handler.onHelloEvent(this);
30-
}
31-
3225
@Override
3326
public void write(PacketSerializer serializer) {
3427
serializer.writeVarInt(environment.getId());

src/main/java/net/hypixel/modapi/packet/impl/clientbound/ClientboundPartyInfoPacket.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package net.hypixel.modapi.packet.impl.clientbound;
22

3-
import net.hypixel.modapi.handler.ClientboundPacketHandler;
43
import net.hypixel.modapi.serializer.PacketSerializer;
54

65
import java.util.*;
@@ -84,11 +83,6 @@ protected int getLatestVersion() {
8483
return CURRENT_VERSION;
8584
}
8685

87-
@Override
88-
public void handle(ClientboundPacketHandler handler) {
89-
handler.onPartyInfoPacket(this);
90-
}
91-
9286
public boolean isInParty() {
9387
return inParty;
9488
}

src/main/java/net/hypixel/modapi/packet/impl/clientbound/ClientboundPingPacket.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package net.hypixel.modapi.packet.impl.clientbound;
22

3-
import net.hypixel.modapi.handler.ClientboundPacketHandler;
4-
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
5-
import net.hypixel.modapi.packet.impl.VersionedPacket;
63
import net.hypixel.modapi.serializer.PacketSerializer;
74

85
public class ClientboundPingPacket extends ClientboundVersionedPacket {
@@ -40,11 +37,6 @@ protected int getLatestVersion() {
4037
return CURRENT_VERSION;
4138
}
4239

43-
@Override
44-
public void handle(ClientboundPacketHandler handler) {
45-
handler.onPingPacket(this);
46-
}
47-
4840
public String getResponse() {
4941
return response;
5042
}

src/main/java/net/hypixel/modapi/packet/impl/clientbound/ClientboundPlayerInfoPacket.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import net.hypixel.data.rank.MonthlyPackageRank;
44
import net.hypixel.data.rank.PackageRank;
55
import net.hypixel.data.rank.PlayerRank;
6-
import net.hypixel.modapi.handler.ClientboundPacketHandler;
7-
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
8-
import net.hypixel.modapi.packet.impl.VersionedPacket;
96
import net.hypixel.modapi.serializer.PacketSerializer;
107
import org.jetbrains.annotations.Nullable;
118

@@ -59,11 +56,6 @@ protected int getLatestVersion() {
5956
return CURRENT_VERSION;
6057
}
6158

62-
@Override
63-
public void handle(ClientboundPacketHandler handler) {
64-
handler.onPlayerInfoPacket(this);
65-
}
66-
6759
public PlayerRank getPlayerRank() {
6860
return playerRank;
6961
}

src/main/java/net/hypixel/modapi/packet/impl/clientbound/event/ClientboundLocationPacket.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.hypixel.modapi.packet.impl.clientbound.event;
22

33
import net.hypixel.data.type.ServerType;
4-
import net.hypixel.modapi.handler.ClientboundPacketHandler;
54
import net.hypixel.modapi.packet.EventPacket;
65
import net.hypixel.modapi.packet.impl.clientbound.ClientboundVersionedPacket;
76
import net.hypixel.modapi.serializer.PacketSerializer;
@@ -64,11 +63,6 @@ protected int getLatestVersion() {
6463
return CURRENT_VERSION;
6564
}
6665

67-
@Override
68-
public void handle(ClientboundPacketHandler handler) {
69-
handler.onLocationEvent(this);
70-
}
71-
7266
public String getServerName() {
7367
return serverName;
7468
}

0 commit comments

Comments
 (0)