From 7b712fe677d38f7b9b82355270ace383e58a8e58 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 9 Sep 2018 11:00:57 -0700 Subject: [PATCH] add synchronization, fixes #145 --- .../baritone/api/event/GameEventHandler.java | 3 +-- src/main/java/baritone/chunk/CachedWorld.java | 21 ++++++++----------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/main/java/baritone/api/event/GameEventHandler.java b/src/main/java/baritone/api/event/GameEventHandler.java index 9e569980..2ac27792 100644 --- a/src/main/java/baritone/api/event/GameEventHandler.java +++ b/src/main/java/baritone/api/event/GameEventHandler.java @@ -48,7 +48,6 @@ import net.minecraft.world.chunk.Chunk; import org.lwjgl.input.Keyboard; import java.util.ArrayList; -import java.util.List; /** * @author Brady @@ -56,7 +55,7 @@ import java.util.List; */ public final class GameEventHandler implements IGameEventListener, Helper { - private final List listeners = new ArrayList<>(); + private final ArrayList listeners = new ArrayList<>(); @Override public final void onTick(TickEvent event) { diff --git a/src/main/java/baritone/chunk/CachedWorld.java b/src/main/java/baritone/chunk/CachedWorld.java index 14097c16..de6ebd5f 100644 --- a/src/main/java/baritone/chunk/CachedWorld.java +++ b/src/main/java/baritone/chunk/CachedWorld.java @@ -28,9 +28,10 @@ import net.minecraft.world.chunk.Chunk; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.LinkedList; +import java.util.List; import java.util.concurrent.LinkedBlockingQueue; -import java.util.function.Consumer; /** * @author Brady @@ -154,7 +155,7 @@ public final class CachedWorld implements IBlockTypeAccess { return; } long start = System.nanoTime() / 1000000L; - this.cachedRegions.values().parallelStream().forEach(region -> { + allRegions().parallelStream().forEach(region -> { if (region != null) { region.save(this.directory); } @@ -163,9 +164,13 @@ public final class CachedWorld implements IBlockTypeAccess { System.out.println("World save took " + (now - start) + "ms"); } + private synchronized List allRegions() { + return new ArrayList<>(this.cachedRegions.values()); + } + public final void reloadAllFromDisk() { long start = System.nanoTime() / 1000000L; - this.cachedRegions.values().forEach(region -> { + allRegions().forEach(region -> { if (region != null) { region.load(this.directory); } @@ -181,7 +186,7 @@ public final class CachedWorld implements IBlockTypeAccess { * @param regionZ The region Z coordinate * @return The region located at the specified coordinates */ - public final CachedRegion getRegion(int regionX, int regionZ) { + public final synchronized CachedRegion getRegion(int regionX, int regionZ) { return cachedRegions.get(getRegionID(regionX, regionZ)); } @@ -201,14 +206,6 @@ public final class CachedWorld implements IBlockTypeAccess { }); } - public void forEachRegion(Consumer consumer) { - this.cachedRegions.forEach((id, r) -> { - if (r != null) { - consumer.accept(r); - } - }); - } - /** * Returns the region ID based on the region coordinates. 0 will be * returned if the specified region coordinates are out of bounds.