mirror of
https://github.com/Ellpeck/NaturesAura.git
synced 2024-12-22 06:59:22 +01:00
packet conversion
This commit is contained in:
parent
852ea30b63
commit
9010b9708e
5 changed files with 163 additions and 164 deletions
|
@ -5,18 +5,22 @@ import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
|||
import de.ellpeck.naturesaura.chunk.AuraChunk;
|
||||
import de.ellpeck.naturesaura.events.ClientEvents;
|
||||
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.neoforged.neoforge.network.NetworkEvent;
|
||||
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class PacketAuraChunk {
|
||||
public class PacketAuraChunk implements CustomPacketPayload {
|
||||
|
||||
private int chunkX;
|
||||
private int chunkZ;
|
||||
private Collection<AuraChunk.DrainSpot> drainSpots;
|
||||
public static final ResourceLocation ID = new ResourceLocation(NaturesAura.MOD_ID, "aura_chunk");
|
||||
|
||||
private final int chunkX;
|
||||
private final int chunkZ;
|
||||
private final Collection<AuraChunk.DrainSpot> drainSpots;
|
||||
|
||||
public PacketAuraChunk(int chunkX, int chunkZ, Collection<AuraChunk.DrainSpot> drainSpots) {
|
||||
this.chunkX = chunkX;
|
||||
|
@ -24,34 +28,33 @@ public class PacketAuraChunk {
|
|||
this.drainSpots = drainSpots;
|
||||
}
|
||||
|
||||
private PacketAuraChunk() {
|
||||
}
|
||||
public PacketAuraChunk(FriendlyByteBuf buf) {
|
||||
this.chunkX = buf.readInt();
|
||||
this.chunkZ = buf.readInt();
|
||||
|
||||
public static PacketAuraChunk fromBytes(FriendlyByteBuf buf) {
|
||||
var packet = new PacketAuraChunk();
|
||||
packet.chunkX = buf.readInt();
|
||||
packet.chunkZ = buf.readInt();
|
||||
|
||||
packet.drainSpots = new ArrayList<>();
|
||||
this.drainSpots = new ArrayList<>();
|
||||
var amount = buf.readInt();
|
||||
for (var i = 0; i < amount; i++)
|
||||
packet.drainSpots.add(new AuraChunk.DrainSpot(buf.readNbt()));
|
||||
|
||||
return packet;
|
||||
this.drainSpots.add(new AuraChunk.DrainSpot(buf.readNbt()));
|
||||
}
|
||||
|
||||
public static void toBytes(PacketAuraChunk packet, FriendlyByteBuf buf) {
|
||||
buf.writeInt(packet.chunkX);
|
||||
buf.writeInt(packet.chunkZ);
|
||||
@Override
|
||||
public void write(FriendlyByteBuf buf) {
|
||||
buf.writeInt(this.chunkX);
|
||||
buf.writeInt(this.chunkZ);
|
||||
|
||||
buf.writeInt(packet.drainSpots.size());
|
||||
for (var entry : packet.drainSpots)
|
||||
buf.writeInt(this.drainSpots.size());
|
||||
for (var entry : this.drainSpots)
|
||||
buf.writeNbt(entry.serializeNBT());
|
||||
}
|
||||
|
||||
public static void onMessage(PacketAuraChunk message, Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(() -> ClientEvents.PENDING_AURA_CHUNKS.add(message));
|
||||
ctx.get().setPacketHandled(true);
|
||||
@Override
|
||||
public ResourceLocation id() {
|
||||
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) {
|
||||
|
@ -69,4 +72,5 @@ public class PacketAuraChunk {
|
|||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package de.ellpeck.naturesaura.packet;
|
||||
|
||||
import de.ellpeck.naturesaura.NaturesAura;
|
||||
import de.ellpeck.naturesaura.items.ItemRangeVisualizer;
|
||||
import de.ellpeck.naturesaura.items.ModItems;
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
@ -7,63 +8,59 @@ import net.minecraft.core.BlockPos;
|
|||
import net.minecraft.core.particles.ParticleTypes;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.sounds.SoundEvents;
|
||||
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 CompoundTag data;
|
||||
private final int type;
|
||||
private final CompoundTag data;
|
||||
|
||||
public PacketClient(int type, CompoundTag data) {
|
||||
this.type = type;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
private PacketClient() {
|
||||
|
||||
public PacketClient(FriendlyByteBuf buf) {
|
||||
this.type = buf.readByte();
|
||||
this.data = buf.readNbt();
|
||||
}
|
||||
|
||||
public static PacketClient fromBytes(FriendlyByteBuf buf) {
|
||||
var client = new PacketClient();
|
||||
client.type = buf.readByte();
|
||||
client.data = buf.readNbt();
|
||||
return client;
|
||||
@Override
|
||||
public void write(FriendlyByteBuf buf) {
|
||||
buf.writeByte(this.type);
|
||||
buf.writeNbt(this.data);
|
||||
}
|
||||
|
||||
public static void toBytes(PacketClient packet, FriendlyByteBuf buf) {
|
||||
buf.writeByte(packet.type);
|
||||
buf.writeNbt(packet.data);
|
||||
@Override
|
||||
public ResourceLocation id() {
|
||||
return PacketClient.ID;
|
||||
}
|
||||
|
||||
// lambda causes classloading issues on a server here
|
||||
@SuppressWarnings("Convert2Lambda")
|
||||
public static void onMessage(PacketClient message, Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
var mc = Minecraft.getInstance();
|
||||
if (mc.level != null) {
|
||||
switch (message.type) {
|
||||
case 0: // dimension rail visualization
|
||||
var goalDim = new ResourceLocation(message.data.getString("dim"));
|
||||
var goalPos = BlockPos.of(message.data.getLong("pos"));
|
||||
ItemRangeVisualizer.visualize(mc.player, ItemRangeVisualizer.VISUALIZED_RAILS, goalDim, goalPos);
|
||||
case 1:
|
||||
var entity = mc.level.getEntity(message.data.getInt("id"));
|
||||
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));
|
||||
}
|
||||
}
|
||||
public static void onMessage(PacketClient message, PlayPayloadContext ctx) {
|
||||
ctx.workHandler().execute(() -> {
|
||||
var mc = Minecraft.getInstance();
|
||||
if (mc.level != null) {
|
||||
switch (message.type) {
|
||||
case 0: // dimension rail visualization
|
||||
var goalDim = new ResourceLocation(message.data.getString("dim"));
|
||||
var goalPos = BlockPos.of(message.data.getLong("pos"));
|
||||
ItemRangeVisualizer.visualize(mc.player, ItemRangeVisualizer.VISUALIZED_RAILS, goalDim, goalPos);
|
||||
case 1:
|
||||
var entity = mc.level.getEntity(message.data.getInt("id"));
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,36 +2,37 @@ package de.ellpeck.naturesaura.packet;
|
|||
|
||||
import de.ellpeck.naturesaura.NaturesAura;
|
||||
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.world.entity.player.Player;
|
||||
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.simple.SimpleChannel;
|
||||
import net.neoforged.neoforge.network.NetworkRegistry;
|
||||
import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent;
|
||||
|
||||
@Mod.EventBusSubscriber
|
||||
public final class PacketHandler {
|
||||
|
||||
private static final String VERSION = "1";
|
||||
private static SimpleChannel network;
|
||||
|
||||
public static void init() {
|
||||
PacketHandler.network = NetworkRegistry.newSimpleChannel(new ResourceLocation(NaturesAura.MOD_ID, "network"), () -> PacketHandler.VERSION, PacketHandler.VERSION::equals, PacketHandler.VERSION::equals);
|
||||
PacketHandler.network.registerMessage(0, PacketParticleStream.class, PacketParticleStream::toBytes, PacketParticleStream::fromBytes, PacketParticleStream::onMessage);
|
||||
PacketHandler.network.registerMessage(1, PacketParticles.class, PacketParticles::toBytes, PacketParticles::fromBytes, PacketParticles::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);
|
||||
@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);
|
||||
}
|
||||
|
||||
public static void sendToAllLoaded(Level level, BlockPos pos, Object message) {
|
||||
PacketHandler.network.send(PacketDistributor.TRACKING_CHUNK.with(() -> level.getChunkAt(pos)), message);
|
||||
public static void sendToAllLoaded(Level level, BlockPos pos, CustomPacketPayload message) {
|
||||
PacketDistributor.TRACKING_CHUNK.with(level.getChunkAt(pos)).send(message);
|
||||
}
|
||||
|
||||
public static void sendToAllAround(Level level, BlockPos pos, int range, Object message) {
|
||||
PacketHandler.network.send(PacketDistributor.NEAR.with(() -> new PacketDistributor.TargetPoint(pos.getX(), pos.getY(), pos.getZ(), range, level.dimension())), message);
|
||||
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);
|
||||
}
|
||||
|
||||
public static void sendTo(Player player, Object message) {
|
||||
PacketHandler.network.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), message);
|
||||
public static void sendTo(Player player, CustomPacketPayload message) {
|
||||
((ServerPlayer) player).connection.send(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,24 +1,27 @@
|
|||
package de.ellpeck.naturesaura.packet;
|
||||
|
||||
import de.ellpeck.naturesaura.NaturesAura;
|
||||
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
||||
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 float startY;
|
||||
private float startZ;
|
||||
private final float startX;
|
||||
private final float startY;
|
||||
private final float startZ;
|
||||
|
||||
private float endX;
|
||||
private float endY;
|
||||
private float endZ;
|
||||
private final float endX;
|
||||
private final float endY;
|
||||
private final float endZ;
|
||||
|
||||
private float speed;
|
||||
private int color;
|
||||
private float scale;
|
||||
private final float speed;
|
||||
private final int color;
|
||||
private final 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;
|
||||
|
@ -32,43 +35,41 @@ public class PacketParticleStream {
|
|||
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) {
|
||||
var packet = new PacketParticleStream();
|
||||
|
||||
packet.startX = buf.readFloat();
|
||||
packet.startY = buf.readFloat();
|
||||
packet.startZ = buf.readFloat();
|
||||
packet.endX = buf.readFloat();
|
||||
packet.endY = buf.readFloat();
|
||||
packet.endZ = buf.readFloat();
|
||||
packet.speed = buf.readFloat();
|
||||
packet.color = buf.readInt();
|
||||
packet.scale = buf.readFloat();
|
||||
|
||||
return packet;
|
||||
@Override
|
||||
public void write(FriendlyByteBuf buf) {
|
||||
buf.writeFloat(this.startX);
|
||||
buf.writeFloat(this.startY);
|
||||
buf.writeFloat(this.startZ);
|
||||
buf.writeFloat(this.endX);
|
||||
buf.writeFloat(this.endY);
|
||||
buf.writeFloat(this.endZ);
|
||||
buf.writeFloat(this.speed);
|
||||
buf.writeInt(this.color);
|
||||
buf.writeFloat(this.scale);
|
||||
}
|
||||
|
||||
public static void toBytes(PacketParticleStream packet, FriendlyByteBuf buf) {
|
||||
buf.writeFloat(packet.startX);
|
||||
buf.writeFloat(packet.startY);
|
||||
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);
|
||||
@Override
|
||||
public ResourceLocation id() {
|
||||
return PacketParticleStream.ID;
|
||||
}
|
||||
|
||||
public static void onMessage(PacketParticleStream message, Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(() -> NaturesAuraAPI.instance().spawnParticleStream(
|
||||
public static void onMessage(PacketParticleStream message, PlayPayloadContext ctx) {
|
||||
ctx.workHandler().execute(() -> NaturesAuraAPI.instance().spawnParticleStream(
|
||||
message.startX, message.startY, message.startZ,
|
||||
message.endX, message.endY, message.endZ,
|
||||
message.speed, message.color, message.scale));
|
||||
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package de.ellpeck.naturesaura.packet;
|
||||
|
||||
import de.ellpeck.naturesaura.NaturesAura;
|
||||
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
||||
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
||||
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.ParticleTypes;
|
||||
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.neoforged.neoforge.network.NetworkEvent;
|
||||
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
|
||||
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class PacketParticles {
|
||||
public class PacketParticles implements CustomPacketPayload {
|
||||
|
||||
private float posX;
|
||||
private float posY;
|
||||
private float posZ;
|
||||
private Type type;
|
||||
private int[] data;
|
||||
public static final ResourceLocation ID = new ResourceLocation(NaturesAura.MOD_ID, "particles");
|
||||
|
||||
private final float posX;
|
||||
private final float posY;
|
||||
private final float posZ;
|
||||
private final Type type;
|
||||
private final int[] data;
|
||||
|
||||
public PacketParticles(float posX, float posY, float posZ, Type type, int... data) {
|
||||
this.posX = posX;
|
||||
|
@ -33,49 +37,40 @@ public class PacketParticles {
|
|||
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) {
|
||||
var packet = new PacketParticles();
|
||||
@Override
|
||||
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();
|
||||
packet.posY = buf.readFloat();
|
||||
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.writeByte(this.data.length);
|
||||
for (var i : this.data)
|
||||
buf.writeInt(i);
|
||||
}
|
||||
}
|
||||
|
||||
// lambda causes classloading issues on a server here
|
||||
@SuppressWarnings("Convert2Lambda")
|
||||
public static void onMessage(PacketParticles message, Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Level level = Minecraft.getInstance().level;
|
||||
if (level != null)
|
||||
message.type.action.accept(message, level);
|
||||
}
|
||||
@Override
|
||||
public ResourceLocation id() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void onMessage(PacketParticles message, PlayPayloadContext ctx) {
|
||||
ctx.workHandler().execute(() -> {
|
||||
Level level = Minecraft.getInstance().level;
|
||||
if (level != null)
|
||||
message.type.action.accept(message, level);
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
|
@ -578,4 +573,5 @@ public class PacketParticles {
|
|||
this.action = action;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue