StatusDisplay.java 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package me.km.networking;
  2. import io.netty.buffer.ByteBuf;
  3. import java.nio.charset.StandardCharsets;
  4. import net.minecraft.client.Minecraft;
  5. import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
  6. import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
  7. import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
  8. public class StatusDisplay implements IMessage
  9. {
  10. // 1 - add - index, text
  11. // 2 - remove - index
  12. // 3 - clear
  13. private byte action;
  14. private byte index;
  15. private int time;
  16. private String text;
  17. public StatusDisplay()
  18. {
  19. action = -1;
  20. index = -1;
  21. text = "";
  22. }
  23. public StatusDisplay(byte action, byte index, int time, String text)
  24. {
  25. this.action = action;
  26. this.index = index;
  27. this.time = time;
  28. if(text.length() > 32)
  29. {
  30. this.text = text.substring(0, 32);
  31. }
  32. else
  33. {
  34. this.text = text;
  35. }
  36. }
  37. public StatusDisplay(byte action, byte index, String text)
  38. {
  39. this(action, index, Integer.MAX_VALUE, text);
  40. }
  41. @Override
  42. public void fromBytes(ByteBuf buf)
  43. {
  44. action = buf.readByte();
  45. index = buf.readByte();
  46. time = buf.readInt();
  47. int length = buf.readInt();
  48. text = buf.readBytes(length).toString(StandardCharsets.UTF_8);
  49. }
  50. @Override
  51. public void toBytes(ByteBuf buf)
  52. {
  53. buf.writeByte(action);
  54. buf.writeByte(index);
  55. buf.writeInt(time);
  56. byte[] b = text.getBytes(StandardCharsets.UTF_8);
  57. buf.writeInt(b.length);
  58. buf.writeBytes(b);
  59. }
  60. public static class Handler implements IMessageHandler<StatusDisplay, IMessage>
  61. {
  62. @Override
  63. public IMessage onMessage(StatusDisplay message, MessageContext ctx)
  64. {
  65. Minecraft mc = net.minecraft.client.Minecraft.getMinecraft();
  66. mc.addScheduledTask(() ->
  67. {
  68. switch(message.action)
  69. {
  70. case 1:
  71. StatusDisplayGui.INSTANCE.add(message.index, message.time, message.text);
  72. break;
  73. case 2:
  74. StatusDisplayGui.INSTANCE.remove(message.index);
  75. break;
  76. case 3:
  77. StatusDisplayGui.INSTANCE.clear();
  78. break;
  79. }
  80. });
  81. return null;
  82. }
  83. }
  84. }