Search in sources :

Example 1 with Chunk

use of com.sk89q.worldedit.world.chunk.Chunk 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 2 with Chunk

use of com.sk89q.worldedit.world.chunk.Chunk 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

BaseEntity (com.sk89q.worldedit.entity.BaseEntity)2 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)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 MissingChunkException (com.sk89q.worldedit.world.storage.MissingChunkException)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 LocalBlockVectorSet (com.fastasyncworldedit.core.math.LocalBlockVectorSet)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1