From 5933a59d2db2e22ff1c310f5ba2e7ee6d7d41cde Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Thu, 20 Mar 2025 17:03:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0fast=20projectile=E7=9A=84add?= =?UTF-8?q?itional=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../projectile/FastThrowableProjectile.java | 17 ++++++++++++- .../message/ClientMotionSyncMessage.java | 24 +++++++++---------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/FastThrowableProjectile.java b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/FastThrowableProjectile.java index ae08f3dbc..c6c9cfa80 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/FastThrowableProjectile.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/FastThrowableProjectile.java @@ -2,13 +2,15 @@ package com.atsuishio.superbwarfare.entity.projectile; import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.network.message.ClientMotionSyncMessage; +import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.projectile.ThrowableItemProjectile; import net.minecraft.world.level.Level; +import net.minecraftforge.entity.IEntityAdditionalSpawnData; import net.minecraftforge.network.PacketDistributor; -public abstract class FastThrowableProjectile extends ThrowableItemProjectile implements CustomSyncMotionEntity { +public abstract class FastThrowableProjectile extends ThrowableItemProjectile implements CustomSyncMotionEntity, IEntityAdditionalSpawnData { public FastThrowableProjectile(EntityType pEntityType, Level pLevel) { super(pEntityType, pLevel); @@ -36,4 +38,17 @@ public abstract class FastThrowableProjectile extends ThrowableItemProjectile im ModUtils.PACKET_HANDLER.send(PacketDistributor.ALL.noArg(), new ClientMotionSyncMessage(this)); } } + + @Override + public void writeSpawnData(FriendlyByteBuf buffer) { + var motion = this.getDeltaMovement(); + buffer.writeFloat((float) motion.x); + buffer.writeFloat((float) motion.y); + buffer.writeFloat((float) motion.z); + } + + @Override + public void readSpawnData(FriendlyByteBuf additionalData) { + this.setDeltaMovement(additionalData.readFloat(), additionalData.readFloat(), additionalData.readFloat()); + } } diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/ClientMotionSyncMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/ClientMotionSyncMessage.java index aa8b74df4..4be651345 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/ClientMotionSyncMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/ClientMotionSyncMessage.java @@ -13,9 +13,9 @@ import java.util.function.Supplier; public class ClientMotionSyncMessage { public final int id; - public final double x; - public final double y; - public final double z; + public final float x; + public final float y; + public final float z; public ClientMotionSyncMessage(Entity entity) { this(entity.getId(), entity.getDeltaMovement()); @@ -23,23 +23,23 @@ public class ClientMotionSyncMessage { public ClientMotionSyncMessage(int id, Vec3 motion) { this.id = id; - this.x = motion.x; - this.y = motion.y; - this.z = motion.z; + this.x = (float) motion.x; + this.y = (float) motion.y; + this.z = (float) motion.z; } public static void encode(ClientMotionSyncMessage message, FriendlyByteBuf buffer) { buffer.writeVarInt(message.id); - buffer.writeDouble(message.x); - buffer.writeDouble(message.y); - buffer.writeDouble(message.z); + buffer.writeFloat(message.x); + buffer.writeFloat(message.y); + buffer.writeFloat(message.z); } public static ClientMotionSyncMessage decode(FriendlyByteBuf buffer) { int id = buffer.readVarInt(); - double x = buffer.readDouble(); - double y = buffer.readDouble(); - double z = buffer.readDouble(); + double x = buffer.readFloat(); + double y = buffer.readFloat(); + double z = buffer.readFloat(); return new ClientMotionSyncMessage(id, new Vec3(x, y, z)); }