| 11 | import net.minecraftforge.network.simple.SimpleChannel; |
| 12 | |
| 13 | public class Messages { |
| 14 | |
| 15 | private static SimpleChannel INSTANCE; |
| 16 | |
| 17 | private static int packetId = 0; |
| 18 | private static int id() { |
| 19 | return packetId++; |
| 20 | } |
| 21 | |
| 22 | public static void register() { |
| 23 | SimpleChannel net = NetworkRegistry.ChannelBuilder |
| 24 | .named(new ResourceLocation(TutorialV3.MODID, "messages")) |
| 25 | .networkProtocolVersion(() -> "1.0") |
| 26 | .clientAcceptedVersions(s -> true) |
| 27 | .serverAcceptedVersions(s -> true) |
| 28 | .simpleChannel(); |
| 29 | |
| 30 | INSTANCE = net; |
| 31 | |
| 32 | net.messageBuilder(PacketGatherMana.class, id(), NetworkDirection.PLAY_TO_SERVER) |
| 33 | .decoder(PacketGatherMana::new) |
| 34 | .encoder(PacketGatherMana::toBytes) |
| 35 | .consumer(PacketGatherMana::handle) |
| 36 | .add(); |
| 37 | net.messageBuilder(PacketSyncManaToClient.class, id(), NetworkDirection.PLAY_TO_CLIENT) |
| 38 | .decoder(PacketSyncManaToClient::new) |
| 39 | .encoder(PacketSyncManaToClient::toBytes) |
| 40 | .consumer(PacketSyncManaToClient::handle) |
| 41 | .add(); |
| 42 | } |
| 43 | |
| 44 | public static <MSG> void sendToServer(MSG message) { |
| 45 | INSTANCE.sendToServer(message); |
| 46 | } |
| 47 | |
| 48 | public static <MSG> void sendToPlayer(MSG message, ServerPlayer player) { |
| 49 | INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), message); |
| 50 | } |
| 51 | |
| 52 | } |
nothing calls this directly
no outgoing calls
no test coverage detected