/src/main/java/com/sk89q/worldedit/commands/SnapshotUtilCommands.java

https://github.com/GuntherDW/worldedit-1 · Java · 160 lines · 117 code · 20 blank · 23 comment · 13 complexity · 93de6999f43d4191a7aeb35a9053cc76 MD5 · raw file

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