diff --git a/src/main/java/com/atsuishio/superbwarfare/event/GunEventHandler.java b/src/main/java/com/atsuishio/superbwarfare/event/GunEventHandler.java index 2af78e52d..b0688300b 100644 --- a/src/main/java/com/atsuishio/superbwarfare/event/GunEventHandler.java +++ b/src/main/java/com/atsuishio/superbwarfare/event/GunEventHandler.java @@ -2,12 +2,13 @@ package com.atsuishio.superbwarfare.event; import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.event.events.ReloadEvent; -import com.atsuishio.superbwarfare.init.*; +import com.atsuishio.superbwarfare.init.ModAttachments; +import com.atsuishio.superbwarfare.init.ModItems; +import com.atsuishio.superbwarfare.init.ModSounds; +import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.data.GunData; -import com.atsuishio.superbwarfare.item.gun.data.value.AttachmentType; import com.atsuishio.superbwarfare.item.gun.data.value.ReloadState; -import com.atsuishio.superbwarfare.perk.Perk; import com.atsuishio.superbwarfare.tools.Ammo; import com.atsuishio.superbwarfare.tools.InventoryTool; import com.atsuishio.superbwarfare.tools.SoundTool; @@ -67,71 +68,6 @@ public class GunEventHandler { } } - /** - * 根据武器的注册名来寻找音效并播放 - */ - public static void playGunSounds(Player player, boolean zoom) { - ItemStack stack = player.getMainHandItem(); - if (!(stack.getItem() instanceof GunItem)) return; - var data = GunData.from(stack); - - if (!player.level().isClientSide) { - String origin = stack.getItem().getDescriptionId(); - String name = origin.substring(origin.lastIndexOf(".") + 1); - - if (stack.getItem() == ModItems.SENTINEL.get()) { - var cap = stack.getCapability(Capabilities.EnergyStorage.ITEM); - - if (cap != null && cap.getEnergyStored() > 0) { - float soundRadius = (float) data.soundRadius(); - - player.playSound(ModSounds.SENTINEL_CHARGE_FAR.get(), soundRadius * 0.7f, 1f); - player.playSound(ModSounds.SENTINEL_CHARGE_FIRE_3P.get(), soundRadius * 0.4f, 1f); - player.playSound(ModSounds.SENTINEL_CHARGE_VERYFAR.get(), soundRadius, 1f); - - return; - } - } - - if (stack.getItem() == ModItems.SECONDARY_CATACLYSM.get()) { - var cap = stack.getCapability(Capabilities.EnergyStorage.ITEM); - - if (cap != null && cap.getEnergyStored() > 3000 && zoom) { - float soundRadius = (float) data.soundRadius(); - - player.playSound(ModSounds.SECONDARY_CATACLYSM_FIRE_3P_CHARGE.get(), soundRadius * 0.4f, 1f); - player.playSound(ModSounds.SECONDARY_CATACLYSM_FAR_CHARGE.get(), soundRadius * 0.7f, 1f); - player.playSound(ModSounds.SECONDARY_CATACLYSM_VERYFAR_CHARGE.get(), soundRadius, 1f); - - return; - } - } - - var perk = data.perk.get(Perk.Type.AMMO); - if (perk == ModPerks.BEAST_BULLET.get()) { - player.playSound(ModSounds.HENG.get(), 4f, 1f); - } - - float soundRadius = (float) data.soundRadius(); - int barrelType = data.attachment.get(AttachmentType.BARREL); - - SoundEvent sound3p = BuiltInRegistries.SOUND_EVENT.get(Mod.loc(name + (barrelType == 2 ? "_fire_3p_s" : "_fire_3p"))); - if (sound3p != null) { - player.playSound(sound3p, soundRadius * 0.4f, 1f); - } - - SoundEvent soundFar = BuiltInRegistries.SOUND_EVENT.get(Mod.loc(name + (barrelType == 2 ? "_far_s" : "_far"))); - if (soundFar != null) { - player.playSound(soundFar, soundRadius * 0.7f, 1f); - } - - SoundEvent soundVeryFar = BuiltInRegistries.SOUND_EVENT.get(Mod.loc(name + (barrelType == 2 ? "_veryfar_s" : "_veryfar"))); - if (soundVeryFar != null) { - player.playSound(soundVeryFar, soundRadius, 1f); - } - } - } - public static void playGunBoltSounds(Player player) { ItemStack stack = player.getMainHandItem(); if (!(stack.getItem() instanceof GunItem)) return; diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/GunItem.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/GunItem.java index 4a43fb3d8..c8d886304 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/GunItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/GunItem.java @@ -4,11 +4,7 @@ import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.client.PoseTool; import com.atsuishio.superbwarfare.client.tooltip.component.GunImageComponent; import com.atsuishio.superbwarfare.entity.projectile.ProjectileEntity; -import com.atsuishio.superbwarfare.event.GunEventHandler; -import com.atsuishio.superbwarfare.init.ModAttachments; -import com.atsuishio.superbwarfare.init.ModItems; -import com.atsuishio.superbwarfare.init.ModPerks; -import com.atsuishio.superbwarfare.init.ModTags; +import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.item.CustomRendererItem; import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.item.gun.data.value.AttachmentType; @@ -19,6 +15,7 @@ import net.minecraft.client.model.HumanoidModel; import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer; import net.minecraft.core.BlockPos; import net.minecraft.core.Holder; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvent; import net.minecraft.world.InteractionHand; @@ -536,9 +533,41 @@ public abstract class GunItem extends Item implements CustomRendererItem { shootBullet(player, data, spread, zoom); } - // TODO 提取对应方法 - // 播放音效 - GunEventHandler.playGunSounds(player, zoom); + playFireSounds(data, player, zoom); + } + + /** + * 播放开火音效 + */ + public void playFireSounds(GunData data, Player player, boolean zoom) { + ItemStack stack = data.stack; + if (!(stack.getItem() instanceof GunItem)) return; + + String origin = stack.getItem().getDescriptionId(); + String name = origin.substring(origin.lastIndexOf(".") + 1); + + var perk = data.perk.get(Perk.Type.AMMO); + if (perk == ModPerks.BEAST_BULLET.get()) { + player.playSound(ModSounds.HENG.get(), 4f, 1f); + } + + float soundRadius = (float) data.soundRadius(); + int barrelType = data.attachment.get(AttachmentType.BARREL); + + SoundEvent sound3p = BuiltInRegistries.SOUND_EVENT.get(Mod.loc(name + (barrelType == 2 ? "_fire_3p_s" : "_fire_3p"))); + if (sound3p != null) { + player.playSound(sound3p, soundRadius * 0.4f, 1f); + } + + SoundEvent soundFar = BuiltInRegistries.SOUND_EVENT.get(Mod.loc(name + (barrelType == 2 ? "_far_s" : "_far"))); + if (soundFar != null) { + player.playSound(soundFar, soundRadius * 0.7f, 1f); + } + + SoundEvent soundVeryFar = BuiltInRegistries.SOUND_EVENT.get(Mod.loc(name + (barrelType == 2 ? "_veryfar_s" : "_veryfar"))); + if (soundVeryFar != null) { + player.playSound(soundVeryFar, soundRadius, 1f); + } } /** diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/JavelinItem.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/JavelinItem.java index 1e6e4cb98..7e9702359 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/JavelinItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/JavelinItem.java @@ -268,6 +268,10 @@ public class JavelinItem extends GunItem implements GeoItem { data.save(); } + @Override + public void onShoot(GunData data, Player player, double spread, boolean zoom) { + } + @Override public void onFireKeyRelease(GunData data, Player player, double power, boolean zoom) { super.onFireKeyRelease(data, player, power, zoom); diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/SecondaryCataclysm.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/SecondaryCataclysm.java index f560f9af6..d38c09742 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/SecondaryCataclysm.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/launcher/SecondaryCataclysm.java @@ -8,6 +8,7 @@ import com.atsuishio.superbwarfare.event.ClientEventHandler; import com.atsuishio.superbwarfare.init.ModEnumExtensions; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModPerks; +import com.atsuishio.superbwarfare.init.ModSounds; import com.atsuishio.superbwarfare.item.EnergyStorageItem; import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.data.GunData; @@ -283,6 +284,21 @@ public class SecondaryCataclysm extends GunItem implements GeoItem, EnergyStorag } } + @Override + public void playFireSounds(GunData data, Player player, boolean zoom) { + var cap = data.stack.getCapability(Capabilities.EnergyStorage.ITEM); + + if (cap != null && cap.getEnergyStored() > 3000 && zoom) { + float soundRadius = (float) data.soundRadius(); + + player.playSound(ModSounds.SECONDARY_CATACLYSM_FIRE_3P_CHARGE.get(), soundRadius * 0.4f, 1f); + player.playSound(ModSounds.SECONDARY_CATACLYSM_FAR_CHARGE.get(), soundRadius * 0.7f, 1f); + player.playSound(ModSounds.SECONDARY_CATACLYSM_VERYFAR_CHARGE.get(), soundRadius, 1f); + } else { + super.playFireSounds(data, player, zoom); + } + } + @Override public int getMaxEnergy() { return 24000; diff --git a/src/main/java/com/atsuishio/superbwarfare/item/gun/sniper/SentinelItem.java b/src/main/java/com/atsuishio/superbwarfare/item/gun/sniper/SentinelItem.java index 080f52643..1182b40a1 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/gun/sniper/SentinelItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/gun/sniper/SentinelItem.java @@ -200,6 +200,21 @@ public class SentinelItem extends GunItem implements GeoItem, EnergyStorageItem } } + @Override + public void playFireSounds(GunData data, Player player, boolean zoom) { + var cap = data.stack.getCapability(Capabilities.EnergyStorage.ITEM); + + if (cap != null && cap.getEnergyStored() > 0) { + float soundRadius = (float) data.soundRadius(); + + player.playSound(ModSounds.SENTINEL_CHARGE_FAR.get(), soundRadius * 0.7f, 1f); + player.playSound(ModSounds.SENTINEL_CHARGE_FIRE_3P.get(), soundRadius * 0.4f, 1f); + player.playSound(ModSounds.SENTINEL_CHARGE_VERYFAR.get(), soundRadius, 1f); + } else { + super.playFireSounds(data, player, zoom); + } + } + @Override public int getMaxEnergy() { return 24000;