Fix bug where two sounds play at once, fixes

Mod should also now resume other previously playing sounds/music after
battle has ended.

Also prevents possible concurrent modification exception when new
Entities are added to Battle server-side.
This commit is contained in:
Stephen Seo 2018-09-27 16:44:28 +09:00
parent 6ead1e1d3d
commit 3e234ea94e
4 changed files with 64 additions and 11 deletions

View file

@ -30,6 +30,7 @@ public class BattleMusic
private Sequencer sequencer;
private Clip clip;
private boolean playingIsSilly;
private boolean isPlaying;
public BattleMusic(Logger logger)
{
@ -37,6 +38,7 @@ public class BattleMusic
this.logger = logger;
battleMusic = new ArrayList<File>();
sillyMusic = new ArrayList<File>();
isPlaying = false;
try {
sequencer = MidiSystem.getSequencer();
@ -159,6 +161,7 @@ public class BattleMusic
play(nextBattle, volume);
pickNextBattle();
playingIsSilly = false;
isPlaying = true;
}
public void playSilly(float volume)
@ -174,6 +177,7 @@ public class BattleMusic
play(nextSilly, volume);
pickNextSilly();
playingIsSilly = true;
isPlaying = true;
}
private void play(File next, float volume)
@ -237,7 +241,7 @@ public class BattleMusic
}
}
public void stopMusic()
public void stopMusic(boolean resumeMCSounds)
{
if(sequencer.isRunning())
{
@ -248,6 +252,13 @@ public class BattleMusic
clip.stop();
clip.close();
}
if(resumeMCSounds)
{
Minecraft.getMinecraft().addScheduledTask(() -> {
Minecraft.getMinecraft().getSoundHandler().resumeSounds();
});
}
isPlaying = false;
}
public boolean isPlayingSilly()
@ -257,6 +268,6 @@ public class BattleMusic
public boolean isPlaying()
{
return sequencer.isRunning() || clip.isActive();
return isPlaying || sequencer.isRunning() || clip.isActive();
}
}

View file

@ -75,7 +75,7 @@ public class ClientProxy extends CommonProxy
Minecraft.getMinecraft().displayGuiScreen(null);
Minecraft.getMinecraft().setIngameFocus();
});
stopMusic();
stopMusic(true);
}
@Override
@ -103,9 +103,9 @@ public class ClientProxy extends CommonProxy
}
@Override
public void stopMusic()
public void stopMusic(boolean resumeMCSounds)
{
battleMusic.stopMusic();
battleMusic.stopMusic(resumeMCSounds);
}
/**
@ -120,7 +120,7 @@ public class ClientProxy extends CommonProxy
{
if(battleMusic.isPlayingSilly())
{
stopMusic();
stopMusic(false);
playBattleMusic();
}
}
@ -135,7 +135,7 @@ public class ClientProxy extends CommonProxy
{
if(!battleMusic.isPlayingSilly())
{
stopMusic();
stopMusic(false);
playSillyMusic();
}
}

View file

@ -36,6 +36,8 @@ public class Battle
private Map<Integer, Combatant> sideB;
private Map<Integer, Combatant> players;
private PriorityQueue<Combatant> turnOrderQueue;
private Queue<Combatant> sideAEntryQueue;
private Queue<Combatant> sideBEntryQueue;
private State state;
private AtomicInteger playerCount;
@ -123,6 +125,8 @@ public class Battle
this.sideB = new Hashtable<Integer, Combatant>();
players = new Hashtable<Integer, Combatant>();
turnOrderQueue = new PriorityQueue<Combatant>(new Combatant.CombatantComparator());
sideAEntryQueue = new ArrayDeque<Combatant>();
sideBEntryQueue = new ArrayDeque<Combatant>();
playerCount = new AtomicInteger(0);
undecidedCount = new AtomicInteger(0);
if(sideA != null)
@ -254,7 +258,17 @@ public class Battle
Combatant newCombatant = new Combatant(e, entityInfo);
newCombatant.isSideA = true;
newCombatant.battleID = getId();
sideA.put(e.getEntityId(), newCombatant);
if(isServer)
{
synchronized(sideAEntryQueue)
{
sideAEntryQueue.add(newCombatant);
}
}
else
{
sideA.put(e.getEntityId(), newCombatant);
}
if(e instanceof EntityPlayer)
{
newCombatant.recalcSpeedOnCompare = true;
@ -293,7 +307,17 @@ public class Battle
Combatant newCombatant = new Combatant(e, entityInfo);
newCombatant.isSideA = false;
newCombatant.battleID = getId();
sideB.put(e.getEntityId(), newCombatant);
if(isServer)
{
synchronized(sideBEntryQueue)
{
sideBEntryQueue.add(newCombatant);
}
}
else
{
sideB.put(e.getEntityId(), newCombatant);
}
if(e instanceof EntityPlayer)
{
newCombatant.recalcSpeedOnCompare = true;
@ -583,7 +607,11 @@ public class Battle
*/
public boolean update()
{
if(battleEnded)
if(!isServer)
{
return false;
}
else if(battleEnded)
{
return true;
}
@ -599,6 +627,20 @@ public class Battle
{
return true;
}
synchronized(sideAEntryQueue)
{
for(Combatant c = sideAEntryQueue.poll(); c != null; c = sideAEntryQueue.poll())
{
sideA.put(c.entity.getEntityId(), c);
}
}
synchronized(sideBEntryQueue)
{
for(Combatant c = sideBEntryQueue.poll(); c != null; c = sideBEntryQueue.poll())
{
sideB.put(c.entity.getEntityId(), c);
}
}
if(TurnBasedMinecraftMod.config.isFreezeCombatantsEnabled())
{
enforceFreezePositions();

View file

@ -52,7 +52,7 @@ public class CommonProxy
public void playSillyMusic() {}
public void stopMusic() {}
public void stopMusic(boolean resumeMCSounds) {}
public void typeEnteredBattle(String type) {}