Skip to content

Commit 2960491

Browse files
Implements support for handling errors as well as debug functionality (#6)
See HypixelDev/ModAPI#39
1 parent 6da96a1 commit 2960491

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ org.gradle.jvmargs=-Xmx1G
88
loader_version=0.15.10
99

1010
# Mod Properties
11-
mod_version = 1.0+build.1
11+
mod_version = 1.0.1+build.1
1212
maven_group = net.hypixel
1313
archives_base_name = HypixelModAPI
14-
mod_api_version = 1.0
14+
mod_api_version = 1.0.1
1515

1616
# Dependencies
1717
# check this on https://modmuss50.me/fabric.html

src/main/java/net/hypixel/modapi/fabric/FabricModAPI.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
66
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
77
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
8+
import net.fabricmc.loader.api.FabricLoader;
89
import net.hypixel.modapi.HypixelModAPI;
910
import net.hypixel.modapi.fabric.event.HypixelModAPICallback;
11+
import net.hypixel.modapi.fabric.event.HypixelModAPIErrorCallback;
1012
import net.hypixel.modapi.fabric.payload.ClientboundHypixelPayload;
1113
import net.hypixel.modapi.fabric.payload.ServerboundHypixelPayload;
14+
import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket;
1215
import net.minecraft.client.MinecraftClient;
1316
import net.minecraft.network.PacketByteBuf;
1417
import net.minecraft.network.codec.PacketCodec;
@@ -17,11 +20,17 @@
1720

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

2125
@Override
2226
public void onInitializeClient() {
2327
reloadRegistrations();
2428
registerPacketSender();
29+
30+
if (DEBUG_MODE) {
31+
LOGGER.info("Debug mode is enabled!");
32+
registerDebug();
33+
}
2534
}
2635

2736
/**
@@ -93,6 +102,17 @@ private static void registerClientbound(String identifier) {
93102
private static void handleIncomingPayload(String identifier, ClientboundHypixelPayload payload) {
94103
if (!payload.isSuccess()) {
95104
LOGGER.warn("Received an error response for packet {}: {}", identifier, payload.getErrorReason());
105+
try {
106+
HypixelModAPI.getInstance().handleError(identifier, payload.getErrorReason());
107+
} catch (Exception e) {
108+
LOGGER.error("An error occurred while handling error response for packet {}", identifier, e);
109+
}
110+
111+
try {
112+
HypixelModAPIErrorCallback.EVENT.invoker().onError(identifier, payload.getErrorReason());
113+
} catch (Exception e) {
114+
LOGGER.error("An error occurred while handling error response for packet {}", identifier, e);
115+
}
96116
return;
97117
}
98118

@@ -119,4 +139,15 @@ private static void registerServerbound(String identifier) {
119139
// Ignored as this is fired when we reload the registrations and the packet is already registered
120140
}
121141
}
142+
143+
private static void registerDebug() {
144+
// Register events
145+
HypixelModAPI.getInstance().subscribeToEventPacket(ClientboundLocationPacket.class);
146+
147+
HypixelModAPI.getInstance().createHandler(ClientboundLocationPacket.class, packet -> LOGGER.info("Received location packet {}", packet))
148+
.onError(error -> LOGGER.error("Received error response for location packet: {}", error));
149+
150+
HypixelModAPICallback.EVENT.register(packet -> LOGGER.info("Received packet {}", packet));
151+
HypixelModAPIErrorCallback.EVENT.register((identifier, error) -> LOGGER.error("Received error response for packet {}: {}", identifier, error));
152+
}
122153
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.hypixel.modapi.fabric.event;
2+
3+
import net.fabricmc.fabric.api.event.Event;
4+
import net.fabricmc.fabric.api.event.EventFactory;
5+
import net.hypixel.modapi.error.ErrorReason;
6+
7+
/**
8+
* Callback for when a Hypixel Mod API error reason is received.
9+
*/
10+
public interface HypixelModAPIErrorCallback {
11+
12+
Event<HypixelModAPIErrorCallback> EVENT = EventFactory.createArrayBacked(HypixelModAPIErrorCallback.class, callbacks -> (identifier, reason) -> {
13+
for (HypixelModAPIErrorCallback callback : callbacks) {
14+
callback.onError(identifier, reason);
15+
}
16+
});
17+
18+
void onError(String identifier, ErrorReason reason);
19+
20+
}

0 commit comments

Comments
 (0)