• Hello Guest! Did you know that ProjectKorra has an official Discord server? A lot of discussion about the official server, development process, and community discussion happens over there. Feel free to join now by clicking the link below.

    Join the Discord Server

Checking blocks

YourAverageNerd

Verified Member
You of course have to know the coordinates of the block you want to check. If you have a Location instance you can call getBlock() on it which returns a value from the Materail enum, so you can then compare it in an if statement, which will return true if a block with the specified type is present at your location.
 

JettMC

Verified Member
You of course have to know the coordinates of the block you want to check. If you have a Location instance you can call getBlock() on it which returns a value from the Materail enum, so you can then compare it in an if statement, which will return true if a block with the specified type is present at your location.
First message. I had to sorry.
 

YourAverageNerd

Verified Member
Oh right, but how do I check for the number of blocks in the specified radius?
You can iterate over a Collection using a loop (for or while). In your case you could use both.
For loop approach:
Code:
int counter = 0;
for (Block block : yourListWithBlocks) {
    // this code runs for all elements in the list
    if (block.getType() == SomeMaterial) {
        counter++;
    }
}
This loop checks how many blocks in the list have the material 'SomeMaterial' and returns that number in the variable 'counter'.

While loop approach: (Disclaimer: I don't work with Iterators much, this could be wrong)
Code:
Iterator<Block> iterator = yourListWithBlocks.iterator(); // get the list's iterator
while (iterator.hasNext()) { // while there is a next entry in the iterator
    Block block = iterator.next(); // move to the next element
    if (block.getType() != SomeMaterial) {
        it.remove(); // if the block's type is not equal to 'SomeMaterial', remove it
    }
}
This method will remove all blocks that don't have 'SomeMaterial' as their type from the list.
 
Top