Create CachedChunk BitSet validity method

This commit is contained in:
Brady 2018-08-10 22:25:02 -05:00
parent ff929b9260
commit 2d16157d1b
No known key found for this signature in database
GPG Key ID: 73A788379A197567

View File

@ -63,8 +63,7 @@ public final class CachedChunk implements IBlockTypeAccess {
private final BitSet data; private final BitSet data;
CachedChunk(int x, int z, BitSet data) { CachedChunk(int x, int z, BitSet data) {
if (data.size() > SIZE) validateSize(data);
throw new IllegalArgumentException("BitSet of invalid length provided");
this.x = x; this.x = x;
this.z = z; this.z = z;
@ -78,8 +77,7 @@ public final class CachedChunk implements IBlockTypeAccess {
} }
void updateContents(BitSet data) { void updateContents(BitSet data) {
if (data.size() > SIZE) validateSize(data);
throw new IllegalArgumentException("BitSet of invalid length provided");
for (int i = 0; i < data.length(); i++) for (int i = 0; i < data.length(); i++)
this.data.set(i, data.get(i)); this.data.set(i, data.get(i));
@ -117,4 +115,17 @@ public final class CachedChunk implements IBlockTypeAccess {
public static int getPositionIndex(int x, int y, int z) { public static int getPositionIndex(int x, int y, int z) {
return (x + (z << 4) + (y << 8)) * 2; return (x + (z << 4) + (y << 8)) * 2;
} }
/**
* Validates the size of an input {@link BitSet} containing the raw
* packed chunk data. Sizes that exceed {@link CachedChunk#SIZE} are
* considered invalid, and thus, an exception will be thrown.
*
* @param data The raw data
* @throws IllegalArgumentException if the bitset size exceeds the maximum size
*/
private static void validateSize(BitSet data) {
if (data.size() > SIZE)
throw new IllegalArgumentException("BitSet of invalid length provided");
}
} }