Skip to content

Commit 89f1a58

Browse files
committed
Implement HypixelModAPIImplementation
1 parent 0d5aa08 commit 89f1a58

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
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.16.10
99

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

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

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

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,86 @@
33
import com.mojang.logging.LogUtils;
44
import net.fabricmc.api.ClientModInitializer;
55
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
6+
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
67
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
78
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
89
import net.fabricmc.loader.api.FabricLoader;
910
import net.hypixel.modapi.HypixelModAPI;
11+
import net.hypixel.modapi.HypixelModAPIImplementation;
1012
import net.hypixel.modapi.fabric.event.HypixelModAPICallback;
1113
import net.hypixel.modapi.fabric.event.HypixelModAPIErrorCallback;
1214
import net.hypixel.modapi.fabric.payload.ClientboundHypixelPayload;
1315
import net.hypixel.modapi.fabric.payload.ServerboundHypixelPayload;
16+
import net.hypixel.modapi.packet.HypixelPacket;
17+
import net.hypixel.modapi.packet.impl.clientbound.ClientboundHelloPacket;
1418
import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket;
1519
import net.minecraft.client.MinecraftClient;
1620
import net.minecraft.network.PacketByteBuf;
1721
import net.minecraft.network.codec.PacketCodec;
1822
import net.minecraft.network.packet.CustomPayload;
23+
import net.minecraft.test.GameTest;
1924
import net.minecraft.util.Identifier;
25+
import org.jetbrains.annotations.ApiStatus;
2026
import org.slf4j.Logger;
2127

22-
public class FabricModAPI implements ClientModInitializer {
28+
public class FabricModAPI implements ClientModInitializer, HypixelModAPIImplementation {
2329
private static final Logger LOGGER = LogUtils.getLogger();
2430
private static final boolean DEBUG_MODE = FabricLoader.getInstance().isDevelopmentEnvironment() || Boolean.getBoolean("net.hypixel.modapi.debug");
2531

32+
private boolean onHypixel = false;
33+
34+
@GameTest
2635
@Override
2736
public void onInitializeClient() {
28-
reloadRegistrations();
29-
registerPacketSender();
37+
HypixelModAPI.getInstance().setModImplementation(this);
38+
}
39+
40+
@Override
41+
public void onInit() {
42+
HypixelModAPI.getInstance().createHandler(ClientboundHelloPacket.class, packet -> onHypixel = true);
43+
ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> onHypixel = false);
3044

3145
if (DEBUG_MODE) {
3246
LOGGER.info("Debug mode is enabled!");
3347
registerDebug();
3448
}
49+
50+
reloadRegistrations();
51+
}
52+
53+
@Override
54+
public boolean sendPacket(HypixelPacket packet) {
55+
if (!isConnectedToHypixel()) {
56+
return false;
57+
}
58+
59+
ServerboundHypixelPayload hypixelPayload = new ServerboundHypixelPayload(packet);
60+
61+
if (MinecraftClient.getInstance().getNetworkHandler() != null) {
62+
ClientPlayNetworking.send(hypixelPayload);
63+
return true;
64+
}
65+
66+
try {
67+
ClientConfigurationNetworking.send(hypixelPayload);
68+
return true;
69+
} catch (IllegalStateException ignored) {
70+
LOGGER.warn("Failed to send a packet as the client is not connected to a server '{}'", packet);
71+
return false;
72+
}
73+
}
74+
75+
@Override
76+
public boolean isConnectedToHypixel() {
77+
return onHypixel;
3578
}
3679

3780
/**
3881
* Reloads the identifiers that are registered in the Hypixel Mod API and makes sure that the packets are registered.
3982
* <p>
4083
* This method is available for internal use by Hypixel to add new packets externally, and is not intended for use by other developers.
4184
*/
85+
@ApiStatus.Internal
4286
public static void reloadRegistrations() {
4387
for (String identifier : HypixelModAPI.getInstance().getRegistry().getClientboundIdentifiers()) {
4488
try {
@@ -59,25 +103,6 @@ public static void reloadRegistrations() {
59103
}
60104
}
61105

62-
private static void registerPacketSender() {
63-
HypixelModAPI.getInstance().setPacketSender((packet) -> {
64-
ServerboundHypixelPayload hypixelPayload = new ServerboundHypixelPayload(packet);
65-
66-
if (MinecraftClient.getInstance().getNetworkHandler() != null) {
67-
ClientPlayNetworking.send(hypixelPayload);
68-
return true;
69-
}
70-
71-
try {
72-
ClientConfigurationNetworking.send(hypixelPayload);
73-
return true;
74-
} catch (IllegalStateException ignored) {
75-
LOGGER.warn("Failed to send a packet as the client is not connected to a server '{}'", packet);
76-
return false;
77-
}
78-
});
79-
}
80-
81106
private static void registerClientbound(String identifier) {
82107
try {
83108
CustomPayload.Id<ClientboundHypixelPayload> clientboundId = new CustomPayload.Id<>(Identifier.of(identifier));

0 commit comments

Comments
 (0)