@@ -6,7 +6,7 @@ public abstract class BaseMultiForest<T extends Tree> extends BaseForest<T> {
6
6
7
7
protected T lastTree ;
8
8
9
- public BaseMultiForest (int baseOffsetX , int baseOffsetY , int baseOffsetZ ,float buildDistance ) {
9
+ public BaseMultiForest (int baseOffsetX , int baseOffsetY , int baseOffsetZ , float buildDistance ) {
10
10
super (baseOffsetX , baseOffsetY , baseOffsetZ , buildDistance );
11
11
12
12
this .forestDim = forestDimFromBuildDistance (buildDistance );
@@ -23,11 +23,13 @@ protected int getTreeIndex(int localX, int localY, int localZ) {
23
23
var treeY = localY >> 6 ;
24
24
var treeZ = localZ >> 6 ;
25
25
26
- return treeX + (treeZ * this .forestDim + treeY ) * this .forestDim ;
27
- }
26
+ if (treeX < 0 || treeX >= this .forestDim ||
27
+ treeY < 0 || treeY >= this .forestDim ||
28
+ treeZ < 0 || treeZ >= this .forestDim ) {
29
+ return Tree .OUT_OF_BOUNDS ;
30
+ }
28
31
29
- protected int getTreeIndexAbsolute (int x , int y , int z ) {
30
- return this .getTreeIndex (x - this .baseOffsetX , y - this .baseOffsetY , z - this .baseOffsetZ );
32
+ return treeX + (treeZ * this .forestDim + treeY ) * this .forestDim ;
31
33
}
32
34
33
35
@ Override
@@ -41,6 +43,10 @@ public void add(int x, int y, int z) {
41
43
var localZ = z - this .baseOffsetZ ;
42
44
43
45
var treeIndex = this .getTreeIndex (localX , localY , localZ );
46
+ if (treeIndex == Tree .OUT_OF_BOUNDS ) {
47
+ return ;
48
+ }
49
+
44
50
var tree = this .trees [treeIndex ];
45
51
46
52
if (tree == null ) {
@@ -59,18 +65,26 @@ public void add(int x, int y, int z) {
59
65
public int getPresence (int x , int y , int z ) {
60
66
if (this .lastTree != null ) {
61
67
var result = this .lastTree .getPresence (x , y , z );
62
- if (result != TraversableTree .OUT_OF_BOUNDS ) {
68
+ if (result != Tree .OUT_OF_BOUNDS ) {
63
69
return result ;
64
70
}
65
71
}
66
72
67
- var treeIndex = this .getTreeIndexAbsolute (x , y , z );
73
+ var localX = x - this .baseOffsetX ;
74
+ var localY = y - this .baseOffsetY ;
75
+ var localZ = z - this .baseOffsetZ ;
76
+
77
+ var treeIndex = this .getTreeIndex (localX , localY , localZ );
78
+ if (treeIndex == Tree .OUT_OF_BOUNDS ) {
79
+ return Tree .OUT_OF_BOUNDS ;
80
+ }
81
+
68
82
var tree = this .trees [treeIndex ];
69
83
if (tree != null ) {
70
84
this .lastTree = tree ;
71
85
return tree .getPresence (x , y , z );
72
86
}
73
- return TraversableTree .OUT_OF_BOUNDS ;
87
+ return Tree .OUT_OF_BOUNDS ;
74
88
}
75
89
76
90
protected abstract T [] makeTrees (int length );
0 commit comments