Search in sources :

Example 11 with Vector3i

use of org.spongepowered.math.vector.Vector3i in project SpongeCommon by SpongePowered.

the class SpongeTeleportHelper method getBlockLocations.

private Stream<Vector3i> getBlockLocations(ServerLocation worldLocation, int height, int width) {
    // We don't want to warp outside of the world border, so we want to check that we're within it.
    final WorldBorder.Settings worldBorder = (WorldBorder.Settings) worldLocation.world().properties().worldBorder();
    final double radius = worldBorder.getSize() / 2.0D;
    int worldBorderMinX = GenericMath.floor(worldBorder.getCenterX() - radius);
    int worldBorderMinZ = GenericMath.floor(worldBorder.getCenterZ() - radius);
    int worldBorderMaxX = GenericMath.floor(worldBorder.getCenterX() + radius);
    int worldBorderMaxZ = GenericMath.floor(worldBorder.getCenterZ() + radius);
    // Get the World and get the maximum Y value.
    int worldMaxY = worldLocation.world().max().y();
    Vector3i vectorLocation = worldLocation.blockPosition();
    // We use clamp to remain within the world confines, so we don't waste time checking blocks outside of the
    // world border and the world height.
    int minY = GenericMath.clamp(vectorLocation.y() - height, 0, worldMaxY);
    int maxY = GenericMath.clamp(vectorLocation.y() + height, 0, worldMaxY);
    int minX = GenericMath.clamp(vectorLocation.x() - width, worldBorderMinX, worldBorderMaxX);
    int maxX = GenericMath.clamp(vectorLocation.x() + width, worldBorderMinX, worldBorderMaxX);
    int minZ = GenericMath.clamp(vectorLocation.z() - width, worldBorderMinZ, worldBorderMaxZ);
    int maxZ = GenericMath.clamp(vectorLocation.z() + width, worldBorderMinZ, worldBorderMaxZ);
    // We now iterate over all possible x, y and z positions to get all possible vectors.
    List<Vector3i> vectors = new ArrayList<>();
    for (int y = minY; y <= maxY; y++) {
        for (int x = minX; x <= maxX; x++) {
            for (int z = minZ; z <= maxZ; z++) {
                vectors.add(new Vector3i(x, y, z));
            }
        }
    }
    Comparator<Vector3i> c = Comparator.comparingInt(vectorLocation::distanceSquared);
    // The compiler seems to need this to be a new line.
    // We check to see what the y location is, preferring changes in Y over X and Z, and higher over lower locations.
    c = c.thenComparing(x -> -Math.abs(vectorLocation.y() - x.y())).thenComparing(x -> -x.y());
    // Sort them according to the distance to the provided worldLocation.
    return vectors.stream().sorted(c);
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) GenericMath(org.spongepowered.math.GenericMath) Collection(java.util.Collection) Set(java.util.Set) HashMap(java.util.HashMap) Sets(com.google.common.collect.Sets) Tristate(org.spongepowered.api.util.Tristate) ArrayList(java.util.ArrayList) BlockState(org.spongepowered.api.block.BlockState) TeleportHelper(org.spongepowered.api.world.teleport.TeleportHelper) TeleportHelperFilter(org.spongepowered.api.world.teleport.TeleportHelperFilter) List(java.util.List) Stream(java.util.stream.Stream) TeleportHelperFilters(org.spongepowered.api.world.teleport.TeleportHelperFilters) WorldBorder(net.minecraft.world.level.border.WorldBorder) World(org.spongepowered.api.world.World) Map(java.util.Map) SpongeConfigs(org.spongepowered.common.applaunch.config.core.SpongeConfigs) Optional(java.util.Optional) Comparator(java.util.Comparator) Singleton(com.google.inject.Singleton) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Vector3i(org.spongepowered.math.vector.Vector3i) WorldBorder(net.minecraft.world.level.border.WorldBorder) Vector3i(org.spongepowered.math.vector.Vector3i) ArrayList(java.util.ArrayList)

Example 12 with Vector3i

use of org.spongepowered.math.vector.Vector3i in project SpongeCommon by SpongePowered.

the class VolumeStreamUtils method getBiomeStream.

@SuppressWarnings("unchecked")
public static <R extends Region<R>> VolumeStream<R, org.spongepowered.api.world.biome.Biome> getBiomeStream(final LevelReader reader, final Vector3i min, final Vector3i max, final StreamOptions options) {
    VolumeStreamUtils.validateStreamArgs(Objects.requireNonNull(min, "min"), Objects.requireNonNull(max, "max"), Objects.requireNonNull(options, "options"));
    final boolean shouldCarbonCopy = options.carbonCopy();
    final Vector3i size = max.sub(min).add(1, 1, 1);
    @MonotonicNonNull final ObjectArrayMutableBiomeBuffer backingVolume;
    if (shouldCarbonCopy) {
        final Registry<Biome> biomeRegistry;
        if (reader instanceof Level) {
            biomeRegistry = ((Level) reader).registryAccess().registry(Registry.BIOME_REGISTRY).get();
        } else {
            biomeRegistry = BuiltinRegistries.BIOME;
        }
        backingVolume = new ObjectArrayMutableBiomeBuffer(min, size, VolumeStreamUtils.nativeToSpongeRegistry(biomeRegistry));
    } else {
        backingVolume = null;
    }
    return VolumeStreamUtils.<R, org.spongepowered.api.world.biome.Biome, net.minecraft.world.level.biome.Biome, ChunkAccess, BlockPos>generateStream(min, max, options, // Ref
    (R) reader, // IdentityFunction
    (pos, biome) -> {
        if (shouldCarbonCopy) {
            backingVolume.setBiome(pos, biome);
        }
    }, // ChunkAccessor
    VolumeStreamUtils.getChunkAccessorByStatus(reader, options.loadingStyle().generateArea()), // Biome by key
    (key, biome) -> key, // Entity Accessor
    VolumeStreamUtils.getBiomesForChunkByPos(reader, min, max), // Filtered Position Entity Accessor
    (blockPos, world) -> {
        final net.minecraft.world.level.biome.Biome biome = shouldCarbonCopy ? backingVolume.getNativeBiome(blockPos.getX(), blockPos.getY(), blockPos.getZ()) : ((LevelReader) world).getBiome(blockPos);
        return new Tuple<>(blockPos, biome);
    });
}
Also used : ObjectArrayMutableBiomeBuffer(org.spongepowered.common.world.volume.buffer.biome.ObjectArrayMutableBiomeBuffer) Biome(net.minecraft.world.level.biome.Biome) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) Biome(net.minecraft.world.level.biome.Biome) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) Vector3i(org.spongepowered.math.vector.Vector3i) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) Tuple(net.minecraft.util.Tuple)

Example 13 with Vector3i

use of org.spongepowered.math.vector.Vector3i in project SpongeCommon by SpongePowered.

the class VolumeStreamUtils method getElementByPosition.

private static <T> Function<ChunkAccess, Stream<Map.Entry<BlockPos, T>>> getElementByPosition(final TriFunction<ChunkAccess, LevelChunkSection, BlockPos, T> elementAccessor, final Vector3i min, final Vector3i max) {
    // Build the min and max
    final ChunkCursor minCursor = new ChunkCursor(min);
    final ChunkCursor maxCursor = new ChunkCursor(max);
    return chunk -> {
        final ChunkPos pos = chunk.getPos();
        final int xStart = pos.x == minCursor.chunkX ? minCursor.xOffset : 0;
        // 16 because IntStream.range is upper range exclusive
        final int xEnd = pos.x == maxCursor.chunkX ? maxCursor.xOffset + 1 : 16;
        final int zStart = pos.z == minCursor.chunkZ ? minCursor.zOffset : 0;
        // 16 because IntStream.range is upper range exclusive
        final int zEnd = pos.z == maxCursor.chunkZ ? maxCursor.zOffset + 1 : 16;
        final int chunkMinX = pos.x << 4;
        final int chunkMinZ = pos.z << 4;
        return Arrays.stream(chunk.getSections()).filter(Objects::nonNull).filter(chunkSection -> chunkSection.bottomBlockY() >= minCursor.ySection && chunkSection.bottomBlockY() <= maxCursor.ySection).flatMap(chunkSection -> IntStream.range(zStart, zEnd).mapToObj(z -> IntStream.range(xStart, xEnd).mapToObj(x -> {
            final int sectionY = chunkSection.bottomBlockY();
            final int yStart = sectionY == minCursor.ySection ? minCursor.yOffset : 0;
            // plus 1 because of IntStream range exclusive
            final int yEnd = sectionY == maxCursor.ySection ? maxCursor.yOffset + 1 : 16;
            return IntStream.range(yStart, yEnd).mapToObj(y -> {
                final int adjustedX = x + chunkMinX;
                final int adjustedY = y + sectionY;
                final int adjustedZ = z + chunkMinZ;
                final BlockPos blockPos = new BlockPos(adjustedX, adjustedY, adjustedZ);
                final T apply = Objects.requireNonNull(elementAccessor.apply(chunk, chunkSection, blockPos), "Element cannot be null");
                return new AbstractMap.SimpleEntry<>(blockPos, apply);
            });
        })).flatMap(Function.identity()).flatMap(Function.identity()));
    };
}
Also used : Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) Registry(net.minecraft.core.Registry) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) ChunkBiomeContainerAccessor(org.spongepowered.common.accessor.world.level.chunk.ChunkBiomeContainerAccessor) Map(java.util.Map) ImposterProtoChunk(net.minecraft.world.level.chunk.ImposterProtoChunk) BlockEntityArchetype(org.spongepowered.api.block.entity.BlockEntityArchetype) BlockEntityAccessor(org.spongepowered.common.accessor.world.level.block.entity.BlockEntityAccessor) BuiltinRegistries(net.minecraft.data.BuiltinRegistries) ObjectArrayMutableBiomeBuffer(org.spongepowered.common.world.volume.buffer.biome.ObjectArrayMutableBiomeBuffer) Predicate(java.util.function.Predicate) Collection(java.util.Collection) StreamOptions(org.spongepowered.api.world.volume.stream.StreamOptions) Set(java.util.Set) UUID(java.util.UUID) LevelReader(net.minecraft.world.level.LevelReader) ObjectArrayMutableEntityBuffer(org.spongepowered.common.world.volume.buffer.entity.ObjectArrayMutableEntityBuffer) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) Objects(java.util.Objects) CompoundTag(net.minecraft.nbt.CompoundTag) Stream(java.util.stream.Stream) BlockPos(net.minecraft.core.BlockPos) BlockEntityType(net.minecraft.world.level.block.entity.BlockEntityType) ChunkBiomeContainer(net.minecraft.world.level.chunk.ChunkBiomeContainer) Volume(org.spongepowered.api.world.volume.Volume) Level(net.minecraft.world.level.Level) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) IntStream(java.util.stream.IntStream) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) NonNull(org.checkerframework.checker.nullness.qual.NonNull) ArrayMutableBlockBuffer(org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer) ProtoChunk(net.minecraft.world.level.chunk.ProtoChunk) BlockState(net.minecraft.world.level.block.state.BlockState) VolumeElement(org.spongepowered.api.world.volume.stream.VolumeElement) Biome(net.minecraft.world.level.biome.Biome) Region(org.spongepowered.api.world.volume.game.Region) Function(java.util.function.Function) Supplier(java.util.function.Supplier) BiConsumer(java.util.function.BiConsumer) ObjectArrayMutableBlockEntityBuffer(org.spongepowered.common.world.volume.buffer.blockentity.ObjectArrayMutableBlockEntityBuffer) WeakReference(java.lang.ref.WeakReference) Nullable(org.checkerframework.checker.nullness.qual.Nullable) LinkedHashSet(java.util.LinkedHashSet) VolumeStream(org.spongepowered.api.world.volume.stream.VolumeStream) Tuple(net.minecraft.util.Tuple) Entity(org.spongepowered.api.entity.Entity) ChunkPos(net.minecraft.world.level.ChunkPos) AbstractMap(java.util.AbstractMap) Vector3d(org.spongepowered.math.vector.Vector3d) LevelChunkSection(net.minecraft.world.level.chunk.LevelChunkSection) VecHelper(org.spongepowered.common.util.VecHelper) ChunkStatus(net.minecraft.world.level.chunk.ChunkStatus) Mth(net.minecraft.util.Mth) EntityArchetype(org.spongepowered.api.entity.EntityArchetype) Vector3i(org.spongepowered.math.vector.Vector3i) AbstractMap(java.util.AbstractMap) ChunkPos(net.minecraft.world.level.ChunkPos) BlockPos(net.minecraft.core.BlockPos)

Example 14 with Vector3i

use of org.spongepowered.math.vector.Vector3i in project SpongeCommon by SpongePowered.

the class AbstractReferentArchetypeVolume method removeBlockEntity.

@Override
public void removeBlockEntity(final int x, final int y, final int z) {
    final Vector3i transformed = this.inverseTransform(x, y, z);
    this.consumeReference(a -> a.removeBlockEntity(transformed.x(), transformed.y(), transformed.z()));
}
Also used : Vector3i(org.spongepowered.math.vector.Vector3i)

Example 15 with Vector3i

use of org.spongepowered.math.vector.Vector3i in project SpongeCommon by SpongePowered.

the class AbstractReferentArchetypeVolume method applyTransformationsToStream.

private <T> VolumeStream<ArchetypeVolume, T> applyTransformationsToStream(final Vector3i min, final Vector3i max, final StreamOptions options, final StreamCreator<A, T> streamCreator, final VolumeStreamUtils.TriFunction<VolumeElement<ArchetypeVolume, T>, Supplier<Rotation>, Supplier<Mirror>, T> elementTransform) {
    final Vector3i transformedMin = this.min();
    final Vector3i transformedMax = this.max();
    VolumeStreamUtils.validateStreamArgs(min, max, transformedMin, transformedMax, options);
    final Vector3i minDiff = min.sub(transformedMin);
    final Vector3i maxDiff = transformedMax.sub(max);
    final boolean xMirror = this.transformation.mirror(Axis.X);
    final boolean zMirror = this.transformation.mirror(Axis.Z);
    final Supplier<Mirror> mirror = xMirror ? Mirrors.FRONT_BACK : zMirror ? Mirrors.LEFT_RIGHT : Mirrors.NONE;
    return this.applyReference(a -> streamCreator.createStream(a, a.min().add(minDiff), a.max().sub(maxDiff), options)).transform(e -> VolumeElement.of(this, elementTransform.apply(e, this.transformation::rotation, mirror), this.transformStreamBlockPosition(e.position().add(VolumePositionTranslators.BLOCK_OFFSET)).sub(VolumePositionTranslators.BLOCK_OFFSET)));
}
Also used : FluidState(org.spongepowered.api.fluid.FluidState) BiFunction(java.util.function.BiFunction) VolumeElement(org.spongepowered.api.world.volume.stream.VolumeElement) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Biome(org.spongepowered.api.world.biome.Biome) Rotation(org.spongepowered.api.util.rotation.Rotation) Mirror(org.spongepowered.api.util.mirror.Mirror) Map(java.util.Map) EntityArchetypeVolume(org.spongepowered.api.world.volume.archetype.entity.EntityArchetypeVolume) ArchetypeVolume(org.spongepowered.api.world.volume.archetype.ArchetypeVolume) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockEntityArchetype(org.spongepowered.api.block.entity.BlockEntityArchetype) BiomeVolume(org.spongepowered.api.world.volume.biome.BiomeVolume) VolumeStream(org.spongepowered.api.world.volume.stream.VolumeStream) BlockEntityArchetypeVolume(org.spongepowered.api.world.volume.archetype.block.entity.BlockEntityArchetypeVolume) VolumePositionTranslators(org.spongepowered.api.world.volume.stream.VolumePositionTranslators) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Axis(org.spongepowered.api.util.Axis) StreamOptions(org.spongepowered.api.world.volume.stream.StreamOptions) Collectors(java.util.stream.Collectors) BlockState(org.spongepowered.api.block.BlockState) Mirrors(org.spongepowered.api.util.mirror.Mirrors) Transformation(org.spongepowered.api.util.transformation.Transformation) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Stream(java.util.stream.Stream) Vector3d(org.spongepowered.math.vector.Vector3d) Volume(org.spongepowered.api.world.volume.Volume) VolumeStreamUtils(org.spongepowered.common.world.volume.VolumeStreamUtils) EntityArchetypeEntry(org.spongepowered.api.world.volume.archetype.entity.EntityArchetypeEntry) MemoizedSupplier(org.spongepowered.common.util.MemoizedSupplier) Optional(java.util.Optional) BlockVolume(org.spongepowered.api.world.volume.block.BlockVolume) EntityArchetype(org.spongepowered.api.entity.EntityArchetype) Vector3i(org.spongepowered.math.vector.Vector3i) Vector3i(org.spongepowered.math.vector.Vector3i) Mirror(org.spongepowered.api.util.mirror.Mirror)

Aggregations

Vector3i (org.spongepowered.math.vector.Vector3i)59 Vector3d (org.spongepowered.math.vector.Vector3d)22 Nullable (org.checkerframework.checker.nullness.qual.Nullable)15 BlockPos (net.minecraft.core.BlockPos)14 BlockState (org.spongepowered.api.block.BlockState)14 Stream (java.util.stream.Stream)13 ChunkAccess (net.minecraft.world.level.chunk.ChunkAccess)12 StreamOptions (org.spongepowered.api.world.volume.stream.StreamOptions)12 MonotonicNonNull (org.checkerframework.checker.nullness.qual.MonotonicNonNull)11 VolumeElement (org.spongepowered.api.world.volume.stream.VolumeElement)11 Tuple (net.minecraft.util.Tuple)10 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)10 VolumeStream (org.spongepowered.api.world.volume.stream.VolumeStream)10 Function (java.util.function.Function)9 Biome (org.spongepowered.api.world.biome.Biome)9 Collection (java.util.Collection)8 Objects (java.util.Objects)8 Optional (java.util.Optional)8 UUID (java.util.UUID)8 Entity (org.spongepowered.api.entity.Entity)8