Skip to content

Instantly share code, notes, and snippets.

@marvin-roesch
Last active January 2, 2016 01:08
Show Gist options
  • Save marvin-roesch/8227870 to your computer and use it in GitHub Desktop.
Save marvin-roesch/8227870 to your computer and use it in GitHub Desktop.
OpenGuiHandler
package de.mineformers.drash.network;
import cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
/**
* DRASH
* <p/>
* DrashCodec
*
* @author PaleoCrafter
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class DrashCodec extends FMLIndexedMessageToMessageCodec<DrashMessage> {
public DrashCodec() {
addDiscriminator(0, DrashMessage.OpenGui.class);
}
@Override
public void encodeInto(ChannelHandlerContext ctx, DrashMessage msg, ByteBuf target) throws Exception {
msg.toBytes(target);
}
@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf source, DrashMessage msg) {
msg.fromBytes(source);
}
}
package de.mineformers.drash.network;
import io.netty.buffer.ByteBuf;
/**
* DRASH
* <p/>
* DrashMessage
*
* @author PaleoCrafter
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public abstract class DrashMessage {
public static class OpenGui extends DrashMessage {
public int guiId;
public int x, y, z;
public int windowId;
public OpenGui() {
}
public OpenGui(int guiId, int x, int y, int z, int windowId) {
this.guiId = guiId;
this.x = x;
this.y = y;
this.z = z;
this.windowId = windowId;
}
@Override
public void toBytes(ByteBuf bytes) {
bytes.writeInt(guiId);
bytes.writeInt(x);
bytes.writeInt(y);
bytes.writeInt(z);
bytes.writeInt(windowId);
}
@Override
public void fromBytes(ByteBuf bytes) {
guiId = bytes.readInt();
x = bytes.readInt();
y = bytes.readInt();
z = bytes.readInt();
windowId = bytes.readInt();
}
}
public abstract void toBytes(ByteBuf bytes);
public abstract void fromBytes(ByteBuf bytes);
}
package de.mineformers.drash.network.handler;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.relauncher.ReflectionHelper;
import de.mineformers.drash.DRASH;
import de.mineformers.drash.network.DrashMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import net.minecraft.client.Minecraft;
import java.util.Map;
/**
* DRASH
* <p/>
* OpenGuiHandler
*
* @author PaleoCrafter
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class OpenGuiHandler extends SimpleChannelInboundHandler<DrashMessage.OpenGui> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, DrashMessage.OpenGui msg) throws Exception {
IGuiHandler guiHandler = ((Map<ModContainer, IGuiHandler>) ReflectionHelper.getPrivateValue(NetworkRegistry.class, NetworkRegistry.INSTANCE, "clientGuiHandlers")).get(FMLCommonHandler.instance().findContainerFor(DRASH.instance));
FMLCommonHandler.instance().showGuiScreen(guiHandler.getClientGuiElement(msg.guiId, Minecraft.getMinecraft().thePlayer, Minecraft.getMinecraft().theWorld, msg.x, msg.y, msg.z));
if(msg.windowId != -1) {
Minecraft.getMinecraft().thePlayer.openContainer.windowId = msg.windowId;
}
}
}
package de.mineformers.drash.network;
import cpw.mods.fml.common.network.FMLEmbeddedChannel;
import cpw.mods.fml.common.network.FMLOutboundHandler;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.relauncher.Side;
import de.mineformers.drash.DRASH;
import de.mineformers.drash.network.handler.OpenGuiHandler;
import io.netty.channel.embedded.EmbeddedChannel;
import net.minecraft.network.Packet;
import java.util.EnumMap;
/**
* DRASH
* <p/>
* NetHandler
*
* @author PaleoCrafter
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class NetHandler {
private static EnumMap<Side, FMLEmbeddedChannel> channelPair;
public static void init(Side side) {
channelPair = NetworkRegistry.INSTANCE.newChannel(DRASH.MOD_ID, new DrashCodec());
EmbeddedChannel embeddedChannel = channelPair.get(Side.SERVER);
embeddedChannel.attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.NOWHERE);
if(side.isClient()) {
FMLEmbeddedChannel channel = channelPair.get(Side.CLIENT);
channel.pipeline().addAfter("DrashCodec#0", "GuiHandler", new OpenGuiHandler());
}
}
public static Packet generatePacket(Side side, DrashMessage msg) {
return channelPair.get(side).generatePacketFrom(msg);
}
}
package de.mineformers.drash.util;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.relauncher.ReflectionHelper;
import cpw.mods.fml.relauncher.Side;
import de.mineformers.drash.DRASH;
import de.mineformers.drash.network.NetHandler;
import de.mineformers.drash.network.DrashMessage;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.Container;
import net.minecraft.world.World;
import java.util.Map;
/**
* DRASH
* <p/>
* NetworkHelper
*
* @author PaleoCrafter
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class NetworkHelper {
public static void openGui(EntityPlayer player, int guiId, World world, int x, int y, int z) {
ModContainer mc = FMLCommonHandler.instance().findContainerFor(DRASH.instance);
if (FMLCommonHandler.instance().getEffectiveSide().isServer()) {
IGuiHandler guiHandler = ((Map<ModContainer, IGuiHandler>) ReflectionHelper.getPrivateValue(NetworkRegistry.class, NetworkRegistry.INSTANCE, "serverGuiHandlers")).get(mc);
Container container = (Container)guiHandler.getServerGuiElement(guiId, player, world, x, y, z);
EntityPlayerMP playerMP = ((EntityPlayerMP) player);
if(container != null) {
playerMP.incrementWindowID();
playerMP.closeContainer();
int windowId = playerMP.currentWindowId;
playerMP.playerNetServerHandler.func_147359_a(NetHandler.generatePacket(Side.SERVER, new
DrashMessage.OpenGui(guiId, x, y, z, windowId)));
playerMP.openContainer = container;
playerMP.openContainer.windowId = windowId;
playerMP.openContainer.addCraftingToCrafters(playerMP);
return;
}
playerMP.playerNetServerHandler.func_147359_a(NetHandler.generatePacket(Side.SERVER, new
DrashMessage.OpenGui(guiId, x, y, z, -1)));
} else {
IGuiHandler guiHandler = ((Map<ModContainer, IGuiHandler>) ReflectionHelper.getPrivateValue(NetworkRegistry.class, NetworkRegistry.INSTANCE, "clientGuiHandlers")).get(mc);
FMLCommonHandler.instance().showGuiScreen(guiHandler.getClientGuiElement(guiId, player, world, x, y, z));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment