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;
|
2019-08-06 15:29:49 +00:00
|
|
|
import com.sk89q.worldedit.world.World;
|
2019-12-16 11:00:12 +00:00
|
|
|
import com.sk89q.worldedit.world.snapshot.experimental.Snapshot;
|
|
|
|
import com.sk89q.worldedit.world.snapshot.experimental.SnapshotRestore;
|
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.IOException;
|
2019-12-16 11:00:12 +00:00
|
|
|
import java.net.URI;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.stream.Stream;
|
2019-11-20 00:11:54 +00:00
|
|
|
|
2019-12-16 11:00:12 +00:00
|
|
|
import static com.sk89q.worldedit.command.SnapshotCommands.checkSnapshotsConfigured;
|
|
|
|
import static com.sk89q.worldedit.command.SnapshotCommands.resolveSnapshotName;
|
2019-11-20 00:11:54 +00:00
|
|
|
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;
|
2019-12-16 11:00:12 +00:00
|
|
|
private final LegacySnapshotUtilCommands legacy;
|
2014-04-03 02:08:50 +00:00
|
|
|
|
|
|
|
public SnapshotUtilCommands(WorldEdit we) {
|
|
|
|
this.we = we;
|
2019-12-16 11:00:12 +00:00
|
|
|
this.legacy = new LegacySnapshotUtilCommands(we);
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-12-16 11:00:12 +00:00
|
|
|
String snapshotName) throws WorldEditException, IOException {
|
2014-04-03 02:08:50 +00:00
|
|
|
LocalConfiguration config = we.getConfiguration();
|
2019-12-16 11:00:12 +00:00
|
|
|
checkSnapshotsConfigured(config);
|
2014-04-03 02:08:50 +00:00
|
|
|
|
2019-12-16 11:00:12 +00:00
|
|
|
if (config.snapshotRepo != null) {
|
|
|
|
legacy.restore(actor, world, session, editSession, snapshotName);
|
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) {
|
2019-12-16 11:00:12 +00:00
|
|
|
URI uri = resolveSnapshotName(config, snapshotName);
|
|
|
|
Optional<Snapshot> snapOpt = config.snapshotDatabase.getSnapshot(uri);
|
|
|
|
if (!snapOpt.isPresent()) {
|
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;
|
|
|
|
}
|
2019-12-16 11:00:12 +00:00
|
|
|
snapshot = snapOpt.get();
|
2014-04-03 02:08:50 +00:00
|
|
|
} else {
|
2019-12-16 11:00:12 +00:00
|
|
|
snapshot = session.getSnapshotExperimental();
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No snapshot set?
|
|
|
|
if (snapshot == null) {
|
2019-12-16 11:00:12 +00:00
|
|
|
try (Stream<Snapshot> snapshotStream =
|
|
|
|
config.snapshotDatabase.getSnapshotsNewestFirst(world.getName())) {
|
|
|
|
snapshot = snapshotStream
|
|
|
|
.findFirst().orElse(null);
|
|
|
|
}
|
2014-04-03 02:08:50 +00:00
|
|
|
|
2019-12-16 11:00:12 +00:00
|
|
|
if (snapshot == null) {
|
|
|
|
actor.printError(TranslatableComponent.of(
|
|
|
|
"worldedit.restore.none-for-specific-world",
|
|
|
|
TextComponent.of(world.getName())
|
|
|
|
));
|
2014-04-03 02:08:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 11:00:12 +00:00
|
|
|
actor.printInfo(TranslatableComponent.of(
|
|
|
|
"worldedit.restore.loaded",
|
|
|
|
TextComponent.of(snapshot.getInfo().getDisplayName())
|
|
|
|
));
|
2014-04-03 02:08:50 +00:00
|
|
|
|
2019-11-20 00:11:54 +00:00
|
|
|
try {
|
2014-04-03 02:08:50 +00:00
|
|
|
// Restore snapshot
|
2019-12-16 11:00:12 +00:00
|
|
|
SnapshotRestore restore = new SnapshotRestore(snapshot, 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",
|
2019-12-16 11:00:12 +00:00
|
|
|
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 {
|
2019-12-16 11:00:12 +00:00
|
|
|
snapshot.close();
|
2019-11-20 00:11:54 +00:00
|
|
|
} catch (IOException ignored) {
|
2014-04-03 02:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 00:11:54 +00:00
|
|
|
}
|
|
|
|
}
|