diff --git a/src/main/java/noppes/npcs/client/controllers/MusicController.java b/src/main/java/noppes/npcs/client/controllers/MusicController.java index e23e77b2a..c860d9e79 100644 --- a/src/main/java/noppes/npcs/client/controllers/MusicController.java +++ b/src/main/java/noppes/npcs/client/controllers/MusicController.java @@ -1,67 +1,69 @@ package noppes.npcs.client.controllers; import net.minecraft.client.Minecraft; -import net.minecraft.client.audio.PositionedSoundRecord; +import net.minecraft.client.audio.ISound; import net.minecraft.client.audio.SoundHandler; import net.minecraft.entity.Entity; import net.minecraft.util.ResourceLocation; - public class MusicController { - public static MusicController Instance; - public PositionedSoundRecord playing; - public ResourceLocation playingResource; - public Entity playingEntity; + public ScriptClientSound sound; public MusicController(){ Instance = this; } public void stopMusic(){ - SoundHandler handler = Minecraft.getMinecraft().getSoundHandler(); - if(playing != null) - handler.stopSound(playing); - handler.stopSounds(); - playingResource = null; - playingEntity = null; - playing = null; + SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler(); + soundHandler.stopSounds(); + this.sound.stopSound(); + this.sound = null; } public void playStreaming(String music, Entity entity){ - if(isPlaying(music)){ + if (this.isPlaying(music)) { return; } - stopMusic(); - playingEntity = entity; - playingResource = new ResourceLocation(music); - SoundHandler handler = Minecraft.getMinecraft().getSoundHandler(); - playing = PositionedSoundRecord.func_147675_a(playingResource, (float)entity.posX, (float)entity.posY, (float)entity.posZ); - handler.playSound(playing); + + ScriptClientSound clientSound = new ScriptClientSound(music); + clientSound.setPos((float)entity.posX, (float)entity.posY, (float)entity.posZ); + clientSound.setVolume(4.0F); + clientSound.setAttenuationType(ISound.AttenuationType.LINEAR); + this.sound = clientSound; + + SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler(); + soundHandler.playSound(clientSound); } public void playMusic(String music, Entity entity) { - if(isPlaying(music)) - return; - stopMusic(); - playingResource = new ResourceLocation(music); + if (this.isPlaying(music)) { + return; + } - playingEntity = entity; - - SoundHandler handler = Minecraft.getMinecraft().getSoundHandler(); - playing = PositionedSoundRecord.func_147673_a(playingResource); - handler.playSound(playing); + ScriptClientSound clientSound = new ScriptClientSound(music); + clientSound.setEntity(entity); + SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler(); + soundHandler.playSound(clientSound); + this.sound = clientSound; } - public boolean isPlaying(String music) { - ResourceLocation resource = new ResourceLocation(music); - if(playingResource == null || !playingResource.equals(resource)){ - return false; - } - return Minecraft.getMinecraft().getSoundHandler().isSoundPlaying(playing); + if (this.sound != null && this.sound.sound.equals(music)) { + ResourceLocation resource = new ResourceLocation(music); + if (!this.sound.getPositionedSoundLocation().equals(resource)) { + return false; + } + return Minecraft.getMinecraft().getSoundHandler().isSoundPlaying(this.sound); + } else { + return false; + } } + public boolean isPlaying() { + return this.sound != null; + } + public void playSound(String music, float x, float y, float z) { Minecraft.getMinecraft().theWorld.playSound(x, y, z, music, 1, 1, false); } diff --git a/src/main/java/noppes/npcs/client/controllers/ScriptClientSound.java b/src/main/java/noppes/npcs/client/controllers/ScriptClientSound.java index f20738670..7992f35e7 100644 --- a/src/main/java/noppes/npcs/client/controllers/ScriptClientSound.java +++ b/src/main/java/noppes/npcs/client/controllers/ScriptClientSound.java @@ -1,5 +1,6 @@ package noppes.npcs.client.controllers; +import net.minecraft.client.Minecraft; import net.minecraft.client.audio.ITickableSound; import net.minecraft.client.audio.MovingSound; import net.minecraft.entity.Entity; @@ -9,16 +10,18 @@ import net.minecraft.world.World; import noppes.npcs.scripted.ScriptSound; public class ScriptClientSound extends MovingSound implements ITickableSound { + public final String sound; private Entity entity; public boolean paused; - protected ScriptClientSound(ResourceLocation location) { - super(location); + public ScriptClientSound(String sound) { + super(new ResourceLocation(sound)); + this.sound = sound; } public static ScriptClientSound fromScriptSound(NBTTagCompound compound, World world) { ScriptSound sound = ScriptSound.fromNBT(compound); - ScriptClientSound clientSound = new ScriptClientSound(new ResourceLocation(sound.directory)); + ScriptClientSound clientSound = new ScriptClientSound(sound.directory); clientSound.xPosF = sound.xPosF; clientSound.yPosF = sound.yPosF; clientSound.zPosF = sound.zPosF; @@ -27,12 +30,25 @@ public class ScriptClientSound extends MovingSound implements ITickableSound { clientSound.repeat = sound.repeat; clientSound.field_147665_h = sound.repeatDelay; if (compound.hasKey("EntityID")) { - clientSound.entity = world.getEntityByID(compound.getInteger("EntityID")); + clientSound.setEntity(world.getEntityByID(compound.getInteger("EntityID"))); clientSound.field_147666_i = AttenuationType.NONE; } return clientSound; } + public void setEntity(Entity entity) { + this.entity = entity; + if (entity != null) { + this.xPosF = (float) (this.entity.posX); + this.yPosF = (float) (this.entity.posY); + this.zPosF = (float) (this.entity.posZ); + } + } + + public Entity getEntity() { + return this.entity; + } + public void stopSound() { this.repeat = false; this.volume = 0; @@ -50,4 +66,22 @@ public class ScriptClientSound extends MovingSound implements ITickableSound { } } } + + public void setPos(float x, float y, float z) { + this.xPosF = x; + this.yPosF = y; + this.zPosF = z; + } + + public double getDistanceSq() { + return Minecraft.getMinecraft().thePlayer.getDistanceSq(this.xPosF, this.yPosF, this.zPosF); + } + + public void setVolume(float volume) { + this.volume = volume; + } + + public void setAttenuationType(AttenuationType attenuationType) { + this.field_147666_i = attenuationType; + } } diff --git a/src/main/java/noppes/npcs/roles/JobBard.java b/src/main/java/noppes/npcs/roles/JobBard.java index d18aa0bf1..1ebb62f82 100644 --- a/src/main/java/noppes/npcs/roles/JobBard.java +++ b/src/main/java/noppes/npcs/roles/JobBard.java @@ -92,21 +92,15 @@ public class JobBard extends JobInterface{ if(!npc.isRemote() || song.isEmpty()) return; - if(!MusicController.Instance.isPlaying(song)){ - List list = npc.worldObj.getEntitiesWithinAABB(EntityPlayer.class, npc.boundingBox.expand(minRange, minRange/2, minRange)); - if(!list.contains(CustomNpcs.proxy.getPlayer())) - return; - if(isStreamer) - MusicController.Instance.playStreaming(song, npc); - else - MusicController.Instance.playMusic(song, npc); + if(!MusicController.Instance.isPlaying()){ + this.play(); } - else if(MusicController.Instance.playingEntity != npc){ + else if(MusicController.Instance.sound.getEntity() != npc){ EntityPlayer player = CustomNpcs.proxy.getPlayer(); - if(npc.getDistanceSqToEntity(player) < MusicController.Instance.playingEntity.getDistanceSqToEntity(player)){ - MusicController.Instance.playingEntity = npc; + if(npc.getDistanceSqToEntity(player) < MusicController.Instance.sound.getDistanceSq()){ + MusicController.Instance.stopMusic(); + this.play(); } - } else if(hasOffRange){ List list = npc.worldObj.getEntitiesWithinAABB(EntityPlayer.class, npc.boundingBox.expand(maxRange, maxRange/2, maxRange)); @@ -119,6 +113,16 @@ public class JobBard extends JobInterface{ } } + private void play() { + List list = npc.worldObj.getEntitiesWithinAABB(EntityPlayer.class, npc.boundingBox.expand(minRange, minRange/2, minRange)); + if(!list.contains(CustomNpcs.proxy.getPlayer())) + return; + if(isStreamer) + MusicController.Instance.playStreaming(song, npc); + else + MusicController.Instance.playMusic(song, npc); + } + @Override public void killed() { delete();