CustomInventory.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package me.km.networking;
  2. import io.netty.buffer.ByteBuf;
  3. import java.nio.charset.StandardCharsets;
  4. import me.km.inventory.InventoryBase;
  5. import net.minecraft.client.Minecraft;
  6. import net.minecraft.client.entity.EntityPlayerSP;
  7. import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
  8. import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
  9. import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
  10. import net.minecraftforge.fml.relauncher.Side;
  11. import net.minecraftforge.fml.relauncher.SideOnly;
  12. public class CustomInventory implements IMessage
  13. {
  14. private int windowId;
  15. private String windowTitle;
  16. private byte slotCount;
  17. private byte[] data;
  18. public CustomInventory()
  19. {
  20. windowId = -1;
  21. windowTitle = "";
  22. slotCount = 9;
  23. data = new byte[14];
  24. }
  25. public CustomInventory(int id, InventoryBase inv)
  26. {
  27. windowId = id;
  28. windowTitle = inv.getName();
  29. slotCount = (byte) inv.getSizeInventory();
  30. data = inv.getData();
  31. }
  32. @Override
  33. public void fromBytes(ByteBuf buf)
  34. {
  35. windowId = buf.readInt();
  36. int length = buf.readInt();
  37. windowTitle = buf.readBytes(length).toString(StandardCharsets.UTF_8);
  38. slotCount = buf.readByte();
  39. for(int i = 0; i < 14; i++)
  40. {
  41. data[i] = buf.readByte();
  42. }
  43. }
  44. @Override
  45. public void toBytes(ByteBuf buf)
  46. {
  47. buf.writeInt(windowId);
  48. byte[] b = windowTitle.getBytes(StandardCharsets.UTF_8);
  49. buf.writeInt(b.length);
  50. buf.writeBytes(b);
  51. buf.writeByte(slotCount);
  52. buf.writeBytes(data);
  53. }
  54. public static class Handler implements IMessageHandler<CustomInventory, IMessage>
  55. {
  56. @SideOnly(Side.CLIENT)
  57. @Override
  58. public IMessage onMessage(CustomInventory m, MessageContext ctx)
  59. {
  60. Minecraft mc = Minecraft.getMinecraft();
  61. EntityPlayerSP p = mc.player;
  62. mc.addScheduledTask(() ->
  63. {
  64. mc.displayGuiScreen(new CustomInventoryGui(p.inventory, new InventoryBase(m.windowTitle, m.data, m.slotCount, p)));
  65. p.openContainer.windowId = m.windowId;
  66. });
  67. return null;
  68. }
  69. }
  70. }