From 202f918f798703245639f74f7df3ff382067fabe Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 29 Nov 2019 13:59:41 +0900 Subject: [PATCH] Remove use of synchronized since not using threads --- .../TurnBasedMinecraft/common/Battle.java | 70 ++++++------------- .../common/BattleManager.java | 45 ++++-------- 2 files changed, 34 insertions(+), 81 deletions(-) diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java index 18290a2..c3f49b5 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java @@ -270,24 +270,18 @@ public class Battle public boolean hasCombatant(int entityID) { - synchronized(sideAEntryQueue) + for(Combatant c : sideAEntryQueue) { - for(Combatant c : sideAEntryQueue) + if(c.entity.getEntityId() == entityID) { - if(c.entity.getEntityId() == entityID) - { - return true; - } + return true; } } - synchronized(sideBEntryQueue) + for(Combatant c : sideBEntryQueue) { - for(Combatant c : sideBEntryQueue) + if(c.entity.getEntityId() == entityID) { - if(c.entity.getEntityId() == entityID) - { - return true; - } + return true; } } return sideA.containsKey(entityID) || sideB.containsKey(entityID); @@ -295,14 +289,11 @@ public class Battle public boolean hasCombatantInSideA(int entityID) { - synchronized(sideAEntryQueue) + for(Combatant c : sideAEntryQueue) { - for(Combatant c : sideAEntryQueue) + if(c.entity.getEntityId() == entityID) { - if(c.entity.getEntityId() == entityID) - { - return true; - } + return true; } } return sideA.containsKey(entityID); @@ -330,10 +321,7 @@ public class Battle newCombatant.battleID = getId(); if(isServer) { - synchronized(sideAEntryQueue) - { - sideAEntryQueue.add(newCombatant); - } + sideAEntryQueue.add(newCombatant); } else { @@ -396,10 +384,7 @@ public class Battle newCombatant.battleID = getId(); if(isServer) { - synchronized(sideBEntryQueue) - { - sideBEntryQueue.add(newCombatant); - } + sideBEntryQueue.add(newCombatant); } else { @@ -542,14 +527,8 @@ public class Battle public int getSize() { int size = sideA.size() + sideB.size(); - synchronized(sideAEntryQueue) - { - size += sideAEntryQueue.size(); - } - synchronized(sideBEntryQueue) - { - size += sideBEntryQueue.size(); - } + size += sideAEntryQueue.size(); + size += sideBEntryQueue.size(); return size; } @@ -820,21 +799,15 @@ public class Battle return true; } 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); - combatantsChanged = true; - } + sideA.put(c.entity.getEntityId(), c); + 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); - combatantsChanged = true; - } + sideB.put(c.entity.getEntityId(), c); + combatantsChanged = true; } if(TurnBasedMinecraftMod.proxy.getConfig().isFreezeCombatantsEnabled()) { @@ -967,10 +940,7 @@ public class Battle // have player look at attack target ((ServerPlayerEntity)nextEntity).connection.setPlayerLocation(nextEntity.posX, nextEntity.posY, nextEntity.posZ, yawDirection, pitchDirection); 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); sendMessageToAllPlayers(PacketBattleMessage.MessageType.FIRED_ARROW, nextEntity.getEntityId(), targetEntity.getEntityId(), 0); } diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/BattleManager.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/BattleManager.java index bae83ae..6c312e7 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/BattleManager.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/BattleManager.java @@ -276,15 +276,12 @@ public class BattleManager private Battle createBattle(Collection sideA, Collection sideB, DimensionType dimension) { Battle newBattle = null; - synchronized(battleMap) + while(battleMap.containsKey(IDCounter)) { - while(battleMap.containsKey(IDCounter)) - { - ++IDCounter; - } - newBattle = new Battle(this, IDCounter, sideA, sideB, true, dimension); - battleMap.put(IDCounter, newBattle); + ++IDCounter; } + newBattle = new Battle(this, IDCounter, sideA, sideB, true, dimension); + battleMap.put(IDCounter, newBattle); for(Entity e : sideA) { entityToBattleMap.put(new EntityIDDimPair(e), newBattle.getId()); } @@ -297,20 +294,14 @@ public class BattleManager public Battle getBattleByID(int id) { - synchronized(battleMap) - { - return battleMap.get(id); - } + return battleMap.get(id); } public void cleanup() { battleUpdater.setRunning(false); MinecraftForge.EVENT_BUS.unregister(battleUpdater); - synchronized(battleMap) - { - battleMap.clear(); - } + battleMap.clear(); battleUpdater = null; } @@ -321,27 +312,22 @@ public class BattleManager 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!")); } - synchronized(recentlyLeftBattle) { - recentlyLeftBattle.put(c.entity.getEntityId(), c); - } + recentlyLeftBattle.put(c.entity.getEntityId(), c); entityToBattleMap.remove(new EntityIDDimPair(c.entity)); } protected void updateRecentlyLeftBattle() { long current = System.nanoTime(); - synchronized(recentlyLeftBattle) + for(Iterator> iter = recentlyLeftBattle.entrySet().iterator(); iter.hasNext();) { - for(Iterator> iter = recentlyLeftBattle.entrySet().iterator(); iter.hasNext();) + Map.Entry entry = iter.next(); + if(current - entry.getValue().time > TurnBasedMinecraftMod.proxy.getConfig().getLeaveBattleCooldownNanos()) { - Map.Entry entry = iter.next(); - if(current - entry.getValue().time > TurnBasedMinecraftMod.proxy.getConfig().getLeaveBattleCooldownNanos()) + iter.remove(); + if(entry.getValue().entity instanceof ServerPlayerEntity) { - iter.remove(); - if(entry.getValue().entity instanceof ServerPlayerEntity) - { - TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(()->(ServerPlayerEntity)entry.getValue().entity), new PacketGeneralMessage("Timer ended, you can now attack/be-attacked again.")); - } + TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(()->(ServerPlayerEntity)entry.getValue().entity), new PacketGeneralMessage("Timer ended, you can now attack/be-attacked again.")); } } } @@ -349,10 +335,7 @@ public class BattleManager public boolean isRecentlyLeftBattle(int entityID) { - synchronized(recentlyLeftBattle) - { - return recentlyLeftBattle.containsKey(entityID); - } + return recentlyLeftBattle.containsKey(entityID); } public boolean forceLeaveBattle(EntityIDDimPair entityInfo) {