Fixes and Refactorings, no major changes

This commit is contained in:
Stephen Seo 2018-10-18 13:34:57 +09:00
parent 3596bfd2ca
commit 0a26eee53a
4 changed files with 152 additions and 134 deletions

View file

@ -1,7 +1,6 @@
package com.seodisparate.TurnBasedMinecraft.common; package com.seodisparate.TurnBasedMinecraft.common;
import java.util.ArrayDeque; import java.util.Iterator;
import java.util.Queue;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage;
@ -23,20 +22,20 @@ public class AttackEventHandler
} }
else else
{ {
Queue<AttackerViaBow> removeQueue = new ArrayDeque<AttackerViaBow>();
final long now = System.nanoTime(); final long now = System.nanoTime();
boolean isValid = false; boolean isValid = false;
synchronized(TurnBasedMinecraftMod.proxy.getAttackerViaBowSet()) 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) if(now - attacker.attackTime >= AttackerViaBow.ATTACK_TIMEOUT)
{ {
removeQueue.add(attacker); iter.remove();
} }
else if(event.getSource().getTrueSource().equals(attacker.entity) && event.getSource().isProjectile()) else if(event.getSource().getTrueSource().equals(attacker.entity) && event.getSource().isProjectile())
{ {
removeQueue.add(attacker); iter.remove();
if(!isValid) if(!isValid)
{ {
Battle b = TurnBasedMinecraftMod.proxy.getBattleManager().getBattleByID(attacker.battleID); 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; return isValid;
} }

View file

@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.PriorityQueue; import java.util.PriorityQueue;
import java.util.Queue; import java.util.Queue;
@ -518,50 +519,52 @@ public class Battle
*/ */
private boolean healthCheck() private boolean healthCheck()
{ {
Queue<Combatant> removeQueue = new ArrayDeque<Combatant>(); boolean didRemove = false;
for(Combatant c : sideA.values()) 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(); 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"; 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(); 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"; 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()) if(players.isEmpty() || sideA.isEmpty() || sideB.isEmpty())
{ {
battleEnded = true; battleEnded = true;
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENDED, 0, 0, 0);
} }
else if(didRemove) else if(didRemove)
{ {
@ -576,20 +579,20 @@ public class Battle
*/ */
private boolean isCreativeCheck() 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; 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; didRemove = true;
removeCombatant(toRemove); }
sendMessageToAllPlayers(PacketBattleMessage.MessageType.BECAME_CREATIVE, toRemove.entity.getEntityId(), 0, 0);
} }
if(didRemove) if(didRemove)
{ {
@ -633,6 +636,14 @@ public class Battle
if(players.remove(c.entity.getEntityId()) != null) if(players.remove(c.entity.getEntityId()) != null)
{ {
playerCount.decrementAndGet(); 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); TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, 0, 0, 0), (EntityPlayerMP)c.entity);
} }
battleManager.addRecentlyLeftBattle(c); battleManager.addRecentlyLeftBattle(c);

View file

@ -1,14 +1,10 @@
package com.seodisparate.TurnBasedMinecraft.common; package com.seodisparate.TurnBasedMinecraft.common;
import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.Iterator;
import java.util.Hashtable;
import java.util.Map; import java.util.Map;
import java.util.Queue;
import java.util.Set;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -32,7 +28,7 @@ public class BattleManager
public BattleManager(Logger logger) public BattleManager(Logger logger)
{ {
this.logger = logger; this.logger = logger;
battleMap = new Hashtable<Integer, Battle>(); battleMap = new HashMap<Integer, Battle>();
battleUpdater = new BattleUpdater(this); battleUpdater = new BattleUpdater(this);
updaterThread = new Thread(battleUpdater); updaterThread = new Thread(battleUpdater);
updaterThread.start(); updaterThread.start();
@ -61,6 +57,8 @@ public class BattleManager
if(entityInfo != null && (TurnBasedMinecraftMod.proxy.getConfig().isIgnoreBattleType(entityInfo.category) || entityInfo.ignoreBattle)) if(entityInfo != null && (TurnBasedMinecraftMod.proxy.getConfig().isIgnoreBattleType(entityInfo.category) || entityInfo.ignoreBattle))
{ {
// attacked entity ignores battle // attacked entity ignores battle
synchronized(battleMap)
{
for(Battle b : battleMap.values()) for(Battle b : battleMap.values())
{ {
if(b.hasCombatant(event.getSource().getTrueSource().getEntityId())) if(b.hasCombatant(event.getSource().getTrueSource().getEntityId()))
@ -69,6 +67,7 @@ public class BattleManager
return true; return true;
} }
} }
}
// logger.debug("Attack Not Canceled: attacked ignores battle"); // logger.debug("Attack Not Canceled: attacked ignores battle");
return false; return false;
} }
@ -76,6 +75,8 @@ public class BattleManager
if(entityInfo != null && (TurnBasedMinecraftMod.proxy.getConfig().isIgnoreBattleType(entityInfo.category) || entityInfo.ignoreBattle)) if(entityInfo != null && (TurnBasedMinecraftMod.proxy.getConfig().isIgnoreBattleType(entityInfo.category) || entityInfo.ignoreBattle))
{ {
// attacker entity ignores battle // attacker entity ignores battle
synchronized(battleMap)
{
for(Battle b : battleMap.values()) for(Battle b : battleMap.values())
{ {
if(b.hasCombatant(event.getEntity().getEntityId())) if(b.hasCombatant(event.getEntity().getEntityId()))
@ -84,6 +85,7 @@ public class BattleManager
return true; return true;
} }
} }
}
// logger.debug("Attack Not Canceled: attacker ignores battle"); // logger.debug("Attack Not Canceled: attacker ignores battle");
return false; return false;
} }
@ -93,6 +95,8 @@ public class BattleManager
Entity notInBattle = null; Entity notInBattle = null;
Battle battle = null; Battle battle = null;
synchronized(battleMap)
{
for(Battle b : battleMap.values()) for(Battle b : battleMap.values())
{ {
if(b.hasCombatant(event.getSource().getTrueSource().getEntityId())) if(b.hasCombatant(event.getSource().getTrueSource().getEntityId()))
@ -126,6 +130,7 @@ public class BattleManager
} }
} }
} }
}
if(inBattle == null) if(inBattle == null)
{ {
@ -185,6 +190,8 @@ public class BattleManager
Entity notInBattle = null; Entity notInBattle = null;
Battle battle = null; Battle battle = null;
synchronized(battleMap)
{
for(Battle b : battleMap.values()) for(Battle b : battleMap.values())
{ {
if(b.hasCombatant(event.getEntity().getEntityId())) if(b.hasCombatant(event.getEntity().getEntityId()))
@ -216,6 +223,7 @@ public class BattleManager
} }
} }
} }
}
if(battle == null) if(battle == null)
{ {
@ -250,29 +258,39 @@ public class BattleManager
} }
private Battle createBattle(Collection<Entity> sideA, Collection<Entity> sideB) private Battle createBattle(Collection<Entity> sideA, Collection<Entity> sideB)
{
Battle newBattle = null;
synchronized(battleMap)
{ {
while(battleMap.containsKey(IDCounter)) while(battleMap.containsKey(IDCounter))
{ {
++IDCounter; ++IDCounter;
} }
Battle newBattle = new Battle(this, IDCounter, sideA, sideB, true); newBattle = new Battle(this, IDCounter, sideA, sideB, true);
battleMap.put(IDCounter, newBattle); battleMap.put(IDCounter, newBattle);
}
newBattle.notifyPlayersBattleInfo(); newBattle.notifyPlayersBattleInfo();
return newBattle; return newBattle;
} }
public Battle getBattleByID(int id) public Battle getBattleByID(int id)
{
synchronized(battleMap)
{ {
return battleMap.get(id); return battleMap.get(id);
} }
}
public void cleanup() public void cleanup()
{ {
battleUpdater.setIsRunning(false); battleUpdater.setIsRunning(false);
battleUpdater = null; battleUpdater = null;
updaterThread = null; updaterThread = null;
synchronized(battleMap)
{
battleMap.clear(); battleMap.clear();
} }
}
protected void addRecentlyLeftBattle(Combatant c) protected void addRecentlyLeftBattle(Combatant c)
{ {
@ -291,23 +309,19 @@ public class BattleManager
protected void updateRecentlyLeftBattle() protected void updateRecentlyLeftBattle()
{ {
long current = System.nanoTime(); long current = System.nanoTime();
Queue<Combatant> removeQueue = new ArrayDeque<Combatant>();
synchronized(recentlyLeftBattle) 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());
} }
} }
} }

View file

@ -1,7 +1,7 @@
package com.seodisparate.TurnBasedMinecraft.common; package com.seodisparate.TurnBasedMinecraft.common;
import java.util.ArrayDeque; import java.util.Iterator;
import java.util.Queue; import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
public class BattleUpdater implements Runnable public class BattleUpdater implements Runnable
@ -18,20 +18,18 @@ public class BattleUpdater implements Runnable
@Override @Override
public void run() public void run()
{ {
Queue<Integer> endedQueue = new ArrayDeque<Integer>();
Integer ended;
while(isRunning.get()) 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(); manager.updateRecentlyLeftBattle();
try { Thread.sleep(250); } catch (Throwable t) { /* ignored */ } try { Thread.sleep(250); } catch (Throwable t) { /* ignored */ }