添加GunKillMessage和IndicatorMessage

This commit is contained in:
Light_Quanta 2025-03-27 01:41:03 +08:00
parent 03276fb3d8
commit 649ba27765
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
7 changed files with 144 additions and 16 deletions

View file

@ -5,7 +5,9 @@ import com.atsuishio.superbwarfare.config.server.MiscConfig;
import com.atsuishio.superbwarfare.config.server.ProjectileConfig; import com.atsuishio.superbwarfare.config.server.ProjectileConfig;
import com.atsuishio.superbwarfare.entity.ICustomKnockback; import com.atsuishio.superbwarfare.entity.ICustomKnockback;
import com.atsuishio.superbwarfare.init.*; 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.ClientMotionSyncMessage;
import com.atsuishio.superbwarfare.network.message.PlayerGunKillMessage;
import com.atsuishio.superbwarfare.tools.*; import com.atsuishio.superbwarfare.tools.*;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; 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) { if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer serverPlayer) {
var holder = score == 10 ? Holder.direct(ModSounds.HEADSHOT.get()) : Holder.direct(ModSounds.INDICATION.get()); 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())); serverPlayer.connection.send(new ClientboundSoundPacket(holder, SoundSource.PLAYERS, player.getX(), player.getY(), player.getZ(), 1f, 1f, player.level().random.nextLong()));
// TODO indicator message PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(score == 10 ? 1 : 0, 5));
// ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new ClientIndicatorMessage(score == 10 ? 1 : 0, 5));
} }
ItemStack stack = player.getOffhandItem(); ItemStack stack = player.getOffhandItem();
@ -486,15 +487,13 @@ public class ProjectileEntity extends Projectile implements IEntityWithComplexSp
// if (living instanceof TargetEntity) return; // if (living instanceof TargetEntity) return;
if (this.shooter instanceof ServerPlayer player) { if (this.shooter instanceof ServerPlayer player) {
// TODO indicator msg PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(0, 5));
// ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(0, 5));
var holder = Holder.direct(ModSounds.INDICATION.get()); 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())); 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); ((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()) { if (MiscConfig.SEND_KILL_FEEDBACK.get()) {
// TODO kill feedback PacketDistributor.sendToAllPlayers(new PlayerGunKillMessage(player.getId(), living.getId(), false, ModDamageTypes.BEAST));
// ModUtils.PACKET_HANDLER.send(PacketDistributor.ALL.noArg(), 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) { if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer player) {
var holder = Holder.direct(ModSounds.HEADSHOT.get()); 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())); player.connection.send(new ClientboundSoundPacket(holder, SoundSource.PLAYERS, player.getX(), player.getY(), player.getZ(), 1f, 1f, player.level().random.nextLong()));
PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(1, 5));
// TODO indicator msg
// ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(1, 5));
} }
performOnHit(entity, this.damage, true, this.knockback); performOnHit(entity, this.damage, true, this.knockback);
} else { } else {
if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer player) { if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer player) {
var holder = Holder.direct(ModSounds.INDICATION.get()); 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())); player.connection.send(new ClientboundSoundPacket(holder, SoundSource.PLAYERS, player.getX(), player.getY(), player.getZ(), 1f, 1f, player.level().random.nextLong()));
// TODO indicator msg PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(0, 5));
// ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(0, 5));
} }
if (legShot) { if (legShot) {

View file

@ -3,6 +3,7 @@ package com.atsuishio.superbwarfare.mobeffect;
import com.atsuishio.superbwarfare.init.ModDamageTypes; import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.atsuishio.superbwarfare.init.ModMobEffects; import com.atsuishio.superbwarfare.init.ModMobEffects;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.network.message.ClientIndicatorMessage;
import net.minecraft.core.registries.Registries; import net.minecraft.core.registries.Registries;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource; import net.minecraft.sounds.SoundSource;
@ -16,6 +17,7 @@ import net.minecraft.world.entity.LivingEntity;
import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.entity.living.MobEffectEvent; import net.neoforged.neoforge.event.entity.living.MobEffectEvent;
import net.neoforged.neoforge.network.PacketDistributor;
@EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME) @EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME)
public class BurnMobEffect extends MobEffect { public class BurnMobEffect extends MobEffect {
@ -38,8 +40,7 @@ public class BurnMobEffect extends MobEffect {
if (attacker instanceof ServerPlayer player) { if (attacker instanceof ServerPlayer player) {
player.level().playSound(null, player.blockPosition(), ModSounds.INDICATION.get(), SoundSource.VOICE, 1, 1); player.level().playSound(null, player.blockPosition(), ModSounds.INDICATION.get(), SoundSource.VOICE, 1, 1);
// TODO indicator message PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(0, 5));
// ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(0, 5));
} }
return false; return false;

View file

@ -3,6 +3,7 @@ package com.atsuishio.superbwarfare.mobeffect;
import com.atsuishio.superbwarfare.init.ModDamageTypes; import com.atsuishio.superbwarfare.init.ModDamageTypes;
import com.atsuishio.superbwarfare.init.ModMobEffects; import com.atsuishio.superbwarfare.init.ModMobEffects;
import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.init.ModSounds;
import com.atsuishio.superbwarfare.network.message.ClientIndicatorMessage;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer; 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.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.entity.living.LivingDamageEvent; import net.neoforged.neoforge.event.entity.living.LivingDamageEvent;
import net.neoforged.neoforge.event.entity.living.MobEffectEvent; import net.neoforged.neoforge.event.entity.living.MobEffectEvent;
import net.neoforged.neoforge.network.PacketDistributor;
@EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME) @EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME)
public class ShockMobEffect extends MobEffect { public class ShockMobEffect extends MobEffect {
@ -43,8 +45,7 @@ public class ShockMobEffect extends MobEffect {
if (attacker instanceof ServerPlayer player) { if (attacker instanceof ServerPlayer player) {
player.level().playSound(null, player.blockPosition(), ModSounds.INDICATION.get(), SoundSource.VOICE, 1, 1); player.level().playSound(null, player.blockPosition(), ModSounds.INDICATION.get(), SoundSource.VOICE, 1, 1);
// TODO indicator message PacketDistributor.sendToPlayer(player, new ClientIndicatorMessage(0, 5));
// ModUtils.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new ClientIndicatorMessage(0, 5));
} }
return false; return false;
} }

View file

@ -11,6 +11,9 @@ public class NetworkRegistry {
registrar.playToClient(PlayerVariablesSyncMessage.TYPE, PlayerVariablesSyncMessage.STREAM_CODEC, PlayerVariablesSyncMessage::handler); registrar.playToClient(PlayerVariablesSyncMessage.TYPE, PlayerVariablesSyncMessage.STREAM_CODEC, PlayerVariablesSyncMessage::handler);
registrar.playToClient(ShakeClientMessage.TYPE, ShakeClientMessage.STREAM_CODEC, ShakeClientMessage::handler); registrar.playToClient(ShakeClientMessage.TYPE, ShakeClientMessage.STREAM_CODEC, ShakeClientMessage::handler);
registrar.playToClient(ClientMotionSyncMessage.TYPE, ClientMotionSyncMessage.STREAM_CODEC, ClientMotionSyncMessage::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(LaserShootMessage.TYPE, LaserShootMessage.STREAM_CODEC, LaserShootMessage::handler);
registrar.playToServer(BreathMessage.TYPE, BreathMessage.STREAM_CODEC, BreathMessage::handler); registrar.playToServer(BreathMessage.TYPE, BreathMessage.STREAM_CODEC, BreathMessage::handler);

View file

@ -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<ClientIndicatorMessage> TYPE = new Type<>(ModUtils.loc("client_indicator"));
public static final StreamCodec<ByteBuf, ClientIndicatorMessage> 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<? extends CustomPacketPayload> type() {
return TYPE;
}
}

View file

@ -12,7 +12,7 @@ import org.jetbrains.annotations.NotNull;
public record ClientMotionSyncMessage(int id, float x, float y, float z) implements CustomPacketPayload { public record ClientMotionSyncMessage(int id, float x, float y, float z) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<ClientMotionSyncMessage> TYPE = new CustomPacketPayload.Type<>(ModUtils.loc("breath")); public static final CustomPacketPayload.Type<ClientMotionSyncMessage> TYPE = new CustomPacketPayload.Type<>(ModUtils.loc("client_motion_sync"));
public static final StreamCodec<ByteBuf, ClientMotionSyncMessage> STREAM_CODEC = StreamCodec.composite( public static final StreamCodec<ByteBuf, ClientMotionSyncMessage> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT, ByteBufCodecs.INT,

View file

@ -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<DamageType> getDamageType() {
return ResourceKey.create(Registries.DAMAGE_TYPE, this.location);
}
public ResourceLocation getLocation() {
return location;
}
public PlayerGunKillMessage(int attackerId, int targetId, boolean headshot, ResourceKey<DamageType> 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<PlayerGunKillMessage> TYPE = new Type<>(ModUtils.loc("player_gun_kill"));
public static final StreamCodec<ByteBuf, PlayerGunKillMessage> 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<? extends CustomPacketPayload> type() {
return TYPE;
}
}