Skip to content

Instantly share code, notes, and snippets.

@macalinao
Created May 13, 2014 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macalinao/3c5a76b50011075ad8c1 to your computer and use it in GitHub Desktop.
Save macalinao/3c5a76b50011075ad8c1 to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package net.og_mc.mattsponge;
import com.google.common.base.Stopwatch;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
/**
*
* @author ian
*/
public class BlockFinder {
public static Set<Block> findNearbyBlocks(Location l, Material m, int radius) {
Stopwatch s = new Stopwatch();
s.start();
World w = l.getWorld();
int minX = l.getBlockX() - radius;
int maxX = l.getBlockX() + radius;
int minZ = l.getBlockZ() - radius;
int maxZ = l.getBlockZ() + radius;
// Find eligible blocks
Set<Block> found = new HashSet<>();
for (int i = minX; i < maxX; i++) {
for (int j = 0; j < 256; j++) {
zLoop:
for (int k = minZ; k < maxZ; k++) {
Block block = w.getBlockAt(i, j, k);
if ((m == block.getType())
&& (block.getLocation().distanceSquared(l) < radius * radius)) {
for (Block blok : found) {
if (blok.getChunk().equals(block.getChunk())) {
continue zLoop;
}
}
found.add(block);
}
}
}
}
s.stop();
System.out.println("elapsed " + s.elapsedMillis() + "; radius " + radius);
return found;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment