public class ModMessages { public static final String PROTOCOL_VERSION = "1"; public static SimpleChannel INSTANCE; private static int packetID = 0; private static int id() { return packetID++; } public static void register() { SimpleChannel net = NetworkRegistry.ChannelBuilder .named(new ResourceLocation(Constants.MOD_ID, "messages")) .networkProtocolVersion(() -> "1.0") .clientAcceptedVersions(s -> true) .serverAcceptedVersions(s -> true) .simpleChannel(); INSTANCE = net; INSTANCE.messageBuilder(PacketSyncTeleporterPosToClient.class, id(), NetworkDirection.PLAY_TO_CLIENT) .decoder(PacketSyncTeleporterPosToClient::new) .encoder(PacketSyncTeleporterPosToClient::toBytes) .consumer(PacketSyncTeleporterPosToClient::handle) .add(); } public static void sendToAllTrackingAndSelf(MSG message, Entity entity) { INSTANCE.send(PacketDistributor.TRACKING_ENTITY_AND_SELF.with(() -> entity), message); } // Send to the server public static void sendToServer(MSG message) { INSTANCE.sendToServer(message); } // Send to one player public static void sendToClient(MSG message, ServerPlayer player) { INSTANCE.send(PacketDistributor.TRACKING_ENTITY.with(() -> player), message); } // Send to all connected players public static void sendToClients(MSG message) { INSTANCE.send(PacketDistributor.ALL.noArg(), message); } }