Search in sources :

Example 6 with BlockState

use of com.sk89q.worldedit.world.block.BlockState in project FastAsyncWorldEdit by IntellectualSites.

the class Extent method getBlockDistributionWithData.

/**
 * Get the block distribution (with data values) inside a region.
 *
 * @param region a region
 * @return the results
 */
default List<Countable<BlockState>> getBlockDistributionWithData(final Region region) {
    int[][] counter = new int[BlockTypes.size()][];
    for (final BlockVector3 pt : region) {
        BlockState blk = this.getBlock(pt);
        BlockType type = blk.getBlockType();
        if (type == BlockTypes.__RESERVED__) {
            int[] stateCounter = counter[1];
            if (stateCounter == null) {
                counter[1] = stateCounter = new int[BlockTypes.AIR.getMaxStateId() + 1];
            }
            stateCounter[BlockTypes.AIR.getDefaultState().getInternalPropertiesId()]++;
        }
        int[] stateCounter = counter[type.getInternalId()];
        if (stateCounter == null) {
            counter[type.getInternalId()] = stateCounter = new int[type.getMaxStateId() + 1];
        }
        stateCounter[blk.getInternalPropertiesId()]++;
    }
    List<Countable<BlockState>> distribution = new ArrayList<>();
    for (int typeId = 0; typeId < counter.length; typeId++) {
        BlockType type = BlockTypes.get(typeId);
        int[] stateCount = counter[typeId];
        if (stateCount != null) {
            for (int propId = 0; propId < stateCount.length; propId++) {
                int count = stateCount[propId];
                if (count != 0) {
                    BlockState state = type.withPropertyId(propId);
                    distribution.add(new Countable<>(state, count));
                }
            }
        }
    }
    // Collections.reverse(distribution);
    return distribution;
}
Also used : Countable(com.sk89q.worldedit.util.Countable) BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) ArrayList(java.util.ArrayList) BlockVector3(com.sk89q.worldedit.math.BlockVector3) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3)

Example 7 with BlockState

use of com.sk89q.worldedit.world.block.BlockState in project FastAsyncWorldEdit by IntellectualSites.

the class BannerBlockCompatibilityHandler method updateNBT.

@Override
public <B extends BlockStateHolder<B>> BlockStateHolder<?> updateNBT(B block, Map<String, Tag> values) {
    Tag typeTag = values.get("Base");
    if (typeTag instanceof IntTag) {
        boolean isWall = block.getBlockType() == BlockTypes.WHITE_WALL_BANNER;
        String bannerType = convertBannerType(((IntTag) typeTag).getValue(), isWall);
        if (bannerType != null) {
            BlockType type = BlockTypes.get("minecraft:" + bannerType);
            if (type != null) {
                BlockState state = type.getDefaultState();
                if (isWall) {
                    Property<Direction> facingProp = type.getProperty("facing");
                    state = state.with(facingProp, block.getState(FacingProperty));
                } else {
                    Property<Integer> rotationProp = type.getProperty("rotation");
                    state = state.with(rotationProp, block.getState(RotationProperty));
                }
                values.remove("Base");
                Tag patternsTag = values.get("Patterns");
                if (patternsTag instanceof ListTag) {
                    List<Tag> tempList = new ArrayList<>();
                    for (Tag pattern : ((ListTag) patternsTag).getValue()) {
                        if (pattern instanceof CompoundTag) {
                            Map<String, Tag> patternMap = ((CompoundTag) pattern).getValue();
                            Tag colorTag = patternMap.get("Color");
                            CompoundTagBuilder builder = CompoundTagBuilder.create();
                            builder.putAll(patternMap);
                            if (colorTag instanceof IntTag) {
                                builder.putInt("Color", 15 - ((IntTag) colorTag).getValue());
                            }
                            tempList.add(builder.build());
                        } else {
                            tempList.add(pattern);
                        }
                    }
                    values.put("Patterns", new ListTag(((ListTag) patternsTag).getType(), tempList));
                }
                return state;
            }
        }
    }
    return block;
}
Also used : ArrayList(java.util.ArrayList) Direction(com.sk89q.worldedit.util.Direction) ListTag(com.sk89q.jnbt.ListTag) CompoundTagBuilder(com.sk89q.jnbt.CompoundTagBuilder) BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) ListTag(com.sk89q.jnbt.ListTag) IntTag(com.sk89q.jnbt.IntTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) IntTag(com.sk89q.jnbt.IntTag) CompoundTag(com.sk89q.jnbt.CompoundTag)

Example 8 with BlockState

use of com.sk89q.worldedit.world.block.BlockState in project FastAsyncWorldEdit by IntellectualSites.

the class OldChunk method getBlock.

@Override
public BaseBlock getBlock(BlockVector3 position) throws DataException {
    if (position.getY() >= 128) {
        return BlockTypes.VOID_AIR.getDefaultState().toBaseBlock();
    }
    int id;
    int dataVal;
    int x = position.getX() - rootX * 16;
    int y = position.getY();
    int z = position.getZ() - rootZ * 16;
    int index = y + (z * 128 + (x * 128 * 16));
    try {
        id = blocks[index];
    } catch (IndexOutOfBoundsException e) {
        throw new DataException("Chunk does not contain position " + position);
    }
    boolean shift = index % 2 == 0;
    index /= 2;
    try {
        if (!shift) {
            dataVal = (data[index] & 0xF0) >> 4;
        } else {
            dataVal = data[index] & 0xF;
        }
    } catch (IndexOutOfBoundsException e) {
        throw new DataException("Chunk does not contain position " + position);
    }
    BlockState state = LegacyMapper.getInstance().getBlockFromLegacy(id, dataVal);
    if (state == null) {
        WorldEdit.logger.warn("Unknown legacy block " + id + ":" + dataVal + " found when loading legacy anvil chunk.");
        return BlockTypes.AIR.getDefaultState().toBaseBlock();
    }
    CompoundBinaryTag tileEntity = getBlockTileEntity(position);
    if (tileEntity != null) {
        return state.toBaseBlock(tileEntity);
    }
    return state.toBaseBlock();
}
Also used : DataException(com.sk89q.worldedit.world.DataException) BlockState(com.sk89q.worldedit.world.block.BlockState) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag)

Example 9 with BlockState

use of com.sk89q.worldedit.world.block.BlockState in project FastAsyncWorldEdit by IntellectualSites.

the class ErodeBrush method erosion.

public void erosion(final EditSession es, int erodeFaces, int erodeRecursion, int fillFaces, int fillRecursion, BlockVector3 target, double size) {
    int brushSize = (int) size;
    int brushSizeSquared = (int) (size * size);
    Location min = new Location(es.getWorld(), target.toVector3().subtract(size, size, size));
    Location max = new Location(es.getWorld(), target.toVector3().add(size, size, size));
    Region region = new CuboidRegion(es.getWorld(), min.toBlockPoint(), max.toBlockPoint());
    Clipboard buffer1 = new CPUOptimizedClipboard(region);
    Clipboard buffer2 = new CPUOptimizedClipboard(region);
    final int bx = target.getBlockX();
    final int by = target.getBlockY();
    final int bz = target.getBlockZ();
    for (int x = -brushSize, relx = 0; x <= brushSize; x++, relx++) {
        int x0 = x + bx;
        for (int y = -brushSize, rely = 0; y <= brushSize; y++, rely++) {
            int y0 = y + by;
            for (int z = -brushSize, relz = 0; z <= brushSize; z++, relz++) {
                int z0 = z + bz;
                BlockState state = es.getBlock(x0, y0, z0);
                buffer1.setBlock(relx, rely, relz, state);
                buffer2.setBlock(relx, rely, relz, state);
            }
        }
    }
    int swap = 0;
    for (int i = 0; i < erodeRecursion; ++i) {
        erosionIteration(brushSize, brushSizeSquared, erodeFaces, swap % 2 == 0 ? buffer1 : buffer2, swap % 2 == 1 ? buffer1 : buffer2);
        swap++;
    }
    for (int i = 0; i < fillRecursion; ++i) {
        fillIteration(brushSize, brushSizeSquared, fillFaces, swap % 2 == 0 ? buffer1 : buffer2, swap % 2 == 1 ? buffer1 : buffer2);
        swap++;
    }
    Clipboard finalBuffer = swap % 2 == 0 ? buffer1 : buffer2;
    for (BlockVector3 pos : finalBuffer) {
        BlockState block = pos.getBlock(finalBuffer);
        es.setBlock(pos.getX() + bx - brushSize, pos.getY() + by - brushSize, pos.getZ() + bz - brushSize, block);
    }
}
Also used : BlockState(com.sk89q.worldedit.world.block.BlockState) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Region(com.sk89q.worldedit.regions.Region) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) CPUOptimizedClipboard(com.fastasyncworldedit.core.extent.clipboard.CPUOptimizedClipboard) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Location(com.sk89q.worldedit.util.Location) CPUOptimizedClipboard(com.fastasyncworldedit.core.extent.clipboard.CPUOptimizedClipboard)

Example 10 with BlockState

use of com.sk89q.worldedit.world.block.BlockState in project FastAsyncWorldEdit by IntellectualSites.

the class SnowHeightMap method applyChanges.

/**
 * Apply a raw heightmap to a region. Use snow layers.
 *
 * @param data        the data
 * @param layerBlocks amount of blocks with type SNOW_BLOCK
 * @return number of blocks affected
 * @throws MaxChangedBlocksException if the maximum block change limit is exceeded
 */
public int applyChanges(float[] data, int layerBlocks) throws MaxChangedBlocksException {
    checkNotNull(data);
    BlockVector3 minY = region.getMinimumPoint();
    int originX = minY.getBlockX();
    int originY = minY.getBlockY();
    int originZ = minY.getBlockZ();
    int maxY = region.getMaximumPoint().getBlockY();
    BlockState fillerAir = BlockTypes.AIR.getDefaultState();
    BlockState fillerSnow = BlockTypes.SNOW_BLOCK.getDefaultState();
    int blocksChanged = 0;
    // Apply heightmap
    for (int z = 0; z < height; ++z) {
        for (int x = 0; x < width; ++x) {
            int index = z * width + x;
            float curHeight = this.data[index];
            if (curHeight == originY) {
                continue;
            }
            // Clamp newHeight within the selection area
            float newHeight = Math.min(maxY, data[index]);
            // Offset x,z to be 'real' coordinates
            int xr = x + originX;
            int zr = z + originZ;
            // We are keeping the topmost blocks so take that in account for the scale
            double scale = (double) (curHeight - originY) / (double) (newHeight - originY);
            // Depending on growing or shrinking we need to start at the bottom or top
            if (newHeight >= curHeight) {
                // Set the top block of the column to be the same type (this might go wrong with rounding)
                // FAWE start - avoid BlockVector3 creation for no reason
                BlockState existing = session.getBlock(xr, (int) Math.floor(curHeight), zr);
                // Skip water/lava
                if (!existing.getBlockType().getMaterial().isLiquid()) {
                    setSnowLayer(xr, zr, newHeight);
                    ++blocksChanged;
                    // Grow -- start from 1 below top replacing airblocks
                    for (int y = (int) Math.floor(newHeight - 1 - originY); y >= 0; --y) {
                        if (y >= Math.floor(newHeight - 1 - originY - layerBlocks)) {
                            // FAWE start - avoid BlockVector3 creation for no reason
                            session.setBlock(xr, originY + y, zr, fillerSnow);
                        // FAWE end
                        } else {
                            int copyFrom = (int) Math.floor(y * scale);
                            // FAWE start - avoid BlockVector3 creation for no reason
                            BlockState block = session.getBlock(xr, originY + copyFrom, zr);
                            session.setBlock(xr, originY + y, zr, block);
                        // FAWE end
                        }
                        ++blocksChanged;
                    }
                }
            } else {
                // Shrink -- start from bottom
                for (int y = 0; y < (int) Math.floor(newHeight - originY); ++y) {
                    if (y >= (int) Math.floor(newHeight - originY - layerBlocks)) {
                        // FAWE start - avoid BlockVector3 creation for no reason
                        session.setBlock(xr, originY + y, zr, fillerSnow);
                    // FAWE end
                    } else {
                        int copyFrom = (int) Math.floor(y * scale);
                        // FAWE start - avoid BlockVector3 creation for no reason
                        BlockState block = session.getBlock(xr, originY + copyFrom, zr);
                        session.setBlock(xr, originY + y, zr, block);
                    // FAWE end
                    }
                    ++blocksChanged;
                }
                setSnowLayer(xr, zr, newHeight);
                ++blocksChanged;
                // Fill rest with air
                for (int y = (int) Math.floor(newHeight + 1); y <= Math.floor(curHeight); ++y) {
                    // FAWE start - avoid BlockVector3 creation for no reason
                    session.setBlock(xr, y, zr, fillerAir);
                    // FAWE end
                    ++blocksChanged;
                }
            }
        }
    }
    // Drop trees to the floor -- TODO
    return blocksChanged;
}
Also used : BlockState(com.sk89q.worldedit.world.block.BlockState) BlockVector3(com.sk89q.worldedit.math.BlockVector3)

Aggregations

BlockState (com.sk89q.worldedit.world.block.BlockState)76 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)23 BlockType (com.sk89q.worldedit.world.block.BlockType)18 HashMap (java.util.HashMap)11 MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)10 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)10 CompoundTag (com.sk89q.jnbt.CompoundTag)9 Tag (com.sk89q.jnbt.Tag)9 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 IntTag (com.sk89q.jnbt.IntTag)7 Region (com.sk89q.worldedit.regions.Region)7 ListTag (com.sk89q.jnbt.ListTag)6 StringTag (com.sk89q.jnbt.StringTag)6 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)6 IOException (java.io.IOException)5 BlockPos (net.minecraft.core.BlockPos)5 NamedTag (com.sk89q.jnbt.NamedTag)4 BlockArrayClipboard (com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard)4 Direction (com.sk89q.worldedit.util.Direction)4