Unicopia/src/main/java/com/minelittlepony/unicopia/util/PosHelper.java

89 lines
2.8 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.util;
2018-09-12 01:29:49 +02:00
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;
import java.util.stream.Stream;
import com.google.common.collect.Lists;
import com.minelittlepony.unicopia.util.shape.IShape;
2018-09-12 01:29:49 +02:00
import net.minecraft.util.math.BlockPos;
2020-01-16 12:35:46 +01:00
import net.minecraft.util.math.Direction;
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;
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) {
2020-01-16 12:35:46 +01:00
while ((pos.getY() > 0 || !World.isHeightInvalid(pos))
&& (world.isAir(pos) || world.getBlockState(pos).canPlaceAt(world, pos))) {
2019-03-11 19:50:06 +01:00
pos = pos.down();
}
return pos;
}
2020-01-16 12:35:46 +01:00
public static void all(BlockPos origin, Consumer<BlockPos> consumer, Direction... directions) {
for (Direction facing : directions) {
2018-09-12 01:29:49 +02:00
consumer.accept(origin.offset(facing));
}
}
2020-01-16 12:35:46 +01:00
public static boolean some(BlockPos origin, Predicate<BlockPos> consumer, Direction... directions) {
for (Direction 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;
}
2020-01-16 12:35:46 +01:00
public static Iterators<BlockPos> adjacentNeighbours(BlockPos origin) {
BlockPos.Mutable pos = new BlockPos.Mutable(origin);
Iterator<Direction> directions = Lists.newArrayList(Direction.values()).iterator();
2019-01-20 00:07:59 +01:00
return Iterators.iterate(() -> {
if (directions.hasNext()) {
2020-01-16 12:35:46 +01:00
Direction next = directions.next();
2020-01-16 12:35:46 +01:00
pos.set(origin.getX() + next.getOffsetX(), origin.getY() + next.getOffsetY(), origin.getZ() + next.getOffsetZ());
return pos;
}
return null;
});
}
2020-01-16 12:35:46 +01:00
public static Iterators<BlockPos> getAllInRegionMutable(BlockPos origin, IShape shape) {
Iterator<BlockPos> iter = BlockPos.iterate(
2019-01-20 00:07:59 +01:00
origin.add(new BlockPos(shape.getLowerBound())),
origin.add(new BlockPos(shape.getUpperBound()))
).iterator();
return Iterators.iterate(() -> {
while (iter.hasNext()) {
2020-01-16 12:35:46 +01:00
BlockPos pos = iter.next();
2019-01-20 00:07:59 +01:00
if (shape.isPointInside(new Vec3d(pos.subtract(origin)))) {
return pos;
2019-01-20 00:07:59 +01:00
}
}
return null;
});
2019-01-20 00:07:59 +01:00
}
/**
* Creates a stream of mutable block positions ranging from the beginning position to end.
*/
2020-01-16 12:35:46 +01:00
public static Stream<BlockPos> inRegion(BlockPos from, BlockPos to) {
return BlockPos.stream(from, to);
}
2018-09-12 01:29:49 +02:00
}