修改意外停止音效的问题

This commit is contained in:
17146 2024-05-25 00:01:32 +08:00
parent 48cf5f5e7c
commit 16fe722935

View file

@ -13,7 +13,6 @@ import net.mcreator.target.network.message.PlayerGunKillMessage;
import net.mcreator.target.tools.SoundTool; import net.mcreator.target.tools.SoundTool;
import net.minecraft.network.protocol.game.ClientboundStopSoundPacket; import net.minecraft.network.protocol.game.ClientboundStopSoundPacket;
import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource; import net.minecraft.sounds.SoundSource;
import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.damagesource.DamageSource;
@ -155,14 +154,14 @@ public class LivingEventHandler {
@SubscribeEvent @SubscribeEvent
public static void handleChangeSlot(LivingEquipmentChangeEvent event) { public static void handleChangeSlot(LivingEquipmentChangeEvent event) {
if (event.getEntity() instanceof Player player && event.getSlot() == EquipmentSlot.MAINHAND) { if (event.getEntity() instanceof Player player && event.getSlot() == EquipmentSlot.MAINHAND) {
if (player.level().isClientSide || player.level().getServer() == null) { if (player.level().isClientSide) {
return; return;
} }
ItemStack oldStack = event.getFrom(); ItemStack oldStack = event.getFrom();
ItemStack newStack = event.getTo(); ItemStack newStack = event.getTo();
if (player.level() instanceof ServerLevel serverLevel) { if (player instanceof ServerPlayer serverPlayer) {
var newTag = newStack.getTag(); var newTag = newStack.getTag();
var oldTag = oldStack.getTag(); var oldTag = oldStack.getTag();
@ -176,7 +175,7 @@ public class LivingEventHandler {
} }
if (oldStack.getItem() instanceof GunItem oldGun) { if (oldStack.getItem() instanceof GunItem oldGun) {
stopGunReloadSound(serverLevel, oldGun); stopGunReloadSound(serverPlayer, oldGun);
} }
player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
@ -195,10 +194,10 @@ public class LivingEventHandler {
} }
} }
private static void stopGunReloadSound(ServerLevel server, GunItem gun) { private static void stopGunReloadSound(ServerPlayer player, GunItem gun) {
gun.getReloadSound().forEach(sound -> { gun.getReloadSound().forEach(sound -> {
var clientboundstopsoundpacket = new ClientboundStopSoundPacket(sound.getLocation(), SoundSource.PLAYERS); var clientboundstopsoundpacket = new ClientboundStopSoundPacket(sound.getLocation(), SoundSource.PLAYERS);
server.players().forEach(p -> p.connection.send(clientboundstopsoundpacket)); player.connection.send(clientboundstopsoundpacket);
}); });
} }