packet conversion

This commit is contained in:
Ell 2024-03-10 10:41:34 +01:00
parent 852ea30b63
commit 9010b9708e
5 changed files with 163 additions and 164 deletions

View file

@ -5,18 +5,22 @@ import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.chunk.AuraChunk; import de.ellpeck.naturesaura.chunk.AuraChunk;
import de.ellpeck.naturesaura.events.ClientEvents; import de.ellpeck.naturesaura.events.ClientEvents;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.neoforged.neoforge.network.NetworkEvent; import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.function.Supplier; import java.util.function.Supplier;
public class PacketAuraChunk { public class PacketAuraChunk implements CustomPacketPayload {
private int chunkX; public static final ResourceLocation ID = new ResourceLocation(NaturesAura.MOD_ID, "aura_chunk");
private int chunkZ;
private Collection<AuraChunk.DrainSpot> drainSpots; private final int chunkX;
private final int chunkZ;
private final Collection<AuraChunk.DrainSpot> drainSpots;
public PacketAuraChunk(int chunkX, int chunkZ, Collection<AuraChunk.DrainSpot> drainSpots) { public PacketAuraChunk(int chunkX, int chunkZ, Collection<AuraChunk.DrainSpot> drainSpots) {
this.chunkX = chunkX; this.chunkX = chunkX;
@ -24,34 +28,33 @@ public class PacketAuraChunk {
this.drainSpots = drainSpots; this.drainSpots = drainSpots;
} }
private PacketAuraChunk() { public PacketAuraChunk(FriendlyByteBuf buf) {
} this.chunkX = buf.readInt();
this.chunkZ = buf.readInt();
public static PacketAuraChunk fromBytes(FriendlyByteBuf buf) { this.drainSpots = new ArrayList<>();
var packet = new PacketAuraChunk();
packet.chunkX = buf.readInt();
packet.chunkZ = buf.readInt();
packet.drainSpots = new ArrayList<>();
var amount = buf.readInt(); var amount = buf.readInt();
for (var i = 0; i < amount; i++) for (var i = 0; i < amount; i++)
packet.drainSpots.add(new AuraChunk.DrainSpot(buf.readNbt())); this.drainSpots.add(new AuraChunk.DrainSpot(buf.readNbt()));
return packet;
} }
public static void toBytes(PacketAuraChunk packet, FriendlyByteBuf buf) { @Override
buf.writeInt(packet.chunkX); public void write(FriendlyByteBuf buf) {
buf.writeInt(packet.chunkZ); buf.writeInt(this.chunkX);
buf.writeInt(this.chunkZ);
buf.writeInt(packet.drainSpots.size()); buf.writeInt(this.drainSpots.size());
for (var entry : packet.drainSpots) for (var entry : this.drainSpots)
buf.writeNbt(entry.serializeNBT()); buf.writeNbt(entry.serializeNBT());
} }
public static void onMessage(PacketAuraChunk message, Supplier<NetworkEvent.Context> ctx) { @Override
ctx.get().enqueueWork(() -> ClientEvents.PENDING_AURA_CHUNKS.add(message)); public ResourceLocation id() {
ctx.get().setPacketHandled(true); return PacketAuraChunk.ID;
}
public static void onMessage(PacketAuraChunk message, PlayPayloadContext ctx) {
ctx.workHandler().execute(() -> ClientEvents.PENDING_AURA_CHUNKS.add(message));
} }
public boolean tryHandle(Level level) { public boolean tryHandle(Level level) {
@ -69,4 +72,5 @@ public class PacketAuraChunk {
return true; return true;
} }
} }
} }

View file

@ -1,5 +1,6 @@
package de.ellpeck.naturesaura.packet; package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.items.ItemRangeVisualizer; import de.ellpeck.naturesaura.items.ItemRangeVisualizer;
import de.ellpeck.naturesaura.items.ModItems; import de.ellpeck.naturesaura.items.ModItems;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
@ -7,63 +8,59 @@ import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.network.NetworkEvent; import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import java.util.function.Supplier; public class PacketClient implements CustomPacketPayload {
public class PacketClient { public static final ResourceLocation ID = new ResourceLocation(NaturesAura.MOD_ID, "client");
private int type; private final int type;
private CompoundTag data; private final CompoundTag data;
public PacketClient(int type, CompoundTag data) { public PacketClient(int type, CompoundTag data) {
this.type = type; this.type = type;
this.data = data; this.data = data;
} }
private PacketClient() { public PacketClient(FriendlyByteBuf buf) {
this.type = buf.readByte();
this.data = buf.readNbt();
} }
public static PacketClient fromBytes(FriendlyByteBuf buf) { @Override
var client = new PacketClient(); public void write(FriendlyByteBuf buf) {
client.type = buf.readByte(); buf.writeByte(this.type);
client.data = buf.readNbt(); buf.writeNbt(this.data);
return client;
} }
public static void toBytes(PacketClient packet, FriendlyByteBuf buf) { @Override
buf.writeByte(packet.type); public ResourceLocation id() {
buf.writeNbt(packet.data); return PacketClient.ID;
} }
// lambda causes classloading issues on a server here public static void onMessage(PacketClient message, PlayPayloadContext ctx) {
@SuppressWarnings("Convert2Lambda") ctx.workHandler().execute(() -> {
public static void onMessage(PacketClient message, Supplier<NetworkEvent.Context> ctx) { var mc = Minecraft.getInstance();
ctx.get().enqueueWork(new Runnable() { if (mc.level != null) {
@Override switch (message.type) {
public void run() { case 0: // dimension rail visualization
var mc = Minecraft.getInstance(); var goalDim = new ResourceLocation(message.data.getString("dim"));
if (mc.level != null) { var goalPos = BlockPos.of(message.data.getLong("pos"));
switch (message.type) { ItemRangeVisualizer.visualize(mc.player, ItemRangeVisualizer.VISUALIZED_RAILS, goalDim, goalPos);
case 0: // dimension rail visualization case 1:
var goalDim = new ResourceLocation(message.data.getString("dim")); var entity = mc.level.getEntity(message.data.getInt("id"));
var goalPos = BlockPos.of(message.data.getLong("pos")); mc.particleEngine.createTrackingEmitter(entity, ParticleTypes.TOTEM_OF_UNDYING, 30);
ItemRangeVisualizer.visualize(mc.player, ItemRangeVisualizer.VISUALIZED_RAILS, goalDim, goalPos); mc.level.playLocalSound(entity.getX(), entity.getY(), entity.getZ(), SoundEvents.TOTEM_USE, entity.getSoundSource(), 1.0F, 1.0F, false);
case 1: if (entity == mc.player) {
var entity = mc.level.getEntity(message.data.getInt("id")); mc.gameRenderer.displayItemActivation(new ItemStack(ModItems.DEATH_RING));
mc.particleEngine.createTrackingEmitter(entity, ParticleTypes.TOTEM_OF_UNDYING, 30); }
mc.level.playLocalSound(entity.getX(), entity.getY(), entity.getZ(), SoundEvents.TOTEM_USE, entity.getSoundSource(), 1.0F, 1.0F, false);
if (entity == mc.player) {
mc.gameRenderer.displayItemActivation(new ItemStack(ModItems.DEATH_RING));
}
}
} }
} }
}); });
ctx.get().setPacketHandled(true);
} }
} }

View file

@ -2,36 +2,37 @@ package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura; import de.ellpeck.naturesaura.NaturesAura;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation; import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.network.PacketDistributor; import net.neoforged.neoforge.network.PacketDistributor;
import net.neoforged.neoforge.network.simple.SimpleChannel; import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent;
import net.neoforged.neoforge.network.NetworkRegistry;
@Mod.EventBusSubscriber
public final class PacketHandler { public final class PacketHandler {
private static final String VERSION = "1"; @SubscribeEvent
private static SimpleChannel network; public static void onPayloadRegister(RegisterPayloadHandlerEvent event) {
var registrar = event.registrar(NaturesAura.MOD_ID);
public static void init() { registrar.play(PacketAuraChunk.ID, PacketAuraChunk::new, PacketAuraChunk::onMessage);
PacketHandler.network = NetworkRegistry.newSimpleChannel(new ResourceLocation(NaturesAura.MOD_ID, "network"), () -> PacketHandler.VERSION, PacketHandler.VERSION::equals, PacketHandler.VERSION::equals); registrar.play(PacketClient.ID, PacketClient::new, PacketClient::onMessage);
PacketHandler.network.registerMessage(0, PacketParticleStream.class, PacketParticleStream::toBytes, PacketParticleStream::fromBytes, PacketParticleStream::onMessage); registrar.play(PacketParticles.ID, PacketParticles::new, PacketParticles::onMessage);
PacketHandler.network.registerMessage(1, PacketParticles.class, PacketParticles::toBytes, PacketParticles::fromBytes, PacketParticles::onMessage); registrar.play(PacketParticleStream.ID, PacketParticleStream::new, PacketParticleStream::onMessage);
PacketHandler.network.registerMessage(2, PacketAuraChunk.class, PacketAuraChunk::toBytes, PacketAuraChunk::fromBytes, PacketAuraChunk::onMessage);
PacketHandler.network.registerMessage(3, PacketClient.class, PacketClient::toBytes, PacketClient::fromBytes, PacketClient::onMessage);
} }
public static void sendToAllLoaded(Level level, BlockPos pos, Object message) { public static void sendToAllLoaded(Level level, BlockPos pos, CustomPacketPayload message) {
PacketHandler.network.send(PacketDistributor.TRACKING_CHUNK.with(() -> level.getChunkAt(pos)), message); PacketDistributor.TRACKING_CHUNK.with(level.getChunkAt(pos)).send(message);
} }
public static void sendToAllAround(Level level, BlockPos pos, int range, Object message) { public static void sendToAllAround(Level level, BlockPos pos, int range, CustomPacketPayload message) {
PacketHandler.network.send(PacketDistributor.NEAR.with(() -> new PacketDistributor.TargetPoint(pos.getX(), pos.getY(), pos.getZ(), range, level.dimension())), message); PacketDistributor.NEAR.with(new PacketDistributor.TargetPoint(pos.getX(), pos.getY(), pos.getZ(), range, level.dimension())).send(message);
} }
public static void sendTo(Player player, Object message) { public static void sendTo(Player player, CustomPacketPayload message) {
PacketHandler.network.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), message); ((ServerPlayer) player).connection.send(message);
} }
} }

View file

@ -1,24 +1,27 @@
package de.ellpeck.naturesaura.packet; package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI; import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.neoforged.neoforge.network.NetworkEvent; import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import java.util.function.Supplier; public class PacketParticleStream implements CustomPacketPayload {
public class PacketParticleStream { public static final ResourceLocation ID = new ResourceLocation(NaturesAura.MOD_ID, "particle_stream");
private float startX; private final float startX;
private float startY; private final float startY;
private float startZ; private final float startZ;
private float endX; private final float endX;
private float endY; private final float endY;
private float endZ; private final float endZ;
private float speed; private final float speed;
private int color; private final int color;
private float scale; private final float scale;
public PacketParticleStream(float startX, float startY, float startZ, float endX, float endY, float endZ, float speed, int color, float scale) { public PacketParticleStream(float startX, float startY, float startZ, float endX, float endY, float endZ, float speed, int color, float scale) {
this.startX = startX; this.startX = startX;
@ -32,43 +35,41 @@ public class PacketParticleStream {
this.scale = scale; this.scale = scale;
} }
private PacketParticleStream() { public PacketParticleStream(FriendlyByteBuf buf) {
this.startX = buf.readFloat();
this.startY = buf.readFloat();
this.startZ = buf.readFloat();
this.endX = buf.readFloat();
this.endY = buf.readFloat();
this.endZ = buf.readFloat();
this.speed = buf.readFloat();
this.color = buf.readInt();
this.scale = buf.readFloat();
} }
public static PacketParticleStream fromBytes(FriendlyByteBuf buf) { @Override
var packet = new PacketParticleStream(); public void write(FriendlyByteBuf buf) {
buf.writeFloat(this.startX);
packet.startX = buf.readFloat(); buf.writeFloat(this.startY);
packet.startY = buf.readFloat(); buf.writeFloat(this.startZ);
packet.startZ = buf.readFloat(); buf.writeFloat(this.endX);
packet.endX = buf.readFloat(); buf.writeFloat(this.endY);
packet.endY = buf.readFloat(); buf.writeFloat(this.endZ);
packet.endZ = buf.readFloat(); buf.writeFloat(this.speed);
packet.speed = buf.readFloat(); buf.writeInt(this.color);
packet.color = buf.readInt(); buf.writeFloat(this.scale);
packet.scale = buf.readFloat();
return packet;
} }
public static void toBytes(PacketParticleStream packet, FriendlyByteBuf buf) { @Override
buf.writeFloat(packet.startX); public ResourceLocation id() {
buf.writeFloat(packet.startY); return PacketParticleStream.ID;
buf.writeFloat(packet.startZ);
buf.writeFloat(packet.endX);
buf.writeFloat(packet.endY);
buf.writeFloat(packet.endZ);
buf.writeFloat(packet.speed);
buf.writeInt(packet.color);
buf.writeFloat(packet.scale);
} }
public static void onMessage(PacketParticleStream message, Supplier<NetworkEvent.Context> ctx) { public static void onMessage(PacketParticleStream message, PlayPayloadContext ctx) {
ctx.get().enqueueWork(() -> NaturesAuraAPI.instance().spawnParticleStream( ctx.workHandler().execute(() -> NaturesAuraAPI.instance().spawnParticleStream(
message.startX, message.startY, message.startZ, message.startX, message.startY, message.startZ,
message.endX, message.endY, message.endZ, message.endX, message.endY, message.endZ,
message.speed, message.color, message.scale)); message.speed, message.color, message.scale));
ctx.get().setPacketHandled(true);
} }
} }

View file

@ -1,5 +1,6 @@
package de.ellpeck.naturesaura.packet; package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI; import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk; import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.type.IAuraType; import de.ellpeck.naturesaura.api.aura.type.IAuraType;
@ -11,19 +12,22 @@ import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ItemParticleOption; import net.minecraft.core.particles.ItemParticleOption;
import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.neoforged.neoforge.network.NetworkEvent; import net.neoforged.neoforge.network.handling.PlayPayloadContext;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Supplier;
public class PacketParticles { public class PacketParticles implements CustomPacketPayload {
private float posX; public static final ResourceLocation ID = new ResourceLocation(NaturesAura.MOD_ID, "particles");
private float posY;
private float posZ; private final float posX;
private Type type; private final float posY;
private int[] data; private final float posZ;
private final Type type;
private final int[] data;
public PacketParticles(float posX, float posY, float posZ, Type type, int... data) { public PacketParticles(float posX, float posY, float posZ, Type type, int... data) {
this.posX = posX; this.posX = posX;
@ -33,49 +37,40 @@ public class PacketParticles {
this.data = data; this.data = data;
} }
private PacketParticles() { public PacketParticles(FriendlyByteBuf buf) {
this.posX = buf.readFloat();
this.posY = buf.readFloat();
this.posZ = buf.readFloat();
this.type = Type.values()[buf.readByte()];
this.data = new int[buf.readByte()];
for (var i = 0; i < this.data.length; i++)
this.data[i] = buf.readInt();
} }
public static PacketParticles fromBytes(FriendlyByteBuf buf) { @Override
var packet = new PacketParticles(); public void write(FriendlyByteBuf buf) {
buf.writeFloat(this.posX);
buf.writeFloat(this.posY);
buf.writeFloat(this.posZ);
buf.writeByte(this.type.ordinal());
packet.posX = buf.readFloat(); buf.writeByte(this.data.length);
packet.posY = buf.readFloat(); for (var i : this.data)
packet.posZ = buf.readFloat();
packet.type = Type.values()[buf.readByte()];
packet.data = new int[buf.readByte()];
for (var i = 0; i < packet.data.length; i++) {
packet.data[i] = buf.readInt();
}
return packet;
}
public static void toBytes(PacketParticles packet, FriendlyByteBuf buf) {
buf.writeFloat(packet.posX);
buf.writeFloat(packet.posY);
buf.writeFloat(packet.posZ);
buf.writeByte(packet.type.ordinal());
buf.writeByte(packet.data.length);
for (var i : packet.data) {
buf.writeInt(i); buf.writeInt(i);
}
} }
// lambda causes classloading issues on a server here @Override
@SuppressWarnings("Convert2Lambda") public ResourceLocation id() {
public static void onMessage(PacketParticles message, Supplier<NetworkEvent.Context> ctx) { return null;
ctx.get().enqueueWork(new Runnable() { }
@Override
public void run() { public static void onMessage(PacketParticles message, PlayPayloadContext ctx) {
Level level = Minecraft.getInstance().level; ctx.workHandler().execute(() -> {
if (level != null) Level level = Minecraft.getInstance().level;
message.type.action.accept(message, level); if (level != null)
} message.type.action.accept(message, level);
}); });
ctx.get().setPacketHandled(true);
} }
public enum Type { public enum Type {
@ -578,4 +573,5 @@ public class PacketParticles {
this.action = action; this.action = action;
} }
} }
} }