Skip to content

Commit df5c92c

Browse files
committed
refactor: create GetDirtAroundBlockGenerator lazy class to get dirts around
1 parent 889ab53 commit df5c92c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package br.minilambda.utils;
2+
3+
import java.util.Iterator;
4+
5+
import org.bukkit.Bukkit;
6+
import org.bukkit.Material;
7+
import org.bukkit.block.Block;
8+
9+
import br.minilambda.Constants;
10+
11+
public class GetDirtAroundBlockGenerator implements Iterator<Block> {
12+
private Block fromBlock;
13+
private int blockOffsetPosition = 0;
14+
private boolean stopNext = false;
15+
16+
public GetDirtAroundBlockGenerator(Block fromBlock){
17+
this.fromBlock = fromBlock;
18+
}
19+
20+
@Override
21+
public boolean hasNext(){
22+
// Has next while block offsets position is less than block offsets length.
23+
return this.blockOffsetPosition < Constants.AROUND_BLOCK_OFFSETS.length && !this.stopNext;
24+
}
25+
26+
@Override
27+
public Block next(){
28+
int[] offsets;
29+
int offsetX, offsetY, offsetZ;
30+
Block block;
31+
32+
while(this.hasNext()){
33+
// Get around block offesets by current position.
34+
offsets = Constants.AROUND_BLOCK_OFFSETS[this.blockOffsetPosition];
35+
// Unpacking offsets.
36+
offsetX = offsets[0];
37+
offsetY = offsets[1];
38+
offsetZ = offsets[2];
39+
40+
// Increase current offset position.
41+
this.blockOffsetPosition++;
42+
43+
// Getting relative block.
44+
block = this.fromBlock.getRelative(offsetX, offsetY, offsetZ);
45+
// Jump to next loop if block is different of dirt.
46+
if(block.getType() != Material.DIRT){
47+
continue;
48+
}
49+
// Returns dirt block.
50+
return block;
51+
}
52+
53+
// Stop next.
54+
this.stopNext = true;
55+
return null;
56+
}
57+
}

0 commit comments

Comments
 (0)