Advertisement
Guest User

Untitled

a guest
Feb 19th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. public class ModMessages {
  2.  
  3. public static final String PROTOCOL_VERSION = "1";
  4. public static SimpleChannel INSTANCE;
  5.  
  6. private static int packetID = 0;
  7.  
  8. private static int id() {
  9. return packetID++;
  10. }
  11.  
  12. public static void register() {
  13. SimpleChannel net = NetworkRegistry.ChannelBuilder
  14. .named(new ResourceLocation(Constants.MOD_ID, "messages"))
  15. .networkProtocolVersion(() -> "1.0")
  16. .clientAcceptedVersions(s -> true)
  17. .serverAcceptedVersions(s -> true)
  18. .simpleChannel();
  19.  
  20. INSTANCE = net;
  21.  
  22. INSTANCE.messageBuilder(PacketSyncTeleporterPosToClient.class, id(), NetworkDirection.PLAY_TO_CLIENT)
  23. .decoder(PacketSyncTeleporterPosToClient::new)
  24. .encoder(PacketSyncTeleporterPosToClient::toBytes)
  25. .consumer(PacketSyncTeleporterPosToClient::handle)
  26. .add();
  27. }
  28.  
  29. public static <MSG> void sendToAllTrackingAndSelf(MSG message, Entity entity) {
  30. INSTANCE.send(PacketDistributor.TRACKING_ENTITY_AND_SELF.with(() -> entity), message);
  31. }
  32. // Send to the server
  33. public static <MSG> void sendToServer(MSG message) {
  34. INSTANCE.sendToServer(message);
  35. }
  36. // Send to one player
  37. public static <MSG> void sendToClient(MSG message, ServerPlayer player) {
  38. INSTANCE.send(PacketDistributor.TRACKING_ENTITY.with(() -> player), message);
  39. }
  40. // Send to all connected players
  41. public static <MSG> void sendToClients(MSG message) {
  42. INSTANCE.send(PacketDistributor.ALL.noArg(), message);
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement