Java Code Examples for com.sk89q.worldedit.blocks.BaseBlock#getData()

The following examples show how to use com.sk89q.worldedit.blocks.BaseBlock#getData() . 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
Source File: DiskOptimizedClipboard.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
@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
Source File: MappedReplacePatternFilter.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void applyBlock(int x, int y, int z, BaseBlock block, MutableLong ignore) {
    int id = block.getId();
    int data = FaweCache.hasData(id) ? block.getData() : 0;
    int combined = FaweCache.getCombined(id, data);
    Pattern p = map[combined];
    if (p != null) {
        BaseBlock newBlock = p.apply(x, y, z);
        int currentId = block.getId();
        if (FaweCache.hasNBT(currentId)) {
            block.setNbtData(null);
        }
        block.setId(newBlock.getId());
        block.setData(newBlock.getData());
    }
}
 
Example 3
Source File: BundledBlockData.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
public String getName(BaseBlock block) {
    BlockEntry entry = legacyMap[block.getId()];
    if (entry != null) {
        String name = entry.id;
        if (block.getData() == 0) return name;
        StringBuilder append = new StringBuilder();
        Map<String, FaweState> states = entry.states;
        FaweState variants = states.get("variant");
        if (variants == null) variants = states.get("color");
        if (variants == null && !states.isEmpty()) variants = states.entrySet().iterator().next().getValue();
        if (variants != null) {
            for (Map.Entry<String, FaweStateValue> variant : variants.valueMap().entrySet()) {
                FaweStateValue state = variant.getValue();
                if (state.isSet(block)) {
                    append.append(append.length() == 0 ? ":" : "|");
                    append.append(variant.getKey());
                }
            }
        }
        if (append.length() == 0) return name + ":" + block.getData();
        return name + append;
    }
    if (block.getData() == 0) return Integer.toString(block.getId());
    return block.getId() + ":" + block.getData();
}
 
Example 4
Source File: LocalSession.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
public void setTool(BaseBlock item, @Nullable Tool tool, Player player) throws InvalidToolBindException {
    int id = item.getId();
    int data = item.getData();
    if (id > 0 && id < 255) {
        throw new InvalidToolBindException(id, "Blocks can't be used");
    } else if (id == config.wandItem) {
        throw new InvalidToolBindException(id, "Already used for the wand");
    } else if (id == config.navigationWand) {
        throw new InvalidToolBindException(id, "Already used for the navigation wand");
    }
    Tool previous;
    if (player != null && (tool instanceof BrushTool || tool == null) && item instanceof BrushHolder) {
        BrushHolder holder = (BrushHolder) item;
        previous = holder.setTool((BrushTool) tool);
        if (tool != null) ((BrushTool) tool).setHolder(holder);
    } else {
        previous = this.tools.put(FaweCache.getCombined(id, data), tool);
    }
    if (previous != null && player != null && previous instanceof BrushTool) {
        BrushTool brushTool = (BrushTool) previous;
        brushTool.clear(player);
    }
}
 
Example 5
Source File: FaweChangeSet.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public void add(int x, int y, int z, int combinedFrom, BaseBlock to) {
    try {
        if (to.hasNbtData()) {
            CompoundTag nbt = to.getNbtData();
            MainUtil.setPosition(nbt, x, y, z);
            addTileCreate(nbt);
        }
        int combinedTo = (to.getId() << 4) + to.getData();
        add(x, y, z, combinedFrom, combinedTo);

    } catch (Exception e) {
        MainUtil.handleError(e);
    }
}
 
Example 6
Source File: HistoryExtent.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@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 7
Source File: IdDataMaskPattern.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@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 8
Source File: Functions.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Dynamic
public static double queryRel(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.getBlockRel(xp, yp, zp);
    int typeId = block.getId();
    int dataValue = block.getData();

    return queryInternal(type, data, typeId, dataValue);
}
 
Example 9
Source File: BlockMask.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public void add(BaseBlock block) {
    blockIds[block.getId()] = true;
    if (block.getData() == -1) {
        for (int data = 0; data < 16; data++) {
            blocks[FaweCache.getCombined(block.getId(), data)] = true;
        }
    } else {
        blocks[FaweCache.getCombined(block)] = true;
    }
    computedLegacyList = null;
}
 
Example 10
Source File: Functions.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Dynamic
public static double queryAbs(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.getBlockAbs(xp, yp, zp);
    int typeId = block.getId();
    int dataValue = block.getData();

    return queryInternal(type, data, typeId, dataValue);
}
 
Example 11
Source File: Functions.java    From FastAsyncWorldedit with 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 12
Source File: SchematicWriter.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void run(int x, int y, int z, BaseBlock block) {
    if (this.x == x - 1 && this.y == y && this.z == z) {
        this.x++;
        index++;
    } else {
        index = yarea[this.y = y] + zwidth[this.z = z] + (this.x = x);
    }
    int id = block.getId();
    blocks[index] = (byte) id;
    if (FaweCache.hasData(id)) {
        blockData[index] = (byte) block.getData();
        if (id > 255) {
            if (addBlocks == null) { // Lazily create section
                addBlocks = new byte[((blocks.length + 1) >> 1)];
            }
            addBlocks[index >> 1] = (byte) (((index & 1) == 0) ? addBlocks[index >> 1] & 0xF0 | (id >> 8) & 0xF : addBlocks[index >> 1] & 0xF | ((id >> 8) & 0xF) << 4);
        }
    }
    CompoundTag rawTag = block.getNbtData();
    if (rawTag != null) {
        Map<String, Tag> values = ReflectionUtils.getMap(rawTag.getValue());
        values.put("id", new StringTag(block.getNbtId()));
        values.put("x", new IntTag(x));
        values.put("y", new IntTag(y));
        values.put("z", new IntTag(z));
        tileEntities.add(rawTag);
    }
}
 
Example 13
Source File: ArbitraryShape.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
private BaseBlock getMaterialCached(int x, int y, int z, Pattern pattern) {
    final int index = (y - cacheOffsetY) + (z - cacheOffsetZ) * cacheSizeY + (x - cacheOffsetX) * cacheSizeY * cacheSizeZ;

    final short cacheEntry = cache[index];
    switch (cacheEntry) {
        case 0:
            // unknown, fetch material
            final BaseBlock material = getMaterial(x, y, z, pattern.apply(new BlockVector(x, y, z)));
            if (material == null) {
                // outside
                cache[index] = -1;
                return null;
            }

            short newCacheEntry = (short) (material.getType() | ((material.getData() + 1) << 8));
            if (newCacheEntry == 0) {
                // type and data 0
                newCacheEntry = -2;
            }

            cache[index] = newCacheEntry;
            return material;

        case -1:
            // outside
            return null;

        case -2:
            // type and data 0
            return new BaseBlock(0, 0);
    }

    return new BaseBlock(cacheEntry & 255, ((cacheEntry >> 8) - 1) & 15);
}
 
Example 14
Source File: CuboidClipboard.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the block distribution inside a clipboard with data values.
 *
 * @return a block distribution
 */
// TODO reduce code duplication
public List<Countable<BaseBlock>> getBlockDistributionWithData() {
    List<Countable<BaseBlock>> distribution = new ArrayList<Countable<BaseBlock>>();
    Map<BaseBlock, Countable<BaseBlock>> map = new HashMap<BaseBlock, Countable<BaseBlock>>();

    int maxX = getWidth();
    int maxY = getHeight();
    int maxZ = getLength();

    for (int x = 0; x < maxX; ++x) {
        for (int y = 0; y < maxY; ++y) {
            for (int z = 0; z < maxZ; ++z) {
                final BaseBlock block = getBlock(x, y, z);
                if (block == null) {
                    continue;
                }

                // Strip the block from metadata that is not part of our key
                final BaseBlock bareBlock = new BaseBlock(block.getId(), block.getData());

                if (map.containsKey(bareBlock)) {
                    map.get(bareBlock).increment();
                } else {
                    Countable<BaseBlock> c = new Countable<BaseBlock>(bareBlock, 1);
                    map.put(bareBlock, c);
                    distribution.add(c);
                }
            }
        }
    }

    Collections.sort(distribution);
    // Collections.reverse(distribution);

    return distribution;
}
 
Example 15
Source File: FlagRegenPercentage.java    From HeavySpleef with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void regenerate(Floor floor, EditSession session, RegenerationCause cause) {
	if (cause == RegenerationCause.COUNTDOWN) {
		return;
	}
	
	Region region = floor.getRegion();
	
	double percentage = getValue() / 100D;
	int area = region.getArea();
	
	int notRegenerating = area - (int) (percentage * area);
	
	Iterator<BlockVector> iterator = region.iterator();
	List<BlockVector> vectors = Lists.newArrayList();
	Map<BlockVector, BaseBlock> clipboardBlockCache = Maps.newHashMap();
	Clipboard clipboard = floor.getClipboard();
	
	while (iterator.hasNext()) {
		BlockVector vector = iterator.next();
		BaseBlock block = clipboard.getLazyBlock(vector);
		clipboardBlockCache.put(vector, block);
		
		if (block.getType() == AIR_ID) {
			continue;
		}
		
		vectors.add(vector);
	}
	
	for (int i = 0; i < notRegenerating; i++) {
		int rnd = random.nextInt(vectors.size());
		vectors.remove(rnd);
	}
	
	World world = region.getWorld();
	
	try {
		//Iterate over all remaining block vectors and regenerate these blocks
		for (BlockVector regeneratingBlock : vectors) {
			BaseBlock clipboardBlock = clipboardBlockCache.get(regeneratingBlock);
			BaseBlock worldBlock = world.getBlock(regeneratingBlock);
			
			int id = clipboardBlock.getId();
			int data = clipboardBlock.getData();
			
			worldBlock.setIdAndData(id, data);
			world.setBlock(regeneratingBlock, worldBlock);
		}
	} catch (WorldEditException e) {
		getHeavySpleef().getLogger().log(Level.SEVERE, "Failed to regenerate floor " + floor.getName() + " for game " + game.getName(), e);
	}
}
 
Example 16
Source File: Extent.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
default public int getNearestSurfaceLayer(int x, int z, int y, int minY, int maxY) {
        int clearanceAbove = maxY - y;
        int clearanceBelow = y - minY;
        int clearance = Math.min(clearanceAbove, clearanceBelow);

        BaseBlock block = getLazyBlock(x, y, z);
        boolean state = FaweCache.isLiquidOrGas(block.getId());
        int data1 = block.getData();
        int data2 = block.getData();
        int offset = state ? 0 : 1;
        for (int d = 0; d <= clearance; d++) {
            int y1 = y + d;
            block = getLazyBlock(x, y1, z);
            if (FaweCache.isLiquidOrGas(block.getId()) != state) {
                return ((y1 - offset) << 3) - (7 - (state ? block.getData() : data1));
            }
            data1 = block.getData();
            int y2 = y - d;
            block = getLazyBlock(x, y2, z);
            if (FaweCache.isLiquidOrGas(block.getId()) != state) {
                return ((y2 + offset) << 3) - (7 - (state ? block.getData() : data2));
            }
            data2 = block.getData();
        }
        if (clearanceAbove != clearanceBelow) {
            if (clearanceAbove < clearanceBelow) {
                for (int layer = y - clearance - 1; layer >= minY; layer--) {
                    block = getLazyBlock(x, layer, z);
                    if (FaweCache.isLiquidOrGas(block.getId()) != state) {

//                        int blockHeight = (newHeight) >> 3;
//                        int layerHeight = (newHeight) & 0x7;

                        int data = (state ? block.getData() : data1);
                        return ((layer + offset) << 3) + 0;
                    }
                    data1 = block.getData();
                }
            } else {
                for (int layer = y + clearance + 1; layer <= maxY; layer++) {
                    block = getLazyBlock(x, layer, z);
                    if (FaweCache.isLiquidOrGas(block.getId()) != state) {
                        return ((layer - offset) << 3) - (7 - (state ? block.getData() : data2));
                    }
                    data2 = block.getData();
                }
            }
        }
        return (state ? minY : maxY) << 4;
    }
 
Example 17
Source File: EditSession.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Make dirt green.
 *
 * @param position       a position
 * @param radius         a radius
 * @param onlyNormalDirt only affect normal dirt (data value 0)
 * @return number of blocks affected
 * @throws MaxChangedBlocksException thrown if too many blocks are changed
 */
public int green(final Vector position, final double radius, final boolean onlyNormalDirt) {

    final double radiusSq = radius * radius;

    final int ox = position.getBlockX();
    final int oy = position.getBlockY();
    final int oz = position.getBlockZ();

    final BaseBlock grass = new BaseBlock(BlockID.GRASS);

    final int ceilRadius = (int) Math.ceil(radius);
    for (int x = ox - ceilRadius; x <= (ox + ceilRadius); ++x) {
        int dx = x - ox;
        int dx2 = dx * dx;
        for (int z = oz - ceilRadius; z <= (oz + ceilRadius); ++z) {
            int dz = z - oz;
            int dz2 = dz * dz;
            if (dx2 + dz2 > radiusSq) {
                continue;
            }
            loop:
            for (int y = maxY; y >= 1; --y) {
                BaseBlock block = getLazyBlock(x, y, z);
                final int id = block.getId();
                final int data = block.getData();

                switch (id) {
                    case BlockID.DIRT:
                        if (onlyNormalDirt && (data != 0)) {
                            break loop;
                        }
                        this.setBlock(x, y, z, grass);
                        break loop;

                    case BlockID.WATER:
                    case BlockID.STATIONARY_WATER:
                    case BlockID.LAVA:
                    case BlockID.STATIONARY_LAVA:
                        // break on liquids...
                        break loop;

                    default:
                        // ...and all non-passable blocks
                        if (!BlockType.canPassThrough(id, data)) {
                            break loop;
                        }
                }
            }
        }
    }

    return changes;
}
 
Example 18
Source File: BundledBlockData.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean isSet(BaseBlock block) {
    return data != null && ((state.dataMask == null && block.getData() == data) || (block.getData() & state.getDataMask()) == data);
}
 
Example 19
Source File: ClipboardRemapper.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
public BaseItem remapItem(String name, int damage) {
    if (name.isEmpty()) return new BaseItem(0);
    if (from == RemapPlatform.PC) {
        BundledBlockData.BlockEntry state = BundledBlockData.getInstance().findById(name);
        if (state != null) {
            BaseBlock remapped = remap(new BaseBlock(state.legacyId, damage));
            return new BaseItem(remapped.getId(), (short) remapped.getData());
        } else {
            try {
                name = name.replace("minecraft:", "");
                WikiScraper scraper = loadItemMapping();
                Map<String, Integer> mapFrom = scraper.scapeOrCache(WikiScraper.Wiki.valueOf("ITEM_MAPPINGS_" + from.name()));
                Map<String, Integer> mapTo = scraper.scapeOrCache(WikiScraper.Wiki.valueOf("ITEM_MAPPINGS_" + from.opposite().name()));
                scraper.expand(mapTo);
                switch (name) {
                    case "spruce_boat":  return new BaseItem(333, (short) 1);
                    case "birch_boat":  return new BaseItem(333, (short) 2);
                    case "jungle_boat":  return new BaseItem(333, (short) 3);
                    case "acacia_boat":  return new BaseItem(333, (short) 4);
                    case "dark_oak_boat": return new BaseItem(333, (short) 5);
                    case "water_bucket": return new BaseItem(325, (short) 8);
                    case "lava_bucket": return new BaseItem(325, (short) 10);
                    case "milk_bucket": return new BaseItem(325, (short) 1);
                    case "tipped_arrow":
                    case "spectral_arrow":
                        name = "arrow"; // Unsupported
                        break;
                    case "totem_of_undying":
                        name = "totem";
                        break;
                    case "furnace_minecart":
                        name = "minecart"; // Unsupported
                        break;
                }
                Integer itemId = mapTo.get(name);
                if (itemId == null) itemId = mapTo.get(name.replace("_", ""));
                if (itemId == null) itemId = mapFrom.get(name);
                if (itemId != null) return new BaseItem(itemId, (short) damage);
            } catch (IOException ignore) {
                ignore.printStackTrace();
            }
        }
    }
    return new BaseItem(0);
}