From 0bdf98346d8b5b504535d64e8e51922fc423196e Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Sun, 19 May 2024 20:33:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0SoundTool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../target/event/GunEventHandler.java | 16 +++------ .../net/mcreator/target/tools/SoundTool.java | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/main/java/net/mcreator/target/tools/SoundTool.java diff --git a/src/main/java/net/mcreator/target/event/GunEventHandler.java b/src/main/java/net/mcreator/target/event/GunEventHandler.java index ece93420e..0f114a834 100644 --- a/src/main/java/net/mcreator/target/event/GunEventHandler.java +++ b/src/main/java/net/mcreator/target/event/GunEventHandler.java @@ -6,15 +6,13 @@ import net.mcreator.target.init.TargetModAttributes; import net.mcreator.target.init.TargetModItems; import net.mcreator.target.init.TargetModTags; import net.mcreator.target.network.TargetModVariables; +import net.mcreator.target.tools.SoundTool; import net.minecraft.commands.CommandSource; import net.minecraft.commands.CommandSourceStack; -import net.minecraft.core.Holder; -import net.minecraft.network.protocol.game.ClientboundSoundPacket; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundEvent; -import net.minecraft.sounds.SoundSource; import net.minecraft.world.InteractionHand; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; @@ -229,8 +227,7 @@ public class GunEventHandler { if (player.getMainHandItem().getItem() == TargetModItems.SENTINEL.get() && stack.getOrCreateTag().getDouble("power") > 0) { SoundEvent sound1p = ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation(TargetMod.MODID, name + "_charge_fire_1p")); if (sound1p != null && player instanceof ServerPlayer serverPlayer) { - serverPlayer.connection.send(new ClientboundSoundPacket(new Holder.Direct<>(sound1p), - SoundSource.PLAYERS, serverPlayer.getX(), serverPlayer.getY(), serverPlayer.getZ(), 2f, 1f, serverPlayer.level().random.nextLong())); + SoundTool.playLocalSound(serverPlayer, sound1p, 2f, 1f); } SoundEvent sound3p = ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation(TargetMod.MODID, name + "v_fire_3p")); @@ -246,14 +243,11 @@ public class GunEventHandler { SoundEvent soundVeryFar = ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation(TargetMod.MODID, name + "_charge_veryfar")); if (soundVeryFar != null) { player.playSound(soundVeryFar, 24f, 1f); - } - } else { SoundEvent sound1p = ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation(TargetMod.MODID, name + "_fire_1p")); if (sound1p != null && player instanceof ServerPlayer serverPlayer) { - serverPlayer.connection.send(new ClientboundSoundPacket(new Holder.Direct<>(sound1p), - SoundSource.PLAYERS, serverPlayer.getX(), serverPlayer.getY(), serverPlayer.getZ(), 2f, 1f, serverPlayer.level().random.nextLong())); + SoundTool.playLocalSound(serverPlayer, sound1p, 2f, 1f); } SoundEvent sound3p = ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation(TargetMod.MODID, name + "_fire_3p")); @@ -269,7 +263,6 @@ public class GunEventHandler { SoundEvent soundVeryFar = ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation(TargetMod.MODID, name + "_veryfar")); if (soundVeryFar != null) { player.playSound(soundVeryFar, 24f, 1f); - } } @@ -288,8 +281,7 @@ public class GunEventHandler { SoundEvent sound1p = ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation(TargetMod.MODID, name + "_bolt")); if (sound1p != null && player instanceof ServerPlayer serverPlayer) { - serverPlayer.connection.send(new ClientboundSoundPacket(new Holder.Direct<>(sound1p), - SoundSource.PLAYERS, serverPlayer.getX(), serverPlayer.getY(), serverPlayer.getZ(), 2f, 1f, serverPlayer.level().random.nextLong())); + SoundTool.playLocalSound(serverPlayer, sound1p, 2f, 1f); } } } diff --git a/src/main/java/net/mcreator/target/tools/SoundTool.java b/src/main/java/net/mcreator/target/tools/SoundTool.java new file mode 100644 index 000000000..889961ec8 --- /dev/null +++ b/src/main/java/net/mcreator/target/tools/SoundTool.java @@ -0,0 +1,33 @@ +package net.mcreator.target.tools; + +import net.minecraft.core.Holder; +import net.minecraft.network.protocol.game.ClientboundSoundPacket; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.sounds.SoundEvent; +import net.minecraft.sounds.SoundSource; +import net.minecraft.world.entity.player.Player; + +public class SoundTool { + public static void playLocalSound(Player player, SoundEvent sound) { + playLocalSound(player, sound, 1.0F, 1.0F); + } + + public static void playLocalSound(Player player, SoundEvent sound, float volume, float pitch) { + if (player instanceof ServerPlayer serverPlayer) { + playLocalSound(serverPlayer, sound, volume, pitch); + } + } + + public static void playLocalSound(ServerPlayer player, SoundEvent sound) { + playLocalSound(player, sound, 1.0F, 1.0F); + } + + public static void playLocalSound(ServerPlayer player, SoundEvent sound, float volume, float pitch) { + playLocalSound(player, sound, SoundSource.PLAYERS, volume, pitch); + } + + public static void playLocalSound(ServerPlayer player, SoundEvent sound, SoundSource source, float volume, float pitch) { + player.connection.send(new ClientboundSoundPacket(new Holder.Direct<>(sound), + source, player.getX(), player.getY(), player.getZ(), volume, pitch, player.level().random.nextLong())); + } +}