Skip to content

Add the ability to register an error handler #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2024
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
68 changes: 58 additions & 10 deletions src/main/java/net/hypixel/modapi/HypixelModAPI.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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.handler.ErrorHandler;
import net.hypixel.modapi.handler.RegisteredHandler;
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
import net.hypixel.modapi.packet.EventPacket;
import net.hypixel.modapi.packet.HypixelPacket;
Expand All @@ -29,7 +30,7 @@ public static HypixelModAPI getInstance() {
}

private final PacketRegistry registry = new PacketRegistry();
private final Map<Class<? extends ClientboundHypixelPacket>, Collection<ClientboundPacketHandler<?>>> handlers = new ConcurrentHashMap<>();
private final Map<String, Collection<RegisteredHandlerImpl<?>>> handlers = new ConcurrentHashMap<>();
private final Set<String> subscribedEvents = ConcurrentHashMap.newKeySet();
private Set<String> lastSubscribedEvents = Collections.emptySet();
private Predicate<HypixelPacket> packetSender = null;
Expand Down Expand Up @@ -109,7 +110,8 @@ public void handle(String identifier, PacketSerializer serializer) {
// 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);
handleError(identifier, reason);
return;
}

ClientboundHypixelPacket packet = registry.createClientboundPacket(identifier, serializer);
Expand All @@ -124,12 +126,20 @@ public void handle(String identifier, PacketSerializer serializer) {
@ApiStatus.Internal
@SuppressWarnings("unchecked")
public void handle(ClientboundHypixelPacket packet) {
Collection<ClientboundPacketHandler<?>> typedHandlers = handlers.get(packet.getClass());
Collection<RegisteredHandlerImpl<?>> typedHandlers = handlers.get(packet.getIdentifier());
// nothing registered for this packet.
if (typedHandlers == null) return;
for (ClientboundPacketHandler<?> handler : typedHandlers) {
// this cast is safe as we ensure its type when it is added to the handlers list in the first place.
((ClientboundPacketHandler<ClientboundHypixelPacket>) handler).handle(packet);
for (RegisteredHandlerImpl<?> handler : typedHandlers) {
handler.handle(packet);
}
}

@ApiStatus.Internal
public void handleError(String identifier, ErrorReason reason) {
Collection<RegisteredHandlerImpl<?>> handlers = this.handlers.get(identifier);
if (handlers == null) return;
for (RegisteredHandlerImpl<?> handler : handlers) {
handler.handleError(reason);
}
}

Expand All @@ -141,9 +151,19 @@ public void setPacketSender(Predicate<HypixelPacket> packetSender) {
this.packetSender = packetSender;
}

public <T extends ClientboundHypixelPacket> void registerHandler(Class<T> packetClass, ClientboundPacketHandler<T> handler) {
if (packetClass == null || handler == null) return;
handlers.computeIfAbsent(packetClass, cls -> new CopyOnWriteArrayList<>()).add(handler);
public <T extends ClientboundHypixelPacket> RegisteredHandler<T> registerHandler(Class<T> packetClass, ClientboundPacketHandler<T> handler) {
if (packetClass == null) {
throw new NullPointerException("packetClass cannot be null");
}

if (handler == null) {
throw new NullPointerException("handler cannot be null");
}

RegisteredHandlerImpl<T> registeredHandler = new RegisteredHandlerImpl<>(handler);
handlers.computeIfAbsent(getRegistry().getIdentifier(packetClass), cls -> new CopyOnWriteArrayList<>())
.add(registeredHandler);
return registeredHandler;
}

public void subscribeToEventPacket(Class<? extends EventPacket> packet) {
Expand All @@ -162,4 +182,32 @@ public boolean sendPacket(HypixelPacket packet) {

return packetSender.test(packet);
}

private static class RegisteredHandlerImpl<T extends ClientboundHypixelPacket> implements RegisteredHandler<T> {
private final ClientboundPacketHandler<T> handler;
private ErrorHandler<T> errorHandler;

RegisteredHandlerImpl(ClientboundPacketHandler<T> handler) {
this.handler = handler;
}

@Override
public void onError(ErrorHandler<T> errorHandler) {
if (this.errorHandler != null) {
throw new IllegalStateException("Error handler already set");
}
this.errorHandler = errorHandler;
}

void handle(ClientboundHypixelPacket packet) {
// this cast is safe as we ensure its type when it is added to the handlers list in the first place.
handler.handle((T) packet);
}

void handleError(ErrorReason reason) {
if (errorHandler != null) {
errorHandler.onError(reason);
}
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/net/hypixel/modapi/handler/ErrorHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.hypixel.modapi.handler;

import net.hypixel.modapi.error.ErrorReason;
import net.hypixel.modapi.packet.ClientboundHypixelPacket;

@FunctionalInterface
public interface ErrorHandler<T extends ClientboundHypixelPacket> {
void onError(ErrorReason reason);
}
12 changes: 12 additions & 0 deletions src/main/java/net/hypixel/modapi/handler/RegisteredHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.hypixel.modapi.handler;

import net.hypixel.modapi.packet.ClientboundHypixelPacket;

public interface RegisteredHandler<T extends ClientboundHypixelPacket> {
/**
* Handling for when an error is received for this registered handler.
* <br>
* Note: This error may be received due to any modification requesting the same packet type.
*/
void onError(ErrorHandler<T> errorHandler);
}