From 867f7a6bdec1b4701db61c95b8de92d85653f551 Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Wed, 19 Mar 2025 18:21:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E8=87=AA=E8=A1=8C=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=AE=9E=E4=BD=93=E5=8A=A8?= =?UTF-8?q?=E9=87=8F=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/atsuishio/superbwarfare/ModUtils.java | 1 + .../entity/projectile/CannonShellEntity.java | 12 ++- .../entity/projectile/FastProjectile.java | 2 + .../entity/projectile/ProjectileEntity.java | 10 +++ .../superbwarfare/init/ModEntities.java | 4 +- ...ClientboundSetEntityMotionPacketMixin.java | 74 ------------------- .../network/ClientPacketHandler.java | 16 +++- .../message/ClientMotionSyncMessage.java | 51 +++++++++++++ src/main/resources/mixins.superbwarfare.json | 1 - 9 files changed, 89 insertions(+), 82 deletions(-) delete mode 100644 src/main/java/com/atsuishio/superbwarfare/mixins/ClientboundSetEntityMotionPacketMixin.java create mode 100644 src/main/java/com/atsuishio/superbwarfare/network/message/ClientMotionSyncMessage.java diff --git a/src/main/java/com/atsuishio/superbwarfare/ModUtils.java b/src/main/java/com/atsuishio/superbwarfare/ModUtils.java index cd5b5b16b..d051f2ef9 100644 --- a/src/main/java/com/atsuishio/superbwarfare/ModUtils.java +++ b/src/main/java/com/atsuishio/superbwarfare/ModUtils.java @@ -178,6 +178,7 @@ public class ModUtils { addNetworkMessage(SwitchVehicleWeaponMessage.class, SwitchVehicleWeaponMessage::encode, SwitchVehicleWeaponMessage::decode, SwitchVehicleWeaponMessage::handler); addNetworkMessage(RadarSetTargetMessage.class, RadarSetTargetMessage::encode, RadarSetTargetMessage::decode, RadarSetTargetMessage::handler); addNetworkMessage(ChangeVehicleSeatMessage.class, ChangeVehicleSeatMessage::encode, ChangeVehicleSeatMessage::decode, ChangeVehicleSeatMessage::handler); + addNetworkMessage(ClientMotionSyncMessage.class, ClientMotionSyncMessage::encode, ClientMotionSyncMessage::decode, ClientMotionSyncMessage::handler, Optional.of(NetworkDirection.PLAY_TO_CLIENT)); event.enqueueWork(() -> BrewingRecipeRegistry.addRecipe(Ingredient.of(PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.WATER)), Ingredient.of(Items.LIGHTNING_ROD), PotionUtils.setPotion(new ItemStack(Items.POTION), ModPotion.SHOCK.get()))); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/CannonShellEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/CannonShellEntity.java index 8588a2b2d..a44badc37 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/CannonShellEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/CannonShellEntity.java @@ -5,6 +5,7 @@ import com.atsuishio.superbwarfare.config.server.ExplosionConfig; import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity; import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.network.message.ClientIndicatorMessage; +import com.atsuishio.superbwarfare.network.message.ClientMotionSyncMessage; import com.atsuishio.superbwarfare.tools.ChunkLoadTool; import com.atsuishio.superbwarfare.tools.CustomExplosion; import com.atsuishio.superbwarfare.tools.ParticleTool; @@ -50,7 +51,7 @@ import software.bernie.geckolib.util.GeckoLibUtil; import java.util.HashSet; import java.util.Set; -public class CannonShellEntity extends ThrowableItemProjectile implements GeoEntity { +public class CannonShellEntity extends ThrowableItemProjectile implements GeoEntity, FastProjectile { public static final EntityDataAccessor ANIMATION = SynchedEntityData.defineId(CannonShellEntity.class, EntityDataSerializers.STRING); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); @@ -255,6 +256,15 @@ public class CannonShellEntity extends ThrowableItemProjectile implements GeoEnt } this.discard(); } + + this.syncMotion(); + } + + @Override + public void syncMotion() { + if (!this.level().isClientSide) { + ModUtils.PACKET_HANDLER.send(PacketDistributor.ALL.noArg(), new ClientMotionSyncMessage(this)); + } } private void causeExplode(Entity entity) { diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/FastProjectile.java b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/FastProjectile.java index bdeb1a6d8..0e18416a9 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/FastProjectile.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/FastProjectile.java @@ -1,4 +1,6 @@ package com.atsuishio.superbwarfare.entity.projectile; public interface FastProjectile { + + void syncMotion(); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/ProjectileEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/ProjectileEntity.java index 20797c7b5..a391b1e0a 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/ProjectileEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/ProjectileEntity.java @@ -9,6 +9,7 @@ import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity; import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.item.Transcript; import com.atsuishio.superbwarfare.network.message.ClientIndicatorMessage; +import com.atsuishio.superbwarfare.network.message.ClientMotionSyncMessage; import com.atsuishio.superbwarfare.network.message.PlayerGunKillMessage; import com.atsuishio.superbwarfare.tools.*; import net.minecraft.core.BlockPos; @@ -300,6 +301,15 @@ public class ProjectileEntity extends Projectile implements IEntityAdditionalSpa Math.max(this.getDeltaMovement().length() - 1.1 * this.tickCount, 0.2), true ); } + + this.syncMotion(); + } + + @Override + public void syncMotion() { + if (!this.level().isClientSide) { + ModUtils.PACKET_HANDLER.send(PacketDistributor.ALL.noArg(), new ClientMotionSyncMessage(this)); + } } @Override diff --git a/src/main/java/com/atsuishio/superbwarfare/init/ModEntities.java b/src/main/java/com/atsuishio/superbwarfare/init/ModEntities.java index 3ed51b9bd..d25d43ad9 100644 --- a/src/main/java/com/atsuishio/superbwarfare/init/ModEntities.java +++ b/src/main/java/com/atsuishio/superbwarfare/init/ModEntities.java @@ -55,9 +55,9 @@ public class ModEntities { public static final RegistryObject> MORTAR_SHELL = register("projectile_mortar_shell", EntityType.Builder.of(MortarShellEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).setCustomClientFactory(MortarShellEntity::new).sized(0.5f, 0.5f)); public static final RegistryObject> PROJECTILE = register("projectile", - EntityType.Builder.of(ProjectileEntity::new, MobCategory.MISC).setCustomClientFactory(ProjectileEntity::new).setTrackingRange(64).noSave().noSummon().sized(0.25f, 0.25f)); + EntityType.Builder.of(ProjectileEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(false).setCustomClientFactory(ProjectileEntity::new).setTrackingRange(64).noSave().noSummon().sized(0.25f, 0.25f)); public static final RegistryObject> CANNON_SHELL = register("projectile_cannon_shell", - EntityType.Builder.of(CannonShellEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).setCustomClientFactory(CannonShellEntity::new).sized(0.5f, 0.5f)); + EntityType.Builder.of(CannonShellEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(false).setTrackingRange(64).setUpdateInterval(1).setCustomClientFactory(CannonShellEntity::new).sized(0.5f, 0.5f)); public static final RegistryObject> HAND_GRENADE_ENTITY = register("projectile_hand_grenade_entity", EntityType.Builder.of(HandGrenadeEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).setCustomClientFactory(HandGrenadeEntity::new).sized(0.3f, 0.3f)); public static final RegistryObject> RGO_GRENADE = register("projectile_rgo_grenade", diff --git a/src/main/java/com/atsuishio/superbwarfare/mixins/ClientboundSetEntityMotionPacketMixin.java b/src/main/java/com/atsuishio/superbwarfare/mixins/ClientboundSetEntityMotionPacketMixin.java deleted file mode 100644 index f429856ea..000000000 --- a/src/main/java/com/atsuishio/superbwarfare/mixins/ClientboundSetEntityMotionPacketMixin.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.atsuishio.superbwarfare.mixins; - -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket; -import net.minecraft.world.phys.Vec3; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Mutable; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.Redirect; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -/** - * From Fast Projectile Fix - *
- * Author: Roki27 - */ -@Mixin(ClientboundSetEntityMotionPacket.class) -public class ClientboundSetEntityMotionPacketMixin { - - @Mutable - @Final - @Shadow - private int id; - - @Mutable - @Final - @Shadow - private int xa; - - @Mutable - @Final - @Shadow - private int ya; - - @Mutable - @Final - @Shadow - private int za; - - // TODO 实现仅对 FastProjectile 对象的动量修改 - @Inject(method = "(ILnet/minecraft/world/phys/Vec3;)V", at = @At(value = "RETURN")) - public void init(int entityId, Vec3 motion, CallbackInfo ci) { - this.xa = (int) (motion.x * 8000.0D); - this.ya = (int) (motion.y * 8000.0D); - this.za = (int) (motion.z * 8000.0D); - } - - @Redirect(method = "(Lnet/minecraft/network/FriendlyByteBuf;)V", - at = @At(value = "INVOKE", target = "Lnet/minecraft/network/FriendlyByteBuf;readShort()S")) - public short readShort(FriendlyByteBuf instance) { - return 0; - } - - @Inject(method = "(Lnet/minecraft/network/FriendlyByteBuf;)V", at = @At(value = "RETURN"), cancellable = true) - public void read(FriendlyByteBuf buf, CallbackInfo ci) { - ci.cancel(); - this.xa = buf.readInt(); - this.ya = buf.readInt(); - this.za = buf.readInt(); - } - - @Inject(method = "Lnet/minecraft/network/protocol/game/ClientboundSetEntityMotionPacket;" + - "write(Lnet/minecraft/network/FriendlyByteBuf;)V", at = @At(value = "HEAD"), cancellable = true) - public void write(FriendlyByteBuf buf, CallbackInfo ci) { - ci.cancel(); - buf.writeVarInt(this.id); - buf.writeInt(this.xa); - buf.writeInt(this.ya); - buf.writeInt(this.za); - } -} diff --git a/src/main/java/com/atsuishio/superbwarfare/network/ClientPacketHandler.java b/src/main/java/com/atsuishio/superbwarfare/network/ClientPacketHandler.java index 0d32f8c95..7a857a980 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/ClientPacketHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/ClientPacketHandler.java @@ -7,10 +7,7 @@ import com.atsuishio.superbwarfare.config.client.KillMessageConfig; import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.event.KillMessageHandler; import com.atsuishio.superbwarfare.menu.EnergyMenu; -import com.atsuishio.superbwarfare.network.message.ClientIndicatorMessage; -import com.atsuishio.superbwarfare.network.message.ContainerDataMessage; -import com.atsuishio.superbwarfare.network.message.GunsDataMessage; -import com.atsuishio.superbwarfare.network.message.RadarMenuOpenMessage; +import com.atsuishio.superbwarfare.network.message.*; import com.atsuishio.superbwarfare.tools.GunsTool; import com.atsuishio.superbwarfare.tools.PlayerKillRecord; import net.minecraft.client.CameraType; @@ -88,4 +85,15 @@ public class ClientPacketHandler { Minecraft.getInstance().options.setCameraType(Objects.requireNonNullElse(ClientEventHandler.lastCameraType, CameraType.FIRST_PERSON)); } } + + public static void handleClientSyncMotion(ClientMotionSyncMessage message, Supplier ctx) { + if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) { + var level = Minecraft.getInstance().level; + if (level == null) return; + Entity entity = level.getEntity(message.id); + if (entity != null) { + entity.lerpMotion(message.x, message.y, message.z); + } + } + } } diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/ClientMotionSyncMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/ClientMotionSyncMessage.java new file mode 100644 index 000000000..aa8b74df4 --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/ClientMotionSyncMessage.java @@ -0,0 +1,51 @@ +package com.atsuishio.superbwarfare.network.message; + +import com.atsuishio.superbwarfare.network.ClientPacketHandler; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.phys.Vec3; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.fml.DistExecutor; +import net.minecraftforge.network.NetworkEvent; + +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 ClientMotionSyncMessage(Entity entity) { + this(entity.getId(), entity.getDeltaMovement()); + } + + public ClientMotionSyncMessage(int id, Vec3 motion) { + this.id = id; + this.x = motion.x; + this.y = motion.y; + this.z = 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); + } + + public static ClientMotionSyncMessage decode(FriendlyByteBuf buffer) { + int id = buffer.readVarInt(); + double x = buffer.readDouble(); + double y = buffer.readDouble(); + double z = buffer.readDouble(); + return new ClientMotionSyncMessage(id, new Vec3(x, y, z)); + } + + public static void handler(ClientMotionSyncMessage message, Supplier ctx) { + ctx.get().enqueueWork(() -> DistExecutor.unsafeRunWhenOn(Dist.CLIENT, + () -> () -> ClientPacketHandler.handleClientSyncMotion(message, ctx))); + ctx.get().setPacketHandled(true); + } +} diff --git a/src/main/resources/mixins.superbwarfare.json b/src/main/resources/mixins.superbwarfare.json index a7b31f989..78d8e979f 100644 --- a/src/main/resources/mixins.superbwarfare.json +++ b/src/main/resources/mixins.superbwarfare.json @@ -4,7 +4,6 @@ "compatibilityLevel": "JAVA_17", "refmap": "mixins.superbwarfare.refmap.json", "mixins": [ - "ClientboundSetEntityMotionPacketMixin", "ClientboundSetPassengersPacketMixin", "ExplosionMixin", "LivingEntityMixin",