Skip to content

Instantly share code, notes, and snippets.

@LiamEarle
Last active August 29, 2015 14:10
Show Gist options
  • Save LiamEarle/4ba03039c017fd9ef5c2 to your computer and use it in GitHub Desktop.
Save LiamEarle/4ba03039c017fd9ef5c2 to your computer and use it in GitHub Desktop.
package org.icannt.royaltrinkets.helper;
import net.minecraft.block.Block;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import java.util.*;
public class RecursiveSelector {
private static final ForgeDirection[] FORGE_DIRECTIONS = new ForgeDirection[] {
ForgeDirection.NORTH, ForgeDirection.EAST, ForgeDirection.SOUTH, ForgeDirection.WEST, ForgeDirection.UP, ForgeDirection.DOWN
};
private List<ChunkCoordinates> blockList = new ArrayList<ChunkCoordinates>();
private LinkedList<ChunkCoordinates> blockQueue = new LinkedList<ChunkCoordinates>();
private Block[] targetBlocks;
private ChunkCoordinates origin;
private World world;
private int blockLimit;
private int blocks = 0;
public RecursiveSelector(World world, int originX, int originY, int originZ, int blockLimit, Block... targetBlocks) {
this.world = world;
this.origin = new ChunkCoordinates(originX, originY, originZ);
this.blockLimit = blockLimit;
this.targetBlocks = targetBlocks;
collectBlocks();
}
public RecursiveSelector(World world, int originX, int originY, int originZ, int blockLimit, Collection<Block> targetBlockList) {
this.world = world;
this.origin = new ChunkCoordinates(originX, originY, originZ);
this.blockLimit = blockLimit;
this.targetBlocks = (Block[]) targetBlockList.toArray();
collectBlocks();
}
public void collectBlocks() {
blockQueue.add(origin);
for (int i = 0; i < blockQueue.size(); i++) {
ChunkCoordinates blockCoord = blockQueue.get(i);
for (ForgeDirection dir : FORGE_DIRECTIONS) {
Block block = world.getBlock(blockCoord.posX + dir.offsetX, blockCoord.posY + dir.offsetY, blockCoord.posZ + dir.offsetZ);
ChunkCoordinates newCoords = new ChunkCoordinates(blockCoord.posX + dir.offsetX, blockCoord.posY + dir.offsetY, blockCoord.posZ + dir.offsetZ);
if (Arrays.asList(targetBlocks).contains(block)) {
if (blocks >= blockLimit)
return;
if (!elementExists(newCoords)) {
blockQueue.add(newCoords);
blockList.add(newCoords);
blocks++;
}
}
}
}
}
private boolean elementExists(ChunkCoordinates coords) {
for (ChunkCoordinates coordList : blockQueue) {
if (coordList.equals(coords))
return true;
}
return false;
}
public List<ChunkCoordinates> getBlockList() {
return blockList;
}
public void setBlockList(List<ChunkCoordinates> blockList) {
this.blockList = blockList;
}
public ChunkCoordinates getOrigin() {
return origin;
}
public void setOrigin(ChunkCoordinates origin) {
this.origin = origin;
}
public int getBlockLimit() {
return blockLimit;
}
public void setBlockLimit(int blockLimit) {
this.blockLimit = blockLimit;
}
public Block[] getTargetBlocks() {
return targetBlocks;
}
public void setTargetBlocks(Block[] targetBlocks) {
this.targetBlocks = targetBlocks;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment