2018-09-12 01:29:49 +02:00
|
|
|
package com.minelittlepony.util;
|
|
|
|
|
2019-01-20 00:07:59 +01:00
|
|
|
import java.util.Iterator;
|
2018-09-12 01:29:49 +02:00
|
|
|
import java.util.function.Consumer;
|
|
|
|
import java.util.function.Function;
|
2019-01-06 22:31:34 +01:00
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
2019-01-20 00:07:59 +01:00
|
|
|
import com.google.common.collect.AbstractIterator;
|
2019-01-06 22:31:34 +01:00
|
|
|
import com.google.common.collect.Streams;
|
2019-01-20 00:07:59 +01:00
|
|
|
import com.minelittlepony.util.shape.IShape;
|
2018-09-12 01:29:49 +02:00
|
|
|
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2019-01-20 00:07:59 +01:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
2019-01-06 22:31:34 +01:00
|
|
|
import net.minecraft.util.math.BlockPos.MutableBlockPos;
|
2018-09-12 01:29:49 +02:00
|
|
|
|
|
|
|
public class PosHelper {
|
|
|
|
|
|
|
|
public static void all(BlockPos origin, Consumer<BlockPos> consumer, EnumFacing... directions) {
|
|
|
|
for (EnumFacing facing : directions) {
|
|
|
|
consumer.accept(origin.offset(facing));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean some(BlockPos origin, Function<BlockPos, Boolean> consumer, EnumFacing... directions) {
|
|
|
|
for (EnumFacing facing : directions) {
|
|
|
|
if (consumer.apply(origin.offset(facing))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-06 22:31:34 +01:00
|
|
|
|
2019-01-20 00:07:59 +01:00
|
|
|
public static Iterable<MutableBlockPos> getAllInRegionMutable(BlockPos origin, IShape shape) {
|
|
|
|
|
|
|
|
Iterator<MutableBlockPos> iter = BlockPos.getAllInBoxMutable(
|
|
|
|
origin.add(new BlockPos(shape.getLowerBound())),
|
|
|
|
origin.add(new BlockPos(shape.getUpperBound()))
|
|
|
|
).iterator();
|
|
|
|
|
|
|
|
return () -> new AbstractIterator<MutableBlockPos>() {
|
|
|
|
@Override
|
|
|
|
protected MutableBlockPos computeNext() {
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
MutableBlockPos pos = iter.next();
|
|
|
|
|
|
|
|
if (shape.isPointInside(new Vec3d(pos.subtract(origin)))) {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endOfData();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-06 22:31:34 +01:00
|
|
|
/**
|
|
|
|
* Creates a stream of mutable block positions ranging from the beginning position to end.
|
|
|
|
*/
|
|
|
|
public static Stream<MutableBlockPos> inRegion(BlockPos from, BlockPos to) {
|
|
|
|
return Streams.stream(BlockPos.getAllInBoxMutable(from, to));
|
|
|
|
}
|
2018-09-12 01:29:49 +02:00
|
|
|
}
|