Search in sources :

Example 16 with CompoundTag

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

the class ChunkStoreHelper method hasLevelSections.

private static boolean hasLevelSections(CompoundTag rootTag) {
    Map<String, Tag> children = rootTag.getValue();
    Tag levelTag = children.get("Level");
    if (levelTag instanceof CompoundTag) {
        return ((CompoundTag) levelTag).getValue().containsKey("Sections");
    }
    return false;
}
Also used : CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) CompoundTag(com.sk89q.jnbt.CompoundTag)

Example 17 with CompoundTag

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

the class ChunkStoreHelper method getChunk.

/**
 * Convert a chunk NBT tag into a {@link Chunk} implementation.
 *
 * @param rootTag     the root tag of the chunk
 * @param entitiesTag supplier to provide entities tag. Only required for 1.17+ where entities are stored in a separate
 *                    location
 * @return a Chunk implementation
 * @throws DataException if the rootTag is not valid chunk data
 * @since 2.1.0
 */
public static Chunk getChunk(CompoundTag rootTag, Supplier<CompoundTag> entitiesTag) throws DataException {
    // FAWE end
    int dataVersion = rootTag.getInt("DataVersion");
    if (dataVersion == 0) {
        dataVersion = -1;
    }
    final Platform platform = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING);
    final int currentDataVersion = platform.getDataVersion();
    if ((dataVersion > 0 || hasLevelSections(rootTag)) && dataVersion < currentDataVersion) {
        // only fix up MCA format, DFU doesn't support MCR chunks
        final DataFixer dataFixer = platform.getDataFixer();
        if (dataFixer != null) {
            // FAWE start - CBT
            rootTag = (CompoundTag) AdventureNBTConverter.fromAdventure(dataFixer.fixUp(DataFixer.FixTypes.CHUNK, rootTag.asBinaryTag(), dataVersion));
            // FAWE end
            dataVersion = currentDataVersion;
        }
    }
    if (dataVersion >= Constants.DATA_VERSION_MC_1_18) {
        return new AnvilChunk18(rootTag, entitiesTag);
    }
    Map<String, Tag> children = rootTag.getValue();
    CompoundTag tag = null;
    // Find Level tag
    for (Map.Entry<String, Tag> entry : children.entrySet()) {
        if (entry.getKey().equals("Level")) {
            if (entry.getValue() instanceof CompoundTag) {
                tag = (CompoundTag) entry.getValue();
                break;
            } else {
                throw new ChunkStoreException("CompoundTag expected for 'Level'; got " + entry.getValue().getClass().getName());
            }
        }
    }
    if (tag == null) {
        throw new ChunkStoreException("Missing root 'Level' tag");
    }
    // FAWE start - biome and entity restore
    if (dataVersion >= Constants.DATA_VERSION_MC_1_17) {
        return new AnvilChunk17(tag, entitiesTag);
    }
    // FAWE end
    if (dataVersion >= Constants.DATA_VERSION_MC_1_16) {
        return new AnvilChunk16(tag);
    }
    // FAWE start - biome and entity restore
    if (dataVersion >= Constants.DATA_VERSION_MC_1_15) {
        return new AnvilChunk15(tag);
    }
    // FAWE end
    if (dataVersion >= Constants.DATA_VERSION_MC_1_13) {
        return new AnvilChunk13(tag);
    }
    Map<String, Tag> tags = tag.getValue();
    if (tags.containsKey("Sections")) {
        return new AnvilChunk(tag);
    }
    return new OldChunk(tag);
}
Also used : AnvilChunk18(com.sk89q.worldedit.world.chunk.AnvilChunk18) Platform(com.sk89q.worldedit.extension.platform.Platform) OldChunk(com.sk89q.worldedit.world.chunk.OldChunk) AnvilChunk13(com.sk89q.worldedit.world.chunk.AnvilChunk13) AnvilChunk15(com.sk89q.worldedit.world.chunk.AnvilChunk15) AnvilChunk16(com.sk89q.worldedit.world.chunk.AnvilChunk16) AnvilChunk17(com.sk89q.worldedit.world.chunk.AnvilChunk17) AnvilChunk(com.sk89q.worldedit.world.chunk.AnvilChunk) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) DataFixer(com.sk89q.worldedit.world.DataFixer) Map(java.util.Map) CompoundTag(com.sk89q.jnbt.CompoundTag)

Example 18 with CompoundTag

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

the class BlockTransformExtent method transformBaseBlockNBT.

private static BaseBlock transformBaseBlockNBT(BlockState transformed, CompoundTag tag, Transform transform) {
    if (tag != null) {
        if (tag.containsKey("Rot")) {
            int rot = tag.asInt("Rot");
            Direction direction = MCDirections.fromRotation(rot);
            if (direction != null) {
                Vector3 applyAbsolute = transform.apply(direction.toVector());
                Vector3 applyOrigin = transform.apply(Vector3.ZERO);
                applyAbsolute.mutX(applyAbsolute.getX() - applyOrigin.getX());
                applyAbsolute.mutY(applyAbsolute.getY() - applyOrigin.getY());
                applyAbsolute.mutZ(applyAbsolute.getZ() - applyOrigin.getZ());
                Direction newDirection = Direction.findClosest(applyAbsolute, Direction.Flag.CARDINAL | Direction.Flag.ORDINAL | Direction.Flag.SECONDARY_ORDINAL);
                if (newDirection != null) {
                    Map<String, Tag> values = new HashMap<>(tag.getValue());
                    values.put("Rot", new ByteTag((byte) MCDirections.toRotation(newDirection)));
                    tag = new CompoundTag(values);
                }
            }
        }
        return new BaseBlock(transformed, tag);
    }
    return transformed.toBaseBlock();
}
Also used : HashMap(java.util.HashMap) ByteTag(com.sk89q.jnbt.ByteTag) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Vector3(com.sk89q.worldedit.math.Vector3) CompoundTag(com.sk89q.jnbt.CompoundTag) ByteTag(com.sk89q.jnbt.ByteTag) Tag(com.sk89q.jnbt.Tag) Direction(com.sk89q.worldedit.util.Direction) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) CompoundTag(com.sk89q.jnbt.CompoundTag)

Example 19 with CompoundTag

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

the class ExtentEntityCopy method apply.

@Override
public boolean apply(Entity entity) throws WorldEditException {
    BaseEntity state = entity.getState();
    // FAWE start - Don't copy players
    if (state != null && state.getType() != EntityTypes.PLAYER) {
        // FAWE end
        Location newLocation;
        Location location = entity.getLocation();
        // If the entity has stored the location in the NBT data, we use that location
        CompoundTag tag = state.getNbtData();
        boolean hasTilePosition = tag != null && tag.containsKey("TileX") && tag.containsKey("TileY") && tag.containsKey("TileZ");
        if (hasTilePosition) {
            location = location.setPosition(Vector3.at(tag.asInt("TileX"), tag.asInt("TileY"), tag.asInt("TileZ")).add(0.5, 0.5, 0.5));
        }
        Vector3 pivot = from.round().add(0.5, 0.5, 0.5);
        Vector3 newPosition = transform.apply(location.toVector().subtract(pivot));
        if (hasTilePosition) {
            newPosition = newPosition.subtract(0.5, 0.5, 0.5);
        }
        Vector3 newDirection;
        newDirection = transform.isIdentity() ? entity.getLocation().getDirection() : transform.apply(location.getDirection()).subtract(transform.apply(Vector3.ZERO)).normalize();
        newLocation = new Location(destination, newPosition.add(to.round().add(0.5, 0.5, 0.5)), newDirection);
        // Some entities store their position data in NBT
        state = transformNbtData(state);
        boolean success = destination.createEntity(newLocation, state) != null;
        // Remove
        if (isRemoving() && success) {
            // FAWE start
            UUID uuid = null;
            if (tag.containsKey("UUID")) {
                int[] arr = tag.getIntArray("UUID");
                uuid = new UUID((long) arr[0] << 32 | (arr[1] & 0xFFFFFFFFL), (long) arr[2] << 32 | (arr[3] & 0xFFFFFFFFL));
            } else if (tag.containsKey("UUIDMost")) {
                uuid = new UUID(tag.getLong("UUIDMost"), tag.getLong("UUIDLeast"));
            } else if (tag.containsKey("PersistentIDMSB")) {
                uuid = new UUID(tag.getLong("PersistentIDMSB"), tag.getLong("PersistentIDLSB"));
            }
            if (uuid != null) {
                if (source != null) {
                    source.removeEntity(entity.getLocation().getBlockX(), entity.getLocation().getBlockY(), entity.getLocation().getBlockZ(), uuid);
                } else {
                    // FAWE end
                    entity.remove();
                }
            }
        }
        return success;
    } else {
        return false;
    }
}
Also used : BaseEntity(com.sk89q.worldedit.entity.BaseEntity) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Vector3(com.sk89q.worldedit.math.Vector3) UUID(java.util.UUID) CompoundTag(com.sk89q.jnbt.CompoundTag) Location(com.sk89q.worldedit.util.Location)

Example 20 with CompoundTag

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

the class FaweDelegateSchematicHandler method save.

public boolean save(CompoundTag tag, String path) {
    if (tag == null) {
        LOGGER.warn("Cannot save empty tag");
        return false;
    }
    try {
        File tmp = FileUtils.getFile(PlotSquared.platform().getDirectory(), path);
        tmp.getParentFile().mkdirs();
        if (tag instanceof CompressedCompoundTag) {
            CompressedCompoundTag cTag = (CompressedCompoundTag) tag;
            if (cTag instanceof CompressedSchematicTag) {
                Clipboard clipboard = (Clipboard) cTag.getSource();
                try (OutputStream stream = new FileOutputStream(tmp);
                    NBTOutputStream output = new NBTOutputStream(new BufferedOutputStream(new ParallelGZIPOutputStream(stream)))) {
                    new FastSchematicWriter(output).write(clipboard);
                }
            } else {
                try (OutputStream stream = new FileOutputStream(tmp);
                    BufferedOutputStream output = new BufferedOutputStream(new ParallelGZIPOutputStream(stream))) {
                    LZ4BlockInputStream is = cTag.adapt(cTag.getSource());
                    IOUtil.copy(is, output);
                }
            }
        } else {
            try (OutputStream stream = new FileOutputStream(tmp);
                NBTOutputStream output = new NBTOutputStream(new ParallelGZIPOutputStream(stream))) {
                Map<String, Tag> map = tag.getValue();
                output.writeNamedTag("Schematic", map.getOrDefault("Schematic", tag));
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
Also used : FastSchematicWriter(com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicWriter) CompressedSchematicTag(com.fastasyncworldedit.core.jnbt.CompressedSchematicTag) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) ParallelGZIPOutputStream(org.anarres.parallelgzip.ParallelGZIPOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) NBTOutputStream(com.sk89q.jnbt.NBTOutputStream) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream) CompressedCompoundTag(com.fastasyncworldedit.core.jnbt.CompressedCompoundTag) ParallelGZIPOutputStream(org.anarres.parallelgzip.ParallelGZIPOutputStream) FileOutputStream(java.io.FileOutputStream) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) CompressedSchematicTag(com.fastasyncworldedit.core.jnbt.CompressedSchematicTag) CompressedCompoundTag(com.fastasyncworldedit.core.jnbt.CompressedCompoundTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

CompoundTag (com.sk89q.jnbt.CompoundTag)69 Tag (com.sk89q.jnbt.Tag)38 StringTag (com.sk89q.jnbt.StringTag)26 HashMap (java.util.HashMap)25 IntTag (com.sk89q.jnbt.IntTag)24 ListTag (com.sk89q.jnbt.ListTag)21 ShortTag (com.sk89q.jnbt.ShortTag)16 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)16 ByteArrayTag (com.sk89q.jnbt.ByteArrayTag)14 IntArrayTag (com.sk89q.jnbt.IntArrayTag)14 IOException (java.io.IOException)14 Map (java.util.Map)13 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)11 NamedTag (com.sk89q.jnbt.NamedTag)9 Location (com.sk89q.worldedit.util.Location)9 BlockState (com.sk89q.worldedit.world.block.BlockState)9 BaseEntity (com.sk89q.worldedit.entity.BaseEntity)8 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)6 Region (com.sk89q.worldedit.regions.Region)6 ArrayList (java.util.ArrayList)6