add synchronization, fixes #145

This commit is contained in:
Leijurv 2018-09-09 11:00:57 -07:00
parent 4a13b54e50
commit 7b712fe677
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
2 changed files with 10 additions and 14 deletions

View File

@ -48,7 +48,6 @@ import net.minecraft.world.chunk.Chunk;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
/** /**
* @author Brady * @author Brady
@ -56,7 +55,7 @@ import java.util.List;
*/ */
public final class GameEventHandler implements IGameEventListener, Helper { public final class GameEventHandler implements IGameEventListener, Helper {
private final List<IGameEventListener> listeners = new ArrayList<>(); private final ArrayList<IGameEventListener> listeners = new ArrayList<>();
@Override @Override
public final void onTick(TickEvent event) { public final void onTick(TickEvent event) {

View File

@ -28,9 +28,10 @@ import net.minecraft.world.chunk.Chunk;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Consumer;
/** /**
* @author Brady * @author Brady
@ -154,7 +155,7 @@ public final class CachedWorld implements IBlockTypeAccess {
return; return;
} }
long start = System.nanoTime() / 1000000L; long start = System.nanoTime() / 1000000L;
this.cachedRegions.values().parallelStream().forEach(region -> { allRegions().parallelStream().forEach(region -> {
if (region != null) { if (region != null) {
region.save(this.directory); region.save(this.directory);
} }
@ -163,9 +164,13 @@ public final class CachedWorld implements IBlockTypeAccess {
System.out.println("World save took " + (now - start) + "ms"); System.out.println("World save took " + (now - start) + "ms");
} }
private synchronized List<CachedRegion> allRegions() {
return new ArrayList<>(this.cachedRegions.values());
}
public final void reloadAllFromDisk() { public final void reloadAllFromDisk() {
long start = System.nanoTime() / 1000000L; long start = System.nanoTime() / 1000000L;
this.cachedRegions.values().forEach(region -> { allRegions().forEach(region -> {
if (region != null) { if (region != null) {
region.load(this.directory); region.load(this.directory);
} }
@ -181,7 +186,7 @@ public final class CachedWorld implements IBlockTypeAccess {
* @param regionZ The region Z coordinate * @param regionZ The region Z coordinate
* @return The region located at the specified coordinates * @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)); return cachedRegions.get(getRegionID(regionX, regionZ));
} }
@ -201,14 +206,6 @@ public final class CachedWorld implements IBlockTypeAccess {
}); });
} }
public void forEachRegion(Consumer<CachedRegion> consumer) {
this.cachedRegions.forEach((id, r) -> {
if (r != null) {
consumer.accept(r);
}
});
}
/** /**
* Returns the region ID based on the region coordinates. 0 will be * Returns the region ID based on the region coordinates. 0 will be
* returned if the specified region coordinates are out of bounds. * returned if the specified region coordinates are out of bounds.