Search in sources :

Example 41 with MutableBlockPos

use of net.minecraft.core.BlockPos.MutableBlockPos in project BYG by AOCAWOL.

the class BYGAbstractTreeFeature method getTransformedPos.

public static BlockPos getTransformedPos(BYGTreeConfig config, BlockPos startPos, BlockPos pos) {
    Rotation rotation = config.getRotation();
    Mirror mirror = config.getMirror();
    BlockPos blockPos = FeatureGenUtil.extractOffset(startPos, pos);
    if (blockPos instanceof MutableBlockPos) {
        FeatureGenUtil.transformMutable((MutableBlockPos) blockPos, mirror, rotation);
        ((MutableBlockPos) blockPos).move(startPos.getX(), 0, startPos.getZ());
        return blockPos;
    }
    return FeatureGenUtil.transform(blockPos, mirror, rotation).offset(startPos.getX(), 0, startPos.getZ());
}
Also used : MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) BlockPos(net.minecraft.core.BlockPos) Rotation(net.minecraft.world.level.block.Rotation) Mirror(net.minecraft.world.level.block.Mirror) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos)

Example 42 with MutableBlockPos

use of net.minecraft.core.BlockPos.MutableBlockPos in project TinkersConstruct by SlimeKnights.

the class MultiblockCuboid method detectLayer.

/**
 * Detects an inner layer of the structure. That is, an area with an empty center
 * @param world     Level instance
 * @param from      Start position for the layer
 * @param to        End position for the layer
 * @param consumer  Consumer for any extra positions in this region
 * @return  True if this layer is valid, false otherwise
 */
@SuppressWarnings("deprecation")
protected MultiblockResult detectLayer(Level world, BlockPos from, BlockPos to, Consumer<Collection<BlockPos>> consumer) {
    // ensure its loaded
    if (!world.hasChunksAt(from, to)) {
        return NOT_LOADED;
    }
    // temporary list of position candidates, so we can only add them if successful
    List<BlockPos> candidates = Lists.newArrayList();
    // validate frame first
    MutableBlockPos mutable = new MutableBlockPos();
    int height = from.getY();
    if (hasFrame) {
        // function to check a single position in the frame
        Predicate<BlockPos> frameCheck = pos -> isValidBlock(world, pos, CuboidSide.WALL, true);
        // we only have 4 corner blocks to check
        if (!frameCheck.test(from))
            return MultiblockResult.error(from.immutable(), INVALID_WALL_FRAME);
        if (!frameCheck.test(mutable.set(from.getX(), height, to.getZ())))
            return MultiblockResult.error(mutable.immutable(), INVALID_WALL_FRAME);
        if (!frameCheck.test(mutable.set(to.getX(), height, from.getZ())))
            return MultiblockResult.error(mutable.immutable(), INVALID_WALL_FRAME);
        if (!frameCheck.test(to))
            return MultiblockResult.error(to.immutable(), INVALID_WALL_FRAME);
    }
    // validate the inside
    for (int x = from.getX() + 1; x < to.getX(); x++) {
        for (int z = from.getZ() + 1; z < to.getZ(); z++) {
            // ensure its a valid block for inside the structure
            mutable.set(x, height, z);
            if (isInnerBlock(world, mutable)) {
                // any non airblocks are added to extra blocks, this region is ignored by default
                if (!world.isEmptyBlock(mutable)) {
                    candidates.add(mutable.immutable());
                }
            } else {
                return MultiblockResult.error(mutable.immutable(), INVALID_INNER_BLOCK);
            }
        }
    }
    // validate the 4 sides
    Predicate<BlockPos> wallCheck = pos -> isValidBlock(world, pos, CuboidSide.WALL, false);
    for (int x = from.getX() + 1; x < to.getX(); x++) {
        if (!wallCheck.test(mutable.set(x, height, from.getZ())))
            return MultiblockResult.error(mutable.immutable(), INVALID_WALL_BLOCK);
        if (!wallCheck.test(mutable.set(x, height, to.getZ())))
            return MultiblockResult.error(mutable.immutable(), INVALID_WALL_BLOCK);
    }
    for (int z = from.getZ() + 1; z < to.getZ(); z++) {
        if (!wallCheck.test(mutable.set(from.getX(), height, z)))
            return MultiblockResult.error(mutable.immutable(), INVALID_WALL_BLOCK);
        if (!wallCheck.test(mutable.set(to.getX(), height, z)))
            return MultiblockResult.error(mutable.immutable(), INVALID_WALL_BLOCK);
    }
    // was successful, add all candidates
    consumer.accept(candidates);
    return MultiblockResult.SUCCESS;
}
Also used : Setter(lombok.Setter) Tag(net.minecraft.nbt.Tag) Getter(lombok.Getter) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) Direction(net.minecraft.core.Direction) RequiredArgsConstructor(lombok.RequiredArgsConstructor) BlockState(net.minecraft.world.level.block.state.BlockState) MultiblockResult.error(slimeknights.tconstruct.smeltery.block.entity.multiblock.MultiblockResult.error) ArrayList(java.util.ArrayList) TagUtil(slimeknights.tconstruct.library.utils.TagUtil) Lists(com.google.common.collect.Lists) AccessLevel(lombok.AccessLevel) Nullable(javax.annotation.Nullable) Plane(net.minecraft.core.Direction.Plane) ImmutableSet(com.google.common.collect.ImmutableSet) Component(net.minecraft.network.chat.Component) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) TConstruct(slimeknights.tconstruct.TConstruct) Consumer(java.util.function.Consumer) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) BlockPos(net.minecraft.core.BlockPos) Level(net.minecraft.world.level.Level) Collections(java.util.Collections) ListTag(net.minecraft.nbt.ListTag) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) BlockPos(net.minecraft.core.BlockPos) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos)

Example 43 with MutableBlockPos

use of net.minecraft.core.BlockPos.MutableBlockPos in project TinkersConstruct by SlimeKnights.

the class AbstractWalkerModifier method onWalk.

@Override
public void onWalk(IToolStackView tool, int level, LivingEntity living, BlockPos prevPos, BlockPos newPos) {
    if (living.isOnGround() && !tool.isBroken() && !living.level.isClientSide) {
        float radius = Math.min(16, getRadius(tool, level));
        MutableBlockPos mutable = new MutableBlockPos();
        Level world = living.level;
        Vec3 posVec = living.position();
        BlockPos center = new BlockPos(posVec.x, posVec.y + 0.5, posVec.z);
        for (BlockPos pos : BlockPos.betweenClosed(center.offset(-radius, 0, -radius), center.offset(radius, 0, radius))) {
            if (pos.closerThan(living.position(), radius)) {
                walkOn(tool, level, living, world, pos, mutable);
                if (tool.isBroken()) {
                    break;
                }
            }
        }
    }
}
Also used : Vec3(net.minecraft.world.phys.Vec3) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos)

Example 44 with MutableBlockPos

use of net.minecraft.core.BlockPos.MutableBlockPos in project Create by Creators-of-Create.

the class ConnectedPillarBlock method updateColumn.

private BlockState updateColumn(Level level, BlockPos pos, BlockState state, boolean present) {
    MutableBlockPos currentPos = new MutableBlockPos();
    Axis axis = state.getValue(AXIS);
    for (Direction connection : Iterate.directions) {
        if (connection.getAxis() == axis)
            continue;
        boolean connect = true;
        Move: for (Direction movement : Iterate.directionsInAxis(axis)) {
            currentPos.set(pos);
            for (int i = 0; i < 1000; i++) {
                if (!level.isAreaLoaded(currentPos, 1))
                    break;
                BlockState other1 = currentPos.equals(pos) ? state : level.getBlockState(currentPos);
                BlockState other2 = level.getBlockState(currentPos.relative(connection));
                boolean col1 = canConnect(state, other1);
                boolean col2 = canConnect(state, other2);
                currentPos.move(movement);
                if (!col1 && !col2)
                    break;
                if (col1 && col2)
                    continue;
                connect = false;
                break Move;
            }
        }
        state = setConnection(state, connection, connect);
    }
    return state;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) Direction(net.minecraft.core.Direction) AxisDirection(net.minecraft.core.Direction.AxisDirection) Axis(net.minecraft.core.Direction.Axis)

Example 45 with MutableBlockPos

use of net.minecraft.core.BlockPos.MutableBlockPos in project Create by Creators-of-Create.

the class LayeredOreFeature method place.

public boolean place(FeaturePlaceContext<ConfigDrivenOreConfiguration> pContext) {
    Random random = pContext.random();
    BlockPos blockpos = pContext.origin();
    WorldGenLevel worldgenlevel = pContext.level();
    ConfigDrivenOreConfiguration config = pContext.config();
    List<LayerPattern> patternPool = config.getLayers();
    if (patternPool.isEmpty())
        return false;
    LayerPattern layerPattern = patternPool.get(random.nextInt(patternPool.size()));
    int placedAmount = 0;
    int size = config.getSize();
    int radius = Mth.ceil(config.getSize() / 2f);
    int x0 = blockpos.getX() - radius;
    int y0 = blockpos.getY() - radius;
    int z0 = blockpos.getZ() - radius;
    int width = size + 1;
    int length = size + 1;
    int height = size + 1;
    if (blockpos.getY() >= worldgenlevel.getHeight(Heightmap.Types.OCEAN_FLOOR_WG, blockpos.getX(), blockpos.getZ()))
        return false;
    List<LayerPattern.Layer> resolvedLayers = new ArrayList<>();
    List<Float> layerDiameterOffsets = new ArrayList<>();
    MutableBlockPos mutablePos = new MutableBlockPos();
    BulkSectionAccess bulksectionaccess = new BulkSectionAccess(worldgenlevel);
    int layerCoordinate = random.nextInt(4);
    int slantyCoordinate = random.nextInt(3);
    float slope = random.nextFloat() * .75f;
    try {
        for (int x = 0; x < width; x++) {
            float dx = x * 2f / width - 1;
            if (dx * dx > 1)
                continue;
            for (int y = 0; y < height; y++) {
                float dy = y * 2f / height - 1;
                if (dx * dx + dy * dy > 1)
                    continue;
                if (worldgenlevel.isOutsideBuildHeight(y0 + y))
                    continue;
                for (int z = 0; z < length; z++) {
                    float dz = z * 2f / height - 1;
                    int layerIndex = layerCoordinate == 0 ? z : layerCoordinate == 1 ? x : y;
                    if (slantyCoordinate != layerCoordinate)
                        layerIndex += Mth.floor(slantyCoordinate == 0 ? z : slantyCoordinate == 1 ? x : y) * slope;
                    while (layerIndex >= resolvedLayers.size()) {
                        Layer next = layerPattern.rollNext(resolvedLayers.isEmpty() ? null : resolvedLayers.get(resolvedLayers.size() - 1), random);
                        float offset = random.nextFloat() * .5f + .5f;
                        for (int i = 0; i < next.minSize + random.nextInt(1 + next.maxSize - next.minSize); i++) {
                            resolvedLayers.add(next);
                            layerDiameterOffsets.add(offset);
                        }
                    }
                    if (dx * dx + dy * dy + dz * dz > 1 * layerDiameterOffsets.get(layerIndex))
                        continue;
                    LayerPattern.Layer layer = resolvedLayers.get(layerIndex);
                    List<TargetBlockState> state = layer.rollBlock(random);
                    int currentX = x0 + x;
                    int currentY = y0 + y;
                    int currentZ = z0 + z;
                    mutablePos.set(currentX, currentY, currentZ);
                    if (!worldgenlevel.ensureCanWrite(mutablePos))
                        continue;
                    LevelChunkSection levelchunksection = bulksectionaccess.getSection(mutablePos);
                    if (levelchunksection == null)
                        continue;
                    int i3 = SectionPos.sectionRelative(currentX);
                    int j3 = SectionPos.sectionRelative(currentY);
                    int k3 = SectionPos.sectionRelative(currentZ);
                    BlockState blockstate = levelchunksection.getBlockState(i3, j3, k3);
                    for (OreConfiguration.TargetBlockState oreconfiguration$targetblockstate : state) {
                        if (!canPlaceOre(blockstate, bulksectionaccess::getBlockState, random, config, oreconfiguration$targetblockstate, mutablePos))
                            continue;
                        if (oreconfiguration$targetblockstate.state.isAir())
                            continue;
                        levelchunksection.setBlockState(i3, j3, k3, oreconfiguration$targetblockstate.state, false);
                        ++placedAmount;
                        break;
                    }
                }
            }
        }
    } catch (Throwable throwable1) {
        try {
            bulksectionaccess.close();
        } catch (Throwable throwable) {
            throwable1.addSuppressed(throwable);
        }
        throw throwable1;
    }
    bulksectionaccess.close();
    return placedAmount > 0;
}
Also used : BulkSectionAccess(net.minecraft.world.level.chunk.BulkSectionAccess) ArrayList(java.util.ArrayList) Layer(com.simibubi.create.foundation.worldgen.LayerPattern.Layer) Layer(com.simibubi.create.foundation.worldgen.LayerPattern.Layer) BlockState(net.minecraft.world.level.block.state.BlockState) TargetBlockState(net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration.TargetBlockState) Random(java.util.Random) LevelChunkSection(net.minecraft.world.level.chunk.LevelChunkSection) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) BlockPos(net.minecraft.core.BlockPos) TargetBlockState(net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration.TargetBlockState) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) WorldGenLevel(net.minecraft.world.level.WorldGenLevel) TargetBlockState(net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration.TargetBlockState) OreConfiguration(net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration)

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