Skip to content

Instantly share code, notes, and snippets.

@tterrag1098
Created October 31, 2014 03:25
Show Gist options
  • Save tterrag1098/d26cd76ab16edf593be4 to your computer and use it in GitHub Desktop.
Save tterrag1098/d26cd76ab16edf593be4 to your computer and use it in GitHub Desktop.
Block Iterators!
package tterrag.core.common.util.blockiterators;
import java.util.Iterator;
import net.minecraft.world.World;
import tterrag.core.common.util.BlockCoord;
public abstract class AbstractBlockIterator implements Iterable<BlockCoord>, Iterator<BlockCoord>
{
protected World world;
protected BlockCoord base;
protected AbstractBlockIterator(World world, BlockCoord base)
{
this.world = world;
this.base = base;
}
@Override
public void remove()
{
throw new UnsupportedOperationException("You can't remove blocks silly!");
}
@Override
public Iterator<BlockCoord> iterator()
{
return this;
}
}
package tterrag.core.common.util.blockiterators;
import net.minecraft.world.World;
import tterrag.core.common.util.BlockCoord;
public class CubicBlockIterator extends AbstractBlockIterator
{
protected int radius;
protected int minX, minY;
protected int curX, curY, curZ;
protected int maxX, maxY, maxZ;
public CubicBlockIterator(World world, BlockCoord base, int radius)
{
super(world, base);
this.radius = radius;
curX = minX = base.x - radius;
curY = minY = base.y - radius;
curZ = base.z - radius;
maxX = curX + radius*2;
maxY = curY + radius*2;
maxZ = curZ + radius*2;
}
@Override
public BlockCoord next()
{
BlockCoord ret = new BlockCoord(curX, curY, curZ);
curX = curX == maxX ? minX : curX + 1;
curY = curX == minX ? curY == maxY ? minY : curY + 1 : curY;
curZ = curY == minY && curX == minX ? curZ + 1 : curZ;
return ret;
}
@Override
public boolean hasNext()
{
return curZ <= maxZ;
}
}
iter = new PlanarBlockIterator(Minecraft.getMinecraft().theWorld, new BlockCoord(64, 64, 64), Orientation.HORIZONTAL, 4);
System.out.println("===============================");
while (iter.hasNext())
{
System.out.println(iter.next().toString());
}
== Output ==
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:34]: ===============================
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 60 Y: 60 Z: 60
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 61 Y: 60 Z: 60
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 62 Y: 60 Z: 60
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 63 Y: 60 Z: 60
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 64 Y: 60 Z: 60
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 65 Y: 60 Z: 60
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 66 Y: 60 Z: 60
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 67 Y: 60 Z: 60
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 68 Y: 60 Z: 60
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 60 Y: 60 Z: 61
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 61 Y: 60 Z: 61
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 62 Y: 60 Z: 61
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 63 Y: 60 Z: 61
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 64 Y: 60 Z: 61
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 65 Y: 60 Z: 61
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 66 Y: 60 Z: 61
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 67 Y: 60 Z: 61
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 68 Y: 60 Z: 61
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 60 Y: 60 Z: 62
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 61 Y: 60 Z: 62
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 62 Y: 60 Z: 62
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 63 Y: 60 Z: 62
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 64 Y: 60 Z: 62
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 65 Y: 60 Z: 62
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 66 Y: 60 Z: 62
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 67 Y: 60 Z: 62
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 68 Y: 60 Z: 62
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 60 Y: 60 Z: 63
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 61 Y: 60 Z: 63
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 62 Y: 60 Z: 63
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 63 Y: 60 Z: 63
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 64 Y: 60 Z: 63
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 65 Y: 60 Z: 63
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 66 Y: 60 Z: 63
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 67 Y: 60 Z: 63
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 68 Y: 60 Z: 63
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 60 Y: 60 Z: 64
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 61 Y: 60 Z: 64
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 62 Y: 60 Z: 64
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 63 Y: 60 Z: 64
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 64 Y: 60 Z: 64
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 65 Y: 60 Z: 64
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 66 Y: 60 Z: 64
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 67 Y: 60 Z: 64
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 68 Y: 60 Z: 64
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 60 Y: 60 Z: 65
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 61 Y: 60 Z: 65
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 62 Y: 60 Z: 65
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 63 Y: 60 Z: 65
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 64 Y: 60 Z: 65
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 65 Y: 60 Z: 65
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 66 Y: 60 Z: 65
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 67 Y: 60 Z: 65
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 68 Y: 60 Z: 65
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 60 Y: 60 Z: 66
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 61 Y: 60 Z: 66
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 62 Y: 60 Z: 66
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 63 Y: 60 Z: 66
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 64 Y: 60 Z: 66
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 65 Y: 60 Z: 66
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 66 Y: 60 Z: 66
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 67 Y: 60 Z: 66
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 68 Y: 60 Z: 66
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 60 Y: 60 Z: 67
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 61 Y: 60 Z: 67
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 62 Y: 60 Z: 67
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 63 Y: 60 Z: 67
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 64 Y: 60 Z: 67
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 65 Y: 60 Z: 67
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 66 Y: 60 Z: 67
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 67 Y: 60 Z: 67
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 68 Y: 60 Z: 67
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 60 Y: 60 Z: 68
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 61 Y: 60 Z: 68
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 62 Y: 60 Z: 68
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 63 Y: 60 Z: 68
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 64 Y: 60 Z: 68
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 65 Y: 60 Z: 68
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 66 Y: 60 Z: 68
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 67 Y: 60 Z: 68
[23:23:27] [Client thread/INFO] [STDOUT]: [tterrag.core.client.handlers.ClientHandler:onClientTick:37]: X: 68 Y: 60 Z: 68
package tterrag.core.common.util.blockiterators;
import net.minecraft.world.World;
import tterrag.core.common.util.BlockCoord;
public class PlanarBlockIterator extends CubicBlockIterator
{
public static enum Orientation
{
EAST_WEST, NORTH_SOUTH, HORIZONTAL
}
private Orientation orientation;
public PlanarBlockIterator(World world, BlockCoord base, Orientation orientation, int radius)
{
super(world, base, radius);
this.orientation = orientation;
}
@Override
public BlockCoord next()
{
BlockCoord coord = new BlockCoord(curX, curY, curZ);
switch (orientation)
{
case EAST_WEST:
curY = curY == maxY ? minY : curY + 1;
curX = curY == minY ? curX + 1 : curX;
case NORTH_SOUTH:
curY = curY == maxY ? minY : curY + 1;
curZ = curY == minY ? curZ + 1 : curZ;
case HORIZONTAL:
curX = curX == maxX ? minX : curX + 1;
curZ = curX == minX ? curZ + 1 : curZ;
}
return coord;
}
@Override
public boolean hasNext()
{
return curX <= maxX && curY <= maxY && curZ <= maxZ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment