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

39 lines
1.7 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;
2021-12-04 15:40:09 +01:00
import net.minecraft.core.BlockPos;
2024-03-10 10:41:34 +01:00
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
2021-12-04 15:40:09 +01:00
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
2024-03-10 10:41:34 +01:00
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
2024-02-03 14:56:07 +01:00
import net.neoforged.neoforge.network.PacketDistributor;
2024-03-10 10:41:34 +01:00
import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent;
2020-01-22 01:35:47 +01:00
2024-03-10 10:41:34 +01:00
@Mod.EventBusSubscriber
2018-10-14 14:27:18 +02:00
public final class PacketHandler {
2024-03-10 10:41:34 +01:00
@SubscribeEvent
public static void onPayloadRegister(RegisterPayloadHandlerEvent event) {
var registrar = event.registrar(NaturesAura.MOD_ID);
registrar.play(PacketAuraChunk.ID, PacketAuraChunk::new, PacketAuraChunk::onMessage);
registrar.play(PacketClient.ID, PacketClient::new, PacketClient::onMessage);
registrar.play(PacketParticles.ID, PacketParticles::new, PacketParticles::onMessage);
registrar.play(PacketParticleStream.ID, PacketParticleStream::new, PacketParticleStream::onMessage);
2020-01-22 01:35:47 +01:00
}
2024-03-10 10:41:34 +01:00
public static void sendToAllLoaded(Level level, BlockPos pos, CustomPacketPayload message) {
PacketDistributor.TRACKING_CHUNK.with(level.getChunkAt(pos)).send(message);
2018-10-14 14:27:18 +02:00
}
2024-03-10 10:41:34 +01:00
public static void sendToAllAround(Level level, BlockPos pos, int range, CustomPacketPayload message) {
PacketDistributor.NEAR.with(new PacketDistributor.TargetPoint(pos.getX(), pos.getY(), pos.getZ(), range, level.dimension())).send(message);
2018-10-14 14:27:18 +02:00
}
2024-03-10 10:41:34 +01:00
public static void sendTo(Player player, CustomPacketPayload message) {
((ServerPlayer) player).connection.send(message);
2020-01-22 01:35:47 +01:00
}
2024-03-10 10:41:34 +01:00
2018-10-14 14:27:18 +02:00
}