Skip to content

Commit 5e13d22

Browse files
Use a Packet Registry instead of HypixelPacketType (#11)
1 parent c896b39 commit 5e13d22

15 files changed

+150
-110
lines changed

pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66

77
<groupId>net.hypixel</groupId>
88
<artifactId>mod-api</artifactId>
9-
<version>0.2.1</version>
9+
<version>dev-SNAPSHOT</version>
1010

1111
<properties>
1212
<maven.compiler.source>8</maven.compiler.source>
1313
<maven.compiler.target>8</maven.compiler.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
</properties>
1616

17+
<repositories>
18+
<repository>
19+
<id>Hypixel</id>
20+
<url>https://repo.hypixel.net/repository/Hypixel/</url>
21+
</repository>
22+
</repositories>
23+
1724
<distributionManagement>
1825
<repository>
1926
<id>Hypixel</id>
@@ -39,6 +46,12 @@
3946
<artifactId>hypixel-data</artifactId>
4047
<version>0.1.2</version>
4148
</dependency>
49+
<dependency>
50+
<groupId>org.junit.jupiter</groupId>
51+
<artifactId>junit-jupiter-engine</artifactId>
52+
<version>5.9.2</version>
53+
<scope>test</scope>
54+
</dependency>
4255
</dependencies>
4356

4457
<build>
@@ -66,6 +79,11 @@
6679
</execution>
6780
</executions>
6881
</plugin>
82+
<plugin>
83+
<groupId>org.apache.maven.plugins</groupId>
84+
<artifactId>maven-surefire-plugin</artifactId>
85+
<version>2.22.2</version>
86+
</plugin>
6987
</plugins>
7088
</build>
7189

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
import net.hypixel.modapi.error.ModAPIException;
55
import net.hypixel.modapi.handler.ClientboundPacketHandler;
66
import net.hypixel.modapi.packet.HypixelPacket;
7-
import net.hypixel.modapi.packet.HypixelPacketType;
7+
import net.hypixel.modapi.packet.PacketRegistry;
8+
import net.hypixel.modapi.packet.impl.clientbound.ClientboundLocationPacket;
9+
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPartyInfoPacket;
10+
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPingPacket;
11+
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPlayerInfoPacket;
12+
import net.hypixel.modapi.packet.impl.serverbound.ServerboundLocationPacket;
13+
import net.hypixel.modapi.packet.impl.serverbound.ServerboundPartyInfoPacket;
14+
import net.hypixel.modapi.packet.impl.serverbound.ServerboundPingPacket;
15+
import net.hypixel.modapi.packet.impl.serverbound.ServerboundPlayerInfoPacket;
816
import net.hypixel.modapi.serializer.PacketSerializer;
917

1018
import java.util.List;
@@ -17,33 +25,49 @@ public static HypixelModAPI getInstance() {
1725
return INSTANCE;
1826
}
1927

20-
private final List<ClientboundPacketHandler> packetHandlers = new CopyOnWriteArrayList<>();
28+
private final PacketRegistry registry = new PacketRegistry();
29+
private final List<ClientboundPacketHandler> handlers = new CopyOnWriteArrayList<>();
2130

2231
private HypixelModAPI() {
32+
registry.registerPacketType("hypixel:ping",
33+
ClientboundPingPacket.class, ClientboundPingPacket::new,
34+
ServerboundPingPacket.class, ServerboundPingPacket::new);
35+
registry.registerPacketType("hypixel:location",
36+
ClientboundLocationPacket.class, ClientboundLocationPacket::new,
37+
ServerboundLocationPacket.class, ServerboundLocationPacket::new);
38+
registry.registerPacketType("hypixel:party_info",
39+
ClientboundPartyInfoPacket.class, ClientboundPartyInfoPacket::new,
40+
ServerboundPartyInfoPacket.class, ServerboundPartyInfoPacket::new);
41+
registry.registerPacketType("hypixel:player_info",
42+
ClientboundPlayerInfoPacket.class, ClientboundPlayerInfoPacket::new,
43+
ServerboundPlayerInfoPacket.class, ServerboundPlayerInfoPacket::new);
44+
}
45+
46+
public PacketRegistry getRegistry() {
47+
return registry;
2348
}
2449

2550
public void registerHandler(ClientboundPacketHandler handler) {
26-
packetHandlers.add(handler);
51+
handlers.add(handler);
2752
}
2853

2954
public void handle(String identifier, PacketSerializer serializer) {
30-
if (packetHandlers.isEmpty()) {
55+
if (handlers.isEmpty()) {
3156
return;
3257
}
3358

34-
HypixelPacketType packetType = HypixelPacketType.getByIdentifier(identifier);
35-
if (packetType == null) {
59+
if (!registry.isRegistered(identifier)) {
3660
return;
3761
}
3862

39-
// All responses contain a boolean of if the response is a success, if not then a string is included with the error message
63+
// 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
4064
if (!serializer.readBoolean()) {
4165
ErrorReason reason = ErrorReason.getById(serializer.readVarInt());
42-
throw new ModAPIException(packetType, reason);
66+
throw new ModAPIException(identifier, reason);
4367
}
4468

45-
HypixelPacket packet = packetType.getPacketFactory().apply(serializer);
46-
for (ClientboundPacketHandler handler : packetHandlers) {
69+
HypixelPacket packet = registry.createClientboundPacket(identifier, serializer);
70+
for (ClientboundPacketHandler handler : handlers) {
4771
handler.handle(packet);
4872
}
4973
}

src/main/java/net/hypixel/modapi/error/ModAPIException.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package net.hypixel.modapi.error;
22

3-
import net.hypixel.modapi.packet.HypixelPacketType;
4-
53
public class ModAPIException extends RuntimeException {
6-
private final HypixelPacketType packetType;
4+
private final String identifier;
75
private final ErrorReason reason;
86

9-
public ModAPIException(HypixelPacketType packetType, ErrorReason reason) {
10-
super(String.format("Received error response '%s' from packet '%s'", reason, packetType));
11-
this.packetType = packetType;
7+
public ModAPIException(String identifier, ErrorReason reason) {
8+
super(String.format("Received error response '%s' from packet '%s'", reason, identifier));
9+
this.identifier = identifier;
1210
this.reason = reason;
1311
}
1412

15-
public HypixelPacketType getPacketType() {
16-
return packetType;
13+
public String getIdentifier() {
14+
return identifier;
1715
}
1816

1917
public ErrorReason getReason() {
@@ -23,7 +21,7 @@ public ErrorReason getReason() {
2321
@Override
2422
public String toString() {
2523
return "ModAPIException{" +
26-
"packetType=" + packetType +
24+
"identifier='" + identifier + '\'' +
2725
", reason=" + reason +
2826
"} " + super.toString();
2927
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package net.hypixel.modapi.packet;
22

3+
import net.hypixel.modapi.HypixelModAPI;
34
import net.hypixel.modapi.serializer.PacketSerializer;
45

56
public interface HypixelPacket {
67

7-
HypixelPacketType getType();
8-
98
void write(PacketSerializer serializer);
109

10+
default String getIdentifier() {
11+
return HypixelModAPI.getInstance().getRegistry().getIdentifier(getClass());
12+
}
13+
1114
}

src/main/java/net/hypixel/modapi/packet/HypixelPacketType.java

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package net.hypixel.modapi.packet;
2+
3+
import net.hypixel.modapi.serializer.PacketSerializer;
4+
5+
import java.util.Collections;
6+
import java.util.Map;
7+
import java.util.Set;
8+
import java.util.concurrent.ConcurrentHashMap;
9+
import java.util.function.Function;
10+
11+
public class PacketRegistry {
12+
13+
private final Map<String, RegisteredType> registrations = new ConcurrentHashMap<>();
14+
private final Map<Class<? extends HypixelPacket>, String> classToIdentifier = new ConcurrentHashMap<>();
15+
16+
public void registerPacketType(String identifier,
17+
Class<? extends HypixelPacket> clientboundClazz, Function<PacketSerializer, HypixelPacket> clientPacketFactory,
18+
Class<? extends HypixelPacket> serverboundClazz, Function<PacketSerializer, HypixelPacket> serverPacketFactory) {
19+
registrations.put(identifier, new RegisteredType(clientboundClazz, clientPacketFactory, serverboundClazz, serverPacketFactory));
20+
classToIdentifier.put(clientboundClazz, identifier);
21+
classToIdentifier.put(serverboundClazz, identifier);
22+
}
23+
24+
private RegisteredType getRegisteredType(String identifier) {
25+
RegisteredType registeredType = registrations.get(identifier);
26+
if (registeredType == null) {
27+
throw new IllegalArgumentException("Unknown packet identifier: " + identifier);
28+
}
29+
return registeredType;
30+
}
31+
32+
public boolean isRegistered(String identifier) {
33+
return registrations.containsKey(identifier);
34+
}
35+
36+
public HypixelPacket createClientboundPacket(String identifier, PacketSerializer serializer) {
37+
return getRegisteredType(identifier).clientPacketFactory.apply(serializer);
38+
}
39+
40+
public HypixelPacket createServerboundPacket(String identifier, PacketSerializer serializer) {
41+
return getRegisteredType(identifier).serverPacketFactory.apply(serializer);
42+
}
43+
44+
public String getIdentifier(Class<? extends HypixelPacket> clazz) {
45+
return classToIdentifier.get(clazz);
46+
}
47+
48+
public Set<String> getIdentifiers() {
49+
return Collections.unmodifiableSet(registrations.keySet());
50+
}
51+
52+
private static final class RegisteredType {
53+
54+
private final Class<? extends HypixelPacket> clientboundClazz;
55+
private final Function<PacketSerializer, HypixelPacket> clientPacketFactory;
56+
private final Class<? extends HypixelPacket> serverboundClazz;
57+
private final Function<PacketSerializer, HypixelPacket> serverPacketFactory;
58+
59+
public RegisteredType(Class<? extends HypixelPacket> clientboundClazz, Function<PacketSerializer, HypixelPacket> clientPacketFactory,
60+
Class<? extends HypixelPacket> serverboundClazz, Function<PacketSerializer, HypixelPacket> serverPacketFactory) {
61+
this.clientboundClazz = clientboundClazz;
62+
this.clientPacketFactory = clientPacketFactory;
63+
this.serverboundClazz = serverboundClazz;
64+
this.serverPacketFactory = serverPacketFactory;
65+
}
66+
}
67+
68+
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import net.hypixel.data.region.Environment;
44
import net.hypixel.data.type.ServerType;
5-
import net.hypixel.modapi.packet.HypixelPacketType;
65
import net.hypixel.modapi.packet.impl.VersionedPacket;
76
import net.hypixel.modapi.serializer.PacketSerializer;
87
import org.jetbrains.annotations.Nullable;
@@ -46,11 +45,6 @@ public ClientboundLocationPacket(PacketSerializer serializer) {
4645
this.map = serializer.readBoolean() ? serializer.readString() : null;
4746
}
4847

49-
@Override
50-
public HypixelPacketType getType() {
51-
return HypixelPacketType.LOCATION;
52-
}
53-
5448
@Override
5549
public void write(PacketSerializer serializer) {
5650
super.write(serializer);

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.packet.HypixelPacketType;
43
import net.hypixel.modapi.packet.impl.VersionedPacket;
54
import net.hypixel.modapi.serializer.PacketSerializer;
65
import org.jetbrains.annotations.Nullable;
@@ -40,11 +39,6 @@ public ClientboundPartyInfoPacket(PacketSerializer serializer) {
4039
this.members = Collections.unmodifiableSet(members);
4140
}
4241

43-
@Override
44-
public HypixelPacketType getType() {
45-
return HypixelPacketType.PARTY_INFO;
46-
}
47-
4842
@Override
4943
public void write(PacketSerializer serializer) {
5044
super.write(serializer);

src/main/java/net/hypixel/modapi/packet/impl/clientbound/ClientboundPingPacket.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.packet.HypixelPacketType;
43
import net.hypixel.modapi.packet.impl.VersionedPacket;
54
import net.hypixel.modapi.serializer.PacketSerializer;
65

@@ -19,11 +18,6 @@ public ClientboundPingPacket(PacketSerializer serializer) {
1918
this.response = serializer.readString();
2019
}
2120

22-
@Override
23-
public HypixelPacketType getType() {
24-
return HypixelPacketType.PING;
25-
}
26-
2721
@Override
2822
public void write(PacketSerializer serializer) {
2923
super.write(serializer);

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +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.packet.HypixelPacketType;
76
import net.hypixel.modapi.packet.impl.VersionedPacket;
87
import net.hypixel.modapi.serializer.PacketSerializer;
98
import org.jetbrains.annotations.Nullable;
@@ -35,11 +34,6 @@ public ClientboundPlayerInfoPacket(PacketSerializer serializer) {
3534
this.prefix = serializer.readBoolean() ? serializer.readString() : null;
3635
}
3736

38-
@Override
39-
public HypixelPacketType getType() {
40-
return HypixelPacketType.PLAYER_INFO;
41-
}
42-
4337
@Override
4438
public void write(PacketSerializer serializer) {
4539
super.write(serializer);

src/main/java/net/hypixel/modapi/packet/impl/serverbound/ServerboundLocationPacket.java

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

3-
import net.hypixel.modapi.packet.HypixelPacketType;
43
import net.hypixel.modapi.packet.impl.VersionedPacket;
54
import net.hypixel.modapi.serializer.PacketSerializer;
65

@@ -15,8 +14,4 @@ public ServerboundLocationPacket(PacketSerializer serializer) {
1514
super(serializer);
1615
}
1716

18-
@Override
19-
public HypixelPacketType getType() {
20-
return HypixelPacketType.LOCATION;
21-
}
2217
}

src/main/java/net/hypixel/modapi/packet/impl/serverbound/ServerboundPartyInfoPacket.java

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

3-
import net.hypixel.modapi.packet.HypixelPacketType;
43
import net.hypixel.modapi.packet.impl.VersionedPacket;
54
import net.hypixel.modapi.serializer.PacketSerializer;
65

@@ -15,8 +14,4 @@ public ServerboundPartyInfoPacket(PacketSerializer serializer) {
1514
super(serializer);
1615
}
1716

18-
@Override
19-
public HypixelPacketType getType() {
20-
return HypixelPacketType.PARTY_INFO;
21-
}
2217
}

0 commit comments

Comments
 (0)