diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/net/hypixel/modapi/forge/ForgeModAPI.java | 78 | ||||
-rwxr-xr-x | src/main/resources/mcmod.info | 17 |
2 files changed, 95 insertions, 0 deletions
diff --git a/src/main/java/net/hypixel/modapi/forge/ForgeModAPI.java b/src/main/java/net/hypixel/modapi/forge/ForgeModAPI.java new file mode 100644 index 0000000..de29195 --- /dev/null +++ b/src/main/java/net/hypixel/modapi/forge/ForgeModAPI.java @@ -0,0 +1,78 @@ +package net.hypixel.modapi.forge; + +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import net.hypixel.modapi.HypixelModAPI; +import net.hypixel.modapi.packet.HypixelPacket; +import net.hypixel.modapi.serializer.PacketSerializer; +import net.minecraft.client.Minecraft; +import net.minecraft.client.network.NetHandlerPlayClient; +import net.minecraft.network.Packet; +import net.minecraft.network.PacketBuffer; +import net.minecraft.network.play.client.C17PacketCustomPayload; +import net.minecraft.network.play.server.S3FPacketCustomPayload; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.network.FMLNetworkEvent; + +import java.util.logging.Level; +import java.util.logging.Logger; + +@Mod(modid = ForgeModAPI.MODID, version = ForgeModAPI.VERSION, clientSideOnly = true, name = "Hypixel Mod API") +public class ForgeModAPI { + public static final String MODID = "hypixel_mod_api"; + public static final String VERSION = "0.4.0"; + private static final Logger LOGGER = Logger.getLogger("HypixelModAPI"); + + @Mod.EventHandler + public void init(FMLPostInitializationEvent event) { + MinecraftForge.EVENT_BUS.register(this); + HypixelModAPI.getInstance().setPacketSender(ForgeModAPI::sendPacket); + } + + @SubscribeEvent + public void onServerConnect(FMLNetworkEvent.ClientConnectedToServerEvent event) { + event.manager.channel().pipeline().addBefore("packet_handler", "hypixel_mod_api_packet_handler", HypixelPacketHandler.INSTANCE); + } + + private static boolean sendPacket(HypixelPacket packet) { + NetHandlerPlayClient netHandler = Minecraft.getMinecraft().getNetHandler(); + if (netHandler == null) { + return false; + } + + PacketBuffer buf = new PacketBuffer(Unpooled.buffer()); + PacketSerializer serializer = new PacketSerializer(buf); + packet.write(serializer); + netHandler.addToSendQueue(new C17PacketCustomPayload(packet.getIdentifier(), buf)); + return true; + } + + private static class HypixelPacketHandler extends SimpleChannelInboundHandler<Packet<?>> { + private static final HypixelPacketHandler INSTANCE = new HypixelPacketHandler(); + + @Override + protected void channelRead0(ChannelHandlerContext ctx, Packet<?> msg) { + ctx.fireChannelRead(msg); + + if (!(msg instanceof S3FPacketCustomPayload)) { + return; + } + + S3FPacketCustomPayload packet = (S3FPacketCustomPayload) msg; + String identifier = packet.getChannelName(); + if (!HypixelModAPI.getInstance().getRegistry().isRegistered(identifier)) { + return; + } + + try { + HypixelModAPI.getInstance().handle(identifier, new PacketSerializer(packet.getBufferData())); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to handle packet " + identifier, e); + } + } + } +} diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info new file mode 100755 index 0000000..ca18cec --- /dev/null +++ b/src/main/resources/mcmod.info @@ -0,0 +1,17 @@ +[ + { + "modid": "hypixel_mod_api", + "name": "Hypixel Mod API", + "description": "Implementation of the Hypixel Mod API for Forge.", + "version": "${version}", + "mcversion": "${mcversion}", + "url": "https://modrinth.com/mod/hypixel-mod-api/", + "updateUrl": "", + "authorList": [ + "Hypixel Inc." + ], + "logoFile": "", + "screenshots": [], + "dependencies": [] + } +] |