PlayerHead.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 PlayerHead implements IMessage
  9. {
  10. // 1 - add - index, name, x, y, scale
  11. // 2 - remove - index
  12. // 3 - clear
  13. private byte action;
  14. private byte index;
  15. private String name;
  16. private int x;
  17. private int y;
  18. private byte scale;
  19. public PlayerHead()
  20. {
  21. action = -1;
  22. index = -1;
  23. name = "";
  24. x = -1;
  25. y = -1;
  26. scale = -1;
  27. }
  28. public PlayerHead(byte action, byte index, String name, int x, int y, byte scale)
  29. {
  30. this.action = action;
  31. this.index = index;
  32. if(name.length() > 16)
  33. {
  34. this.name = name.substring(0, 16);
  35. }
  36. else
  37. {
  38. this.name = name;
  39. }
  40. this.x = x;
  41. this.y = y;
  42. this.scale = scale;
  43. }
  44. @Override
  45. public void fromBytes(ByteBuf buf)
  46. {
  47. action = buf.readByte();
  48. switch(action)
  49. {
  50. case 1:
  51. x = buf.readInt();
  52. y = buf.readInt();
  53. scale = buf.readByte();
  54. index = buf.readByte();
  55. int length = buf.readInt();
  56. name = buf.readBytes(length).toString(StandardCharsets.UTF_8);
  57. break;
  58. case 2:
  59. index = buf.readByte();
  60. break;
  61. }
  62. }
  63. @Override
  64. public void toBytes(ByteBuf buf)
  65. {
  66. buf.writeByte(action);
  67. switch(action)
  68. {
  69. case 1:
  70. buf.writeInt(x);
  71. buf.writeInt(y);
  72. buf.writeByte(scale);
  73. buf.writeByte(index);
  74. byte[] b = name.getBytes(StandardCharsets.UTF_8);
  75. buf.writeInt(b.length);
  76. buf.writeBytes(b);
  77. break;
  78. case 2:
  79. buf.writeByte(index);
  80. break;
  81. }
  82. }
  83. public static class Handler implements IMessageHandler<PlayerHead, IMessage>
  84. {
  85. @Override
  86. public IMessage onMessage(PlayerHead message, MessageContext ctx)
  87. {
  88. Minecraft mc = net.minecraft.client.Minecraft.getMinecraft();
  89. mc.addScheduledTask(() ->
  90. {
  91. switch(message.action)
  92. {
  93. case 1:
  94. PlayerHeadGui.INSTANCE.add(message.index, message.x, message.y, message.scale, message.name);
  95. break;
  96. case 2:
  97. PlayerHeadGui.INSTANCE.remove(message.index);
  98. break;
  99. case 3:
  100. PlayerHeadGui.INSTANCE.clear();
  101. break;
  102. }
  103. });
  104. return null;
  105. }
  106. }
  107. }