Fixed bard music bug where music would keep playing, unattached to a particular NPC.

This commit is contained in:
Luis C.M. committed 2024-05-16 02:01:04 -05:00
1 parent 03c45c70c5
commit a88f814c7a
7 files changed
+92 -16

No files matched your search

@@ -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 &&
@@ -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<ScriptClientSound> 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();
}
}
@@ -50,6 +50,7 @@ public class ScriptClientSound extends MovingSound implements ITickableSound {
}
public void stopSound() {
this.donePlaying = true;
this.repeat = false;
this.volume = 0;
}
@@ -693,6 +693,7 @@ public class GuiDialogInteract extends GuiNPCInterface implements IGuiClose
super.close();
if(parent != null)
NoppesUtil.openGUI(player, parent);
MusicController.Instance.stopAllSounds();
}
@Override
@@ -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();
}
}
}
@@ -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){
+5 -6
View File
@@ -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<EntityPlayer> list = npc.worldObj.getEntitiesWithinAABB(EntityPlayer.class, npc.boundingBox.expand(maxRange, maxRange/2, maxRange));
List<EntityPlayer> 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<EntityPlayer> list = npc.worldObj.getEntitiesWithinAABB(EntityPlayer.class, npc.boundingBox.expand(minRange, minRange/2, minRange));
List<EntityPlayer> 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();
}
}
}