Search in sources :

Example 11 with BlockType

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

the class SimpleBukkitAdapter method adapt.

/**
 * Create a Bukkit BlockData from a WorldEdit BlockStateHolder
 *
 * @param block The WorldEdit BlockStateHolder
 * @return The Bukkit BlockData
 */
@Override
public <B extends BlockStateHolder<B>> BlockData adapt(B block) {
    try {
        checkNotNull(block);
        int typeId = block.getInternalBlockTypeId();
        BlockData[] dataCache = blockDataCache[typeId];
        if (dataCache == null) {
            BlockType type = BlockTypes.get(typeId);
            blockDataCache[typeId] = dataCache = new BlockData[type.getMaxStateId() + 1];
        }
        int propId = block.getInternalPropertiesId();
        BlockData blockData = dataCache[propId];
        if (blockData == null) {
            dataCache[propId] = blockData = Bukkit.createBlockData(block.getAsString());
        }
        return blockData;
    } catch (NullPointerException e) {
        if (init()) {
            return adapt(block);
        }
        throw e;
    }
}
Also used : BlockType(com.sk89q.worldedit.world.block.BlockType) BlockData(org.bukkit.block.data.BlockData)

Example 12 with BlockType

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

the class BlockTransformExtent method transformState.

private static int transformState(BlockState state, Transform transform) {
    int newMaskedId = state.getInternalId();
    BlockType type = state.getBlockType();
    // Rotate North, East, South, West
    if (type.hasProperty(PropertyKey.NORTH) && type.hasProperty(PropertyKey.EAST) && type.hasProperty(PropertyKey.SOUTH) && type.hasProperty(PropertyKey.WEST)) {
        BlockState tmp = state;
        for (Map.Entry<Direction, PropertyKey> entry : directionMap.entrySet()) {
            Direction newDir = findClosest(transform.apply(entry.getKey().toVector()), Flag.CARDINAL);
            if (newDir != null) {
                Object dirState = state.getState(entry.getValue());
                tmp = tmp.with(directionMap.get(newDir), dirState);
            }
        }
        newMaskedId = tmp.getInternalId();
    }
    // True if relying on two different "directions" for the result, e.g. stairs with both facing and shape
    for (AbstractProperty property : (List<AbstractProperty<?>>) type.getProperties()) {
        if (isDirectional(property)) {
            long[] directions = getDirections(property);
            if (directions != null) {
                int oldIndex = property.getIndex(state.getInternalId());
                if (oldIndex >= directions.length) {
                    if (Settings.settings().ENABLED_COMPONENTS.DEBUG) {
                        LOGGER.warn(String.format("Index outside direction array length found for block:{%s} property:{%s}", state.getBlockType().getId(), property.getName()));
                    }
                    continue;
                }
                Integer newIndex = getNewStateIndex(transform, directions, oldIndex);
                if (newIndex != null) {
                    newMaskedId = property.modifyIndex(newMaskedId, newIndex);
                }
            }
        }
    }
    return newMaskedId;
}
Also used : BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) AbstractProperty(com.sk89q.worldedit.registry.state.AbstractProperty) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Direction(com.sk89q.worldedit.util.Direction) PropertyKey(com.fastasyncworldedit.core.registry.state.PropertyKey)

Example 13 with BlockType

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

the class DataValidatorExtent method setBlock.

@Override
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block) throws WorldEditException {
    final int y = location.getBlockY();
    final BlockType type = block.getBlockType();
    if (y < world.getMinY() || y > world.getMaxY()) {
        return false;
    }
    // No invalid blocks
    if (type == null) {
        return false;
    }
    return super.setBlock(location, block);
}
Also used : BlockType(com.sk89q.worldedit.world.block.BlockType)

Example 14 with BlockType

use of com.sk89q.worldedit.world.block.BlockType 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 15 with BlockType

use of com.sk89q.worldedit.world.block.BlockType 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)

Aggregations

BlockType (com.sk89q.worldedit.world.block.BlockType)63 BlockState (com.sk89q.worldedit.world.block.BlockState)20 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)18 Map (java.util.Map)12 HashMap (java.util.HashMap)9 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)8 ArrayList (java.util.ArrayList)8 TextureUtil (com.fastasyncworldedit.core.util.TextureUtil)7 World (com.sk89q.worldedit.world.World)7 List (java.util.List)7 CompoundTag (com.sk89q.jnbt.CompoundTag)5 Tag (com.sk89q.jnbt.Tag)5 EditSession (com.sk89q.worldedit.EditSession)5 Property (com.sk89q.worldedit.registry.state.Property)5 Direction (com.sk89q.worldedit.util.Direction)5 IOException (java.io.IOException)5 Locale (java.util.Locale)5 Set (java.util.Set)5 MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)4 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)4