Search in sources :

Example 1 with BlockData

use of net.aufdemrand.denizen.nms.interfaces.BlockData in project Denizen-For-Bukkit by DenizenScript.

the class BlockData_v1_11_R1 method fromCompressedString.

public static BlockData fromCompressedString(String str) {
    BlockData data = new BlockData_v1_11_R1();
    String inner = str.substring(1, str.length() - 1);
    String[] datas = inner.split(":");
    data.setMaterial(Material.getMaterial(Integer.parseInt(datas[0])));
    data.setData(Byte.parseByte(datas[1]));
    if (data.getMaterial() == null) {
        throw new RuntimeException("Null material: " + datas[0]);
    }
    return data;
}
Also used : BlockData(net.aufdemrand.denizen.nms.interfaces.BlockData)

Example 2 with BlockData

use of net.aufdemrand.denizen.nms.interfaces.BlockData in project Denizen-For-Bukkit by DenizenScript.

the class CuboidBlockSet method fromMCEditStream.

public static CuboidBlockSet fromMCEditStream(InputStream is) {
    CuboidBlockSet cbs = new CuboidBlockSet();
    try {
        NBTInputStream nbtStream = new NBTInputStream(new GZIPInputStream(is));
        NamedTag rootTag = nbtStream.readNamedTag();
        nbtStream.close();
        if (!rootTag.getName().equals("Schematic")) {
            throw new Exception("Tag 'Schematic' does not exist or is not first!");
        }
        CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
        Map<String, Tag> schematic = schematicTag.getValue();
        short width = (getChildTag(schematic, "Width", ShortTag.class).getValue());
        short length = (getChildTag(schematic, "Length", ShortTag.class).getValue());
        short height = (getChildTag(schematic, "Height", ShortTag.class).getValue());
        int originX = 0;
        int originY = 0;
        int originZ = 0;
        try {
            originX = getChildTag(schematic, "DenizenOriginX", IntTag.class).getValue();
            originY = getChildTag(schematic, "DenizenOriginY", IntTag.class).getValue();
            originZ = getChildTag(schematic, "DenizenOriginZ", IntTag.class).getValue();
        } catch (Exception e) {
        // Default origin, why not
        }
        cbs.x_width = width;
        cbs.z_height = length;
        cbs.y_length = height;
        cbs.center_x = originX;
        cbs.center_y = originY;
        cbs.center_z = originZ;
        // Disregard Offset
        String materials = getChildTag(schematic, "Materials", StringTag.class).getValue();
        if (!materials.equals("Alpha")) {
            throw new Exception("Schematic file is not an Alpha schematic!");
        }
        byte[] blockId = getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
        byte[] blockData = getChildTag(schematic, "Data", ByteArrayTag.class).getValue();
        byte[] addId = new byte[0];
        short[] blocks = new short[blockId.length];
        if (schematic.containsKey("AddBlocks")) {
            addId = getChildTag(schematic, "AddBlocks", ByteArrayTag.class).getValue();
        }
        for (int index = 0; index < blockId.length; index++) {
            if ((index >> 1) >= addId.length) {
                blocks[index] = (short) (blockId[index] & 0xFF);
            } else {
                if ((index & 1) == 0) {
                    blocks[index] = (short) (((addId[index >> 1] & 0x0F) << 8) + (blockId[index] & 0xFF));
                } else {
                    blocks[index] = (short) (((addId[index >> 1] & 0xF0) << 4) + (blockId[index] & 0xFF));
                }
            }
        }
        List<Tag> tileEntities = getChildTag(schematic, "TileEntities", ListTag.class).getValue();
        Map<BlockVector, Map<String, Tag>> tileEntitiesMap = new HashMap<BlockVector, Map<String, Tag>>();
        for (Tag tag : tileEntities) {
            if (!(tag instanceof CompoundTag)) {
                continue;
            }
            CompoundTag t = (CompoundTag) tag;
            int x = 0;
            int y = 0;
            int z = 0;
            Map<String, Tag> values = new HashMap<String, Tag>();
            for (Map.Entry<String, Tag> entry : t.getValue().entrySet()) {
                if (entry.getKey().equals("x")) {
                    if (entry.getValue() instanceof IntTag) {
                        x = ((IntTag) entry.getValue()).getValue();
                    }
                } else if (entry.getKey().equals("y")) {
                    if (entry.getValue() instanceof IntTag) {
                        y = ((IntTag) entry.getValue()).getValue();
                    }
                } else if (entry.getKey().equals("z")) {
                    if (entry.getValue() instanceof IntTag) {
                        z = ((IntTag) entry.getValue()).getValue();
                    }
                }
                values.put(entry.getKey(), entry.getValue());
            }
            BlockVector vec = new BlockVector(x, y, z);
            tileEntitiesMap.put(vec, values);
        }
        org.bukkit.util.Vector vec = new Vector(width, height, length);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                for (int z = 0; z < length; z++) {
                    int index = y * width * length + z * width + x;
                    BlockVector pt = new BlockVector(x, y, z);
                    BlockData block = NMSHandler.getInstance().getBlockHelper().getBlockData(blocks[index], blockData[index]);
                    if (tileEntitiesMap.containsKey(pt)) {
                        CompoundTag otag = NMSHandler.getInstance().createCompoundTag(tileEntitiesMap.get(pt));
                        block.setCompoundTag(otag);
                    }
                    cbs.blocks.add(block);
                }
            }
        }
    } catch (Exception e) {
        dB.echoError(e);
    }
    return cbs;
}
Also used : HashMap(java.util.HashMap) GZIPInputStream(java.util.zip.GZIPInputStream) BlockData(net.aufdemrand.denizen.nms.interfaces.BlockData) BlockVector(org.bukkit.util.BlockVector) Vector(org.bukkit.util.Vector) Vector(org.bukkit.util.Vector) BlockVector(org.bukkit.util.BlockVector) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with BlockData

use of net.aufdemrand.denizen.nms.interfaces.BlockData in project Denizen-For-Bukkit by DenizenScript.

the class CuboidBlockSet method saveMCEditFormatToStream.

// Thanks to WorldEdit for sample code
public void saveMCEditFormatToStream(OutputStream os) {
    try {
        HashMap<String, Tag> schematic = new HashMap<String, Tag>();
        schematic.put("Width", new ShortTag((short) (x_width)));
        schematic.put("Length", new ShortTag((short) (z_height)));
        schematic.put("Height", new ShortTag((short) (y_length)));
        schematic.put("Materials", new StringTag("Alpha"));
        schematic.put("DenizenOriginX", new IntTag((int) center_x));
        schematic.put("DenizenOriginY", new IntTag((int) center_y));
        schematic.put("DenizenOriginZ", new IntTag((int) center_z));
        schematic.put("WEOriginX", new IntTag((int) center_x));
        schematic.put("WEOriginY", new IntTag((int) center_y));
        schematic.put("WEOriginZ", new IntTag((int) center_z));
        schematic.put("WEOffsetX", new IntTag(0));
        schematic.put("WEOffsetY", new IntTag(0));
        schematic.put("WEOffsetZ", new IntTag(0));
        byte[] blocks = new byte[(int) ((x_width) * (y_length) * (z_height))];
        byte[] addBlocks = null;
        byte[] blockData = new byte[blocks.length];
        ArrayList<Tag> tileEntities = new ArrayList<Tag>();
        int indexer = 0;
        for (int x = 0; x < x_width; x++) {
            for (int y = 0; y < y_length; y++) {
                for (int z = 0; z < z_height; z++) {
                    int index = (int) (y * (x_width) * (z_height) + z * (x_width) + x);
                    //blockAt(x, y, z);
                    BlockData bd = this.blocks.get(indexer);
                    indexer++;
                    if (bd.getMaterial().getId() > 255) {
                        if (addBlocks == null) {
                            addBlocks = new byte[(blocks.length >> 1) + 1];
                        }
                        addBlocks[index >> 1] = (byte) (((index & 1) == 0) ? addBlocks[index >> 1] & 0xF0 | (bd.getMaterial().getId() >> 8) & 0xF : addBlocks[index >> 1] & 0xF | ((bd.getMaterial().getId() >> 8) & 0xF) << 4);
                    }
                    blocks[index] = (byte) bd.getMaterial().getId();
                    blockData[index] = bd.getData();
                    CompoundTag rawTag = bd.getCompoundTag();
                    if (rawTag != null) {
                        HashMap<String, Tag> values = new HashMap<String, Tag>();
                        for (Map.Entry<String, Tag> entry : rawTag.getValue().entrySet()) {
                            values.put(entry.getKey(), entry.getValue());
                        }
                        // TODO: ??? -> values.put("id", new StringTag(null)); // block.getNbtId()
                        values.put("x", new IntTag(x));
                        values.put("y", new IntTag(y));
                        values.put("z", new IntTag(z));
                        CompoundTag tileEntityTag = NMSHandler.getInstance().createCompoundTag(values);
                        tileEntities.add(tileEntityTag);
                    }
                }
            }
        }
        schematic.put("Blocks", new ByteArrayTag(blocks));
        schematic.put("Data", new ByteArrayTag(blockData));
        schematic.put("Entities", new ListTag(CompoundTag.class, new ArrayList<Tag>()));
        schematic.put("TileEntities", new ListTag(CompoundTag.class, tileEntities));
        if (addBlocks != null) {
            schematic.put("AddBlocks", new ByteArrayTag(addBlocks));
        }
        CompoundTag schematicTag = NMSHandler.getInstance().createCompoundTag(schematic);
        NBTOutputStream stream = new NBTOutputStream(new GZIPOutputStream(os));
        stream.writeNamedTag("Schematic", schematicTag);
        os.flush();
        stream.close();
    } catch (Exception e) {
        dB.echoError(e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GZIPOutputStream(java.util.zip.GZIPOutputStream) BlockData(net.aufdemrand.denizen.nms.interfaces.BlockData) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with BlockData

use of net.aufdemrand.denizen.nms.interfaces.BlockData in project Denizen-For-Bukkit by DenizenScript.

the class SchematicCommand method schematicTags.

@TagManager.TagEvents
public void schematicTags(ReplaceableTagEvent event) {
    if (!event.matches("schematic, schem")) {
        return;
    }
    String id = event.hasNameContext() ? event.getNameContext().toUpperCase() : null;
    Attribute attribute = event.getAttributes().fulfill(1);
    // -->
    if (attribute.startsWith("list")) {
        event.setReplaced(new dList(schematics.keySet()).getAttribute(attribute.fulfill(1)));
    }
    if (id == null) {
        return;
    }
    if (!schematics.containsKey(id)) {
        // Meta below
        if (attribute.startsWith("exists")) {
            event.setReplaced(new Element(false).getAttribute(attribute.fulfill(1)));
            return;
        }
        dB.echoError(attribute.getScriptEntry() != null ? attribute.getScriptEntry().getResidingQueue() : null, "Schematic file " + id + " is not loaded.");
        return;
    }
    CuboidBlockSet set = schematics.get(id);
    // -->
    if (attribute.startsWith("exists")) {
        event.setReplaced(new Element(true).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("height")) {
        event.setReplaced(new Element(set.y_length).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("length")) {
        event.setReplaced(new Element(set.z_height).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("width")) {
        event.setReplaced(new Element(set.x_width).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("block")) {
        if (attribute.hasContext(1) && dLocation.matches(attribute.getContext(1))) {
            dLocation location = dLocation.valueOf(attribute.getContext(1));
            BlockData block = set.blockAt(location.getX(), location.getY(), location.getZ());
            event.setReplaced(dMaterial.getMaterialFrom(block.getMaterial(), block.getData()).getAttribute(attribute.fulfill(1)));
            return;
        }
    }
    // -->
    if (attribute.startsWith("origin")) {
        event.setReplaced(new dLocation(null, set.center_x, set.center_y, set.center_z).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("blocks")) {
        event.setReplaced(new Element(set.blocks.size()).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("cuboid") && attribute.hasContext(1)) {
        dLocation origin = dLocation.valueOf(attribute.getContext(1));
        event.setReplaced(set.getCuboid(origin).getAttribute(attribute.fulfill(1)));
        return;
    }
}
Also used : Attribute(net.aufdemrand.denizencore.tags.Attribute) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) BlockData(net.aufdemrand.denizen.nms.interfaces.BlockData) CuboidBlockSet(net.aufdemrand.denizen.utilities.blocks.CuboidBlockSet)

Example 5 with BlockData

use of net.aufdemrand.denizen.nms.interfaces.BlockData in project Denizen-For-Bukkit by DenizenScript.

the class BlockData_v1_8_R3 method fromCompressedString.

public static BlockData fromCompressedString(String str) {
    BlockData data = new BlockData_v1_8_R3();
    String inner = str.substring(1, str.length() - 1);
    String[] datas = inner.split(":");
    data.setMaterial(Material.getMaterial(Integer.parseInt(datas[0])));
    data.setData(Byte.parseByte(datas[1]));
    if (data.getMaterial() == null) {
        throw new RuntimeException("Null material: " + datas[0]);
    }
    return data;
}
Also used : BlockData(net.aufdemrand.denizen.nms.interfaces.BlockData)

Aggregations

BlockData (net.aufdemrand.denizen.nms.interfaces.BlockData)7 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ArrayList (java.util.ArrayList)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)1 CuboidBlockSet (net.aufdemrand.denizen.utilities.blocks.CuboidBlockSet)1 Element (net.aufdemrand.denizencore.objects.Element)1 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)1 Attribute (net.aufdemrand.denizencore.tags.Attribute)1 BlockVector (org.bukkit.util.BlockVector)1 Vector (org.bukkit.util.Vector)1