diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotUtilCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotUtilCommands.java index d9b1871262..75fb6b6c0f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotUtilCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotUtilCommands.java @@ -38,6 +38,7 @@ import org.enginehub.piston.annotation.Command; import org.enginehub.piston.annotation.CommandContainer; import org.enginehub.piston.annotation.param.Arg; +import org.enginehub.piston.annotation.param.Switch; import java.io.IOException; import java.net.URI; @@ -66,7 +67,9 @@ public SnapshotUtilCommands(WorldEdit we) { @CommandPermissions("worldedit.snapshots.restore") public void restore(Actor actor, World world, LocalSession session, EditSession editSession, @Arg(name = "snapshot", desc = "The snapshot to restore", def = "") - String snapshotName) throws WorldEditException, IOException { + String snapshotName, + @Switch(name = 'e', desc = "Also restore entities") + boolean restoreEntities) throws WorldEditException, IOException { LocalConfiguration config = we.getConfiguration(); checkSnapshotsConfigured(config); @@ -125,6 +128,7 @@ public void restore(Actor actor, World world, LocalSession session, EditSession try (snapshot) { // Restore snapshot SnapshotRestore restore = new SnapshotRestore(snapshot, editSession, region); + restore.setRestoreEntities(restoreEntities); //player.print(restore.getChunksAffected() + " chunk(s) will be loaded."); restore.restore(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/Snapshot.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/Snapshot.java index b648615e34..492e6ed669 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/Snapshot.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/Snapshot.java @@ -27,6 +27,7 @@ import java.io.Closeable; import java.io.IOException; +import java.util.List; /** * Represents a world snapshot. @@ -54,6 +55,16 @@ default Chunk getChunk(BlockVector3 position) throws DataException, IOException return ChunkStoreHelper.getChunk(getChunkTag(position)); } + /** + * Get the entities stored for the chunk at the given position. + * + * @param position the position of the chunk + * @return the tags of the entities in the chunk or an empty list if there are none + */ + default List getEntityTags(BlockVector3 position) throws DataException, IOException { + return List.of(); + } + /** * Close this snapshot. This releases the IO handles used to load chunk information. */ diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/SnapshotRestore.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/SnapshotRestore.java index 6477343ccf..61bc13a456 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/SnapshotRestore.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/SnapshotRestore.java @@ -21,14 +21,22 @@ import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.MaxChangedBlocksException; +import com.sk89q.worldedit.entity.BaseEntity; import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.Region; +import com.sk89q.worldedit.util.Location; +import com.sk89q.worldedit.util.concurrency.LazyReference; import com.sk89q.worldedit.world.DataException; import com.sk89q.worldedit.world.chunk.Chunk; +import com.sk89q.worldedit.world.entity.EntityType; +import com.sk89q.worldedit.world.entity.EntityTypes; import com.sk89q.worldedit.world.storage.ChunkStore; import com.sk89q.worldedit.world.storage.MissingChunkException; +import com.sk89q.worldedit.world.storage.NBTConversions; +import org.enginehub.linbus.tree.LinCompoundTag; +import org.enginehub.linbus.tree.LinTagType; import java.io.IOException; import java.util.ArrayList; @@ -44,6 +52,8 @@ public class SnapshotRestore { private final Map> neededChunks = new LinkedHashMap<>(); private final Snapshot snapshot; private final EditSession editSession; + private final Region region; + private boolean restoreEntities; private ArrayList missingChunks; private ArrayList errorChunks; private String lastErrorMessage; @@ -58,6 +68,7 @@ public class SnapshotRestore { public SnapshotRestore(Snapshot snapshot, EditSession editSession, Region region) { this.snapshot = snapshot; this.editSession = editSession; + this.region = region; if (region instanceof CuboidRegion) { findNeededCuboidChunks(region); @@ -115,6 +126,15 @@ private void checkAndAddBlock(BlockVector3 pos) { neededChunks.get(chunkPos).add(pos); } + /** + * Set whether entities stored in the snapshot should also be restored, when supported. + * + * @param restoreEntities true to restore entities + */ + public void setRestoreEntities(boolean restoreEntities) { + this.restoreEntities = restoreEntities; + } + /** * Get the number of chunks that are needed. * @@ -152,6 +172,10 @@ public void restore() throws MaxChangedBlocksException { // this is a workaround: just ignore for now } } + + if (restoreEntities) { + restoreEntities(chunkPos); + } } catch (MissingChunkException _) { missingChunks.add(chunkPos); } catch (IOException | DataException me) { @@ -161,6 +185,26 @@ public void restore() throws MaxChangedBlocksException { } } + private void restoreEntities(BlockVector2 chunkPos) throws DataException, IOException { + for (LinCompoundTag tag : snapshot.getEntityTags(chunkPos.toBlockVector3())) { + var idTag = tag.findTag("id", LinTagType.stringTag()); + var posTag = tag.findListTag("Pos", LinTagType.doubleTag()); + var rotationTag = tag.findListTag("Rotation", LinTagType.floatTag()); + if (idTag == null || posTag == null || rotationTag == null) { + continue; + } + EntityType entityType = EntityTypes.get(idTag.value()); + if (entityType == null) { + continue; + } + Location location = NBTConversions.toLocation(editSession, posTag, rotationTag); + if (!region.contains(location.toVector().toBlockPoint())) { + continue; + } + editSession.createEntity(location, new BaseEntity(entityType, LazyReference.computed(tag))); + } + } + /** * Get a list of the missing chunks. restore() must have been called * already. diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/fs/FolderSnapshot.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/fs/FolderSnapshot.java index cf90a4c7db..90f1dc8a2b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/fs/FolderSnapshot.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/fs/FolderSnapshot.java @@ -32,12 +32,14 @@ import org.enginehub.linbus.stream.LinBinaryIO; import org.enginehub.linbus.tree.LinCompoundTag; import org.enginehub.linbus.tree.LinRootEntry; +import org.enginehub.linbus.tree.LinTagType; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Stream; @@ -159,6 +161,34 @@ public LinCompoundTag getChunkTag(BlockVector3 position) throws DataException, I } } + @Override + public List getEntityTags(BlockVector3 position) throws DataException, IOException { + BlockVector2 pos = position.toBlockVector2(); + Optional regFolder = getRegionFolder(); + if (regFolder.isEmpty()) { + // Legacy (pre-region) worlds keep entities inside the chunk + return List.of(); + } + // entities are stored in an "entities" folder next to "region" + Path entitiesFile = regFolder.get().resolveSibling("entities") + .resolve(McRegionChunkStore.getFilename(pos)); + if (!Files.exists(entitiesFile)) { + return List.of(); + } + try (InputStream stream = Files.newInputStream(entitiesFile)) { + McRegionReader regionReader = new McRegionReader(stream); + if (!regionReader.hasChunk(pos.x() & 31, pos.z() & 31)) { + // Chunks without entities are not written to the entities file + return List.of(); + } + try (var chunkStream = new DataInputStream(regionReader.getChunkInputStream(pos))) { + LinCompoundTag tag = LinBinaryIO.readUsing(chunkStream, LinRootEntry::readFrom).value(); + var entities = tag.findListTag("Entities", LinTagType.compoundTag()); + return entities == null ? List.of() : entities.value(); + } + } + } + @Override public void close() throws IOException { if (closeCallback != null) {