Checking on unloading
This commit is contained in:
@@ -5,15 +5,17 @@
|
|||||||
package mc.core;
|
package mc.core;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import mc.core.exception.ResourceUnloadException;
|
||||||
import mc.core.world.World;
|
import mc.core.world.World;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.lang.ref.Reference;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class Location implements Serializable{
|
public class Location implements Serializable{
|
||||||
private double x, y, z;
|
private double x, y, z;
|
||||||
private WeakReference<World> world;
|
private Reference<World> world;
|
||||||
|
|
||||||
private static int floor_double(double value) {
|
private static int floor_double(double value) {
|
||||||
int i = (int)value;
|
int i = (int)value;
|
||||||
@@ -53,6 +55,7 @@ public class Location implements Serializable{
|
|||||||
|
|
||||||
public Location(long compactValue, World world) {
|
public Location(long compactValue, World world) {
|
||||||
set(compactValue);
|
set(compactValue);
|
||||||
|
this.world = new WeakReference<>(world);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set(Location location) {
|
public void set(Location location) {
|
||||||
@@ -95,6 +98,9 @@ public class Location implements Serializable{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public World getWorld () {
|
public World getWorld () {
|
||||||
|
if (world.get() == null) {
|
||||||
|
throw new ResourceUnloadException("You're trying to get unloaded world");
|
||||||
|
}
|
||||||
return this.world.get();
|
return this.world.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package mc.core.exception;
|
||||||
|
|
||||||
|
public abstract class McCoreUncheckedException extends RuntimeException {
|
||||||
|
|
||||||
|
public McCoreUncheckedException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public McCoreUncheckedException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package mc.core.exception;
|
||||||
|
|
||||||
|
public class ResourceUnloadException extends McCoreUncheckedException {
|
||||||
|
|
||||||
|
public ResourceUnloadException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,11 +3,13 @@ package mc.world.generated_world.chunk;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import mc.core.block.Block;
|
import mc.core.block.Block;
|
||||||
import mc.core.block.BlockType;
|
import mc.core.block.BlockType;
|
||||||
|
import mc.core.exception.ResourceUnloadException;
|
||||||
import mc.core.world.Biome;
|
import mc.core.world.Biome;
|
||||||
import mc.core.world.Chunk;
|
import mc.core.world.Chunk;
|
||||||
import mc.core.world.Region;
|
import mc.core.world.Region;
|
||||||
import mc.core.world.World;
|
import mc.core.world.World;
|
||||||
|
|
||||||
|
import java.lang.ref.Reference;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
import static mc.world.generated_world.WorldConstants.WORLD_CHUNK_SIZE;
|
import static mc.world.generated_world.WorldConstants.WORLD_CHUNK_SIZE;
|
||||||
@@ -20,7 +22,7 @@ public class ChunkImpl implements Chunk{
|
|||||||
@Getter
|
@Getter
|
||||||
private final int z;
|
private final int z;
|
||||||
private final Block[][][] blocks = new Block[WORLD_CHUNK_SIZE][WORLD_CHUNK_SIZE][WORLD_CHUNK_SIZE];
|
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) {
|
public ChunkImpl(int x, int y, int z, Region region) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
@@ -109,6 +111,9 @@ public class ChunkImpl implements Chunk{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Region getRegion() {
|
public Region getRegion() {
|
||||||
|
if (region.get() == null) {
|
||||||
|
throw new ResourceUnloadException("Region is unloaded");
|
||||||
|
}
|
||||||
return region.get();
|
return region.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package mc.world.generated_world.region;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import mc.core.exception.ResourceUnloadException;
|
||||||
import mc.core.serialization.IRegionReaderWriter;
|
import mc.core.serialization.IRegionReaderWriter;
|
||||||
import mc.core.serialization.Serializer;
|
import mc.core.serialization.Serializer;
|
||||||
import mc.core.world.*;
|
import mc.core.world.*;
|
||||||
@@ -14,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.ref.Reference;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
@@ -27,7 +29,7 @@ public class RegionImpl implements Region{
|
|||||||
private final int z;
|
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 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 Biome[][] biomes = new Biome[WORLD_REGION_SIZE][WORLD_REGION_SIZE];
|
||||||
private final transient WeakReference<World> world;
|
private final transient Reference<World> world;
|
||||||
@Autowired
|
@Autowired
|
||||||
private ChunkLoader chunkLoader;
|
private ChunkLoader chunkLoader;
|
||||||
|
|
||||||
@@ -79,6 +81,9 @@ public class RegionImpl implements Region{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public World getWorld() {
|
public World getWorld() {
|
||||||
|
if (world.get() == null) {
|
||||||
|
throw new ResourceUnloadException("World is unloaded");
|
||||||
|
}
|
||||||
return world.get();
|
return world.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user