40
40
import static com .google .common .base .Preconditions .checkNotNull ;
41
41
42
42
/**
43
- * Stores block data as a multi-dimensional array of {@link BaseBlock}s and
43
+ * Stores block data as an array of {@link BaseBlock}s and
44
44
* other data as lists or maps.
45
45
*/
46
46
public class BlockArrayClipboard implements Clipboard {
47
47
48
48
private final Region region ;
49
49
private BlockVector3 origin ;
50
- private final BaseBlock [][][] blocks ;
51
- private BiomeType [][][] biomes = null ;
50
+ /**
51
+ * Stride for y-index, for faster access to blocks/biomes array.
52
+ */
53
+ private final int yStride ;
54
+ /**
55
+ * Stride for z-index, for faster access to blocks/biomes array.
56
+ */
57
+ private final int zStride ;
58
+ // Laid out in x-y-z order, as that's how we currently loop over positions.
59
+ private final BaseBlock [] blocks ;
60
+ // Laid out in x-y-z order, as that's how we currently loop over positions.
61
+ private BiomeType [] biomes = null ;
52
62
private final List <ClipboardEntity > entities = new ArrayList <>();
53
63
54
64
/**
@@ -64,7 +74,13 @@ public BlockArrayClipboard(Region region) {
64
74
this .origin = region .getMinimumPoint ();
65
75
66
76
BlockVector3 dimensions = getDimensions ();
67
- blocks = new BaseBlock [dimensions .x ()][dimensions .y ()][dimensions .z ()];
77
+ blocks = new BaseBlock [dimensions .x () * dimensions .y () * dimensions .z ()];
78
+ yStride = dimensions .x ();
79
+ zStride = yStride * dimensions .y ();
80
+ }
81
+
82
+ private int indexBlockVecBasedArray (BlockVector3 v ) {
83
+ return v .x () + (v .y () * yStride ) + (v .z () * zStride );
68
84
}
69
85
70
86
@ Override
@@ -125,7 +141,7 @@ public Entity createEntity(Location location, BaseEntity entity) {
125
141
public BlockState getBlock (BlockVector3 position ) {
126
142
if (region .contains (position )) {
127
143
BlockVector3 v = position .subtract (region .getMinimumPoint ());
128
- BaseBlock block = blocks [v . x ()][ v . y ()][ v . z ( )];
144
+ BaseBlock block = blocks [indexBlockVecBasedArray ( v )];
129
145
if (block != null ) {
130
146
return block .toImmutableState ();
131
147
}
@@ -138,7 +154,7 @@ public BlockState getBlock(BlockVector3 position) {
138
154
public BaseBlock getFullBlock (BlockVector3 position ) {
139
155
if (region .contains (position )) {
140
156
BlockVector3 v = position .subtract (region .getMinimumPoint ());
141
- BaseBlock block = blocks [v . x ()][ v . y ()][ v . z ( )];
157
+ BaseBlock block = blocks [indexBlockVecBasedArray ( v )];
142
158
if (block != null ) {
143
159
return block ;
144
160
}
@@ -151,7 +167,7 @@ public BaseBlock getFullBlock(BlockVector3 position) {
151
167
public <B extends BlockStateHolder <B >> boolean setBlock (BlockVector3 position , B block ) {
152
168
if (region .contains (position )) {
153
169
BlockVector3 v = position .subtract (region .getMinimumPoint ());
154
- blocks [v . x ()][ v . y ()][ v . z ( )] = block .toBaseBlock ();
170
+ blocks [indexBlockVecBasedArray ( v )] = block .toBaseBlock ();
155
171
return true ;
156
172
} else {
157
173
return false ;
@@ -168,7 +184,7 @@ public BiomeType getBiome(BlockVector3 position) {
168
184
if (biomes != null
169
185
&& position .containedWithin (getMinimumPoint (), getMaximumPoint ())) {
170
186
BlockVector3 v = position .subtract (region .getMinimumPoint ());
171
- BiomeType biomeType = biomes [v . x ()][ v . y ()][ v . z ( )];
187
+ BiomeType biomeType = biomes [indexBlockVecBasedArray ( v )];
172
188
if (biomeType != null ) {
173
189
return biomeType ;
174
190
}
@@ -182,9 +198,10 @@ public boolean setBiome(BlockVector3 position, BiomeType biome) {
182
198
if (position .containedWithin (getMinimumPoint (), getMaximumPoint ())) {
183
199
BlockVector3 v = position .subtract (region .getMinimumPoint ());
184
200
if (biomes == null ) {
185
- biomes = new BiomeType [region .getWidth ()][region .getHeight ()][region .getLength ()];
201
+ BlockVector3 dimensions = getDimensions ();
202
+ biomes = new BiomeType [dimensions .x () * dimensions .y () * dimensions .z ()];
186
203
}
187
- biomes [v . x ()][ v . y ()][ v . z ( )] = biome ;
204
+ biomes [indexBlockVecBasedArray ( v )] = biome ;
188
205
return true ;
189
206
}
190
207
return false ;
0 commit comments