Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.io.Closeable;
import java.io.IOException;
import java.util.List;

/**
* Represents a world snapshot.
Expand Down Expand Up @@ -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<LinCompoundTag> getEntityTags(BlockVector3 position) throws DataException, IOException {
return List.of();
}

/**
* Close this snapshot. This releases the IO handles used to load chunk information.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,6 +52,8 @@ public class SnapshotRestore {
private final Map<BlockVector2, ArrayList<BlockVector3>> neededChunks = new LinkedHashMap<>();
private final Snapshot snapshot;
private final EditSession editSession;
private final Region region;
private boolean restoreEntities;
private ArrayList<BlockVector2> missingChunks;
private ArrayList<BlockVector2> errorChunks;
private String lastErrorMessage;
Expand All @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -159,6 +161,34 @@ public LinCompoundTag getChunkTag(BlockVector3 position) throws DataException, I
}
}

@Override
public List<LinCompoundTag> getEntityTags(BlockVector3 position) throws DataException, IOException {
BlockVector2 pos = position.toBlockVector2();
Optional<Path> 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) {
Expand Down
Loading