Search in sources :

Example 1 with MissingChunkException

use of com.sk89q.worldedit.world.storage.MissingChunkException in project FastAsyncWorldEdit by IntellectualSites.

the class FolderSnapshot method getChunkTag.

@Override
public CompoundTag getChunkTag(BlockVector3 position) throws DataException, IOException {
    BlockVector2 pos = position.toBlockVector2();
    Optional<Path> regFolder = getRegionFolder();
    if (!regFolder.isPresent()) {
        Path chunkFile = getFolder().resolve(LegacyChunkStore.getFilename(pos, "/"));
        if (!Files.exists(chunkFile)) {
            throw new MissingChunkException();
        }
        return ChunkStoreHelper.readCompoundTag(() -> new GZIPInputStream(Files.newInputStream(chunkFile)));
    }
    Path regionFile = regFolder.get().resolve(McRegionChunkStore.getFilename(pos));
    if (!Files.exists(regionFile)) {
        // Try mcr as well
        regionFile = regionFile.resolveSibling(regionFile.getFileName().toString().replace(".mca", ".mcr"));
        if (!Files.exists(regionFile)) {
            throw new MissingChunkException();
        }
    }
    try (InputStream stream = Files.newInputStream(regionFile)) {
        McRegionReader regionReader = new McRegionReader(stream);
        return ChunkStoreHelper.readCompoundTag(() -> regionReader.getChunkInputStream(pos));
    }
}
Also used : Path(java.nio.file.Path) GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) McRegionReader(com.sk89q.worldedit.world.storage.McRegionReader) BlockVector2(com.sk89q.worldedit.math.BlockVector2) MissingChunkException(com.sk89q.worldedit.world.storage.MissingChunkException)

Example 2 with MissingChunkException

use of com.sk89q.worldedit.world.storage.MissingChunkException in project FastAsyncWorldEdit by IntellectualSites.

the class SnapshotRestore method restore.

/**
 * Restores to world.
 *
 * @throws MaxChangedBlocksException if the max block change limit is exceeded
 */
public void restore() throws MaxChangedBlocksException {
    missingChunks = new ArrayList<>();
    errorChunks = new ArrayList<>();
    // Now let's start restoring!
    for (Map.Entry<BlockVector2, ArrayList<BlockVector3>> entry : neededChunks.entrySet()) {
        BlockVector2 chunkPos = entry.getKey();
        Chunk chunk;
        try {
            // This will need to be changed if we start officially supporting 3d snapshots.
            chunk = snapshot.getChunk(chunkPos.toBlockVector3());
            // Now just copy blocks!
            for (BlockVector3 pos : entry.getValue()) {
                try {
                    editSession.setBlock(pos, chunk.getBlock(pos));
                    // FAWE start - biome and entity restore
                    if (restoreBiomes && (pos.getX() & 3) == 0 && (pos.getY() & 3) == 0 && (pos.getZ() & 3) == 0) {
                        editSession.setBiome(pos, chunk.getBiome(pos));
                    }
                // FAWE end
                } catch (DataException e) {
                // this is a workaround: just ignore for now
                }
            }
            // FAWE start - biome and entity restore
            if (restoreEntities) {
                try {
                    for (BaseEntity entity : chunk.getEntities()) {
                        CompoundBinaryTag tag = entity.getNbtReference().getValue();
                        ListBinaryTag pos = tag.getList("Pos", BinaryTagTypes.LIST);
                        ListBinaryTag rotation = tag.getList("Rotation", BinaryTagTypes.LIST);
                        double x = pos.getDouble(0);
                        double y = pos.getDouble(1);
                        double z = pos.getDouble(2);
                        float yRot = rotation.getFloat(0);
                        float xRot = rotation.getFloat(1);
                        Location location = new Location(editSession.getWorld(), x, y, z, yRot, xRot);
                        editSession.createEntity(location, entity);
                    }
                } catch (DataException e) {
                // this is a workaround: just ignore for now
                }
            }
        // FAWE end
        } catch (MissingChunkException me) {
            missingChunks.add(chunkPos);
        } catch (IOException | DataException me) {
            errorChunks.add(chunkPos);
            lastErrorMessage = me.getMessage();
        }
    }
}
Also used : ListBinaryTag(com.sk89q.worldedit.util.nbt.ListBinaryTag) ArrayList(java.util.ArrayList) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) IOException(java.io.IOException) Chunk(com.sk89q.worldedit.world.chunk.Chunk) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BlockVector2(com.sk89q.worldedit.math.BlockVector2) MissingChunkException(com.sk89q.worldedit.world.storage.MissingChunkException) DataException(com.sk89q.worldedit.world.DataException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag) Location(com.sk89q.worldedit.util.Location)

Example 3 with MissingChunkException

use of com.sk89q.worldedit.world.storage.MissingChunkException in project FastAsyncWorldEdit by IntellectualSites.

the class SnapshotRestore method restore.

/**
 * Restores to world.
 *
 * @throws MaxChangedBlocksException if the max block change limit is exceeded
 */
public void restore() throws MaxChangedBlocksException {
    missingChunks = new ArrayList<>();
    errorChunks = new ArrayList<>();
    // Now let's start restoring!
    for (Map.Entry<BlockVector2, Set<BlockVector3>> entry : neededChunks.entrySet()) {
        BlockVector2 chunkPos = entry.getKey();
        Chunk chunk;
        try {
            chunk = chunkStore.getChunk(chunkPos, editSession.getWorld());
            // Now just copy blocks!
            for (BlockVector3 pos : entry.getValue()) {
                try {
                    editSession.setBlock(pos, chunk.getBlock(pos));
                    // FAWE start - biome and entity restore
                    if (restoreBiomes && (pos.getX() & 3) == 0 && (pos.getY() & 3) == 0 && (pos.getZ() & 3) == 0) {
                        editSession.setBiome(pos, chunk.getBiome(pos));
                    }
                // FAWE end
                } catch (DataException e) {
                // this is a workaround: just ignore for now
                }
            }
            // FAWE start - biome and entity restore
            if (restoreEntities) {
                try {
                    for (BaseEntity entity : chunk.getEntities()) {
                        CompoundBinaryTag tag = entity.getNbtReference().getValue();
                        ListBinaryTag pos = tag.getList("Pos");
                        ListBinaryTag rotation = tag.getList("Rotation");
                        double x = pos.getDouble(0);
                        double y = pos.getDouble(1);
                        double z = pos.getDouble(2);
                        float yRot = rotation.getFloat(0);
                        float xRot = rotation.getFloat(1);
                        Location location = new Location(editSession.getWorld(), x, y, z, yRot, xRot);
                        editSession.createEntity(location, entity);
                    }
                } catch (DataException e) {
                // this is a workaround: just ignore for now
                }
            }
        // FAWE end
        } catch (MissingChunkException me) {
            missingChunks.add(chunkPos);
        } catch (IOException | DataException me) {
            errorChunks.add(chunkPos);
            lastErrorMessage = me.getMessage();
        }
    }
}
Also used : Set(java.util.Set) LocalBlockVectorSet(com.fastasyncworldedit.core.math.LocalBlockVectorSet) ListBinaryTag(com.sk89q.worldedit.util.nbt.ListBinaryTag) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) IOException(java.io.IOException) Chunk(com.sk89q.worldedit.world.chunk.Chunk) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BlockVector2(com.sk89q.worldedit.math.BlockVector2) MissingChunkException(com.sk89q.worldedit.world.storage.MissingChunkException) DataException(com.sk89q.worldedit.world.DataException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag) Location(com.sk89q.worldedit.util.Location)

Aggregations

BlockVector2 (com.sk89q.worldedit.math.BlockVector2)3 MissingChunkException (com.sk89q.worldedit.world.storage.MissingChunkException)3 BaseEntity (com.sk89q.worldedit.entity.BaseEntity)2 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)2 Location (com.sk89q.worldedit.util.Location)2 CompoundBinaryTag (com.sk89q.worldedit.util.nbt.CompoundBinaryTag)2 ListBinaryTag (com.sk89q.worldedit.util.nbt.ListBinaryTag)2 DataException (com.sk89q.worldedit.world.DataException)2 Chunk (com.sk89q.worldedit.world.chunk.Chunk)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 LocalBlockVectorSet (com.fastasyncworldedit.core.math.LocalBlockVectorSet)1 McRegionReader (com.sk89q.worldedit.world.storage.McRegionReader)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 GZIPInputStream (java.util.zip.GZIPInputStream)1