From c2c19f6af289ee5bc8ddd4f7865488e19451d6f2 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 27 Sep 2018 18:09:40 +0900 Subject: [PATCH] Fix passive-type battle music occurrences, fixes --- .../client/BattleMusic.java | 46 ++++----- .../client/ClientProxy.java | 97 ++++++++++++++----- .../TurnBasedMinecraft/common/Battle.java | 84 ++++++++++++++-- .../common/CommonProxy.java | 2 + .../networking/PacketBattleMessage.java | 11 +-- .../assets/TurnBasedMinecraft/TBM_Config.xml | 1 + 6 files changed, 178 insertions(+), 63 deletions(-) diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/client/BattleMusic.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/BattleMusic.java index c8ae178..f5d85d3 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/client/BattleMusic.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/BattleMusic.java @@ -133,9 +133,11 @@ public class BattleMusic if(!initialized || battleMusic.isEmpty()) { nextBattle = null; - return; } - nextBattle = battleMusic.get((int)(Math.random() * battleMusic.size())); + else + { + nextBattle = battleMusic.get((int)(Math.random() * battleMusic.size())); + } } private void pickNextSilly() @@ -143,9 +145,11 @@ public class BattleMusic if(!initialized || sillyMusic.isEmpty()) { 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) @@ -191,15 +195,9 @@ public class BattleMusic String suffix = next.getName().substring(next.getName().length() - 3).toLowerCase(); if(suffix.equals("mid")) { - if(sequencer.isRunning()) - { - sequencer.stop(); - } - if(clip.isActive()) - { - clip.stop(); - clip.close(); - } + sequencer.stop(); + clip.stop(); + clip.close(); try { sequencer.setSequence(new BufferedInputStream(new FileInputStream(next))); } catch (Throwable t) @@ -243,15 +241,9 @@ public class BattleMusic public void stopMusic(boolean resumeMCSounds) { - if(sequencer.isRunning()) - { - sequencer.stop(); - } - if(clip.isActive()) - { - clip.stop(); - clip.close(); - } + sequencer.stop(); + clip.stop(); + clip.close(); if(resumeMCSounds) { Minecraft.getMinecraft().addScheduledTask(() -> { @@ -270,4 +262,14 @@ public class BattleMusic { return isPlaying || sequencer.isRunning() || clip.isActive(); } + + public boolean hasBattleMusic() + { + return !battleMusic.isEmpty(); + } + + public boolean hasSillyMusic() + { + return !sillyMusic.isEmpty(); + } } diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/client/ClientProxy.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/ClientProxy.java index 4ab8298..63142f1 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/client/ClientProxy.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/client/ClientProxy.java @@ -17,12 +17,16 @@ public class ClientProxy extends CommonProxy private BattleMusic battleMusic; private Logger logger; private Config config; + private int battleMusicCount; + private int sillyMusicCount; @Override public void initialize() { battleGui = new BattleGui(); battleMusic = null; // will be initialized in postInit() + battleMusicCount = 0; + sillyMusicCount = 0; } @Override @@ -76,6 +80,8 @@ public class ClientProxy extends CommonProxy Minecraft.getMinecraft().setIngameFocus(); }); stopMusic(true); + battleMusicCount = 0; + sillyMusicCount = 0; } @Override @@ -114,36 +120,45 @@ public class ClientProxy extends CommonProxy @Override public void typeEnteredBattle(String type) { + if(TurnBasedMinecraftMod.currentBattle == null) + { + return; + } if(type == null || type.isEmpty() || config.isBattleMusicType(type)) { - if(battleMusic.isPlaying()) - { - if(battleMusic.isPlayingSilly()) - { - stopMusic(false); - playBattleMusic(); - } - } - else - { - playBattleMusic(); - } + ++battleMusicCount; } else if(config.isSillyMusicType(type)) { - if(battleMusic.isPlaying()) - { - if(!battleMusic.isPlayingSilly()) - { - stopMusic(false); - playSillyMusic(); - } - } - else - { - playSillyMusic(); - } + ++sillyMusicCount; } + 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 @@ -163,4 +178,38 @@ public class ClientProxy extends CommonProxy { 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(); + } + } + } } diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java index b1338a6..9d06b12 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java @@ -192,6 +192,10 @@ public class Battle { 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 { 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); } + else if(c.entity instanceof EntityPlayer) + { + sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, "player"); + } else { 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); } + else if(newCombatant.entity instanceof EntityPlayer) + { + sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, "player"); + } else { 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); } + else if(newCombatant.entity instanceof EntityPlayer) + { + sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, "player"); + } else { sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id); @@ -350,6 +366,8 @@ public class Battle { sideA.clear(); sideB.clear(); + sideAEntryQueue.clear(); + sideBEntryQueue.clear(); players.clear(); playerCount.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); } - 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()) @@ -518,7 +545,16 @@ public class Battle { 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(); @@ -538,13 +574,15 @@ public class Battle } else if(didRemove) { - notifyPlayersBattleInfo(); resetUndecidedCount(); } return didRemove; } + /** + * @return true if at least one combatant was removed + */ private boolean isCreativeCheck() { Queue removeQueue = new ArrayDeque(); @@ -568,7 +606,6 @@ public class Battle } if(didRemove) { - notifyPlayersBattleInfo(); resetUndecidedCount(); } @@ -627,11 +664,13 @@ public class Battle { return true; } + boolean combatantsChanged = false; synchronized(sideAEntryQueue) { for(Combatant c = sideAEntryQueue.poll(); c != null; c = sideAEntryQueue.poll()) { sideA.put(c.entity.getEntityId(), c); + combatantsChanged = true; } } synchronized(sideBEntryQueue) @@ -639,6 +678,7 @@ public class Battle for(Combatant c = sideBEntryQueue.poll(); c != null; c = sideBEntryQueue.poll()) { sideB.put(c.entity.getEntityId(), c); + combatantsChanged = true; } } if(TurnBasedMinecraftMod.config.isFreezeCombatantsEnabled()) @@ -706,8 +746,14 @@ public class Battle } else { - healthCheck(); - isCreativeCheck(); + if(healthCheck()) + { + combatantsChanged = true; + } + if(isCreativeCheck()) + { + combatantsChanged = true; + } } break; case ACTION: @@ -1019,7 +1065,17 @@ public class Battle { 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) { players.remove(next.entity.getEntityId()); @@ -1094,8 +1150,14 @@ public class Battle } state = State.DECISION; undecidedCount.set(players.size()); - healthCheck(); - isCreativeCheck(); + if(healthCheck()) + { + combatantsChanged = true; + } + if(isCreativeCheck()) + { + combatantsChanged = true; + } FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(() -> { sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_END, 0, 0, 0); }); @@ -1105,6 +1167,10 @@ public class Battle state = State.DECISION; break; } // switch(state) + if(combatantsChanged) + { + notifyPlayersBattleInfo(); + } return battleEnded; } // update(final long dt) } diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommonProxy.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommonProxy.java index 6800cc5..259693e 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommonProxy.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/CommonProxy.java @@ -56,6 +56,8 @@ public class CommonProxy public void typeEnteredBattle(String type) {} + public void typeLeftBattle(String type) {} + public void setConfig(Config config) {} public void displayString(String message) {} diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleMessage.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleMessage.java index 3bb7ca2..2114ac5 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleMessage.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleMessage.java @@ -233,19 +233,13 @@ public class PacketBattleMessage implements IMessage TurnBasedMinecraftMod.currentBattle = new Battle(message.amount, null, null, false); } TurnBasedMinecraftMod.commonProxy.battleStarted(); - if(message.custom.isEmpty()) - { - TurnBasedMinecraftMod.commonProxy.typeEnteredBattle(null); - } - else - { - TurnBasedMinecraftMod.commonProxy.typeEnteredBattle(message.custom); - } + TurnBasedMinecraftMod.commonProxy.typeEnteredBattle(message.custom); break; case FLEE: if(message.amount != 0) { TurnBasedMinecraftMod.commonProxy.displayString(from + " fled battle!"); + TurnBasedMinecraftMod.commonProxy.typeLeftBattle(message.custom); } else { @@ -254,6 +248,7 @@ public class PacketBattleMessage implements IMessage break; case DIED: TurnBasedMinecraftMod.commonProxy.displayString(from + " died in battle!"); + TurnBasedMinecraftMod.commonProxy.typeLeftBattle(message.custom); break; case ENDED: TurnBasedMinecraftMod.commonProxy.displayString("Battle has ended!"); diff --git a/src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml b/src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml index 896338f..ffbf514 100644 --- a/src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml +++ b/src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml @@ -18,6 +18,7 @@ +