PageRenderTime 222ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotUtilCommands.java

https://gitlab.com/Skull3x/WorldEdit
Java | 154 lines | 108 code | 23 blank | 23 comment | 13 complexity | 8e61965b5baae05b8c012197cd9ad15b MD5 | raw file
  1. /*
  2. * WorldEdit, a Minecraft world manipulation toolkit
  3. * Copyright (C) sk89q <http://www.sk89q.com>
  4. * Copyright (C) WorldEdit team and contributors
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldedit.command;
  20. import com.sk89q.minecraft.util.commands.Command;
  21. import com.sk89q.minecraft.util.commands.CommandContext;
  22. import com.sk89q.minecraft.util.commands.CommandPermissions;
  23. import com.sk89q.minecraft.util.commands.Logging;
  24. import com.sk89q.worldedit.*;
  25. import com.sk89q.worldedit.entity.Player;
  26. import com.sk89q.worldedit.regions.Region;
  27. import com.sk89q.worldedit.world.DataException;
  28. import com.sk89q.worldedit.world.snapshot.InvalidSnapshotException;
  29. import com.sk89q.worldedit.world.snapshot.Snapshot;
  30. import com.sk89q.worldedit.world.snapshot.SnapshotRestore;
  31. import com.sk89q.worldedit.world.storage.ChunkStore;
  32. import com.sk89q.worldedit.world.storage.MissingWorldException;
  33. import java.io.File;
  34. import java.io.IOException;
  35. import java.util.logging.Logger;
  36. import static com.sk89q.minecraft.util.commands.Logging.LogMode.REGION;
  37. public class SnapshotUtilCommands {
  38. private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
  39. private final WorldEdit we;
  40. public SnapshotUtilCommands(WorldEdit we) {
  41. this.we = we;
  42. }
  43. @Command(
  44. aliases = { "restore", "/restore" },
  45. usage = "[snapshot]",
  46. desc = "Restore the selection from a snapshot",
  47. min = 0,
  48. max = 1
  49. )
  50. @Logging(REGION)
  51. @CommandPermissions("worldedit.snapshots.restore")
  52. public void restore(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
  53. LocalConfiguration config = we.getConfiguration();
  54. if (config.snapshotRepo == null) {
  55. player.printError("Snapshot/backup restore is not configured.");
  56. return;
  57. }
  58. Region region = session.getSelection(player.getWorld());
  59. Snapshot snapshot;
  60. if (args.argsLength() > 0) {
  61. try {
  62. snapshot = config.snapshotRepo.getSnapshot(args.getString(0));
  63. } catch (InvalidSnapshotException e) {
  64. player.printError("That snapshot does not exist or is not available.");
  65. return;
  66. }
  67. } else {
  68. snapshot = session.getSnapshot();
  69. }
  70. // No snapshot set?
  71. if (snapshot == null) {
  72. try {
  73. snapshot = config.snapshotRepo.getDefaultSnapshot(player.getWorld().getName());
  74. if (snapshot == null) {
  75. player.printError("No snapshots were found. See console for details.");
  76. // Okay, let's toss some debugging information!
  77. File dir = config.snapshotRepo.getDirectory();
  78. try {
  79. logger.info("WorldEdit found no snapshots: looked in: "
  80. + dir.getCanonicalPath());
  81. } catch (IOException e) {
  82. logger.info("WorldEdit found no snapshots: looked in "
  83. + "(NON-RESOLVABLE PATH - does it exist?): "
  84. + dir.getPath());
  85. }
  86. return;
  87. }
  88. } catch (MissingWorldException ex) {
  89. player.printError("No snapshots were found for this world.");
  90. return;
  91. }
  92. }
  93. ChunkStore chunkStore = null;
  94. // Load chunk store
  95. try {
  96. chunkStore = snapshot.getChunkStore();
  97. player.print("Snapshot '" + snapshot.getName() + "' loaded; now restoring...");
  98. } catch (DataException e) {
  99. player.printError("Failed to load snapshot: " + e.getMessage());
  100. return;
  101. } catch (IOException e) {
  102. player.printError("Failed to load snapshot: " + e.getMessage());
  103. return;
  104. }
  105. try {
  106. // Restore snapshot
  107. SnapshotRestore restore = new SnapshotRestore(chunkStore, editSession, region);
  108. //player.print(restore.getChunksAffected() + " chunk(s) will be loaded.");
  109. restore.restore();
  110. if (restore.hadTotalFailure()) {
  111. String error = restore.getLastErrorMessage();
  112. if (error != null) {
  113. player.printError("Errors prevented any blocks from being restored.");
  114. player.printError("Last error: " + error);
  115. } else {
  116. player.printError("No chunks could be loaded. (Bad archive?)");
  117. }
  118. } else {
  119. player.print(String.format("Restored; %d "
  120. + "missing chunks and %d other errors.",
  121. restore.getMissingChunks().size(),
  122. restore.getErrorChunks().size()));
  123. }
  124. } finally {
  125. try {
  126. chunkStore.close();
  127. } catch (IOException ignored) {
  128. }
  129. }
  130. }
  131. }