toCheck = type.getPossibleDirections(railsBlock);
} else {
toCheck = new BlockFace[] {direction};
}
double length = 0.0;
TrackIterator iter = new TrackIterator(null, null, 20, false);
// Check all directions
for (BlockFace face : toCheck) {
double trackLength = 0.0;
iter.reset(railsBlock, face);
// Skip the start block, abort if no start block was found
if (iter.hasNext()) {
iter.next();
} else {
continue;
}
// Two modes: diagonal and straight
if (diagonal) {
// Diagonal mode
BlockFace lastFace = null;
int lastAngle = Integer.MAX_VALUE;
while (iter.hasNext()) {
iter.next();
// Check that the direction alternates
if (lastFace == null) {
// Start block: store it's information
lastFace = iter.currentDirection();
} else {
BlockFace newFace = iter.currentDirection();
int newAngle = MathUtil.wrapAngle(FaceUtil.faceToYaw(newFace) - FaceUtil.faceToYaw(lastFace));
if (Math.abs(newAngle) != 90) {
// Not a 90-degree angle!
break;
}
if (lastAngle != Integer.MAX_VALUE && newAngle != -lastAngle) {
// Not the exact opposite from last time
break;
}
lastFace = newFace;
lastAngle = newAngle;
}
trackLength += MathUtil.HALFROOTOFTWO;
}
} else {
// Straight mode
while (iter.hasNext()) {
iter.next();
// Check that the direction stays the same
if (iter.currentDirection() != face) {
break;
}
trackLength++;
}
}