Search in sources :

Example 1 with NamedTag

use of com.sk89q.jnbt.NamedTag in project FastAsyncWorldEdit by IntellectualSites.

the class MinecraftStructure method read.

@Override
public Clipboard read(UUID clipboardId) throws IOException {
    NamedTag rootTag = inputStream.readNamedTag();
    // MC structures are all unnamed, but this doesn't seem to be necessary? might remove this later
    if (!rootTag.getName().isEmpty()) {
        throw new IOException("Root tag has name - are you sure this is a structure?");
    }
    Map<String, Tag> tags = ((CompoundTag) rootTag.getTag()).getValue();
    ListTag size = (ListTag) tags.get("size");
    int width = size.getInt(0);
    int height = size.getInt(1);
    int length = size.getInt(2);
    // Init clipboard
    BlockVector3 origin = BlockVector3.at(0, 0, 0);
    CuboidRegion region = new CuboidRegion(origin, origin.add(width, height, length).subtract(BlockVector3.ONE));
    Clipboard clipboard = new BlockArrayClipboard(region, clipboardId);
    // Blocks
    ListTag blocks = (ListTag) tags.get("blocks");
    if (blocks != null) {
        // Palette
        List<CompoundTag> palette = (List<CompoundTag>) tags.get("palette").getValue();
        BlockState[] combinedArray = new BlockState[palette.size()];
        for (int i = 0; i < palette.size(); i++) {
            CompoundTag compound = palette.get(i);
            Map<String, Tag> map = compound.getValue();
            String name = ((StringTag) map.get("Name")).getValue();
            BlockType type = BlockTypes.get(name);
            BlockState state = type.getDefaultState();
            CompoundTag properties = (CompoundTag) map.get("Properties");
            if (properties != null) {
                for (Map.Entry<String, Tag> entry : properties.getValue().entrySet()) {
                    String key = entry.getKey();
                    String value = ((StringTag) entry.getValue()).getValue();
                    Property<Object> property = type.getProperty(key);
                    state = state.with(property, property.getValueFor(value));
                }
            }
            combinedArray[i] = state;
        }
        // Populate blocks
        List<CompoundTag> blocksList = (List<CompoundTag>) tags.get("blocks").getValue();
        try {
            for (CompoundTag compound : blocksList) {
                Map<String, Tag> blockMap = compound.getValue();
                IntTag stateTag = (IntTag) blockMap.get("state");
                ListTag posTag = (ListTag) blockMap.get("pos");
                BlockState state = combinedArray[stateTag.getValue()];
                int x = posTag.getInt(0);
                int y = posTag.getInt(1);
                int z = posTag.getInt(2);
                if (state.getBlockType().getMaterial().hasContainer()) {
                    CompoundTag nbt = (CompoundTag) blockMap.get("nbt");
                    if (nbt != null) {
                        BaseBlock block = state.toBaseBlock(nbt);
                        clipboard.setBlock(x, y, z, block);
                        continue;
                    }
                }
                clipboard.setBlock(x, y, z, state);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // Entities
    ListTag entities = (ListTag) tags.get("entities");
    if (entities != null) {
        List<CompoundTag> entityList = (List<CompoundTag>) (List<?>) entities.getValue();
        for (CompoundTag entityEntry : entityList) {
            Map<String, Tag> entityEntryMap = entityEntry.getValue();
            ListTag posTag = (ListTag) entityEntryMap.get("pos");
            CompoundTag nbtTag = (CompoundTag) entityEntryMap.get("nbt");
            String id = nbtTag.getString("Id");
            Location location = NBTConversions.toLocation(clipboard, posTag, nbtTag.getListTag("Rotation"));
            if (!id.isEmpty()) {
                BaseEntity state = new BaseEntity(EntityTypes.get(id), nbtTag);
                clipboard.createEntity(location, state);
            }
        }
    }
    return clipboard;
}
Also used : StringTag(com.sk89q.jnbt.StringTag) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) NamedTag(com.sk89q.jnbt.NamedTag) ArrayList(java.util.ArrayList) List(java.util.List) CompoundTag(com.sk89q.jnbt.CompoundTag) IntTag(com.sk89q.jnbt.IntTag) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) IOException(java.io.IOException) BlockVector3(com.sk89q.worldedit.math.BlockVector3) ListTag(com.sk89q.jnbt.ListTag) IOException(java.io.IOException) BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) StringTag(com.sk89q.jnbt.StringTag) ListTag(com.sk89q.jnbt.ListTag) IntTag(com.sk89q.jnbt.IntTag) NamedTag(com.sk89q.jnbt.NamedTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) HashMap(java.util.HashMap) Map(java.util.Map) Int2ObjectArrayMap(it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap) Location(com.sk89q.worldedit.util.Location)

Example 2 with NamedTag

use of com.sk89q.jnbt.NamedTag in project FastAsyncWorldEdit by IntellectualSites.

the class MCEditSchematicReader method read.

@Override
public Clipboard read() throws IOException {
    // Schematic tag
    NamedTag rootTag = inputStream.readNamedTag();
    if (!rootTag.getName().equals("Schematic")) {
        throw new IOException("Tag 'Schematic' does not exist or is not first");
    }
    CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
    // Check
    Map<String, Tag> schematic = schematicTag.getValue();
    if (!schematic.containsKey("Blocks")) {
        throw new IOException("Schematic file is missing a 'Blocks' tag");
    }
    // Check type of Schematic
    String materials = requireTag(schematic, "Materials", StringTag.class).getValue();
    if (!materials.equals("Alpha")) {
        throw new IOException("Schematic file is not an Alpha schematic");
    }
    // ====================================================================
    // Metadata
    // ====================================================================
    BlockVector3 origin;
    Region region;
    // Get information
    short width = requireTag(schematic, "Width", ShortTag.class).getValue();
    short height = requireTag(schematic, "Height", ShortTag.class).getValue();
    short length = requireTag(schematic, "Length", ShortTag.class).getValue();
    try {
        int originX = requireTag(schematic, "WEOriginX", IntTag.class).getValue();
        int originY = requireTag(schematic, "WEOriginY", IntTag.class).getValue();
        int originZ = requireTag(schematic, "WEOriginZ", IntTag.class).getValue();
        BlockVector3 min = BlockVector3.at(originX, originY, originZ);
        int offsetX = requireTag(schematic, "WEOffsetX", IntTag.class).getValue();
        int offsetY = requireTag(schematic, "WEOffsetY", IntTag.class).getValue();
        int offsetZ = requireTag(schematic, "WEOffsetZ", IntTag.class).getValue();
        BlockVector3 offset = BlockVector3.at(offsetX, offsetY, offsetZ);
        origin = min.subtract(offset);
        region = new CuboidRegion(min, min.add(width, height, length).subtract(BlockVector3.ONE));
    } catch (IOException ignored) {
        origin = BlockVector3.ZERO;
        region = new CuboidRegion(origin, origin.add(width, height, length).subtract(BlockVector3.ONE));
    }
    // ====================================================================
    // Blocks
    // ====================================================================
    // Get blocks
    byte[] blockId = requireTag(schematic, "Blocks", ByteArrayTag.class).getValue();
    byte[] blockData = requireTag(schematic, "Data", ByteArrayTag.class).getValue();
    byte[] addId = new byte[0];
    // Have to later combine IDs
    short[] blocks = new short[blockId.length];
    // the highest 4 bits are stored in a separate byte array.
    if (schematic.containsKey("AddBlocks")) {
        addId = requireTag(schematic, "AddBlocks", ByteArrayTag.class).getValue();
    }
    // Combine the AddBlocks data with the first 8-bit block ID
    for (int index = 0; index < blockId.length; index++) {
        if ((index >> 1) >= addId.length) {
            // No corresponding AddBlocks index
            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));
            }
        }
    }
    // Need to pull out tile entities
    final ListTag tileEntityTag = getTag(schematic, "TileEntities", ListTag.class);
    List<Tag> tileEntities = tileEntityTag == null ? new ArrayList<>() : tileEntityTag.getValue();
    BlockMap<BaseBlock> tileEntityBlocks = BlockMap.createForBaseBlock();
    for (Tag tag : tileEntities) {
        if (!(tag instanceof CompoundTag)) {
            continue;
        }
        CompoundTag t = (CompoundTag) tag;
        Map<String, Tag> values = new HashMap<>(t.getValue());
        String id = t.getString("id");
        values.put("id", new StringTag(convertBlockEntityId(id)));
        int x = t.getInt("x");
        int y = t.getInt("y");
        int z = t.getInt("z");
        int index = y * width * length + z * width + x;
        // position in schematics?
        if (index >= blocks.length) {
            LOGGER.warn("Skipping corrupt tile entity at position {} {} {} in schematic.", x, y, z);
            continue;
        }
        BlockState block = getBlockState(blocks[index], blockData[index]);
        BlockState newBlock = block;
        if (newBlock != null) {
            for (NBTCompatibilityHandler handler : COMPATIBILITY_HANDLERS) {
                if (handler.isAffectedBlock(newBlock)) {
                    newBlock = handler.updateNBT(block, values).toImmutableState();
                    if (newBlock == null || values.isEmpty()) {
                        break;
                    }
                }
            }
        }
        if (values.isEmpty()) {
            t = null;
        } else {
            t = new CompoundTag(values);
        }
        if (fixer != null && t != null) {
            // FAWE start - BinaryTag
            t = (CompoundTag) AdventureNBTConverter.fromAdventure(fixer.fixUp(DataFixer.FixTypes.BLOCK_ENTITY, t.asBinaryTag(), -1));
        // FAWE end
        }
        BlockVector3 vec = BlockVector3.at(x, y, z);
        // Insert into the map if we have changed the block or have a tag
        BlockState blockToInsert = newBlock != null ? newBlock : (t != null ? block : null);
        if (blockToInsert != null) {
            BaseBlock baseBlock = t != null ? blockToInsert.toBaseBlock(new CompoundTag(t.getValue())) : blockToInsert.toBaseBlock();
            tileEntityBlocks.put(vec, baseBlock);
        }
    }
    BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
    clipboard.setOrigin(origin);
    Set<Integer> unknownBlocks = new HashSet<>();
    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;
                BlockVector3 pt = BlockVector3.at(x, y, z);
                BaseBlock state = Optional.ofNullable(tileEntityBlocks.get(pt)).orElseGet(() -> {
                    BlockState blockState = getBlockState(blocks[index], blockData[index]);
                    return blockState == null ? null : blockState.toBaseBlock();
                });
                try {
                    if (state != null) {
                        clipboard.setBlock(region.getMinimumPoint().add(pt), state);
                    } else {
                        short block = blocks[index];
                        byte data = blockData[index];
                        int combined = block << 8 | data;
                        if (unknownBlocks.add(combined)) {
                            LOGGER.warn("Unknown block when loading schematic: {} {}. This is most likely a" + "bad schematic.", block, data);
                        }
                    }
                } catch (WorldEditException ignored) {
                // BlockArrayClipboard won't throw this
                }
            }
        }
    }
    // ====================================================================
    // Entities
    // ====================================================================
    ListTag entityList = getTag(schematic, "Entities", ListTag.class);
    if (entityList != null) {
        List<Tag> entityTags = entityList.getValue();
        for (Tag tag : entityTags) {
            if (tag instanceof CompoundTag) {
                CompoundTag compound = (CompoundTag) tag;
                if (fixer != null) {
                    // FAWE start - BinaryTag
                    compound = (CompoundTag) AdventureNBTConverter.fromAdventure(fixer.fixUp(DataFixer.FixTypes.ENTITY, compound.asBinaryTag(), -1));
                // FAWE end
                }
                String id = convertEntityId(compound.getString("id"));
                Location location = NBTConversions.toLocation(clipboard, compound.getListTag("Pos"), compound.getListTag("Rotation"));
                if (!id.isEmpty()) {
                    EntityType entityType = EntityTypes.get(id.toLowerCase(Locale.ROOT));
                    if (entityType != null) {
                        for (EntityNBTCompatibilityHandler compatibilityHandler : ENTITY_COMPATIBILITY_HANDLERS) {
                            if (compatibilityHandler.isAffectedEntity(entityType, compound)) {
                                compound = compatibilityHandler.updateNBT(entityType, compound);
                            }
                        }
                        BaseEntity state = new BaseEntity(entityType, compound);
                        clipboard.createEntity(location, state);
                    } else {
                        LOGGER.warn("Unknown entity when pasting schematic: " + id.toLowerCase(Locale.ROOT));
                    }
                }
            }
        }
    }
    return clipboard;
}
Also used : StringTag(com.sk89q.jnbt.StringTag) HashMap(java.util.HashMap) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) EntityNBTCompatibilityHandler(com.sk89q.worldedit.extent.clipboard.io.legacycompat.EntityNBTCompatibilityHandler) NBTCompatibilityHandler(com.sk89q.worldedit.extent.clipboard.io.legacycompat.NBTCompatibilityHandler) EntityNBTCompatibilityHandler(com.sk89q.worldedit.extent.clipboard.io.legacycompat.EntityNBTCompatibilityHandler) NamedTag(com.sk89q.jnbt.NamedTag) CompoundTag(com.sk89q.jnbt.CompoundTag) IntTag(com.sk89q.jnbt.IntTag) HashSet(java.util.HashSet) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) IOException(java.io.IOException) BlockVector3(com.sk89q.worldedit.math.BlockVector3) ListTag(com.sk89q.jnbt.ListTag) ShortTag(com.sk89q.jnbt.ShortTag) EntityType(com.sk89q.worldedit.world.entity.EntityType) BlockState(com.sk89q.worldedit.world.block.BlockState) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Region(com.sk89q.worldedit.regions.Region) StringTag(com.sk89q.jnbt.StringTag) ShortTag(com.sk89q.jnbt.ShortTag) ListTag(com.sk89q.jnbt.ListTag) IntTag(com.sk89q.jnbt.IntTag) NamedTag(com.sk89q.jnbt.NamedTag) ByteArrayTag(com.sk89q.jnbt.ByteArrayTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) WorldEditException(com.sk89q.worldedit.WorldEditException) ByteArrayTag(com.sk89q.jnbt.ByteArrayTag) Location(com.sk89q.worldedit.util.Location)

Example 3 with NamedTag

use of com.sk89q.jnbt.NamedTag in project FastAsyncWorldEdit by IntellectualSites.

the class SpongeSchematicReader method getBaseTag.

private CompoundTag getBaseTag() throws IOException {
    NamedTag rootTag = inputStream.readNamedTag();
    CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
    // Check
    Map<String, Tag> schematic = schematicTag.getValue();
    schematicVersion = requireTag(schematic, "Version", IntTag.class).getValue();
    return schematicTag;
}
Also used : NamedTag(com.sk89q.jnbt.NamedTag) StringTag(com.sk89q.jnbt.StringTag) ShortTag(com.sk89q.jnbt.ShortTag) IntArrayTag(com.sk89q.jnbt.IntArrayTag) ListTag(com.sk89q.jnbt.ListTag) IntTag(com.sk89q.jnbt.IntTag) NamedTag(com.sk89q.jnbt.NamedTag) ByteArrayTag(com.sk89q.jnbt.ByteArrayTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) CompoundTag(com.sk89q.jnbt.CompoundTag)

Aggregations

CompoundTag (com.sk89q.jnbt.CompoundTag)3 IntTag (com.sk89q.jnbt.IntTag)3 ListTag (com.sk89q.jnbt.ListTag)3 NamedTag (com.sk89q.jnbt.NamedTag)3 StringTag (com.sk89q.jnbt.StringTag)3 Tag (com.sk89q.jnbt.Tag)3 ByteArrayTag (com.sk89q.jnbt.ByteArrayTag)2 ShortTag (com.sk89q.jnbt.ShortTag)2 BaseEntity (com.sk89q.worldedit.entity.BaseEntity)2 BlockArrayClipboard (com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard)2 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)2 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)2 Location (com.sk89q.worldedit.util.Location)2 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)2 BlockState (com.sk89q.worldedit.world.block.BlockState)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 IntArrayTag (com.sk89q.jnbt.IntArrayTag)1 WorldEditException (com.sk89q.worldedit.WorldEditException)1 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)1