PlayerDisplay.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package me.km.networking;
  2. import java.nio.charset.StandardCharsets;
  3. import java.util.function.Supplier;
  4. import net.minecraft.network.PacketBuffer;
  5. import net.minecraftforge.fml.network.NetworkEvent;
  6. public class PlayerDisplay {
  7. // 1 - add - index, text
  8. // 2 - remove - index
  9. // 3 - clear
  10. private byte action;
  11. private byte index;
  12. private String text;
  13. public PlayerDisplay() {
  14. action = -1;
  15. index = -1;
  16. text = "";
  17. }
  18. public PlayerDisplay(byte action, byte index, String text) {
  19. this.action = action;
  20. this.index = index;
  21. this.text = text;
  22. }
  23. public static void writeBytes(PlayerDisplay pd, PacketBuffer buf) {
  24. buf.writeByte(pd.action);
  25. buf.writeByte(pd.index);
  26. byte[] b = pd.text.getBytes(StandardCharsets.UTF_8);
  27. buf.writeInt(b.length);
  28. buf.writeBytes(b);
  29. }
  30. public static PlayerDisplay fromBytes(PacketBuffer buf) {
  31. PlayerDisplay pd = new PlayerDisplay();
  32. pd.action = buf.readByte();
  33. pd.index = buf.readByte();
  34. int length = buf.readInt();
  35. pd.text = buf.readBytes(length).toString(StandardCharsets.UTF_8);
  36. return pd;
  37. }
  38. public static void handle(PlayerDisplay pd, Supplier<NetworkEvent.Context> context) {
  39. context.get().enqueueWork(() -> {
  40. switch(pd.action) {
  41. case 1:
  42. PlayerDisplayGui.INSTANCE.add(pd.index, pd.text);
  43. break;
  44. case 2:
  45. PlayerDisplayGui.INSTANCE.remove(pd.index);
  46. break;
  47. case 3:
  48. PlayerDisplayGui.INSTANCE.clear();
  49. break;
  50. }
  51. });
  52. context.get().setPacketHandled(true);
  53. }
  54. }