Deal with potential exceptions better.
This commit is contained in:
parent
07763cafdf
commit
1e83703f9b
|
@ -11,6 +11,7 @@ import org.bukkit.Material;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.World.Environment;
|
import org.bukkit.World.Environment;
|
||||||
import org.bukkit.util.Vector;
|
import org.bukkit.util.Vector;
|
||||||
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
|
||||||
import world.bentobox.bentobox.util.Pair;
|
import world.bentobox.bentobox.util.Pair;
|
||||||
import world.bentobox.bentobox.util.Util;
|
import world.bentobox.bentobox.util.Util;
|
||||||
|
@ -66,23 +67,20 @@ public class AsyncWorldCache {
|
||||||
* Get snapshot from cache or world
|
* Get snapshot from cache or world
|
||||||
* @param x - block coord
|
* @param x - block coord
|
||||||
* @param z - block coord
|
* @param z - block coord
|
||||||
* @return chunk snapshot
|
* @return chunk snapshot or null if there's an error getting the chunk
|
||||||
|
* @throws ExecutionException - if the chunk getting throws an exception
|
||||||
|
* @throws InterruptedException - if the future is interrupted
|
||||||
*/
|
*/
|
||||||
private ChunkSnapshot getSnap(final int x, final int z) {
|
@Nullable
|
||||||
|
private ChunkSnapshot getSnap(final int x, final int z) throws InterruptedException, ExecutionException {
|
||||||
// Convert from block to chunk coords
|
// Convert from block to chunk coords
|
||||||
Pair<Integer, Integer> key = new Pair<>((x >> 4), (z >> 4));
|
Pair<Integer, Integer> key = new Pair<>((x >> 4), (z >> 4));
|
||||||
// Get from cache if it is available
|
// Get from cache if it is available
|
||||||
if (cache.containsKey(key)) {
|
if (cache.containsKey(key)) {
|
||||||
return cache.get(key);
|
return cache.get(key);
|
||||||
}
|
}
|
||||||
ChunkSnapshot cs = null;
|
// Block on getting the chunk because this is running async
|
||||||
try {
|
ChunkSnapshot cs = getAChunk(key.x, key.z).get();
|
||||||
// Block on getting the chunk because this is running async
|
|
||||||
cs = getAChunk(key.x, key.z).get();
|
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
|
||||||
Greenhouses.getInstance().logError("Could not get chunk! " + e);
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
// Store in cache
|
// Store in cache
|
||||||
cache.put(key, cs);
|
cache.put(key, cs);
|
||||||
return cs;
|
return cs;
|
||||||
|
@ -95,15 +93,19 @@ public class AsyncWorldCache {
|
||||||
* @param x block coordinate
|
* @param x block coordinate
|
||||||
* @param y 0-255
|
* @param y 0-255
|
||||||
* @param z block coordinate
|
* @param z block coordinate
|
||||||
* @return material type
|
* @return material type or Material.AIR if there is an exception
|
||||||
*/
|
*/
|
||||||
public Material getBlockType(final int x, final int y, final int z) {
|
public Material getBlockType(final int x, final int y, final int z) {
|
||||||
// Convert block coords to chunk coords
|
// Convert block coords to chunk coords
|
||||||
// TODO: simplify this - it must be easier than this!
|
// TODO: simplify this - it must be easier than this!
|
||||||
int xx = x >= 0 ? x % 16 : (16 + (x % 16)) % 16;
|
int xx = x >= 0 ? x % 16 : (16 + (x % 16)) % 16;
|
||||||
int zz = z >= 0 ? z % 16 : (16 + (z % 16)) % 16;
|
int zz = z >= 0 ? z % 16 : (16 + (z % 16)) % 16;
|
||||||
Material m = getSnap(x,z).getBlockType(xx, y, zz);
|
try {
|
||||||
return m;
|
return getSnap(x,z).getBlockType(xx, y, zz);
|
||||||
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
|
Greenhouses.getInstance().logError("Chunk could not be obtained async! " + e);
|
||||||
|
return Material.AIR;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue