From 9072fc27882b1e8e051d7149bee415a9985461de Mon Sep 17 00:00:00 2001 From: MagicBot Date: Mon, 21 Aug 2023 04:40:11 -0400 Subject: [PATCH] Lock for dirtyload integration. --- .../InterestManagement/InterestManager.java | 42 +++++-------------- src/engine/objects/PlayerCharacter.java | 18 +++++++- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/engine/InterestManagement/InterestManager.java b/src/engine/InterestManagement/InterestManager.java index 20574411..07186d5b 100644 --- a/src/engine/InterestManagement/InterestManager.java +++ b/src/engine/InterestManagement/InterestManager.java @@ -236,7 +236,7 @@ public enum InterestManager implements Runnable { if (distanceSquared > sqr(25)) player.setLastStaticLoc(player.getLoc()); - else if (player.dirtyLoad.get() == false) + else if (player.isDirtyLoad() == false) return; // Get Statics in range @@ -314,7 +314,7 @@ public enum InterestManager implements Runnable { } loadedStaticObjects.addAll(toLoad); - player.dirtyLoad.set(false); + player.setDirtyLoad(false); } private void updateMobileList(PlayerCharacter player, ClientConnection origin) { @@ -519,22 +519,12 @@ public enum InterestManager implements Runnable { if (origin == null) return; - //Update static list + // Update loaded upbjects lists - try { - player.dirtyLoad.set(true); - updateStaticList(player, origin); - } catch (Exception e) { - Logger.error("InterestManager.updateAllStaticPlayers: " + player.getObjectUUID(), e); - } - - //Update mobile list + player.setDirtyLoad(true); + updateStaticList(player, origin); + updateMobileList(player, origin); - try { - updateMobileList(player, origin); - } catch (Exception e) { - Logger.error("InterestManager.updateAllMobilePlayers: " + player.getObjectUUID(), e); - } } public synchronized void HandleLoadForTeleport(PlayerCharacter playerCharacter) { @@ -547,22 +537,12 @@ public enum InterestManager implements Runnable { if (origin == null) return; - //Update static list + // Update loaded upbjects lists - try { - playerCharacter.dirtyLoad.set(true); - updateStaticList(playerCharacter, origin); - } catch (Exception e) { - Logger.error("InterestManager.updateAllStaticPlayers: " + playerCharacter.getObjectUUID(), e); - } - - //Update mobile list + playerCharacter.setDirtyLoad(true); + updateStaticList(playerCharacter, origin); + updateMobileList(playerCharacter, origin); - try { - updateMobileList(playerCharacter, origin); - } catch (Exception e) { - Logger.error("InterestManager.updateAllMobilePlayers: " + playerCharacter.getObjectUUID(), e); - } } public static void setObjectDirty(AbstractWorldObject abstractWorldObject) { @@ -575,7 +555,7 @@ public enum InterestManager implements Runnable { for (AbstractWorldObject playerObject : playerList) { PlayerCharacter playerCharacter = (PlayerCharacter) playerObject; - playerCharacter.dirtyLoad.set(true); + playerCharacter.setDirtyLoad(true); } } } \ No newline at end of file diff --git a/src/engine/objects/PlayerCharacter.java b/src/engine/objects/PlayerCharacter.java index 090d13ad..35dd8c5a 100644 --- a/src/engine/objects/PlayerCharacter.java +++ b/src/engine/objects/PlayerCharacter.java @@ -49,7 +49,6 @@ import java.sql.SQLException; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; @@ -173,7 +172,8 @@ public class PlayerCharacter extends AbstractCharacter { private float characterHeight = 0; private boolean lastSwimming = false; private boolean isTeleporting = false; - public AtomicBoolean dirtyLoad = new AtomicBoolean(true); + private boolean dirtyLoad = true; + private final ReadWriteLock dirtyLock = new ReentrantReadWriteLock(true); /** * No Id Constructor @@ -5512,4 +5512,18 @@ public class PlayerCharacter extends AbstractCharacter { return bargain; } + + public boolean isDirtyLoad() { + boolean dirtyValue; + dirtyLock.readLock().lock(); + dirtyValue = dirtyLoad; + dirtyLock.readLock().unlock(); + return dirtyValue; + } + + public void setDirtyLoad(boolean dirtyLoad) { + dirtyLock.writeLock().lock(); + this.dirtyLoad = dirtyLoad; + dirtyLock.writeLock().unlock(); + } }