diff --git a/src/main/java/noppes/npcs/client/ClientTickHandler.java b/src/main/java/noppes/npcs/client/ClientTickHandler.java index 66c68bffa..8938ed01e 100644 --- a/src/main/java/noppes/npcs/client/ClientTickHandler.java +++ b/src/main/java/noppes/npcs/client/ClientTickHandler.java @@ -8,6 +8,8 @@ import cpw.mods.fml.common.gameevent.TickEvent.Phase; import kamkeel.addon.DBCAddon; import kamkeel.addon.client.DBCClient; import net.minecraft.client.Minecraft; +import net.minecraft.client.audio.SoundHandler; +import net.minecraft.client.audio.SoundManager; import net.minecraft.entity.Entity; import net.minecraft.inventory.ContainerPlayer; import net.minecraft.world.World; @@ -57,7 +59,7 @@ public class ClientTickHandler{ RenderNPCInterface.LastTextureTick++; if(prevWorld != mc.theWorld){ prevWorld = mc.theWorld; - MusicController.Instance.stopMusic(); + MusicController.Instance.stopAllSounds(); } else if (MusicController.Instance.isPlaying() && MusicController.Instance.getEntity() != null) { Entity entity = MusicController.Instance.getEntity(); if (MusicController.Instance.getOffRange() > 0 && diff --git a/src/main/java/noppes/npcs/client/controllers/MusicController.java b/src/main/java/noppes/npcs/client/controllers/MusicController.java index 9d131adc1..d41bd84c9 100644 --- a/src/main/java/noppes/npcs/client/controllers/MusicController.java +++ b/src/main/java/noppes/npcs/client/controllers/MusicController.java @@ -6,8 +6,14 @@ import net.minecraft.client.audio.SoundHandler; import net.minecraft.entity.Entity; import net.minecraft.util.ResourceLocation; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; + public class MusicController { public static MusicController Instance; + private final HashSet sounds = new HashSet<>(); + private int playDelay; public ScriptClientSound playingSound; private Entity entity; @@ -30,6 +36,15 @@ public class MusicController { this.playMusicJukebox(sound, this.entity, this.offRange); } } + + SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler(); + this.sounds.removeIf(sound -> !soundHandler.isSoundPlaying(sound) && !sound.canRepeat() && !sound.paused); + + if (this.playDelay > 0) { + this.playDelay--; + } else if (Minecraft.getMinecraft().currentScreen != null) { + this.playDelay = 20; + } } public void stopMusic(){ @@ -42,7 +57,12 @@ public class MusicController { } public void playMusicJukebox(String music, Entity entity, int offRange){ - if (this.isPlaying(music)) { + if (this.playDelay > 0) { + return; + } + this.playDelay = 10; + + if (this.isPlaying(music)) { return; } this.stopMusic(); @@ -55,12 +75,22 @@ public class MusicController { this.playingSound = clientSound; this.entity = entity; this.offRange = offRange; + this.sounds.add(clientSound); - SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler(); - soundHandler.playSound(clientSound); + try { + SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler(); + soundHandler.playSound(clientSound); + } catch (Exception e) { + this.stopAllSounds(); + } } public void playMusicBackground(String music, Entity entity, int offRange) { + if (this.playDelay > 0) { + return; + } + this.playDelay = 10; + if (this.isPlaying(music)) { return; } @@ -72,11 +102,38 @@ public class MusicController { this.playingSound = clientSound; this.entity = entity; this.offRange = offRange; + this.sounds.add(clientSound); - SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler(); - soundHandler.playSound(clientSound); + try { + SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler(); + soundHandler.playSound(clientSound); + } catch (Exception e) { + this.stopAllSounds(); + } } + public void playSound(String music, float x, float y, float z) { + if (this.playDelay > 0) { + return; + } + this.playDelay = 10; + + ScriptClientSound clientSound = new ScriptClientSound(music); + clientSound.setPos(x, y, z); + clientSound.setVolume(1.0F); + clientSound.setAttenuationType(ISound.AttenuationType.NONE); + clientSound.setRepeat(false); + + try { + SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler(); + soundHandler.playSound(clientSound); + } catch (Exception e) { + this.stopAllSounds(); + return; + } + this.sounds.add(clientSound); + } + public boolean isPlaying(String music) { if (this.playingSound != null && this.playingSound.sound.equals(music)) { ResourceLocation resource = new ResourceLocation(music); @@ -109,7 +166,11 @@ public class MusicController { return Double.MAX_VALUE; } - public void playSound(String music, float x, float y, float z) { - Minecraft.getMinecraft().theWorld.playSound(x, y, z, music, 1, 1, false); - } + public void stopAllSounds() { + this.stopMusic(); + for (ScriptClientSound sound : this.sounds) { + sound.stopSound(); + } + this.sounds.clear(); + } } diff --git a/src/main/java/noppes/npcs/client/controllers/ScriptClientSound.java b/src/main/java/noppes/npcs/client/controllers/ScriptClientSound.java index 31b38ae30..85cc36697 100644 --- a/src/main/java/noppes/npcs/client/controllers/ScriptClientSound.java +++ b/src/main/java/noppes/npcs/client/controllers/ScriptClientSound.java @@ -50,6 +50,7 @@ public class ScriptClientSound extends MovingSound implements ITickableSound { } public void stopSound() { + this.donePlaying = true; this.repeat = false; this.volume = 0; } diff --git a/src/main/java/noppes/npcs/client/gui/player/GuiDialogInteract.java b/src/main/java/noppes/npcs/client/gui/player/GuiDialogInteract.java index e2aebb814..648e9511d 100644 --- a/src/main/java/noppes/npcs/client/gui/player/GuiDialogInteract.java +++ b/src/main/java/noppes/npcs/client/gui/player/GuiDialogInteract.java @@ -693,6 +693,7 @@ public class GuiDialogInteract extends GuiNPCInterface implements IGuiClose super.close(); if(parent != null) NoppesUtil.openGUI(player, parent); + MusicController.Instance.stopAllSounds(); } @Override diff --git a/src/main/java/noppes/npcs/client/gui/roles/GuiNpcBard.java b/src/main/java/noppes/npcs/client/gui/roles/GuiNpcBard.java index 9645518fc..c56a19ca5 100644 --- a/src/main/java/noppes/npcs/client/gui/roles/GuiNpcBard.java +++ b/src/main/java/noppes/npcs/client/gui/roles/GuiNpcBard.java @@ -89,10 +89,15 @@ public class GuiNpcBard extends GuiNPCInterface2 implements ISubGuiListener if(job.minRange > job.maxRange) job.maxRange = job.minRange; - MusicController.Instance.stopMusic(); + MusicController.Instance.stopAllSounds(); Client.sendData(EnumPacketServer.JobSave, job.writeToNBT(new NBTTagCompound())); } + @Override + public void close() { + super.close(); + MusicController.Instance.stopAllSounds(); + } @Override public void subGuiClosed(SubGuiInterface subgui) { @@ -101,6 +106,7 @@ public class GuiNpcBard extends GuiNPCInterface2 implements ISubGuiListener job.song = gss.selectedResource.toString(); getLabel(0).label = job.song; initGui(); + MusicController.Instance.stopAllSounds(); } } } diff --git a/src/main/java/noppes/npcs/client/gui/select/GuiSoundSelection.java b/src/main/java/noppes/npcs/client/gui/select/GuiSoundSelection.java index d82e3ca1b..52733af78 100644 --- a/src/main/java/noppes/npcs/client/gui/select/GuiSoundSelection.java +++ b/src/main/java/noppes/npcs/client/gui/select/GuiSoundSelection.java @@ -113,6 +113,12 @@ public class GuiSoundSelection extends SubGuiInterface implements ICustomScrollL close(); } + @Override + public void close() { + super.close(); + MusicController.Instance.stopAllSounds(); + } + @Override public void customScrollClicked(int i, int j, int k, GuiCustomScroll guiCustomScroll) { if(guiCustomScroll.id == 0){ diff --git a/src/main/java/noppes/npcs/roles/JobBard.java b/src/main/java/noppes/npcs/roles/JobBard.java index 64d873d38..b17105629 100644 --- a/src/main/java/noppes/npcs/roles/JobBard.java +++ b/src/main/java/noppes/npcs/roles/JobBard.java @@ -92,20 +92,19 @@ public class JobBard extends JobInterface{ if(!npc.isRemote() || song.isEmpty()) return; - if(!MusicController.Instance.isPlaying()){ + if(!MusicController.Instance.isPlaying() && Minecraft.getMinecraft().currentScreen == null){ if (!this.play()) return; } else if(MusicController.Instance.getEntity() != npc){ EntityPlayer player = CustomNpcs.proxy.getPlayer(); double distanceToPlayer = npc.getDistanceToEntity(player); double distanceToMusic = MusicController.Instance.getDistance(); - if(distanceToPlayer < distanceToMusic){ - MusicController.Instance.stopMusic(); + if(Math.ceil(distanceToPlayer) < Math.ceil(distanceToMusic)){ if (!this.play()) return; } } else if(hasOffRange){ - List list = npc.worldObj.getEntitiesWithinAABB(EntityPlayer.class, npc.boundingBox.expand(maxRange, maxRange/2, maxRange)); + List list = npc.worldObj.getEntitiesWithinAABB(EntityPlayer.class, npc.boundingBox.expand(maxRange, maxRange, maxRange)); if(!list.contains(CustomNpcs.proxy.getPlayer())) MusicController.Instance.stopMusic(); } @@ -116,7 +115,7 @@ public class JobBard extends JobInterface{ } private boolean play() { - List list = npc.worldObj.getEntitiesWithinAABB(EntityPlayer.class, npc.boundingBox.expand(minRange, minRange/2, minRange)); + List list = npc.worldObj.getEntitiesWithinAABB(EntityPlayer.class, npc.boundingBox.expand(minRange, minRange, minRange)); if(!list.contains(CustomNpcs.proxy.getPlayer())) return false; @@ -136,7 +135,7 @@ public class JobBard extends JobInterface{ public void delete() { if(npc.worldObj.isRemote && hasOffRange){ if(MusicController.Instance.isPlaying(song)){ - MusicController.Instance.stopMusic(); + MusicController.Instance.stopAllSounds(); } } }