From 649ba277652026ba676cd7c8975dfb2f6a08c17a Mon Sep 17 00:00:00 2001 From: Light_Quanta Date: Thu, 27 Mar 2025 01:41:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0GunKillMessage=E5=92=8CIndica?= =?UTF-8?q?torMessage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/projectile/ProjectileEntity.java | 18 ++-- .../mobeffect/BurnMobEffect.java | 5 +- .../mobeffect/ShockMobEffect.java | 5 +- .../network/NetworkRegistry.java | 3 + .../message/ClientIndicatorMessage.java | 31 ++++++ .../message/ClientMotionSyncMessage.java | 2 +- .../network/message/PlayerGunKillMessage.java | 96 +++++++++++++++++++ 7 files changed, 144 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/atsuishio/superbwarfare/network/message/ClientIndicatorMessage.java create mode 100644 src/main/java/com/atsuishio/superbwarfare/network/message/PlayerGunKillMessage.java 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 0a13a650b..ea6faabee 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/ProjectileEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/ProjectileEntity.java @@ -5,7 +5,9 @@ import com.atsuishio.superbwarfare.config.server.MiscConfig; import com.atsuishio.superbwarfare.config.server.ProjectileConfig; import com.atsuishio.superbwarfare.entity.ICustomKnockback; import com.atsuishio.superbwarfare.init.*; +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; import net.minecraft.core.Direction; @@ -422,8 +424,7 @@ public class ProjectileEntity extends Projectile implements IEntityWithComplexSp if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer serverPlayer) { var holder = score == 10 ? Holder.direct(ModSounds.HEADSHOT.get()) : Holder.direct(ModSounds.INDICATION.get()); serverPlayer.connection.send(new ClientboundSoundPacket(holder, SoundSource.PLAYERS, player.getX(), player.getY(), player.getZ(), 1f, 1f, player.level().random.nextLong())); - // TODO indicator message - // ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new ClientIndicatorMessage(score == 10 ? 1 : 0, 5)); + PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(score == 10 ? 1 : 0, 5)); } ItemStack stack = player.getOffhandItem(); @@ -486,15 +487,13 @@ public class ProjectileEntity extends Projectile implements IEntityWithComplexSp // if (living instanceof TargetEntity) return; if (this.shooter instanceof ServerPlayer player) { - // TODO indicator msg - // ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(0, 5)); + PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(0, 5)); var holder = Holder.direct(ModSounds.INDICATION.get()); player.connection.send(new ClientboundSoundPacket(holder, SoundSource.PLAYERS, player.getX(), player.getY(), player.getZ(), 1f, 1f, player.level().random.nextLong())); ((ServerLevel) this.level()).sendParticles(ParticleTypes.DAMAGE_INDICATOR, living.getX(), living.getY() + .5, living.getZ(), 1000, .4, .7, .4, 0); if (MiscConfig.SEND_KILL_FEEDBACK.get()) { - // TODO kill feedback - // ModUtils.PACKET_HANDLER.send(PacketDistributor.ALL.noArg(), new PlayerGunKillMessage(player.getId(), living.getId(), false, ModDamageTypes.BEAST)); + PacketDistributor.sendToAllPlayers(new PlayerGunKillMessage(player.getId(), living.getId(), false, ModDamageTypes.BEAST)); } } @@ -545,17 +544,14 @@ public class ProjectileEntity extends Projectile implements IEntityWithComplexSp if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer player) { var holder = Holder.direct(ModSounds.HEADSHOT.get()); player.connection.send(new ClientboundSoundPacket(holder, SoundSource.PLAYERS, player.getX(), player.getY(), player.getZ(), 1f, 1f, player.level().random.nextLong())); - - // TODO indicator msg - // ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(1, 5)); + PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(1, 5)); } performOnHit(entity, this.damage, true, this.knockback); } else { if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer player) { var holder = Holder.direct(ModSounds.INDICATION.get()); player.connection.send(new ClientboundSoundPacket(holder, SoundSource.PLAYERS, player.getX(), player.getY(), player.getZ(), 1f, 1f, player.level().random.nextLong())); - // TODO indicator msg - // ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(0, 5)); + PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(0, 5)); } if (legShot) { diff --git a/src/main/java/com/atsuishio/superbwarfare/mobeffect/BurnMobEffect.java b/src/main/java/com/atsuishio/superbwarfare/mobeffect/BurnMobEffect.java index 1305e8e27..768aea81d 100644 --- a/src/main/java/com/atsuishio/superbwarfare/mobeffect/BurnMobEffect.java +++ b/src/main/java/com/atsuishio/superbwarfare/mobeffect/BurnMobEffect.java @@ -3,6 +3,7 @@ package com.atsuishio.superbwarfare.mobeffect; import com.atsuishio.superbwarfare.init.ModDamageTypes; import com.atsuishio.superbwarfare.init.ModMobEffects; import com.atsuishio.superbwarfare.init.ModSounds; +import com.atsuishio.superbwarfare.network.message.ClientIndicatorMessage; import net.minecraft.core.registries.Registries; import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundSource; @@ -16,6 +17,7 @@ import net.minecraft.world.entity.LivingEntity; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.neoforge.event.entity.living.MobEffectEvent; +import net.neoforged.neoforge.network.PacketDistributor; @EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME) public class BurnMobEffect extends MobEffect { @@ -38,8 +40,7 @@ public class BurnMobEffect extends MobEffect { if (attacker instanceof ServerPlayer player) { player.level().playSound(null, player.blockPosition(), ModSounds.INDICATION.get(), SoundSource.VOICE, 1, 1); - // TODO indicator message - // ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(0, 5)); + PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(0, 5)); } return false; diff --git a/src/main/java/com/atsuishio/superbwarfare/mobeffect/ShockMobEffect.java b/src/main/java/com/atsuishio/superbwarfare/mobeffect/ShockMobEffect.java index 648fc5e94..317179bf6 100644 --- a/src/main/java/com/atsuishio/superbwarfare/mobeffect/ShockMobEffect.java +++ b/src/main/java/com/atsuishio/superbwarfare/mobeffect/ShockMobEffect.java @@ -3,6 +3,7 @@ package com.atsuishio.superbwarfare.mobeffect; import com.atsuishio.superbwarfare.init.ModDamageTypes; import com.atsuishio.superbwarfare.init.ModMobEffects; import com.atsuishio.superbwarfare.init.ModSounds; +import com.atsuishio.superbwarfare.network.message.ClientIndicatorMessage; import net.minecraft.core.BlockPos; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; @@ -20,6 +21,7 @@ import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.neoforge.event.entity.living.LivingDamageEvent; import net.neoforged.neoforge.event.entity.living.MobEffectEvent; +import net.neoforged.neoforge.network.PacketDistributor; @EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME) public class ShockMobEffect extends MobEffect { @@ -43,8 +45,7 @@ public class ShockMobEffect extends MobEffect { if (attacker instanceof ServerPlayer player) { player.level().playSound(null, player.blockPosition(), ModSounds.INDICATION.get(), SoundSource.VOICE, 1, 1); - // TODO indicator message - // ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(0, 5)); + PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(0, 5)); } return false; } diff --git a/src/main/java/com/atsuishio/superbwarfare/network/NetworkRegistry.java b/src/main/java/com/atsuishio/superbwarfare/network/NetworkRegistry.java index f5acfd669..c47afd285 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/NetworkRegistry.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/NetworkRegistry.java @@ -11,6 +11,9 @@ public class NetworkRegistry { registrar.playToClient(PlayerVariablesSyncMessage.TYPE, PlayerVariablesSyncMessage.STREAM_CODEC, PlayerVariablesSyncMessage::handler); registrar.playToClient(ShakeClientMessage.TYPE, ShakeClientMessage.STREAM_CODEC, ShakeClientMessage::handler); registrar.playToClient(ClientMotionSyncMessage.TYPE, ClientMotionSyncMessage.STREAM_CODEC, ClientMotionSyncMessage::handler); + registrar.playToClient(ClientIndicatorMessage.TYPE, ClientIndicatorMessage.STREAM_CODEC, ClientIndicatorMessage::handler); + registrar.playToClient(PlayerGunKillMessage.TYPE, PlayerGunKillMessage.STREAM_CODEC, PlayerGunKillMessage::handler); + registrar.playToServer(LaserShootMessage.TYPE, LaserShootMessage.STREAM_CODEC, LaserShootMessage::handler); registrar.playToServer(BreathMessage.TYPE, BreathMessage.STREAM_CODEC, BreathMessage::handler); diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/ClientIndicatorMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/ClientIndicatorMessage.java new file mode 100644 index 000000000..2f619a81f --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/ClientIndicatorMessage.java @@ -0,0 +1,31 @@ +package com.atsuishio.superbwarfare.network.message; + +import com.atsuishio.superbwarfare.ModUtils; +import io.netty.buffer.ByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.neoforged.neoforge.network.handling.IPayloadContext; +import org.jetbrains.annotations.NotNull; + +public record ClientIndicatorMessage(int messageType, int value) implements CustomPacketPayload { + public static final Type TYPE = new Type<>(ModUtils.loc("client_indicator")); + + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + ByteBufCodecs.INT, + ClientIndicatorMessage::messageType, + ByteBufCodecs.INT, + ClientIndicatorMessage::value, + ClientIndicatorMessage::new + ); + + public static void handler(final ClientIndicatorMessage message, final IPayloadContext context) { + // TODO indicator process +// ClientPacketHandler.handleClientIndicatorMessage(message, ctx); + } + + @Override + public @NotNull Type type() { + return TYPE; + } +} 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 ba332a496..53a72d207 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/ClientMotionSyncMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/ClientMotionSyncMessage.java @@ -12,7 +12,7 @@ import org.jetbrains.annotations.NotNull; public record ClientMotionSyncMessage(int id, float x, float y, float z) implements CustomPacketPayload { - public static final CustomPacketPayload.Type TYPE = new CustomPacketPayload.Type<>(ModUtils.loc("breath")); + public static final CustomPacketPayload.Type TYPE = new CustomPacketPayload.Type<>(ModUtils.loc("client_motion_sync")); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( ByteBufCodecs.INT, diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/PlayerGunKillMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/PlayerGunKillMessage.java new file mode 100644 index 000000000..d1af88d8c --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/PlayerGunKillMessage.java @@ -0,0 +1,96 @@ +package com.atsuishio.superbwarfare.network.message; + +import com.atsuishio.superbwarfare.ModUtils; +import io.netty.buffer.ByteBuf; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientLevel; +import net.minecraft.core.registries.Registries; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.damagesource.DamageType; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.player.Player; +import net.neoforged.neoforge.network.handling.IPayloadContext; +import org.jetbrains.annotations.NotNull; + +public class PlayerGunKillMessage implements CustomPacketPayload { + private final int attackerId; + private final int targetId; + + private final boolean headshot; + private final ResourceLocation location; + + + public int getAttackerId() { + return attackerId; + } + + public boolean isHeadshot() { + return headshot; + } + + public int getTargetId() { + return targetId; + } + + public ResourceKey getDamageType() { + return ResourceKey.create(Registries.DAMAGE_TYPE, this.location); + } + + public ResourceLocation getLocation() { + return location; + } + + + public PlayerGunKillMessage(int attackerId, int targetId, boolean headshot, ResourceKey damageType) { + this.attackerId = attackerId; + this.targetId = targetId; + this.headshot = headshot; + this.location = damageType.location(); + } + + public PlayerGunKillMessage(int attackerId, int targetId, boolean headshot, ResourceLocation location) { + this.attackerId = attackerId; + this.targetId = targetId; + this.headshot = headshot; + this.location = location; + } + + public static final Type TYPE = new Type<>(ModUtils.loc("player_gun_kill")); + + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + ByteBufCodecs.INT, + PlayerGunKillMessage::getAttackerId, + ByteBufCodecs.INT, + PlayerGunKillMessage::getTargetId, + ByteBufCodecs.BOOL, + PlayerGunKillMessage::isHeadshot, + ResourceLocation.STREAM_CODEC, + PlayerGunKillMessage::getLocation, + PlayerGunKillMessage::new + ); + + + public static void handler(PlayerGunKillMessage message, final IPayloadContext context) { + ClientLevel level = Minecraft.getInstance().level; + if (level != null) { + Player player = level.getEntity(message.attackerId) instanceof Player ? (Player) level.getEntity(message.attackerId) : null; + Entity target = level.getEntity(message.targetId); + + if (player != null && target != null) { + var type = message.getDamageType(); + + // TODO 处理kill message + // DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientPacketHandler.handlePlayerKillMessage(player, target, message.headshot, type, ctx)); + } + } + } + + @Override + public @NotNull Type type() { + return TYPE; + } +}