package me.km.networking; import io.netty.buffer.ByteBuf; import java.nio.charset.StandardCharsets; import me.km.inventory.InventoryBase; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; public class CustomInventory implements IMessage { private int windowId; private String windowTitle; private byte slotCount; private byte[] data; public CustomInventory() { windowId = -1; windowTitle = ""; slotCount = 9; data = new byte[14]; } public CustomInventory(int id, InventoryBase inv) { windowId = id; windowTitle = inv.getName(); slotCount = (byte) inv.getSizeInventory(); data = inv.getData(); } @Override public void fromBytes(ByteBuf buf) { windowId = buf.readInt(); int length = buf.readInt(); windowTitle = buf.readBytes(length).toString(StandardCharsets.UTF_8); slotCount = buf.readByte(); for(int i = 0; i < 14; i++) { data[i] = buf.readByte(); } } @Override public void toBytes(ByteBuf buf) { buf.writeInt(windowId); byte[] b = windowTitle.getBytes(StandardCharsets.UTF_8); buf.writeInt(b.length); buf.writeBytes(b); buf.writeByte(slotCount); buf.writeBytes(data); } public static class Handler implements IMessageHandler { @SideOnly(Side.CLIENT) @Override public IMessage onMessage(CustomInventory m, MessageContext ctx) { Minecraft mc = Minecraft.getMinecraft(); EntityPlayerSP p = mc.player; mc.addScheduledTask(() -> { mc.displayGuiScreen(new CustomInventoryGui(p.inventory, new InventoryBase(m.windowTitle, m.data, m.slotCount, p))); p.openContainer.windowId = m.windowId; }); return null; } } }