Search in sources :

Example 46 with MutableBlockPos

use of net.minecraft.core.BlockPos.MutableBlockPos in project BCLib by paulevsGitch.

the class SplineHelper method fillLine.

public static boolean fillLine(Vector3f start, Vector3f end, WorldGenLevel world, BlockState state, BlockPos pos, Function<BlockState, Boolean> replace) {
    float dx = end.x() - start.x();
    float dy = end.y() - start.y();
    float dz = end.z() - start.z();
    float max = MHelper.max(Math.abs(dx), Math.abs(dy), Math.abs(dz));
    int count = MHelper.floor(max + 1);
    dx /= max;
    dy /= max;
    dz /= max;
    float x = start.x();
    float y = start.y();
    float z = start.z();
    boolean down = Math.abs(dy) > 0.2;
    BlockState bState;
    MutableBlockPos bPos = new MutableBlockPos();
    for (int i = 0; i < count; i++) {
        bPos.set(x + pos.getX(), y + pos.getY(), z + pos.getZ());
        bState = world.getBlockState(bPos);
        if (bState.equals(state) || replace.apply(bState)) {
            BlocksHelper.setWithoutUpdate(world, bPos, state);
            bPos.setY(bPos.getY() - 1);
            bState = world.getBlockState(bPos);
            if (down && bState.equals(state) || replace.apply(bState)) {
                BlocksHelper.setWithoutUpdate(world, bPos, state);
            }
        } else {
            return false;
        }
        x += dx;
        y += dy;
        z += dz;
    }
    bPos.set(end.x() + pos.getX(), end.y() + pos.getY(), end.z() + pos.getZ());
    bState = world.getBlockState(bPos);
    if (bState.equals(state) || replace.apply(bState)) {
        BlocksHelper.setWithoutUpdate(world, bPos, state);
        bPos.setY(bPos.getY() - 1);
        bState = world.getBlockState(bPos);
        if (down && bState.equals(state) || replace.apply(bState)) {
            BlocksHelper.setWithoutUpdate(world, bPos, state);
        }
        return true;
    } else {
        return false;
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos)

Example 47 with MutableBlockPos

use of net.minecraft.core.BlockPos.MutableBlockPos in project BCLib by paulevsGitch.

the class SplineHelper method fillLineForce.

public static void fillLineForce(Vector3f start, Vector3f end, WorldGenLevel world, BlockState state, BlockPos pos, Function<BlockState, Boolean> replace) {
    float dx = end.x() - start.x();
    float dy = end.y() - start.y();
    float dz = end.z() - start.z();
    float max = MHelper.max(Math.abs(dx), Math.abs(dy), Math.abs(dz));
    int count = MHelper.floor(max + 1);
    dx /= max;
    dy /= max;
    dz /= max;
    float x = start.x();
    float y = start.y();
    float z = start.z();
    boolean down = Math.abs(dy) > 0.2;
    BlockState bState;
    MutableBlockPos bPos = new MutableBlockPos();
    for (int i = 0; i < count; i++) {
        bPos.set(x + pos.getX(), y + pos.getY(), z + pos.getZ());
        bState = world.getBlockState(bPos);
        if (replace.apply(bState)) {
            BlocksHelper.setWithoutUpdate(world, bPos, state);
            bPos.setY(bPos.getY() - 1);
            bState = world.getBlockState(bPos);
            if (down && replace.apply(bState)) {
                BlocksHelper.setWithoutUpdate(world, bPos, state);
            }
        }
        x += dx;
        y += dy;
        z += dz;
    }
    bPos.set(end.x() + pos.getX(), end.y() + pos.getY(), end.z() + pos.getZ());
    bState = world.getBlockState(bPos);
    if (replace.apply(bState)) {
        BlocksHelper.setWithoutUpdate(world, bPos, state);
        bPos.setY(bPos.getY() - 1);
        bState = world.getBlockState(bPos);
        if (down && replace.apply(bState)) {
            BlocksHelper.setWithoutUpdate(world, bPos, state);
        }
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos)

Example 48 with MutableBlockPos

use of net.minecraft.core.BlockPos.MutableBlockPos in project BCLib by paulevsGitch.

the class SDF method getPositions.

public Set<BlockPos> getPositions(ServerLevelAccessor world, BlockPos start) {
    Set<BlockPos> blocks = Sets.newHashSet();
    Set<BlockPos> ends = Sets.newHashSet();
    Set<BlockPos> add = Sets.newHashSet();
    ends.add(new BlockPos(0, 0, 0));
    boolean run = true;
    MutableBlockPos bPos = new MutableBlockPos();
    while (run) {
        for (BlockPos center : ends) {
            for (Direction dir : Direction.values()) {
                bPos.set(center).move(dir);
                BlockPos wpos = bPos.offset(start);
                BlockState state = world.getBlockState(wpos);
                if (!blocks.contains(wpos) && canReplace.apply(state)) {
                    if (this.getDistance(bPos.getX(), bPos.getY(), bPos.getZ()) < 0) {
                        add.add(bPos.immutable());
                    }
                }
            }
        }
        ends.forEach((end) -> blocks.add(end.offset(start)));
        ends.clear();
        ends.addAll(add);
        add.clear();
        run &= !ends.isEmpty();
    }
    return blocks;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) BlockPos(net.minecraft.core.BlockPos) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) Direction(net.minecraft.core.Direction)

Example 49 with MutableBlockPos

use of net.minecraft.core.BlockPos.MutableBlockPos in project BCLib by paulevsGitch.

the class NBTStructureFeature method place.

@Override
public boolean place(FeaturePlaceContext<NoneFeatureConfiguration> context) {
    WorldGenLevel world = context.level();
    Random random = context.random();
    BlockPos center = context.origin();
    center = new BlockPos(((center.getX() >> 4) << 4) | 8, 128, ((center.getZ() >> 4) << 4) | 8);
    center = getGround(world, center);
    if (!canSpawn(world, center, random)) {
        return false;
    }
    int posY = center.getY() + 1;
    StructureTemplate structure = getStructure(world, center, random);
    Rotation rotation = getRotation(world, center, random);
    Mirror mirror = getMirror(world, center, random);
    BlockPos offset = StructureTemplate.transform(new BlockPos(structure.getSize()), mirror, rotation, BlockPos.ZERO);
    center = center.offset(0, getYOffset(structure, world, center, random) + 0.5, 0);
    BoundingBox bounds = makeBox(center);
    StructurePlaceSettings placementData = new StructurePlaceSettings().setRotation(rotation).setMirror(mirror).setBoundingBox(bounds);
    addStructureData(placementData);
    center = center.offset(-offset.getX() * 0.5, 0, -offset.getZ() * 0.5);
    structure.placeInWorld(world, center, center, placementData, random, 4);
    TerrainMerge merge = getTerrainMerge(world, center, random);
    int x1 = center.getX();
    int z1 = center.getZ();
    int x2 = x1 + offset.getX();
    int z2 = z1 + offset.getZ();
    if (merge != TerrainMerge.NONE) {
        MutableBlockPos mut = new MutableBlockPos();
        if (x2 < x1) {
            int a = x1;
            x1 = x2;
            x2 = a;
        }
        if (z2 < z1) {
            int a = z1;
            z1 = z2;
            z2 = a;
        }
        int surfMax = posY - 1;
        for (int x = x1; x <= x2; x++) {
            mut.setX(x);
            for (int z = z1; z <= z2; z++) {
                mut.setZ(z);
                mut.setY(surfMax);
                BlockState state = world.getBlockState(mut);
                if (!isTerrain(state) && state.isFaceSturdy(world, mut, Direction.DOWN)) {
                    for (int i = 0; i < 10; i++) {
                        mut.setY(mut.getY() - 1);
                        BlockState stateSt = world.getBlockState(mut);
                        if (!isTerrain(stateSt)) {
                            if (merge == TerrainMerge.SURFACE) {
                                boolean isTop = mut.getY() == surfMax && state.getMaterial().isSolidBlocking();
                                Biome b = world.getBiome(mut);
                                BlockState top = (isTop ? BiomeAPI.findTopMaterial(b) : BiomeAPI.findUnderMaterial(b)).orElse(defaultBlock);
                                BlocksHelper.setWithoutUpdate(world, mut, top);
                            } else {
                                BlocksHelper.setWithoutUpdate(world, mut, state);
                            }
                        } else {
                            if (isTerrain(state) && state.getMaterial().isSolidBlocking()) {
                                if (merge == TerrainMerge.SURFACE) {
                                    Biome b = world.getBiome(mut);
                                    BlockState bottom = BiomeAPI.findUnderMaterial(b).orElse(defaultBlock);
                                    BlocksHelper.setWithoutUpdate(world, mut, bottom);
                                } else {
                                    BlocksHelper.setWithoutUpdate(world, mut, state);
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
    return true;
}
Also used : Rotation(net.minecraft.world.level.block.Rotation) BlockState(net.minecraft.world.level.block.state.BlockState) Biome(net.minecraft.world.level.biome.Biome) Random(java.util.Random) BoundingBox(net.minecraft.world.level.levelgen.structure.BoundingBox) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) BlockPos(net.minecraft.core.BlockPos) StructureTemplate(net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate) StructurePlaceSettings(net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings) Mirror(net.minecraft.world.level.block.Mirror) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) WorldGenLevel(net.minecraft.world.level.WorldGenLevel)

Example 50 with MutableBlockPos

use of net.minecraft.core.BlockPos.MutableBlockPos in project BCLib by paulevsGitch.

the class StalactiteBlock method setPlacedBy.

@Override
public void setPlacedBy(Level world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
    boolean hasUp = isThis(world, pos.above());
    boolean hasDown = isThis(world, pos.below());
    MutableBlockPos mut = new MutableBlockPos();
    if (hasUp && hasDown) {
        boolean floor = state.getValue(IS_FLOOR);
        BlockPos second = floor ? pos.above() : pos.below();
        BlockState bState = world.getBlockState(second);
        world.setBlockAndUpdate(pos, state.setValue(SIZE, 1).setValue(IS_FLOOR, floor));
        world.setBlockAndUpdate(second, bState.setValue(SIZE, 1).setValue(IS_FLOOR, !floor));
        bState = state;
        int startSize = floor ? 1 : 2;
        mut.set(pos.getX(), pos.getY() + 1, pos.getZ());
        for (int i = 0; i < 8 && isThis(bState); i++) {
            world.setBlockAndUpdate(mut, bState.setValue(SIZE, startSize++).setValue(IS_FLOOR, false));
            mut.setY(mut.getY() + 1);
            bState = world.getBlockState(mut);
        }
        bState = state;
        startSize = floor ? 2 : 1;
        mut.set(pos.getX(), pos.getY() - 1, pos.getZ());
        for (int i = 0; i < 8 && isThis(bState); i++) {
            world.setBlockAndUpdate(mut, bState.setValue(SIZE, startSize++).setValue(IS_FLOOR, true));
            mut.setY(mut.getY() - 1);
            bState = world.getBlockState(mut);
        }
    } else if (hasDown) {
        mut.setX(pos.getX());
        mut.setZ(pos.getZ());
        for (int i = 1; i < 8; i++) {
            mut.setY(pos.getY() - i);
            if (isThis(world, mut)) {
                BlockState state2 = world.getBlockState(mut);
                int size = state2.getValue(SIZE);
                if (size < i) {
                    world.setBlockAndUpdate(mut, state2.setValue(SIZE, i).setValue(IS_FLOOR, true));
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    } else if (hasUp) {
        mut.setX(pos.getX());
        mut.setZ(pos.getZ());
        for (int i = 1; i < 8; i++) {
            mut.setY(pos.getY() + i);
            if (isThis(world, mut)) {
                BlockState state2 = world.getBlockState(mut);
                int size = state2.getValue(SIZE);
                if (size < i) {
                    world.setBlockAndUpdate(mut, state2.setValue(SIZE, i).setValue(IS_FLOOR, false));
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) BlockPos(net.minecraft.core.BlockPos) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos)

Aggregations

MutableBlockPos (net.minecraft.core.BlockPos.MutableBlockPos)60 BlockPos (net.minecraft.core.BlockPos)32 BlockState (net.minecraft.world.level.block.state.BlockState)31 Random (java.util.Random)16 WorldGenLevel (net.minecraft.world.level.WorldGenLevel)16 Direction (net.minecraft.core.Direction)11 ArrayList (java.util.ArrayList)8 Block (net.minecraft.world.level.block.Block)4 Level (net.minecraft.world.level.Level)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Lists (com.google.common.collect.Lists)2 Vector3f (com.mojang.math.Vector3f)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 Set (java.util.Set)2 Consumer (java.util.function.Consumer)2 Predicate (java.util.function.Predicate)2 Nullable (javax.annotation.Nullable)2 AccessLevel (lombok.AccessLevel)2