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

77 lines
2.7 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.util;
2018-09-12 01:29:49 +02:00
2021-02-13 12:50:24 +01:00
import java.util.Arrays;
2019-01-20 00:07:59 +01:00
import java.util.Iterator;
2020-04-24 00:17:38 +02:00
import java.util.Spliterator;
import java.util.Spliterators.AbstractSpliterator;
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;
2020-04-24 00:17:38 +02:00
import java.util.stream.StreamSupport;
import com.google.common.collect.Lists;
2020-04-15 18:12:00 +02:00
import com.minelittlepony.unicopia.util.shape.Shape;
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
2020-04-15 19:06:45 +02:00
public interface PosHelper {
2018-09-12 01:29:49 +02:00
2021-02-13 12:50:24 +01:00
Direction[] HORIZONTAL = Arrays.stream(Direction.values()).filter(d -> d.getAxis().isHorizontal()).toArray(Direction[]::new);
2020-04-15 19:06:45 +02:00
static Vec3d offset(Vec3d a, Vec3i b) {
2019-03-11 19:50:06 +01:00
return a.add(b.getX(), b.getY(), b.getZ());
}
2020-04-15 19:06:45 +02:00
static BlockPos findSolidGroundAt(World world, BlockPos pos) {
2020-12-17 19:17:47 +01:00
while ((pos.getY() > 0 || !World.isInBuildLimit(pos))
2020-05-07 13:13:57 +02:00
&& (world.isAir(pos) || !world.getBlockState(pos).canPlaceAt(world, pos))) {
2019-03-11 19:50:06 +01:00
pos = pos.down();
}
return pos;
}
2020-04-15 19:06:45 +02:00
static void all(BlockPos origin, Consumer<BlockPos> consumer, Direction... directions) {
2020-01-16 12:35:46 +01:00
for (Direction facing : directions) {
2018-09-12 01:29:49 +02:00
consumer.accept(origin.offset(facing));
}
}
2020-10-10 16:25:49 +02:00
static boolean any(BlockPos origin, Predicate<BlockPos> consumer, Direction... directions) {
2020-01-16 12:35:46 +01:00
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-04-24 00:17:38 +02:00
static Stream<BlockPos> adjacentNeighbours(BlockPos origin) {
2020-10-10 16:25:49 +02:00
return StreamSupport.stream(new AbstractSpliterator<BlockPos>(Direction.values().length, Spliterator.SIZED) {
private final BlockPos.Mutable pos = new BlockPos.Mutable();
private final Iterator<Direction> iter = Lists.newArrayList(Direction.values()).iterator();
2020-04-24 00:17:38 +02:00
@Override
public boolean tryAdvance(Consumer<? super BlockPos> consumer) {
if (iter.hasNext()) {
Direction next = iter.next();
2020-10-10 16:25:49 +02:00
consumer.accept(pos.set(origin.getX() + next.getOffsetX(), origin.getY() + next.getOffsetY(), origin.getZ() + next.getOffsetZ()));
2020-04-24 00:17:38 +02:00
return true;
2019-01-20 00:07:59 +01:00
}
2020-04-24 00:17:38 +02:00
return false;
2019-01-20 00:07:59 +01:00
}
2020-04-24 00:17:38 +02:00
}, false);
2019-01-20 00:07:59 +01:00
}
2020-04-24 00:17:38 +02:00
static Stream<BlockPos> getAllInRegionMutable(BlockPos origin, Shape shape) {
return BlockPos.stream(
origin.add(new BlockPos(shape.getLowerBound())),
origin.add(new BlockPos(shape.getUpperBound()))
2020-06-26 11:44:47 +02:00
).filter(pos -> shape.isPointInside(Vec3d.of(pos.subtract(origin))));
}
2018-09-12 01:29:49 +02:00
}