NaturesAura/src/main/java/de/ellpeck/naturesaura/packet/PacketHandler.java

38 lines
1.9 KiB
Java
Raw Normal View History

2018-10-14 14:27:18 +02:00
package de.ellpeck.naturesaura.packet;
2020-01-22 01:35:47 +01:00
import de.ellpeck.naturesaura.NaturesAura;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.PacketDistributor;
import net.minecraftforge.fml.network.simple.SimpleChannel;
2018-10-14 14:27:18 +02:00
public final class PacketHandler {
2020-01-22 23:21:52 +01:00
private static final String VERSION = "1";
2020-01-22 01:35:47 +01:00
private static SimpleChannel network;
2018-10-14 14:27:18 +02:00
public static void init() {
2020-01-22 23:21:52 +01:00
network = NetworkRegistry.newSimpleChannel(NaturesAura.createRes("network"), () -> VERSION, VERSION::equals, VERSION::equals);
network.registerMessage(0, PacketParticleStream.class, PacketParticleStream::toBytes, PacketParticleStream::fromBytes, PacketParticleStream::onMessage);
network.registerMessage(1, PacketParticles.class, PacketParticles::toBytes, PacketParticles::fromBytes, PacketParticles::onMessage);
network.registerMessage(2, PacketAuraChunk.class, PacketAuraChunk::toBytes, PacketAuraChunk::fromBytes, PacketAuraChunk::onMessage);
network.registerMessage(3, PacketClient.class, PacketClient::toBytes, PacketClient::fromBytes, PacketClient::onMessage);
2020-01-22 01:35:47 +01:00
}
2020-01-22 23:21:52 +01:00
public static void sendToAllLoaded(World world, BlockPos pos, Object message) {
network.send(PacketDistributor.TRACKING_CHUNK.with(() -> world.getChunkAt(pos)), message);
2018-10-14 14:27:18 +02:00
}
2020-01-22 23:21:52 +01:00
public static void sendToAllAround(IWorld world, BlockPos pos, int range, Object message) {
network.send(PacketDistributor.NEAR.with(() -> new PacketDistributor.TargetPoint(pos.getX(), pos.getY(), pos.getZ(), range, world.getDimension().getType())), message);
2018-10-14 14:27:18 +02:00
}
2020-01-22 23:21:52 +01:00
public static void sendTo(PlayerEntity player, Object message) {
2020-01-22 01:35:47 +01:00
network.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) player), message);
}
2018-10-14 14:27:18 +02:00
}