Archived
0

Checking on unloading

This commit is contained in:
Forwolk
2018-08-04 14:31:43 +03:00
parent 610981b7b8
commit eab9947aa9
5 changed files with 39 additions and 3 deletions

View File

@@ -5,15 +5,17 @@
package mc.core;
import lombok.Data;
import mc.core.exception.ResourceUnloadException;
import mc.core.world.World;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
@Data
public class Location implements Serializable{
private double x, y, z;
private WeakReference<World> world;
private Reference<World> world;
private static int floor_double(double value) {
int i = (int)value;
@@ -53,6 +55,7 @@ public class Location implements Serializable{
public Location(long compactValue, World world) {
set(compactValue);
this.world = new WeakReference<>(world);
}
public void set(Location location) {
@@ -95,6 +98,9 @@ public class Location implements Serializable{
}
public World getWorld () {
if (world.get() == null) {
throw new ResourceUnloadException("You're trying to get unloaded world");
}
return this.world.get();
}

View File

@@ -0,0 +1,12 @@
package mc.core.exception;
public abstract class McCoreUncheckedException extends RuntimeException {
public McCoreUncheckedException() {
super();
}
public McCoreUncheckedException(String msg) {
super(msg);
}
}

View File

@@ -0,0 +1,8 @@
package mc.core.exception;
public class ResourceUnloadException extends McCoreUncheckedException {
public ResourceUnloadException(String msg) {
super(msg);
}
}

View File

@@ -3,11 +3,13 @@ package mc.world.generated_world.chunk;
import lombok.Getter;
import mc.core.block.Block;
import mc.core.block.BlockType;
import mc.core.exception.ResourceUnloadException;
import mc.core.world.Biome;
import mc.core.world.Chunk;
import mc.core.world.Region;
import mc.core.world.World;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import static mc.world.generated_world.WorldConstants.WORLD_CHUNK_SIZE;
@@ -20,7 +22,7 @@ public class ChunkImpl implements Chunk{
@Getter
private final int z;
private final Block[][][] blocks = new Block[WORLD_CHUNK_SIZE][WORLD_CHUNK_SIZE][WORLD_CHUNK_SIZE];
private final transient WeakReference<Region> region;
private final transient Reference<Region> region;
public ChunkImpl(int x, int y, int z, Region region) {
this.x = x;
@@ -109,6 +111,9 @@ public class ChunkImpl implements Chunk{
@Override
public Region getRegion() {
if (region.get() == null) {
throw new ResourceUnloadException("Region is unloaded");
}
return region.get();
}

View File

@@ -3,6 +3,7 @@ package mc.world.generated_world.region;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import mc.core.exception.ResourceUnloadException;
import mc.core.serialization.IRegionReaderWriter;
import mc.core.serialization.Serializer;
import mc.core.world.*;
@@ -14,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.text.MessageFormat;
@@ -27,7 +29,7 @@ public class RegionImpl implements Region{
private final int z;
private final ChunkProxy[][][] chunks = new ChunkProxy[WORLD_REGION_SIZE/WORLD_CHUNK_SIZE][WORLD_REGION_SIZE/WORLD_CHUNK_SIZE][WORLD_REGION_SIZE/WORLD_CHUNK_SIZE];
private final Biome[][] biomes = new Biome[WORLD_REGION_SIZE][WORLD_REGION_SIZE];
private final transient WeakReference<World> world;
private final transient Reference<World> world;
@Autowired
private ChunkLoader chunkLoader;
@@ -79,6 +81,9 @@ public class RegionImpl implements Region{
@Override
public World getWorld() {
if (world.get() == null) {
throw new ResourceUnloadException("World is unloaded");
}
return world.get();
}