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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ configurations {
}

dependencies {
shade "net.hypixel:mod-api:1.0.1"
shade "net.hypixel:mod-api:dev-SNAPSHOT"
runtimeOnly "me.djtheredstoner:DevAuth-forge-legacy:1.2.1"
}

Expand Down
43 changes: 38 additions & 5 deletions src/main/java/net/hypixel/modapi/forge/ForgeModAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import net.hypixel.modapi.HypixelModAPI;
import net.hypixel.modapi.HypixelModAPIImplementation;
import net.hypixel.modapi.packet.HypixelPacket;
import net.hypixel.modapi.packet.impl.clientbound.ClientboundHelloPacket;
import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket;
import net.hypixel.modapi.serializer.PacketSerializer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.network.NetHandlerPlayClient;
Expand All @@ -19,23 +22,23 @@
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Mod(modid = ForgeModAPI.MODID, version = ForgeModAPI.VERSION, clientSideOnly = true, name = "Hypixel Mod API")
public class ForgeModAPI {
public class ForgeModAPI implements HypixelModAPIImplementation {
public static final String MODID = "hypixel_mod_api";
public static final String VERSION = "${version}";
private static final Logger LOGGER = LogManager.getLogger("HypixelModAPI");
private static final boolean DEBUG_MODE = Boolean.getBoolean("net.hypixel.modapi.debug");

// We store a local reference to the net handler, so it's instantly available from the moment we connect
private NetHandlerPlayClient netHandler;
private boolean onHypixel;

@Mod.EventHandler
public void init(FMLPostInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(this);
HypixelModAPI.getInstance().setPacketSender(this::sendPacket);
HypixelModAPI.getInstance().setModImplementation(this);
}

@SubscribeEvent(priority = EventPriority.LOWEST)
Expand All @@ -47,13 +50,30 @@ public void onServerConnect(FMLNetworkEvent.ClientConnectedToServerEvent event)
@SubscribeEvent(priority = EventPriority.LOWEST)
public void onServerDisconnect(FMLNetworkEvent.ClientDisconnectionFromServerEvent event) {
netHandler = null;
onHypixel = false;
}

@Override
public void onInit() {
HypixelModAPI.getInstance().createHandler(ClientboundHelloPacket.class, packet -> onHypixel = true);
MinecraftForge.EVENT_BUS.register(this);

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

private boolean sendPacket(HypixelPacket packet) {
@Override
public boolean sendPacket(HypixelPacket packet) {
if (netHandler == null) {
return false;
}

if (!isConnectedToHypixel()) {
return false;
}

if (!netHandler.getNetworkManager().isChannelOpen()) {
LOGGER.warn("Attempted to send packet while channel is closed!");
netHandler = null;
Expand All @@ -67,6 +87,11 @@ private boolean sendPacket(HypixelPacket packet) {
return true;
}

@Override
public boolean isConnectedToHypixel() {
return onHypixel;
}

@ChannelHandler.Sharable
private static class HypixelPacketHandler extends SimpleChannelInboundHandler<Packet<?>> {
private static final HypixelPacketHandler INSTANCE = new HypixelPacketHandler();
Expand Down Expand Up @@ -99,4 +124,12 @@ protected void channelRead0(ChannelHandlerContext ctx, Packet<?> msg) {
});
}
}

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));
}
}