Skip to content

Commit

Permalink
Implements support for handling errors as well as debug functionality (
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorLinfoot authored Jul 31, 2024
1 parent 6da96a1 commit 2960491
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.15.10

# Mod Properties
mod_version = 1.0+build.1
mod_version = 1.0.1+build.1
maven_group = net.hypixel
archives_base_name = HypixelModAPI
mod_api_version = 1.0
mod_api_version = 1.0.1

# Dependencies
# check this on https://modmuss50.me/fabric.html
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/net/hypixel/modapi/fabric/FabricModAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.hypixel.modapi.HypixelModAPI;
import net.hypixel.modapi.fabric.event.HypixelModAPICallback;
import net.hypixel.modapi.fabric.event.HypixelModAPIErrorCallback;
import net.hypixel.modapi.fabric.payload.ClientboundHypixelPayload;
import net.hypixel.modapi.fabric.payload.ServerboundHypixelPayload;
import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket;
import net.minecraft.client.MinecraftClient;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
Expand All @@ -17,11 +20,17 @@

public class FabricModAPI implements ClientModInitializer {
private static final Logger LOGGER = LogUtils.getLogger();
private static final boolean DEBUG_MODE = FabricLoader.getInstance().isDevelopmentEnvironment() || Boolean.getBoolean("net.hypixel.modapi.debug");

@Override
public void onInitializeClient() {
reloadRegistrations();
registerPacketSender();

if (DEBUG_MODE) {
LOGGER.info("Debug mode is enabled!");
registerDebug();
}
}

/**
Expand Down Expand Up @@ -93,6 +102,17 @@ private static void registerClientbound(String identifier) {
private static void handleIncomingPayload(String identifier, ClientboundHypixelPayload payload) {
if (!payload.isSuccess()) {
LOGGER.warn("Received an error response for packet {}: {}", identifier, payload.getErrorReason());
try {
HypixelModAPI.getInstance().handleError(identifier, payload.getErrorReason());
} catch (Exception e) {
LOGGER.error("An error occurred while handling error response for packet {}", identifier, e);
}

try {
HypixelModAPIErrorCallback.EVENT.invoker().onError(identifier, payload.getErrorReason());
} catch (Exception e) {
LOGGER.error("An error occurred while handling error response for packet {}", identifier, e);
}
return;
}

Expand All @@ -119,4 +139,15 @@ private static void registerServerbound(String identifier) {
// Ignored as this is fired when we reload the registrations and the packet is already registered
}
}

private static void registerDebug() {
// Register events
HypixelModAPI.getInstance().subscribeToEventPacket(ClientboundLocationPacket.class);

HypixelModAPI.getInstance().createHandler(ClientboundLocationPacket.class, packet -> LOGGER.info("Received location packet {}", packet))
.onError(error -> LOGGER.error("Received error response for location packet: {}", error));

HypixelModAPICallback.EVENT.register(packet -> LOGGER.info("Received packet {}", packet));
HypixelModAPIErrorCallback.EVENT.register((identifier, error) -> LOGGER.error("Received error response for packet {}: {}", identifier, error));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.hypixel.modapi.fabric.event;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.hypixel.modapi.error.ErrorReason;

/**
* Callback for when a Hypixel Mod API error reason is received.
*/
public interface HypixelModAPIErrorCallback {

Event<HypixelModAPIErrorCallback> EVENT = EventFactory.createArrayBacked(HypixelModAPIErrorCallback.class, callbacks -> (identifier, reason) -> {
for (HypixelModAPIErrorCallback callback : callbacks) {
callback.onError(identifier, reason);
}
});

void onError(String identifier, ErrorReason reason);

}

0 comments on commit 2960491

Please sign in to comment.