Java Code Examples for com.sk89q.worldedit.blocks.BaseBlock

The following examples show how to use com.sk89q.worldedit.blocks.BaseBlock. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public boolean setBlock(int x, int y, int z, BaseBlock block) {
    try {
        int index = (HEADER_SIZE) + (getIndex(x, y, z) << 1);
        final int id = block.getId();
        final int data = block.getData();
        int combined = (id << 4) + data;
        mbb.putChar(index, (char) combined);
        CompoundTag tile = block.getNbtData();
        if (tile != null) {
            setTile(x, y, z, tile);
        }
        return true;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
public List<CompoundTag> getTileEntities() {
    final List<CompoundTag> tiles = new ArrayList<>();
    forEach(new BlockReader() {
        private int index = 0;

        @Override
        public void run(int x, int y, int z, BaseBlock block) {
            CompoundTag tag = block.getNbtData();
            if (tag != null) {
                Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
                values.put("x", new IntTag(x));
                values.put("y", new IntTag(y));
                values.put("z", new IntTag(z));
                tiles.add(tag);
            }
        }
    }, false);
    return tiles;
}
 
Example 3
public void setMain(Pattern value) {
    if (value instanceof BaseBlock) {
        setMain(((BaseBlock) value).getCombined());
    } else {
        main.record(() -> {
            char[] mainArr = main.get();
            int index = 0;
            for (int z = 0; z < getLength(); z++) {
                mutable.mutZ(z);
                for (int x = 0; x < getWidth(); x++, index++) {
                    int y = heights.getByte(index) & 0xFF;
                    mutable.mutX(x);
                    mutable.mutY(y);
                    mainArr[index] = (char) value.apply(mutable).getCombined();
                }
            }
        });
    }
}
 
Example 4
public BaseBlock getBlock(Location location)
{
    Preconditions.checkNotNull(location);

    CraftWorld craftWorld = (CraftWorld)location.getWorld();
    int x = location.getBlockX();
    int y = location.getBlockY();
    int z = location.getBlockZ();

    Block bukkitBlock = location.getBlock();
    BaseBlock block = new BaseBlock(bukkitBlock.getTypeId(), bukkitBlock.getData());

    TileEntity te = craftWorld.getHandle().getTileEntity(new BlockPosition(x, y, z));
    if (te != null)
    {
        NBTTagCompound tag = new NBTTagCompound();
        readTileEntityIntoTag(te, tag);
        block.setNbtData((CompoundTag)toNative(tag));
    }
    return block;
}
 
Example 5
Source Project: FastAsyncWorldedit   Source File: FaweQueue.java    License: GNU General Public License v3.0 6 votes vote down vote up
default void forEachTileInChunk(int cx, int cz, RunnableVal2<Vector, BaseBlock> onEach) {
    int bx = cx << 4;
    int bz = cz << 4;
    MutableBlockVector mutable = new MutableBlockVector(0, 0, 0);
    for (int x = 0; x < 16; x++) {
        int xx = x + bx;
        for (int z = 0; z < 16; z++) {
            int zz = z + bz;
            for (int y = 0; y < getMaxY(); y++) {
                int combined = getCombinedId4Data(xx, y, zz);
                if (combined == 0) {
                    continue;
                }
                int id = FaweCache.getId(combined);
                if (FaweCache.hasNBT(id)) {
                    mutable.mutX(xx);
                    mutable.mutZ(zz);
                    mutable.mutY(y);
                    CompoundTag tile = getTileEntity(x, y, z);
                    BaseBlock block = new BaseBlock(id, FaweCache.getData(combined), tile);
                    onEach.run(mutable, block);
                }
            }
        }
    }
}
 
Example 6
public void setOverlay(Mask mask, Pattern pattern) {
    if (pattern instanceof BaseBlock) {
        setOverlay(mask, (char) ((BaseBlock) pattern).getCombined());
    } else {
        int index = 0;
        if (overlay == null) overlay = new DifferentialArray<>(new char[getArea()]);
        for (int z = 0; z < getLength(); z++) {
            mutable.mutZ(z);
            for (int x = 0; x < getWidth(); x++, index++) {
                int y = heights.getByte(index) & 0xFF;
                mutable.mutX(x);
                mutable.mutY(y);
                if (mask.test(mutable)) {
                    overlay.setChar(index, (char) pattern.apply(mutable).getCombined());
                }
            }
        }
    }
}
 
Example 7
public BaseBlock getNearestBlock(int color) {
    long min = Long.MAX_VALUE;
    int closest = 0;
    int red1 = (color >> 16) & 0xFF;
    int green1 = (color >> 8) & 0xFF;
    int blue1 = (color >> 0) & 0xFF;
    int alpha = (color >> 24) & 0xFF;
    for (int i = 0; i < validColors.length; i++) {
        int other = validColors[i];
        if (((other >> 24) & 0xFF) == alpha) {
            long distance = colorDistance(red1, green1, blue1, other);
            if (distance < min) {
                min = distance;
                closest = validBlockIds[i];
            }
        }
    }
    if (min == Long.MAX_VALUE) return null;
    return FaweCache.CACHE_BLOCK[closest];
}
 
Example 8
Source Project: FastAsyncWorldedit   Source File: MCAWorld.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean clearContainerBlockContents(Vector position) {
    BaseBlock block = extent.getLazyBlock(position);
    if (block.hasNbtData()) {
        Map<String, Tag> nbt = ReflectionUtils.getMap(block.getNbtData().getValue());
        if (nbt.containsKey("Items")) {
            nbt.put("Items", new ListTag(CompoundTag.class, new ArrayList<CompoundTag>()));
            try {
                extent.setBlock(position, block);
            } catch (WorldEditException e) {
                e.printStackTrace();
            }
        }
    }
    return true;
}
 
Example 9
public void setFloor(Pattern value) {
    if (value instanceof BaseBlock) {
        setFloor(((BaseBlock) value).getCombined());
    } else {
        floor.record(() -> {
            char[] floorArr = floor.get();
            int index = 0;
            for (int z = 0; z < getLength(); z++) {
                mutable.mutZ(z);
                for (int x = 0; x < getWidth(); x++, index++) {
                    int y = heights.getByte(index) & 0xFF;
                    mutable.mutX(x);
                    mutable.mutY(y);
                    floorArr[index] = (char) value.apply(mutable).getCombined();
                }
            }
        });
    }
}
 
Example 10
@Override
public int getSlope(BaseBlock block, Vector vector) {
    int slope = super.getSlope(block, vector);
    if (slope != -1) {
        int x = vector.getBlockX();
        int y = vector.getBlockY();
        int z = vector.getBlockZ();
        int height = extent.getNearestSurfaceTerrainBlock(x, z, y, 0, maxY);
        if (height > 0) {
            BaseBlock below = extent.getLazyBlock(x, height - 1, z);
            if (FaweCache.canPassThrough(below.getId(), below.getData())) {
                return Integer.MAX_VALUE;
            }
        }
    }
    return slope;
}
 
Example 11
public BaseBlock getNextNearestBlock(int color) {
    long min = Long.MAX_VALUE;
    int closest = 0;
    int red1 = (color >> 16) & 0xFF;
    int green1 = (color >> 8) & 0xFF;
    int blue1 = (color >> 0) & 0xFF;
    int alpha = (color >> 24) & 0xFF;
    for (int i = 0; i < validColors.length; i++) {
        int other = validColors[i];
        if (other != color && ((other >> 24) & 0xFF) == alpha) {
            long distance = colorDistance(red1, green1, blue1, other);
            if (distance < min) {
                min = distance;
                closest = validBlockIds[i];
            }
        }
    }
    if (min == Long.MAX_VALUE) return null;
    return FaweCache.CACHE_BLOCK[closest];
}
 
Example 12
public void setColumn(Mask mask, Pattern pattern) {
    if (pattern instanceof BaseBlock) {
        setColumn(mask, (char) ((BaseBlock) pattern).getCombined());
    } else {
        primtives.modifiedMain = true;
        int index = 0;
        for (int z = 0; z < getLength(); z++) {
            mutable.mutZ(z);
            for (int x = 0; x < getWidth(); x++, index++) {
                int y = heights.getByte(index) & 0xFF;
                mutable.mutX(x);
                mutable.mutY(y);
                if (mask.test(mutable)) {
                    char combined = (char) pattern.apply(mutable).getCombined();
                    floor.setChar(index, combined);
                    main.setChar(index, combined);
                }
            }
        }
    }
}
 
Example 13
@Override
public boolean setBlock(final Vector location, final BaseBlock block) throws WorldEditException {
    if (super.setBlock(location, block)) {
        if (MemUtil.isMemoryLimited()) {
            if (this.player != null) {
                player.sendMessage(BBC.WORLDEDIT_CANCEL_REASON.format(BBC.WORLDEDIT_CANCEL_REASON_LOW_MEMORY.s()));
                if (Perm.hasPermission(this.player, "worldedit.fast")) {
                    BBC.WORLDEDIT_OOM_ADMIN.send(this.player);
                }
            }
            WEManager.IMP.cancelEdit(this, BBC.WORLDEDIT_CANCEL_REASON_LOW_MEMORY);
            return false;
        }
        return true;
    }
    return false;
}
 
Example 14
@Override
default Mask createLiquidMask() {
    return new BlockMask(this,
            new BaseBlock(BlockID.STATIONARY_LAVA, -1),
            new BaseBlock(BlockID.LAVA, -1),
            new BaseBlock(BlockID.STATIONARY_WATER, -1),
            new BaseBlock(BlockID.WATER, -1));
}
 
Example 15
/**
 * Set a block, bypassing both history and block re-ordering.
 *
 * @param position the position to set the block at
 * @param block    the block
 * @param stage    the level
 * @return whether the block changed
 * @throws WorldEditException thrown on a set error
 */
public boolean setBlock(final Vector position, final BaseBlock block, final Stage stage) throws WorldEditException {
    this.changes++;
    switch (stage) {
        case BEFORE_HISTORY:
            return this.extent.setBlock(position, block);
        case BEFORE_CHANGE:
            return this.bypassHistory.setBlock(position, block);
        case BEFORE_REORDER:
            return this.bypassAll.setBlock(position, block);
    }

    throw new RuntimeException("New enum entry added that is unhandled here");
}
 
Example 16
@Override
public void add(Vector loc, BaseBlock from, BaseBlock to) {
    int x = loc.getBlockX();
    int y = loc.getBlockY();
    int z = loc.getBlockZ();
    add(x, y, z, from, to);
}
 
Example 17
@Command(
        aliases = {"baseid", "bedrockid"},
        usage = "<block>",
        desc = "Change the block used for the base\n" +
                "e.g. Bedrock"
)
@CommandPermissions("worldedit.anvil.cfi")
public void baseId(FawePlayer fp, BaseBlock block) throws ParameterException, WorldEditException {
    CFISettings settings = assertSettings(fp);
    settings.getGenerator().setBedrockId(block.getId());
    msg("Set base id!").send(fp);
    settings.resetComponent();
    component(fp);
}
 
Example 18
/**
 * Remove a cuboid below the given position with a given apothem and a given height.
 *
 * @param position base position
 * @param apothem  an apothem of the cuboid (on the XZ plane), where the minimum is 1
 * @param height   the height of the cuboid, where the minimum is 1
 * @return number of blocks affected
 * @throws MaxChangedBlocksException thrown if too many blocks are changed
 */
@SuppressWarnings("deprecation")
public int removeBelow(final Vector position, final int apothem, final int height) {
    checkNotNull(position);
    checkArgument(apothem >= 1, "apothem >= 1");
    checkArgument(height >= 1, "height >= 1");

    final Region region = new CuboidRegion(this.getWorld(), // Causes clamping of Y range
            position.add(-apothem + 1, 0, -apothem + 1), position.add(apothem - 1, -height + 1, apothem - 1));
    final Pattern pattern = (new BaseBlock(BlockID.AIR));
    return this.setBlocks(region, pattern);
}
 
Example 19
@Override
public boolean setBlock(Vector position, BaseBlock block, boolean notifyAndLight) throws WorldEditException {
    BukkitImplAdapter adapter = BukkitQueue_0.getAdapter();
    if (adapter != null) {
        return adapter.setBlock(adapt(getWorld(), position), block, notifyAndLight);
    } else {
        Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ());
        return bukkitBlock.setTypeIdAndData(block.getType(), (byte) block.getData(), notifyAndLight);
    }
}
 
Example 20
@Override
public boolean set(BaseBlock block) {
    if (data != null) {
        block.setData((block.getData() & ~state.getDataMask()) | data);
        return true;
    } else {
        return false;
    }
}
 
Example 21
Source Project: FastAsyncWorldedit   Source File: Functions.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Dynamic
public static double query(RValue x, RValue y, RValue z, RValue type, RValue data) throws EvaluationException {
    final double xp = x.getValue();
    final double yp = y.getValue();
    final double zp = z.getValue();

    final ExpressionEnvironment environment = Expression.getInstance().getEnvironment();

    // Read values from world
    BaseBlock block = environment.getBlock(xp, yp, zp);
    int typeId = block.getId();
    int dataValue = block.getData();

    return queryInternal(type, data, typeId, dataValue);
}
 
Example 22
public static boolean setBlock(Level level, Vector pos, BaseBlock block) {
    int x = pos.getBlockX();
    int y = pos.getBlockY();
    int z = pos.getBlockZ();
    level.setBlockIdAt(x, y, z, block.getId());
    level.setBlockDataAt(x, y, z, block.getData());
    return true;

}
 
Example 23
@Override
public BaseBlock apply(Vector position) {
    BaseBlock oldBlock = getExtent().getBlock(position);
    BaseBlock newBlock = pattern.apply(position);
    int oldData = oldBlock.getData();
    int newData = newBlock.getData() + oldData - (oldData & bitMask);
    return FaweCache.getBlock(newBlock.getId(), newData);
}
 
Example 24
@Override
default boolean setTypeIdAndData(Vector position, int type, int data) {
    try {
        return setBlock(position, new BaseBlock(type, data));
    } catch (WorldEditException ignored) {
        return false;
    }
}
 
Example 25
public void setColumn(BufferedImage img, Pattern pattern, boolean white) {
    if (pattern instanceof BaseBlock) {
        setColumn(img, (char) ((BaseBlock) pattern).getCombined(), white);
    } else {
        if (img.getWidth() != getWidth() || img.getHeight() != getLength())
            throw new IllegalArgumentException("Input image dimensions do not match the current height map!");
        primtives.modifiedMain = true;

        main.record(() -> floor.record(() -> {
            char[] floorArr = floor.get();
            char[] mainArr = main.get();
            int index = 0;
            for (int z = 0; z < getLength(); z++) {
                mutable.mutZ(z);
                for (int x = 0; x < getWidth(); x++, index++) {
                    int height = img.getRGB(x, z) & 0xFF;
                    if (height == 255 || height > 0 && !white && PseudoRandom.random.nextInt(256) <= height) {
                        mutable.mutX(x);
                        mutable.mutY(height);
                        char combined = (char) pattern.apply(mutable).getCombined();
                        mainArr[index] = combined;
                        floorArr[index] = combined;
                    }
                }
            }
        }));
    }
}
 
Example 26
@Override
public BaseBlock apply(Vector position) {
    int index = (position.getBlockX() + position.getBlockY() + position.getBlockZ()) % patternsArray.length;
    if (index < 0) {
        index += patternsArray.length;
    }
    return patternsArray[index].apply(position);
}
 
Example 27
@Override
public BaseBlock apply(Vector position) {
    if (index == patternsArray.length) {
        index = 0;
    }
    return patternsArray[index++].apply(position);
}
 
Example 28
@Override
public boolean apply(Extent extent, Vector setPosition, Vector getPosition) throws WorldEditException {
    BaseBlock block = extent.getBlock(getPosition);
    TextureUtil util = holder.getTextureUtil();
    int currentColor = util.getColor(block);
    if (currentColor == 0) return false;
    int newColor = util.multiplyColor(currentColor, color);
    BaseBlock newBlock = util.getNearestBlock(newColor);
    if (newBlock.equals(block)) return false;
    return extent.setBlock(setPosition, newBlock);
}
 
Example 29
@Override
public boolean setBlock(int x, int y, int z, BaseBlock block) throws WorldEditException {
    int combined = queue.getCombinedId4DataDebug(x, y, z, 0, session);
    int id = (combined >> 4);
    if (id == block.getId()) {
        if (!FaweCache.hasData(id)) {
            if (!block.hasNbtData()) {
                return false;
            }
        } else if (!block.hasNbtData()) {
            int data = combined & 0xF;
            if (data == block.getData()) {
                return false;
            }
        }
    }
    try {
        if (!FaweCache.hasNBT(id)) {
            if (block.canStoreNBTData()) {
                this.changeSet.add(x, y, z, combined, block);
            } else {
                this.changeSet.add(x, y, z, combined, (block.getId() << 4) + block.getData());
            }
        } else {
            try {
                CompoundTag tag = queue.getTileEntity(x, y, z);
                this.changeSet.add(x, y, z, new BaseBlock(id, combined & 0xF, tag), block);
            } catch (Throwable e) {
                e.printStackTrace();
                this.changeSet.add(x, y, z, combined, block);
            }
        }
    } catch (FaweException ignore) {
        return false;
    }
    return getExtent().setBlock(x, y, z, block);
}
 
Example 30
@Override
public boolean apply(Extent extent, Vector setPosition, Vector getPosition) throws WorldEditException {
    BaseBlock block = extent.getBlock(getPosition);
    int slope = getSlope(block, getPosition);
    if (slope == -1) return false;
    int color = util.getTextureUtil().getColor(block);
    if (color == 0) return false;
    int newColor = getColor(color, slope);
    BaseBlock newBlock = util.getTextureUtil().getNearestBlock(newColor);
    if (newBlock == null) return false;
    return extent.setBlock(setPosition, newBlock);
}