Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@

<groupId>net.hypixel</groupId>
<artifactId>mod-api</artifactId>
<version>0.2.1</version>
<version>dev-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<repositories>
<repository>
<id>Hypixel</id>
<url>https://repo.hypixel.net/repository/Hypixel/</url>
</repository>
</repositories>

<distributionManagement>
<repository>
<id>Hypixel</id>
Expand All @@ -39,6 +46,12 @@
<artifactId>hypixel-data</artifactId>
<version>0.1.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -66,6 +79,11 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>

Expand Down
44 changes: 34 additions & 10 deletions src/main/java/net/hypixel/modapi/HypixelModAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
import net.hypixel.modapi.error.ModAPIException;
import net.hypixel.modapi.handler.ClientboundPacketHandler;
import net.hypixel.modapi.packet.HypixelPacket;
import net.hypixel.modapi.packet.HypixelPacketType;
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;
Expand All @@ -17,33 +25,49 @@ public static HypixelModAPI getInstance() {
return INSTANCE;
}

private final List<ClientboundPacketHandler> packetHandlers = new CopyOnWriteArrayList<>();
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) {
packetHandlers.add(handler);
handlers.add(handler);
}

public void handle(String identifier, PacketSerializer serializer) {
if (packetHandlers.isEmpty()) {
if (handlers.isEmpty()) {
return;
}

HypixelPacketType packetType = HypixelPacketType.getByIdentifier(identifier);
if (packetType == null) {
if (!registry.isRegistered(identifier)) {
return;
}

// All responses contain a boolean of if the response is a success, if not then a string is included with the error message
// 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(packetType, reason);
throw new ModAPIException(identifier, reason);
}

HypixelPacket packet = packetType.getPacketFactory().apply(serializer);
for (ClientboundPacketHandler handler : packetHandlers) {
HypixelPacket packet = registry.createClientboundPacket(identifier, serializer);
for (ClientboundPacketHandler handler : handlers) {
handler.handle(packet);
}
}
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/net/hypixel/modapi/error/ModAPIException.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package net.hypixel.modapi.error;

import net.hypixel.modapi.packet.HypixelPacketType;

public class ModAPIException extends RuntimeException {
private final HypixelPacketType packetType;
private final String identifier;
private final ErrorReason reason;

public ModAPIException(HypixelPacketType packetType, ErrorReason reason) {
super(String.format("Received error response '%s' from packet '%s'", reason, packetType));
this.packetType = packetType;
public ModAPIException(String identifier, ErrorReason reason) {
super(String.format("Received error response '%s' from packet '%s'", reason, identifier));
this.identifier = identifier;
this.reason = reason;
}

public HypixelPacketType getPacketType() {
return packetType;
public String getIdentifier() {
return identifier;
}

public ErrorReason getReason() {
Expand All @@ -23,7 +21,7 @@ public ErrorReason getReason() {
@Override
public String toString() {
return "ModAPIException{" +
"packetType=" + packetType +
"identifier='" + identifier + '\'' +
", reason=" + reason +
"} " + super.toString();
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/net/hypixel/modapi/packet/HypixelPacket.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package net.hypixel.modapi.packet;

import net.hypixel.modapi.HypixelModAPI;
import net.hypixel.modapi.serializer.PacketSerializer;

public interface HypixelPacket {

HypixelPacketType getType();

void write(PacketSerializer serializer);

default String getIdentifier() {
return HypixelModAPI.getInstance().getRegistry().getIdentifier(getClass());
}

}
44 changes: 0 additions & 44 deletions src/main/java/net/hypixel/modapi/packet/HypixelPacketType.java

This file was deleted.

68 changes: 68 additions & 0 deletions src/main/java/net/hypixel/modapi/packet/PacketRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package net.hypixel.modapi.packet;

import net.hypixel.modapi.serializer.PacketSerializer;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

public class PacketRegistry {

private final Map<String, RegisteredType> registrations = new ConcurrentHashMap<>();
private final Map<Class<? extends HypixelPacket>, String> classToIdentifier = new ConcurrentHashMap<>();

public void registerPacketType(String identifier,
Class<? extends HypixelPacket> clientboundClazz, Function<PacketSerializer, HypixelPacket> clientPacketFactory,
Class<? extends HypixelPacket> serverboundClazz, Function<PacketSerializer, HypixelPacket> serverPacketFactory) {
registrations.put(identifier, new RegisteredType(clientboundClazz, clientPacketFactory, serverboundClazz, serverPacketFactory));
classToIdentifier.put(clientboundClazz, identifier);
classToIdentifier.put(serverboundClazz, identifier);
}

private RegisteredType getRegisteredType(String identifier) {
RegisteredType registeredType = registrations.get(identifier);
if (registeredType == null) {
throw new IllegalArgumentException("Unknown packet identifier: " + identifier);
}
return registeredType;
}

public boolean isRegistered(String identifier) {
return registrations.containsKey(identifier);
}

public HypixelPacket createClientboundPacket(String identifier, PacketSerializer serializer) {
return getRegisteredType(identifier).clientPacketFactory.apply(serializer);
}

public HypixelPacket createServerboundPacket(String identifier, PacketSerializer serializer) {
return getRegisteredType(identifier).serverPacketFactory.apply(serializer);
}

public String getIdentifier(Class<? extends HypixelPacket> clazz) {
return classToIdentifier.get(clazz);
}

public Set<String> getIdentifiers() {
return Collections.unmodifiableSet(registrations.keySet());
}

private static final class RegisteredType {

private final Class<? extends HypixelPacket> clientboundClazz;
private final Function<PacketSerializer, HypixelPacket> clientPacketFactory;
private final Class<? extends HypixelPacket> serverboundClazz;
private final Function<PacketSerializer, HypixelPacket> serverPacketFactory;

public RegisteredType(Class<? extends HypixelPacket> clientboundClazz, Function<PacketSerializer, HypixelPacket> clientPacketFactory,
Class<? extends HypixelPacket> serverboundClazz, Function<PacketSerializer, HypixelPacket> serverPacketFactory) {
this.clientboundClazz = clientboundClazz;
this.clientPacketFactory = clientPacketFactory;
this.serverboundClazz = serverboundClazz;
this.serverPacketFactory = serverPacketFactory;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.hypixel.data.region.Environment;
import net.hypixel.data.type.ServerType;
import net.hypixel.modapi.packet.HypixelPacketType;
import net.hypixel.modapi.packet.impl.VersionedPacket;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -46,11 +45,6 @@ public ClientboundLocationPacket(PacketSerializer serializer) {
this.map = serializer.readBoolean() ? serializer.readString() : null;
}

@Override
public HypixelPacketType getType() {
return HypixelPacketType.LOCATION;
}

@Override
public void write(PacketSerializer serializer) {
super.write(serializer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.hypixel.modapi.packet.impl.clientbound;

import net.hypixel.modapi.packet.HypixelPacketType;
import net.hypixel.modapi.packet.impl.VersionedPacket;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -40,11 +39,6 @@ public ClientboundPartyInfoPacket(PacketSerializer serializer) {
this.members = Collections.unmodifiableSet(members);
}

@Override
public HypixelPacketType getType() {
return HypixelPacketType.PARTY_INFO;
}

@Override
public void write(PacketSerializer serializer) {
super.write(serializer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.hypixel.modapi.packet.impl.clientbound;

import net.hypixel.modapi.packet.HypixelPacketType;
import net.hypixel.modapi.packet.impl.VersionedPacket;
import net.hypixel.modapi.serializer.PacketSerializer;

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

@Override
public HypixelPacketType getType() {
return HypixelPacketType.PING;
}

@Override
public void write(PacketSerializer serializer) {
super.write(serializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.hypixel.data.rank.MonthlyPackageRank;
import net.hypixel.data.rank.PackageRank;
import net.hypixel.data.rank.PlayerRank;
import net.hypixel.modapi.packet.HypixelPacketType;
import net.hypixel.modapi.packet.impl.VersionedPacket;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -35,11 +34,6 @@ public ClientboundPlayerInfoPacket(PacketSerializer serializer) {
this.prefix = serializer.readBoolean() ? serializer.readString() : null;
}

@Override
public HypixelPacketType getType() {
return HypixelPacketType.PLAYER_INFO;
}

@Override
public void write(PacketSerializer serializer) {
super.write(serializer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.hypixel.modapi.packet.impl.serverbound;

import net.hypixel.modapi.packet.HypixelPacketType;
import net.hypixel.modapi.packet.impl.VersionedPacket;
import net.hypixel.modapi.serializer.PacketSerializer;

Expand All @@ -15,8 +14,4 @@ public ServerboundLocationPacket(PacketSerializer serializer) {
super(serializer);
}

@Override
public HypixelPacketType getType() {
return HypixelPacketType.LOCATION;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.hypixel.modapi.packet.impl.serverbound;

import net.hypixel.modapi.packet.HypixelPacketType;
import net.hypixel.modapi.packet.impl.VersionedPacket;
import net.hypixel.modapi.serializer.PacketSerializer;

Expand All @@ -15,8 +14,4 @@ public ServerboundPartyInfoPacket(PacketSerializer serializer) {
super(serializer);
}

@Override
public HypixelPacketType getType() {
return HypixelPacketType.PARTY_INFO;
}
}
Loading