ModPacketHandler.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package me.km.networking;
  2. import me.km.KajetansMod;
  3. import me.km.entities.EntityHuman;
  4. import me.km.inventory.InventoryBase;
  5. import net.minecraft.entity.player.EntityPlayerMP;
  6. import net.minecraftforge.fml.common.network.NetworkRegistry;
  7. import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
  8. import net.minecraftforge.fml.relauncher.Side;
  9. public class ModPacketHandler
  10. {
  11. public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(KajetansMod.MODID);
  12. private static int id = 0;
  13. public static void init()
  14. {
  15. INSTANCE.registerMessage(PlayerDisplay.Handler.class, PlayerDisplay.class, id++, Side.CLIENT);
  16. INSTANCE.registerMessage(FunctionKey.Handler.class, FunctionKey.class, id++, Side.SERVER);
  17. INSTANCE.registerMessage(HumanUpdate.Handler.class, HumanUpdate.class, id++, Side.CLIENT);
  18. INSTANCE.registerMessage(HumanScaleUpdate.Handler.class, HumanScaleUpdate.class, id++, Side.CLIENT);
  19. INSTANCE.registerMessage(CustomInventory.Handler.class, CustomInventory.class, id++, Side.CLIENT);
  20. INSTANCE.registerMessage(CampFireInventory.Handler.class, CampFireInventory.class, id++, Side.CLIENT);
  21. INSTANCE.registerMessage(SeasonUpdate.Handler.class, SeasonUpdate.class, id++, Side.CLIENT);
  22. INSTANCE.registerMessage(StatusDisplay.Handler.class, StatusDisplay.class, id++, Side.CLIENT);
  23. INSTANCE.registerMessage(PlayerHead.Handler.class, PlayerHead.class, id++, Side.CLIENT);
  24. INSTANCE.registerMessage(ItemStackDisplay.Handler.class, ItemStackDisplay.class, id++, Side.CLIENT);
  25. }
  26. public static void sendToDisplay(EntityPlayerMP p, byte action, byte index, String text)
  27. {
  28. INSTANCE.sendTo(new PlayerDisplay(action, index, text), p);
  29. }
  30. public static void sendFunctionKey(int key)
  31. {
  32. INSTANCE.sendToServer(new FunctionKey(key));
  33. }
  34. public static void sendHumanUpdate(EntityPlayerMP p, EntityHuman human)
  35. {
  36. INSTANCE.sendTo(new HumanUpdate(human), p);
  37. }
  38. public static void sendHumanUpdate(EntityHuman human)
  39. {
  40. HumanUpdate update = new HumanUpdate(human);
  41. KajetansMod.server.getPlayerList().getPlayers().forEach(p -> INSTANCE.sendTo(update, p));
  42. }
  43. public static void sendHumanScaleUpdate(EntityHuman human)
  44. {
  45. HumanScaleUpdate update = new HumanScaleUpdate(human);
  46. KajetansMod.server.getPlayerList().getPlayers().forEach(p -> INSTANCE.sendTo(update, p));
  47. }
  48. public static void sendCustomInventory(EntityPlayerMP p, int id, InventoryBase inv)
  49. {
  50. INSTANCE.sendTo(new CustomInventory(id, inv), p);
  51. }
  52. public static void sendCampFireInventory(EntityPlayerMP p, int id, String name)
  53. {
  54. INSTANCE.sendTo(new CampFireInventory(id, name), p);
  55. }
  56. public static void sendSeasonUpdate(EntityPlayerMP p, byte season)
  57. {
  58. INSTANCE.sendTo(new SeasonUpdate(season), p);
  59. }
  60. public static void addStatus(EntityPlayerMP p, byte index, String text)
  61. {
  62. INSTANCE.sendTo(new StatusDisplay((byte) 1, index, text), p);
  63. }
  64. public static void addTimedStatus(EntityPlayerMP p, byte index, String text, int time)
  65. {
  66. INSTANCE.sendTo(new StatusDisplay((byte) 1, index, time, text), p);
  67. }
  68. public static void removeStatus(EntityPlayerMP p, byte index)
  69. {
  70. INSTANCE.sendTo(new StatusDisplay((byte) 2, index, ""), p);
  71. }
  72. public static void clearStatus(EntityPlayerMP p)
  73. {
  74. INSTANCE.sendTo(new StatusDisplay((byte) 3, (byte) 0, ""), p);
  75. }
  76. public static void sendToHead(EntityPlayerMP p, byte action, byte index, String name, int x, int y, byte scale)
  77. {
  78. INSTANCE.sendTo(new PlayerHead(action, index, name, x, y, scale), p);
  79. }
  80. //--------------------------------------------------------------------------
  81. // item stack display
  82. //--------------------------------------------------------------------------
  83. public static void setItemStackIndex(EntityPlayerMP p, byte index, int iconIndex, int count)
  84. {
  85. INSTANCE.sendTo(new ItemStackDisplay(index, iconIndex, count), p);
  86. }
  87. public static void setItemStackActive(EntityPlayerMP p, boolean active)
  88. {
  89. INSTANCE.sendTo(new ItemStackDisplay(active ? (byte) -1 : (byte) -2, -1, -1), p);
  90. }
  91. public static void clearItemStacks(EntityPlayerMP p)
  92. {
  93. INSTANCE.sendTo(new ItemStackDisplay((byte) -3, -1, -1), p);
  94. }
  95. }