2014-04-03 02:08:50 +00:00
|
|
|
/*
|
2014-04-04 22:03:18 +00:00
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
2014-04-03 02:08:50 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
2014-04-03 02:08:50 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
2014-04-03 02:08:50 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-04-03 02:08:50 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldedit.command;
|
|
|
|
|
2018-07-30 13:26:06 +00:00
|
|
|
import com.sk89q.worldedit.EditSession;
|
|
|
|
import com.sk89q.worldedit.LocalConfiguration;
|
|
|
|
import com.sk89q.worldedit.LocalSession;
|
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2019-07-06 00:46:48 +00:00
|
|
|
import com.sk89q.worldedit.command.util.CommandPermissions;
|
|
|
|
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
|
|
|
|
import com.sk89q.worldedit.command.util.Logging;
|
2019-08-06 15:29:49 +00:00
|
|
|
import com.sk89q.worldedit.extension.platform.Actor;
|
2014-04-03 02:08:50 +00:00
|
|
|
import com.sk89q.worldedit.regions.Region;
|
2019-10-13 11:47:26 +00:00
|
|
|
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
|
|
|
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
|
2014-06-28 08:01:49 +00:00
|
|
|
import com.sk89q.worldedit.world.DataException;
|
2019-08-06 15:29:49 +00:00
|
|
|
import com.sk89q.worldedit.world.World;
|
2014-04-03 02:08:50 +00:00
|
|
|
import com.sk89q.worldedit.world.snapshot.InvalidSnapshotException;
|
|
|
|
import com.sk89q.worldedit.world.snapshot.Snapshot;
|
|
|
|
import com.sk89q.worldedit.world.snapshot.SnapshotRestore;
|
2014-06-28 08:01:49 +00:00
|
|
|
import com.sk89q.worldedit.world.storage.ChunkStore;
|
|
|
|
import com.sk89q.worldedit.world.storage.MissingWorldException;
|
2019-07-06 00:46:48 +00:00
|
|
|
import org.enginehub.piston.annotation.Command;
|
|
|
|
import org.enginehub.piston.annotation.CommandContainer;
|
|
|
|
import org.enginehub.piston.annotation.param.Arg;
|
2014-06-28 08:01:49 +00:00
|
|
|
|
2019-11-20 00:11:54 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import static com.sk89q.worldedit.command.util.Logging.LogMode.REGION;
|
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class)
|
2014-04-03 02:08:50 +00:00
|
|
|
public class SnapshotUtilCommands {
|
|
|
|
|
|
|
|
private final WorldEdit we;
|
|
|
|
|
|
|
|
public SnapshotUtilCommands(WorldEdit we) {
|
|
|
|
this.we = we;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Command(
|
2019-04-24 06:47:22 +00:00
|
|
|
name = "restore",
|
|
|
|
aliases = { "/restore" },
|
|
|
|
desc = "Restore the selection from a snapshot"
|
2014-04-03 02:08:50 +00:00
|
|
|
)
|
|
|
|
@Logging(REGION)
|
|
|
|
@CommandPermissions("worldedit.snapshots.restore")
|
2019-08-06 15:29:49 +00:00
|
|
|
public void restore(Actor actor, World world, LocalSession session, EditSession editSession,
|
2019-07-06 00:46:48 +00:00
|
|
|
@Arg(name = "snapshot", desc = "The snapshot to restore", def = "")
|
2019-04-24 06:47:22 +00:00
|
|
|
String snapshotName) throws WorldEditException {
|
2014-04-03 02:08:50 +00:00
|
|
|
|
|
|
|
LocalConfiguration config = we.getConfiguration();
|
|
|
|
|
|
|
|
if (config.snapshotRepo == null) {
|
2019-10-13 11:47:26 +00:00
|
|
|
actor.printError(TranslatableComponent.of("worldedit.restore.not-configured"));
|
2014-04-03 02:08:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:29:49 +00:00
|
|
|
Region region = session.getSelection(world);
|
2014-04-03 02:08:50 +00:00
|
|
|
Snapshot snapshot;
|
|
|
|
|
2019-07-06 00:46:48 +00:00
|
|
|
if (snapshotName != null) {
|
2014-04-03 02:08:50 +00:00
|
|
|
try {
|
2019-07-06 00:46:48 +00:00
|
|
|
snapshot = config.snapshotRepo.getSnapshot(snapshotName);
|
2014-04-03 02:08:50 +00:00
|
|
|
} catch (InvalidSnapshotException e) {
|
2019-10-13 11:47:26 +00:00
|
|
|
actor.printError(TranslatableComponent.of("worldedit.restore.not-available"));
|
2014-04-03 02:08:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
snapshot = session.getSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
// No snapshot set?
|
|
|
|
if (snapshot == null) {
|
|
|
|
try {
|
2019-08-06 15:29:49 +00:00
|
|
|
snapshot = config.snapshotRepo.getDefaultSnapshot(world.getName());
|
2014-04-03 02:08:50 +00:00
|
|
|
|
|
|
|
if (snapshot == null) {
|
2019-10-13 11:47:26 +00:00
|
|
|
actor.printError(TranslatableComponent.of("worldedit.restore.none-found-console"));
|
2014-04-03 02:08:50 +00:00
|
|
|
|
|
|
|
// Okay, let's toss some debugging information!
|
|
|
|
File dir = config.snapshotRepo.getDirectory();
|
|
|
|
|
|
|
|
try {
|
2019-11-20 00:11:54 +00:00
|
|
|
WorldEdit.logger.info("WorldEdit found no snapshots: looked in: "
|
2014-04-03 02:08:50 +00:00
|
|
|
+ dir.getCanonicalPath());
|
|
|
|
} catch (IOException e) {
|
2019-11-20 00:11:54 +00:00
|
|
|
WorldEdit.logger.info("WorldEdit found no snapshots: looked in "
|
2014-04-03 02:08:50 +00:00
|
|
|
+ "(NON-RESOLVABLE PATH - does it exist?): "
|
|
|
|
+ dir.getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (MissingWorldException ex) {
|
2019-10-13 11:47:26 +00:00
|
|
|
actor.printError(TranslatableComponent.of("worldedit.restore.none-for-world"));
|
2014-04-03 02:08:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 06:47:22 +00:00
|
|
|
ChunkStore chunkStore;
|
2014-04-03 02:08:50 +00:00
|
|
|
|
|
|
|
// Load chunk store
|
|
|
|
try {
|
|
|
|
chunkStore = snapshot.getChunkStore();
|
2019-10-13 11:47:26 +00:00
|
|
|
actor.printInfo(TranslatableComponent.of("worldedit.restore.loaded", TextComponent.of(snapshot.getName())));
|
2018-06-16 06:36:55 +00:00
|
|
|
} catch (DataException | IOException e) {
|
2019-10-13 11:47:26 +00:00
|
|
|
actor.printError(TranslatableComponent.of("worldedit.restore.failed", TextComponent.of(e.getMessage())));
|
2014-04-03 02:08:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-20 00:11:54 +00:00
|
|
|
try {
|
2014-04-03 02:08:50 +00:00
|
|
|
// Restore snapshot
|
2019-07-06 00:46:48 +00:00
|
|
|
SnapshotRestore restore = new SnapshotRestore(chunkStore, editSession, region);
|
2014-04-03 02:08:50 +00:00
|
|
|
//player.print(restore.getChunksAffected() + " chunk(s) will be loaded.");
|
|
|
|
|
|
|
|
restore.restore();
|
|
|
|
|
|
|
|
if (restore.hadTotalFailure()) {
|
|
|
|
String error = restore.getLastErrorMessage();
|
2019-07-06 00:46:48 +00:00
|
|
|
if (!restore.getMissingChunks().isEmpty()) {
|
2019-10-13 11:47:26 +00:00
|
|
|
actor.printError(TranslatableComponent.of("worldedit.restore.chunk-not-present"));
|
2019-07-06 00:46:48 +00:00
|
|
|
} else if (error != null) {
|
2019-10-13 11:47:26 +00:00
|
|
|
actor.printError(TranslatableComponent.of("worldedit.restore.block-place-failed"));
|
|
|
|
actor.printError(TranslatableComponent.of("worldedit.restore.block-place-error", TextComponent.of(error)));
|
2014-04-03 02:08:50 +00:00
|
|
|
} else {
|
2019-10-13 11:47:26 +00:00
|
|
|
actor.printError(TranslatableComponent.of("worldedit.restore.chunk-load-failed"));
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-13 11:47:26 +00:00
|
|
|
actor.printInfo(TranslatableComponent.of("worldedit.restore.restored",
|
|
|
|
TextComponent.of(restore.getMissingChunks().size()),
|
|
|
|
TextComponent.of(restore.getErrorChunks().size())));
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
2019-11-20 00:11:54 +00:00
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
chunkStore.close();
|
|
|
|
} catch (IOException ignored) {
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 00:11:54 +00:00
|
|
|
}
|
|
|
|
}
|