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 Sequencer sequencer;
private Clip clip; private Clip clip;
private boolean playingIsSilly; private boolean playingIsSilly;
private boolean isPlaying;
public BattleMusic(Logger logger) public BattleMusic(Logger logger)
{ {
@ -37,6 +38,7 @@ public class BattleMusic
this.logger = logger; this.logger = logger;
battleMusic = new ArrayList<File>(); battleMusic = new ArrayList<File>();
sillyMusic = new ArrayList<File>(); sillyMusic = new ArrayList<File>();
isPlaying = false;
try { try {
sequencer = MidiSystem.getSequencer(); sequencer = MidiSystem.getSequencer();
@ -159,6 +161,7 @@ public class BattleMusic
play(nextBattle, volume); play(nextBattle, volume);
pickNextBattle(); pickNextBattle();
playingIsSilly = false; playingIsSilly = false;
isPlaying = true;
} }
public void playSilly(float volume) public void playSilly(float volume)
@ -174,6 +177,7 @@ public class BattleMusic
play(nextSilly, volume); play(nextSilly, volume);
pickNextSilly(); pickNextSilly();
playingIsSilly = true; playingIsSilly = true;
isPlaying = true;
} }
private void play(File next, float volume) 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()) if(sequencer.isRunning())
{ {
@ -248,6 +252,13 @@ public class BattleMusic
clip.stop(); clip.stop();
clip.close(); clip.close();
} }
if(resumeMCSounds)
{
Minecraft.getMinecraft().addScheduledTask(() -> {
Minecraft.getMinecraft().getSoundHandler().resumeSounds();
});
}
isPlaying = false;
} }
public boolean isPlayingSilly() public boolean isPlayingSilly()
@ -257,6 +268,6 @@ public class BattleMusic
public boolean isPlaying() 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().displayGuiScreen(null);
Minecraft.getMinecraft().setIngameFocus(); Minecraft.getMinecraft().setIngameFocus();
}); });
stopMusic(); stopMusic(true);
} }
@Override @Override
@ -103,9 +103,9 @@ public class ClientProxy extends CommonProxy
} }
@Override @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()) if(battleMusic.isPlayingSilly())
{ {
stopMusic(); stopMusic(false);
playBattleMusic(); playBattleMusic();
} }
} }
@ -135,7 +135,7 @@ public class ClientProxy extends CommonProxy
{ {
if(!battleMusic.isPlayingSilly()) if(!battleMusic.isPlayingSilly())
{ {
stopMusic(); stopMusic(false);
playSillyMusic(); playSillyMusic();
} }
} }

View file

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

View file

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