Remove use of synchronized since not using threads

This commit is contained in:
Stephen Seo 2019-11-29 13:59:41 +09:00
parent 532cb21731
commit 202f918f79
2 changed files with 34 additions and 81 deletions

View file

@ -269,8 +269,6 @@ public class Battle
} }
public boolean hasCombatant(int entityID) public boolean hasCombatant(int entityID)
{
synchronized(sideAEntryQueue)
{ {
for(Combatant c : sideAEntryQueue) for(Combatant c : sideAEntryQueue)
{ {
@ -279,9 +277,6 @@ public class Battle
return true; return true;
} }
} }
}
synchronized(sideBEntryQueue)
{
for(Combatant c : sideBEntryQueue) for(Combatant c : sideBEntryQueue)
{ {
if(c.entity.getEntityId() == entityID) if(c.entity.getEntityId() == entityID)
@ -289,13 +284,10 @@ public class Battle
return true; return true;
} }
} }
}
return sideA.containsKey(entityID) || sideB.containsKey(entityID); return sideA.containsKey(entityID) || sideB.containsKey(entityID);
} }
public boolean hasCombatantInSideA(int entityID) public boolean hasCombatantInSideA(int entityID)
{
synchronized(sideAEntryQueue)
{ {
for(Combatant c : sideAEntryQueue) for(Combatant c : sideAEntryQueue)
{ {
@ -304,7 +296,6 @@ public class Battle
return true; return true;
} }
} }
}
return sideA.containsKey(entityID); return sideA.containsKey(entityID);
} }
@ -329,12 +320,9 @@ public class Battle
newCombatant.isSideA = true; newCombatant.isSideA = true;
newCombatant.battleID = getId(); newCombatant.battleID = getId();
if(isServer) if(isServer)
{
synchronized(sideAEntryQueue)
{ {
sideAEntryQueue.add(newCombatant); sideAEntryQueue.add(newCombatant);
} }
}
else else
{ {
sideA.put(e.getEntityId(), newCombatant); sideA.put(e.getEntityId(), newCombatant);
@ -395,12 +383,9 @@ public class Battle
newCombatant.isSideA = false; newCombatant.isSideA = false;
newCombatant.battleID = getId(); newCombatant.battleID = getId();
if(isServer) if(isServer)
{
synchronized(sideBEntryQueue)
{ {
sideBEntryQueue.add(newCombatant); sideBEntryQueue.add(newCombatant);
} }
}
else else
{ {
sideB.put(e.getEntityId(), newCombatant); sideB.put(e.getEntityId(), newCombatant);
@ -542,14 +527,8 @@ public class Battle
public int getSize() public int getSize()
{ {
int size = sideA.size() + sideB.size(); int size = sideA.size() + sideB.size();
synchronized(sideAEntryQueue)
{
size += sideAEntryQueue.size(); size += sideAEntryQueue.size();
}
synchronized(sideBEntryQueue)
{
size += sideBEntryQueue.size(); size += sideBEntryQueue.size();
}
return size; return size;
} }
@ -820,22 +799,16 @@ public class Battle
return true; return true;
} }
boolean combatantsChanged = false; boolean combatantsChanged = false;
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; combatantsChanged = true;
} }
}
synchronized(sideBEntryQueue)
{
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; combatantsChanged = true;
} }
}
if(TurnBasedMinecraftMod.proxy.getConfig().isFreezeCombatantsEnabled()) if(TurnBasedMinecraftMod.proxy.getConfig().isFreezeCombatantsEnabled())
{ {
enforceFreezePositions(); enforceFreezePositions();
@ -967,10 +940,7 @@ public class Battle
// have player look at attack target // have player look at attack target
((ServerPlayerEntity)nextEntity).connection.setPlayerLocation(nextEntity.posX, nextEntity.posY, nextEntity.posZ, yawDirection, pitchDirection); ((ServerPlayerEntity)nextEntity).connection.setPlayerLocation(nextEntity.posX, nextEntity.posY, nextEntity.posZ, yawDirection, pitchDirection);
BowItem itemBow = (BowItem)heldItemStack.getItem(); BowItem itemBow = (BowItem)heldItemStack.getItem();
synchronized(TurnBasedMinecraftMod.proxy.getAttackerViaBowSet())
{
TurnBasedMinecraftMod.proxy.getAttackerViaBowSet().add(new AttackerViaBow(nextEntity, getId())); TurnBasedMinecraftMod.proxy.getAttackerViaBowSet().add(new AttackerViaBow(nextEntity, getId()));
}
itemBow.onPlayerStoppedUsing(((PlayerEntity)nextEntity).getHeldItemMainhand(), nextEntity.getEntityWorld(), (LivingEntity) nextEntity, randomTimeLeft); itemBow.onPlayerStoppedUsing(((PlayerEntity)nextEntity).getHeldItemMainhand(), nextEntity.getEntityWorld(), (LivingEntity) nextEntity, randomTimeLeft);
sendMessageToAllPlayers(PacketBattleMessage.MessageType.FIRED_ARROW, nextEntity.getEntityId(), targetEntity.getEntityId(), 0); sendMessageToAllPlayers(PacketBattleMessage.MessageType.FIRED_ARROW, nextEntity.getEntityId(), targetEntity.getEntityId(), 0);
} }

View file

@ -276,15 +276,12 @@ public class BattleManager
private Battle createBattle(Collection<Entity> sideA, Collection<Entity> sideB, DimensionType dimension) private Battle createBattle(Collection<Entity> sideA, Collection<Entity> sideB, DimensionType dimension)
{ {
Battle newBattle = null; Battle newBattle = null;
synchronized(battleMap)
{
while(battleMap.containsKey(IDCounter)) while(battleMap.containsKey(IDCounter))
{ {
++IDCounter; ++IDCounter;
} }
newBattle = new Battle(this, IDCounter, sideA, sideB, true, dimension); newBattle = new Battle(this, IDCounter, sideA, sideB, true, dimension);
battleMap.put(IDCounter, newBattle); battleMap.put(IDCounter, newBattle);
}
for(Entity e : sideA) { for(Entity e : sideA) {
entityToBattleMap.put(new EntityIDDimPair(e), newBattle.getId()); entityToBattleMap.put(new EntityIDDimPair(e), newBattle.getId());
} }
@ -296,21 +293,15 @@ public class BattleManager
} }
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.setRunning(false); battleUpdater.setRunning(false);
MinecraftForge.EVENT_BUS.unregister(battleUpdater); MinecraftForge.EVENT_BUS.unregister(battleUpdater);
synchronized(battleMap)
{
battleMap.clear(); battleMap.clear();
}
battleUpdater = null; battleUpdater = null;
} }
@ -321,17 +312,13 @@ public class BattleManager
if(c.entity instanceof ServerPlayerEntity) { if(c.entity instanceof ServerPlayerEntity) {
TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(()->(ServerPlayerEntity) c.entity), new PacketGeneralMessage("You just left battle! " + config.getLeaveBattleCooldownSeconds() + " seconds until you can attack/be-attacked again!")); TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(()->(ServerPlayerEntity) c.entity), new PacketGeneralMessage("You just left battle! " + config.getLeaveBattleCooldownSeconds() + " seconds until you can attack/be-attacked again!"));
} }
synchronized(recentlyLeftBattle) {
recentlyLeftBattle.put(c.entity.getEntityId(), c); recentlyLeftBattle.put(c.entity.getEntityId(), c);
}
entityToBattleMap.remove(new EntityIDDimPair(c.entity)); entityToBattleMap.remove(new EntityIDDimPair(c.entity));
} }
protected void updateRecentlyLeftBattle() protected void updateRecentlyLeftBattle()
{ {
long current = System.nanoTime(); long current = System.nanoTime();
synchronized(recentlyLeftBattle)
{
for(Iterator<Map.Entry<Integer, Combatant>> iter = recentlyLeftBattle.entrySet().iterator(); iter.hasNext();) for(Iterator<Map.Entry<Integer, Combatant>> iter = recentlyLeftBattle.entrySet().iterator(); iter.hasNext();)
{ {
Map.Entry<Integer, Combatant> entry = iter.next(); Map.Entry<Integer, Combatant> entry = iter.next();
@ -345,15 +332,11 @@ public class BattleManager
} }
} }
} }
}
public boolean isRecentlyLeftBattle(int entityID) public boolean isRecentlyLeftBattle(int entityID)
{
synchronized(recentlyLeftBattle)
{ {
return recentlyLeftBattle.containsKey(entityID); return recentlyLeftBattle.containsKey(entityID);
} }
}
public boolean forceLeaveBattle(EntityIDDimPair entityInfo) { public boolean forceLeaveBattle(EntityIDDimPair entityInfo) {
boolean result = false; boolean result = false;