Fixes and Refactorings, no major changes
This commit is contained in:
parent
3596bfd2ca
commit
0a26eee53a
4 changed files with 152 additions and 134 deletions
|
@ -1,7 +1,6 @@
|
|||
package com.seodisparate.TurnBasedMinecraft.common;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
import java.util.Iterator;
|
||||
|
||||
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage;
|
||||
|
||||
|
@ -23,20 +22,20 @@ public class AttackEventHandler
|
|||
}
|
||||
else
|
||||
{
|
||||
Queue<AttackerViaBow> removeQueue = new ArrayDeque<AttackerViaBow>();
|
||||
final long now = System.nanoTime();
|
||||
boolean isValid = false;
|
||||
synchronized(TurnBasedMinecraftMod.proxy.getAttackerViaBowSet())
|
||||
{
|
||||
for(AttackerViaBow attacker : TurnBasedMinecraftMod.proxy.getAttackerViaBowSet())
|
||||
for(Iterator<AttackerViaBow> iter = TurnBasedMinecraftMod.proxy.getAttackerViaBowSet().iterator(); iter.hasNext();)
|
||||
{
|
||||
AttackerViaBow attacker = iter.next();
|
||||
if(now - attacker.attackTime >= AttackerViaBow.ATTACK_TIMEOUT)
|
||||
{
|
||||
removeQueue.add(attacker);
|
||||
iter.remove();
|
||||
}
|
||||
else if(event.getSource().getTrueSource().equals(attacker.entity) && event.getSource().isProjectile())
|
||||
{
|
||||
removeQueue.add(attacker);
|
||||
iter.remove();
|
||||
if(!isValid)
|
||||
{
|
||||
Battle b = TurnBasedMinecraftMod.proxy.getBattleManager().getBattleByID(attacker.battleID);
|
||||
|
@ -48,10 +47,6 @@ public class AttackEventHandler
|
|||
}
|
||||
}
|
||||
}
|
||||
for(AttackerViaBow next = removeQueue.poll(); next != null; next = removeQueue.poll())
|
||||
{
|
||||
TurnBasedMinecraftMod.proxy.getAttackerViaBowSet().remove(next);
|
||||
}
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.util.ArrayList;
|
|||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
|
@ -518,50 +519,52 @@ public class Battle
|
|||
*/
|
||||
private boolean healthCheck()
|
||||
{
|
||||
Queue<Combatant> removeQueue = new ArrayDeque<Combatant>();
|
||||
for(Combatant c : sideA.values())
|
||||
boolean didRemove = false;
|
||||
for(Iterator<Map.Entry<Integer, Combatant>> iter = sideA.entrySet().iterator(); iter.hasNext();)
|
||||
{
|
||||
if(!c.entity.isEntityAlive())
|
||||
Map.Entry<Integer, Combatant> entry = iter.next();
|
||||
if(!entry.getValue().entity.isEntityAlive())
|
||||
{
|
||||
removeQueue.add(c);
|
||||
iter.remove();
|
||||
players.remove(entry.getKey());
|
||||
removeCombatantPostRemove(entry.getValue());
|
||||
didRemove = true;
|
||||
String category = new String();
|
||||
if(c.entityInfo != null)
|
||||
if(entry.getValue().entityInfo != null)
|
||||
{
|
||||
category = c.entityInfo.category;
|
||||
category = entry.getValue().entityInfo.category;
|
||||
}
|
||||
else if(c.entity instanceof EntityPlayer)
|
||||
else if(entry.getValue().entity instanceof EntityPlayer)
|
||||
{
|
||||
category = "player";
|
||||
}
|
||||
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DIED, c.entity.getEntityId(), 0, 0, category);
|
||||
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DIED, entry.getValue().entity.getEntityId(), 0, 0, category);
|
||||
}
|
||||
}
|
||||
for(Combatant c : sideB.values())
|
||||
for(Iterator<Map.Entry<Integer, Combatant>> iter = sideB.entrySet().iterator(); iter.hasNext();)
|
||||
{
|
||||
if(!c.entity.isEntityAlive())
|
||||
Map.Entry<Integer, Combatant> entry = iter.next();
|
||||
if(!entry.getValue().entity.isEntityAlive())
|
||||
{
|
||||
removeQueue.add(c);
|
||||
iter.remove();
|
||||
players.remove(entry.getKey());
|
||||
removeCombatantPostRemove(entry.getValue());
|
||||
didRemove = true;
|
||||
String category = new String();
|
||||
if(c.entityInfo != null)
|
||||
if(entry.getValue().entityInfo != null)
|
||||
{
|
||||
category = c.entityInfo.category;
|
||||
category = entry.getValue().entityInfo.category;
|
||||
}
|
||||
else if(c.entity instanceof EntityPlayer)
|
||||
else if(entry.getValue().entity instanceof EntityPlayer)
|
||||
{
|
||||
category = "player";
|
||||
}
|
||||
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DIED, c.entity.getEntityId(), 0, 0, category);
|
||||
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DIED, entry.getValue().entity.getEntityId(), 0, 0, category);
|
||||
}
|
||||
}
|
||||
boolean didRemove = !removeQueue.isEmpty();
|
||||
for(Combatant toRemove = removeQueue.poll(); toRemove != null; toRemove = removeQueue.poll())
|
||||
{
|
||||
removeCombatant(toRemove);
|
||||
}
|
||||
if(players.isEmpty() || sideA.isEmpty() || sideB.isEmpty())
|
||||
{
|
||||
battleEnded = true;
|
||||
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENDED, 0, 0, 0);
|
||||
}
|
||||
else if(didRemove)
|
||||
{
|
||||
|
@ -576,20 +579,20 @@ public class Battle
|
|||
*/
|
||||
private boolean isCreativeCheck()
|
||||
{
|
||||
Queue<Combatant> removeQueue = new ArrayDeque<Combatant>();
|
||||
for(Combatant c : players.values())
|
||||
{
|
||||
if(c.entity != null && ((EntityPlayer)c.entity).isCreative())
|
||||
{
|
||||
removeQueue.add(c);
|
||||
}
|
||||
}
|
||||
boolean didRemove = false;
|
||||
for(Combatant toRemove = removeQueue.poll(); toRemove != null; toRemove = removeQueue.poll())
|
||||
for(Iterator<Map.Entry<Integer, Combatant>> iter = players.entrySet().iterator(); iter.hasNext();)
|
||||
{
|
||||
Map.Entry<Integer, Combatant> entry = iter.next();
|
||||
if(entry.getValue().entity != null && ((EntityPlayer)entry.getValue().entity).isCreative())
|
||||
{
|
||||
sendMessageToAllPlayers(PacketBattleMessage.MessageType.BECAME_CREATIVE, entry.getValue().entity.getEntityId(), 0, 0);
|
||||
iter.remove();
|
||||
sideA.remove(entry.getKey());
|
||||
sideB.remove(entry.getKey());
|
||||
playerCount.decrementAndGet();
|
||||
removeCombatantPostRemove(entry.getValue());
|
||||
didRemove = true;
|
||||
removeCombatant(toRemove);
|
||||
sendMessageToAllPlayers(PacketBattleMessage.MessageType.BECAME_CREATIVE, toRemove.entity.getEntityId(), 0, 0);
|
||||
}
|
||||
}
|
||||
if(didRemove)
|
||||
{
|
||||
|
@ -633,6 +636,14 @@ public class Battle
|
|||
if(players.remove(c.entity.getEntityId()) != null)
|
||||
{
|
||||
playerCount.decrementAndGet();
|
||||
}
|
||||
removeCombatantPostRemove(c);
|
||||
}
|
||||
|
||||
private void removeCombatantPostRemove(Combatant c)
|
||||
{
|
||||
if(c.entity instanceof EntityPlayer)
|
||||
{
|
||||
TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, 0, 0, 0), (EntityPlayerMP)c.entity);
|
||||
}
|
||||
battleManager.addRecentlyLeftBattle(c);
|
||||
|
|
|
@ -1,14 +1,10 @@
|
|||
package com.seodisparate.TurnBasedMinecraft.common;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
@ -32,7 +28,7 @@ public class BattleManager
|
|||
public BattleManager(Logger logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
battleMap = new Hashtable<Integer, Battle>();
|
||||
battleMap = new HashMap<Integer, Battle>();
|
||||
battleUpdater = new BattleUpdater(this);
|
||||
updaterThread = new Thread(battleUpdater);
|
||||
updaterThread.start();
|
||||
|
@ -61,6 +57,8 @@ public class BattleManager
|
|||
if(entityInfo != null && (TurnBasedMinecraftMod.proxy.getConfig().isIgnoreBattleType(entityInfo.category) || entityInfo.ignoreBattle))
|
||||
{
|
||||
// attacked entity ignores battle
|
||||
synchronized(battleMap)
|
||||
{
|
||||
for(Battle b : battleMap.values())
|
||||
{
|
||||
if(b.hasCombatant(event.getSource().getTrueSource().getEntityId()))
|
||||
|
@ -69,6 +67,7 @@ public class BattleManager
|
|||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// logger.debug("Attack Not Canceled: attacked ignores battle");
|
||||
return false;
|
||||
}
|
||||
|
@ -76,6 +75,8 @@ public class BattleManager
|
|||
if(entityInfo != null && (TurnBasedMinecraftMod.proxy.getConfig().isIgnoreBattleType(entityInfo.category) || entityInfo.ignoreBattle))
|
||||
{
|
||||
// attacker entity ignores battle
|
||||
synchronized(battleMap)
|
||||
{
|
||||
for(Battle b : battleMap.values())
|
||||
{
|
||||
if(b.hasCombatant(event.getEntity().getEntityId()))
|
||||
|
@ -84,6 +85,7 @@ public class BattleManager
|
|||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// logger.debug("Attack Not Canceled: attacker ignores battle");
|
||||
return false;
|
||||
}
|
||||
|
@ -93,6 +95,8 @@ public class BattleManager
|
|||
Entity notInBattle = null;
|
||||
Battle battle = null;
|
||||
|
||||
synchronized(battleMap)
|
||||
{
|
||||
for(Battle b : battleMap.values())
|
||||
{
|
||||
if(b.hasCombatant(event.getSource().getTrueSource().getEntityId()))
|
||||
|
@ -126,6 +130,7 @@ public class BattleManager
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(inBattle == null)
|
||||
{
|
||||
|
@ -185,6 +190,8 @@ public class BattleManager
|
|||
Entity notInBattle = null;
|
||||
Battle battle = null;
|
||||
|
||||
synchronized(battleMap)
|
||||
{
|
||||
for(Battle b : battleMap.values())
|
||||
{
|
||||
if(b.hasCombatant(event.getEntity().getEntityId()))
|
||||
|
@ -216,6 +223,7 @@ public class BattleManager
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(battle == null)
|
||||
{
|
||||
|
@ -250,29 +258,39 @@ public class BattleManager
|
|||
}
|
||||
|
||||
private Battle createBattle(Collection<Entity> sideA, Collection<Entity> sideB)
|
||||
{
|
||||
Battle newBattle = null;
|
||||
synchronized(battleMap)
|
||||
{
|
||||
while(battleMap.containsKey(IDCounter))
|
||||
{
|
||||
++IDCounter;
|
||||
}
|
||||
Battle newBattle = new Battle(this, IDCounter, sideA, sideB, true);
|
||||
newBattle = new Battle(this, IDCounter, sideA, sideB, true);
|
||||
battleMap.put(IDCounter, newBattle);
|
||||
}
|
||||
newBattle.notifyPlayersBattleInfo();
|
||||
return newBattle;
|
||||
}
|
||||
|
||||
public Battle getBattleByID(int id)
|
||||
{
|
||||
synchronized(battleMap)
|
||||
{
|
||||
return battleMap.get(id);
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanup()
|
||||
{
|
||||
battleUpdater.setIsRunning(false);
|
||||
battleUpdater = null;
|
||||
updaterThread = null;
|
||||
synchronized(battleMap)
|
||||
{
|
||||
battleMap.clear();
|
||||
}
|
||||
}
|
||||
|
||||
protected void addRecentlyLeftBattle(Combatant c)
|
||||
{
|
||||
|
@ -291,23 +309,19 @@ public class BattleManager
|
|||
protected void updateRecentlyLeftBattle()
|
||||
{
|
||||
long current = System.nanoTime();
|
||||
Queue<Combatant> removeQueue = new ArrayDeque<Combatant>();
|
||||
synchronized(recentlyLeftBattle)
|
||||
{
|
||||
for(Combatant c : recentlyLeftBattle.values())
|
||||
for(Iterator<Map.Entry<Integer, Combatant>> iter = recentlyLeftBattle.entrySet().iterator(); iter.hasNext();)
|
||||
{
|
||||
if(current - c.time > TurnBasedMinecraftMod.proxy.getConfig().getLeaveBattleCooldownNanos())
|
||||
Map.Entry<Integer, Combatant> entry = iter.next();
|
||||
if(current - entry.getValue().time > TurnBasedMinecraftMod.proxy.getConfig().getLeaveBattleCooldownNanos())
|
||||
{
|
||||
removeQueue.add(c);
|
||||
iter.remove();
|
||||
if(entry.getValue().entity instanceof EntityPlayerMP)
|
||||
{
|
||||
TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("Timer ended, you can now attack/be-attacked again."), (EntityPlayerMP)entry.getValue().entity);
|
||||
}
|
||||
}
|
||||
for(Combatant c = removeQueue.poll(); c != null; c = removeQueue.poll())
|
||||
{
|
||||
if(c.entity instanceof EntityPlayerMP)
|
||||
{
|
||||
TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("Timer ended, you can now attack/be-attacked again."), (EntityPlayerMP)c.entity);
|
||||
}
|
||||
recentlyLeftBattle.remove(c.entity.getEntityId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.seodisparate.TurnBasedMinecraft.common;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class BattleUpdater implements Runnable
|
||||
|
@ -18,20 +18,18 @@ public class BattleUpdater implements Runnable
|
|||
@Override
|
||||
public void run()
|
||||
{
|
||||
Queue<Integer> endedQueue = new ArrayDeque<Integer>();
|
||||
Integer ended;
|
||||
while(isRunning.get())
|
||||
{
|
||||
for(Battle e : manager.battleMap.values())
|
||||
synchronized(manager.battleMap)
|
||||
{
|
||||
if(e.update())
|
||||
for(Iterator<Map.Entry<Integer, Battle>> iter = manager.battleMap.entrySet().iterator(); iter.hasNext();)
|
||||
{
|
||||
endedQueue.add(e.getId());
|
||||
Map.Entry<Integer, Battle> entry = iter.next();
|
||||
if(entry.getValue().update())
|
||||
{
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
for(ended = endedQueue.poll(); ended != null; ended = endedQueue.poll())
|
||||
{
|
||||
manager.battleMap.remove(ended);
|
||||
}
|
||||
manager.updateRecentlyLeftBattle();
|
||||
try { Thread.sleep(250); } catch (Throwable t) { /* ignored */ }
|
||||
|
|
Loading…
Reference in a new issue