Fix passive-type battle music occurrences, fixes

This commit is contained in:
Stephen Seo 2018-09-27 18:09:40 +09:00
parent 3e234ea94e
commit c2c19f6af2
6 changed files with 178 additions and 63 deletions

View file

@ -133,9 +133,11 @@ public class BattleMusic
if(!initialized || battleMusic.isEmpty()) if(!initialized || battleMusic.isEmpty())
{ {
nextBattle = null; nextBattle = null;
return;
} }
nextBattle = battleMusic.get((int)(Math.random() * battleMusic.size())); else
{
nextBattle = battleMusic.get((int)(Math.random() * battleMusic.size()));
}
} }
private void pickNextSilly() private void pickNextSilly()
@ -143,9 +145,11 @@ public class BattleMusic
if(!initialized || sillyMusic.isEmpty()) if(!initialized || sillyMusic.isEmpty())
{ {
nextSilly = null; nextSilly = null;
return;
} }
nextSilly = sillyMusic.get((int)(Math.random() * sillyMusic.size())); else
{
nextSilly = sillyMusic.get((int)(Math.random() * sillyMusic.size()));
}
} }
public void playBattle(float volume) public void playBattle(float volume)
@ -191,15 +195,9 @@ public class BattleMusic
String suffix = next.getName().substring(next.getName().length() - 3).toLowerCase(); String suffix = next.getName().substring(next.getName().length() - 3).toLowerCase();
if(suffix.equals("mid")) if(suffix.equals("mid"))
{ {
if(sequencer.isRunning()) sequencer.stop();
{ clip.stop();
sequencer.stop(); clip.close();
}
if(clip.isActive())
{
clip.stop();
clip.close();
}
try { try {
sequencer.setSequence(new BufferedInputStream(new FileInputStream(next))); sequencer.setSequence(new BufferedInputStream(new FileInputStream(next)));
} catch (Throwable t) } catch (Throwable t)
@ -243,15 +241,9 @@ public class BattleMusic
public void stopMusic(boolean resumeMCSounds) public void stopMusic(boolean resumeMCSounds)
{ {
if(sequencer.isRunning()) sequencer.stop();
{ clip.stop();
sequencer.stop(); clip.close();
}
if(clip.isActive())
{
clip.stop();
clip.close();
}
if(resumeMCSounds) if(resumeMCSounds)
{ {
Minecraft.getMinecraft().addScheduledTask(() -> { Minecraft.getMinecraft().addScheduledTask(() -> {
@ -270,4 +262,14 @@ public class BattleMusic
{ {
return isPlaying || sequencer.isRunning() || clip.isActive(); return isPlaying || sequencer.isRunning() || clip.isActive();
} }
public boolean hasBattleMusic()
{
return !battleMusic.isEmpty();
}
public boolean hasSillyMusic()
{
return !sillyMusic.isEmpty();
}
} }

View file

@ -17,12 +17,16 @@ public class ClientProxy extends CommonProxy
private BattleMusic battleMusic; private BattleMusic battleMusic;
private Logger logger; private Logger logger;
private Config config; private Config config;
private int battleMusicCount;
private int sillyMusicCount;
@Override @Override
public void initialize() public void initialize()
{ {
battleGui = new BattleGui(); battleGui = new BattleGui();
battleMusic = null; // will be initialized in postInit() battleMusic = null; // will be initialized in postInit()
battleMusicCount = 0;
sillyMusicCount = 0;
} }
@Override @Override
@ -76,6 +80,8 @@ public class ClientProxy extends CommonProxy
Minecraft.getMinecraft().setIngameFocus(); Minecraft.getMinecraft().setIngameFocus();
}); });
stopMusic(true); stopMusic(true);
battleMusicCount = 0;
sillyMusicCount = 0;
} }
@Override @Override
@ -114,36 +120,45 @@ public class ClientProxy extends CommonProxy
@Override @Override
public void typeEnteredBattle(String type) public void typeEnteredBattle(String type)
{ {
if(TurnBasedMinecraftMod.currentBattle == null)
{
return;
}
if(type == null || type.isEmpty() || config.isBattleMusicType(type)) if(type == null || type.isEmpty() || config.isBattleMusicType(type))
{ {
if(battleMusic.isPlaying()) ++battleMusicCount;
{
if(battleMusic.isPlayingSilly())
{
stopMusic(false);
playBattleMusic();
}
}
else
{
playBattleMusic();
}
} }
else if(config.isSillyMusicType(type)) else if(config.isSillyMusicType(type))
{ {
if(battleMusic.isPlaying()) ++sillyMusicCount;
{
if(!battleMusic.isPlayingSilly())
{
stopMusic(false);
playSillyMusic();
}
}
else
{
playSillyMusic();
}
} }
else
{
++battleMusicCount;
}
checkBattleTypes();
}
@Override
public void typeLeftBattle(String type)
{
if(TurnBasedMinecraftMod.currentBattle == null)
{
return;
}
if(type == null || type.isEmpty() || config.isBattleMusicType(type))
{
--battleMusicCount;
}
else if(config.isSillyMusicType(type))
{
--sillyMusicCount;
}
else
{
--battleMusicCount;
}
checkBattleTypes();
} }
@Override @Override
@ -163,4 +178,38 @@ public class ClientProxy extends CommonProxy
{ {
return Minecraft.getMinecraft().world.getEntityByID(id); return Minecraft.getMinecraft().world.getEntityByID(id);
} }
private void checkBattleTypes()
{
if(battleMusicCount <= 1 && sillyMusicCount > 0)
{
if(battleMusic.isPlaying())
{
if(!battleMusic.isPlayingSilly() && battleMusic.hasSillyMusic())
{
stopMusic(false);
playSillyMusic();
}
}
else if(battleMusic.hasSillyMusic())
{
playSillyMusic();
}
}
else
{
if(battleMusic.isPlaying())
{
if(battleMusic.isPlayingSilly() && battleMusic.hasBattleMusic())
{
stopMusic(false);
playBattleMusic();
}
}
else if(battleMusic.hasBattleMusic())
{
playBattleMusic();
}
}
}
} }

View file

@ -192,6 +192,10 @@ public class Battle
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, c.entityInfo.category); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, c.entityInfo.category);
} }
else if(c.entity instanceof EntityPlayer)
{
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, "player");
}
else else
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id);
@ -203,6 +207,10 @@ public class Battle
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, c.entityInfo.category); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, c.entityInfo.category);
} }
else if(c.entity instanceof EntityPlayer)
{
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, "player");
}
else else
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id);
@ -290,6 +298,10 @@ public class Battle
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, newCombatant.entityInfo.category); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, newCombatant.entityInfo.category);
} }
else if(newCombatant.entity instanceof EntityPlayer)
{
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, "player");
}
else else
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id);
@ -339,6 +351,10 @@ public class Battle
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, newCombatant.entityInfo.category); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, newCombatant.entityInfo.category);
} }
else if(newCombatant.entity instanceof EntityPlayer)
{
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, "player");
}
else else
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id);
@ -350,6 +366,8 @@ public class Battle
{ {
sideA.clear(); sideA.clear();
sideB.clear(); sideB.clear();
sideAEntryQueue.clear();
sideBEntryQueue.clear();
players.clear(); players.clear();
playerCount.set(0); playerCount.set(0);
undecidedCount.set(0); undecidedCount.set(0);
@ -506,7 +524,16 @@ public class Battle
{ {
TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, c.entity.getEntityId(), 0, 0), (EntityPlayerMP)c.entity); TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, c.entity.getEntityId(), 0, 0), (EntityPlayerMP)c.entity);
} }
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DIED, c.entity.getEntityId(), 0, 0); String category = new String();
if(c.entityInfo != null)
{
category = c.entityInfo.category;
}
else if(c.entity instanceof EntityPlayer)
{
category = "player";
}
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DIED, c.entity.getEntityId(), 0, 0, category);
} }
} }
for(Combatant c : sideB.values()) for(Combatant c : sideB.values())
@ -518,7 +545,16 @@ public class Battle
{ {
TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, c.entity.getEntityId(), 0, 0), (EntityPlayerMP)c.entity); TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, c.entity.getEntityId(), 0, 0), (EntityPlayerMP)c.entity);
} }
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DIED, c.entity.getEntityId(), 0, 0); String category = new String();
if(c.entityInfo != null)
{
category = c.entityInfo.category;
}
else if(c.entity instanceof EntityPlayer)
{
category = "player";
}
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DIED, c.entity.getEntityId(), 0, 0, category);
} }
} }
boolean didRemove = !removeQueue.isEmpty(); boolean didRemove = !removeQueue.isEmpty();
@ -538,13 +574,15 @@ public class Battle
} }
else if(didRemove) else if(didRemove)
{ {
notifyPlayersBattleInfo();
resetUndecidedCount(); resetUndecidedCount();
} }
return didRemove; return didRemove;
} }
/**
* @return true if at least one combatant was removed
*/
private boolean isCreativeCheck() private boolean isCreativeCheck()
{ {
Queue<Integer> removeQueue = new ArrayDeque<Integer>(); Queue<Integer> removeQueue = new ArrayDeque<Integer>();
@ -568,7 +606,6 @@ public class Battle
} }
if(didRemove) if(didRemove)
{ {
notifyPlayersBattleInfo();
resetUndecidedCount(); resetUndecidedCount();
} }
@ -627,11 +664,13 @@ public class Battle
{ {
return true; return true;
} }
boolean combatantsChanged = false;
synchronized(sideAEntryQueue) synchronized(sideAEntryQueue)
{ {
for(Combatant c = sideAEntryQueue.poll(); c != null; c = sideAEntryQueue.poll()) for(Combatant c = sideAEntryQueue.poll(); c != null; c = sideAEntryQueue.poll())
{ {
sideA.put(c.entity.getEntityId(), c); sideA.put(c.entity.getEntityId(), c);
combatantsChanged = true;
} }
} }
synchronized(sideBEntryQueue) synchronized(sideBEntryQueue)
@ -639,6 +678,7 @@ public class Battle
for(Combatant c = sideBEntryQueue.poll(); c != null; c = sideBEntryQueue.poll()) for(Combatant c = sideBEntryQueue.poll(); c != null; c = sideBEntryQueue.poll())
{ {
sideB.put(c.entity.getEntityId(), c); sideB.put(c.entity.getEntityId(), c);
combatantsChanged = true;
} }
} }
if(TurnBasedMinecraftMod.config.isFreezeCombatantsEnabled()) if(TurnBasedMinecraftMod.config.isFreezeCombatantsEnabled())
@ -706,8 +746,14 @@ public class Battle
} }
else else
{ {
healthCheck(); if(healthCheck())
isCreativeCheck(); {
combatantsChanged = true;
}
if(isCreativeCheck())
{
combatantsChanged = true;
}
} }
break; break;
case ACTION: case ACTION:
@ -1019,7 +1065,17 @@ public class Battle
{ {
sideB.remove(next.entity.getEntityId()); sideB.remove(next.entity.getEntityId());
} }
sendMessageToAllPlayers(PacketBattleMessage.MessageType.FLEE, next.entity.getEntityId(), 0, 1); combatantsChanged = true;
String fleeingCategory = new String();
if(next.entityInfo != null)
{
fleeingCategory = next.entityInfo.category;
}
else if(next.entity instanceof EntityPlayer)
{
fleeingCategory = "player";
}
sendMessageToAllPlayers(PacketBattleMessage.MessageType.FLEE, next.entity.getEntityId(), 0, 1, fleeingCategory);
if(next.entity instanceof EntityPlayer) if(next.entity instanceof EntityPlayer)
{ {
players.remove(next.entity.getEntityId()); players.remove(next.entity.getEntityId());
@ -1094,8 +1150,14 @@ public class Battle
} }
state = State.DECISION; state = State.DECISION;
undecidedCount.set(players.size()); undecidedCount.set(players.size());
healthCheck(); if(healthCheck())
isCreativeCheck(); {
combatantsChanged = true;
}
if(isCreativeCheck())
{
combatantsChanged = true;
}
FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(() -> { FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(() -> {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_END, 0, 0, 0); sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_END, 0, 0, 0);
}); });
@ -1105,6 +1167,10 @@ public class Battle
state = State.DECISION; state = State.DECISION;
break; break;
} // switch(state) } // switch(state)
if(combatantsChanged)
{
notifyPlayersBattleInfo();
}
return battleEnded; return battleEnded;
} // update(final long dt) } // update(final long dt)
} }

View file

@ -56,6 +56,8 @@ public class CommonProxy
public void typeEnteredBattle(String type) {} public void typeEnteredBattle(String type) {}
public void typeLeftBattle(String type) {}
public void setConfig(Config config) {} public void setConfig(Config config) {}
public void displayString(String message) {} public void displayString(String message) {}

View file

@ -233,19 +233,13 @@ public class PacketBattleMessage implements IMessage
TurnBasedMinecraftMod.currentBattle = new Battle(message.amount, null, null, false); TurnBasedMinecraftMod.currentBattle = new Battle(message.amount, null, null, false);
} }
TurnBasedMinecraftMod.commonProxy.battleStarted(); TurnBasedMinecraftMod.commonProxy.battleStarted();
if(message.custom.isEmpty()) TurnBasedMinecraftMod.commonProxy.typeEnteredBattle(message.custom);
{
TurnBasedMinecraftMod.commonProxy.typeEnteredBattle(null);
}
else
{
TurnBasedMinecraftMod.commonProxy.typeEnteredBattle(message.custom);
}
break; break;
case FLEE: case FLEE:
if(message.amount != 0) if(message.amount != 0)
{ {
TurnBasedMinecraftMod.commonProxy.displayString(from + " fled battle!"); TurnBasedMinecraftMod.commonProxy.displayString(from + " fled battle!");
TurnBasedMinecraftMod.commonProxy.typeLeftBattle(message.custom);
} }
else else
{ {
@ -254,6 +248,7 @@ public class PacketBattleMessage implements IMessage
break; break;
case DIED: case DIED:
TurnBasedMinecraftMod.commonProxy.displayString(from + " died in battle!"); TurnBasedMinecraftMod.commonProxy.displayString(from + " died in battle!");
TurnBasedMinecraftMod.commonProxy.typeLeftBattle(message.custom);
break; break;
case ENDED: case ENDED:
TurnBasedMinecraftMod.commonProxy.displayString("Battle has ended!"); TurnBasedMinecraftMod.commonProxy.displayString("Battle has ended!");

View file

@ -18,6 +18,7 @@
<monster></monster> <monster></monster>
<animal></animal> <animal></animal>
<boss></boss> <boss></boss>
<player></player>
</Normal> </Normal>
<Silly> <Silly>
<passive></passive> <passive></passive>