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;
|
2019-03-02 11:16:23 +01:00
|
|
|
import java.util.function.Predicate;
|
2019-01-06 22:31:34 +01:00
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
2019-01-22 17:39:30 +01:00
|
|
|
import com.google.common.collect.Lists;
|
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-03-11 19:50:06 +01:00
|
|
|
import net.minecraft.util.math.Vec3i;
|
2019-01-06 22:31:34 +01:00
|
|
|
import net.minecraft.util.math.BlockPos.MutableBlockPos;
|
2019-03-11 19:50:06 +01:00
|
|
|
import net.minecraft.world.World;
|
2018-09-12 01:29:49 +02:00
|
|
|
|
|
|
|
public class PosHelper {
|
|
|
|
|
2019-03-11 19:50:06 +01:00
|
|
|
public static Vec3d offset(Vec3d a, Vec3i b) {
|
|
|
|
return a.add(b.getX(), b.getY(), b.getZ());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static BlockPos findSolidGroundAt(World world, BlockPos pos) {
|
|
|
|
while ((pos.getY() > 0 || !world.isOutsideBuildHeight(pos))
|
|
|
|
&& (world.isAirBlock(pos) || world.getBlockState(pos).getBlock().isReplaceable(world, pos))) {
|
|
|
|
pos = pos.down();
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2018-09-12 01:29:49 +02:00
|
|
|
public static void all(BlockPos origin, Consumer<BlockPos> consumer, EnumFacing... directions) {
|
|
|
|
for (EnumFacing facing : directions) {
|
|
|
|
consumer.accept(origin.offset(facing));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 11:16:23 +01:00
|
|
|
public static boolean some(BlockPos origin, Predicate<BlockPos> consumer, EnumFacing... directions) {
|
2018-09-12 01:29:49 +02:00
|
|
|
for (EnumFacing facing : directions) {
|
2019-03-02 11:16:23 +01:00
|
|
|
if (consumer.test(origin.offset(facing))) {
|
2018-09-12 01:29:49 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-06 22:31:34 +01:00
|
|
|
|
2019-01-22 17:39:30 +01:00
|
|
|
public static Iterators<MutableBlockPos> adjacentNeighbours(BlockPos origin) {
|
|
|
|
MutableBlockPos pos = new MutableBlockPos(origin);
|
2019-03-02 11:16:23 +01:00
|
|
|
Iterator<EnumFacing> directions = Lists.newArrayList(EnumFacing.VALUES).iterator();
|
2019-01-20 00:07:59 +01:00
|
|
|
|
2019-01-22 17:39:30 +01:00
|
|
|
return Iterators.iterate(() -> {
|
|
|
|
if (directions.hasNext()) {
|
|
|
|
EnumFacing next = directions.next();
|
|
|
|
|
|
|
|
pos.setPos(origin.getX() + next.getXOffset(), origin.getY() + next.getYOffset(), origin.getZ() + next.getZOffset());
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Iterators<MutableBlockPos> getAllInRegionMutable(BlockPos origin, IShape shape) {
|
2019-01-20 00:07:59 +01:00
|
|
|
Iterator<MutableBlockPos> iter = BlockPos.getAllInBoxMutable(
|
|
|
|
origin.add(new BlockPos(shape.getLowerBound())),
|
|
|
|
origin.add(new BlockPos(shape.getUpperBound()))
|
|
|
|
).iterator();
|
|
|
|
|
2019-01-22 17:39:30 +01:00
|
|
|
return Iterators.iterate(() -> {
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
MutableBlockPos pos = iter.next();
|
2019-01-20 00:07:59 +01:00
|
|
|
|
2019-01-22 17:39:30 +01:00
|
|
|
if (shape.isPointInside(new Vec3d(pos.subtract(origin)))) {
|
|
|
|
return pos;
|
2019-01-20 00:07:59 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-22 17:39:30 +01:00
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
2019-01-20 00:07:59 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|