PageRenderTime 75ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Skull3x/WorldEdit
Java | 272 lines | 207 code | 41 blank | 24 comment | 34 complexity | af6c613496b4ddbaad1710f0b2e8bbdf 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. // $Id$
  20. package com.sk89q.worldedit.command;
  21. import com.sk89q.minecraft.util.commands.Command;
  22. import com.sk89q.minecraft.util.commands.CommandContext;
  23. import com.sk89q.minecraft.util.commands.CommandPermissions;
  24. import com.sk89q.worldedit.*;
  25. import com.sk89q.worldedit.entity.Player;
  26. import com.sk89q.worldedit.world.snapshot.InvalidSnapshotException;
  27. import com.sk89q.worldedit.world.snapshot.Snapshot;
  28. import com.sk89q.worldedit.world.storage.MissingWorldException;
  29. import java.io.File;
  30. import java.io.IOException;
  31. import java.text.DateFormat;
  32. import java.text.SimpleDateFormat;
  33. import java.util.Calendar;
  34. import java.util.List;
  35. import java.util.logging.Logger;
  36. /**
  37. * Snapshot commands.
  38. */
  39. public class SnapshotCommands {
  40. private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
  41. private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
  42. private final WorldEdit we;
  43. public SnapshotCommands(WorldEdit we) {
  44. this.we = we;
  45. }
  46. @Command(
  47. aliases = { "list" },
  48. usage = "[num]",
  49. desc = "List snapshots",
  50. min = 0,
  51. max = 1
  52. )
  53. @CommandPermissions("worldedit.snapshots.list")
  54. public void list(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
  55. LocalConfiguration config = we.getConfiguration();
  56. if (config.snapshotRepo == null) {
  57. player.printError("Snapshot/backup restore is not configured.");
  58. return;
  59. }
  60. try {
  61. List<Snapshot> snapshots = config.snapshotRepo.getSnapshots(true, player.getWorld().getName());
  62. if (!snapshots.isEmpty()) {
  63. int num = args.argsLength() > 0 ? Math.min(40, Math.max(5, args.getInteger(0))) : 5;
  64. player.print("Snapshots for world: '" + player.getWorld().getName() + "'");
  65. for (byte i = 0; i < Math.min(num, snapshots.size()); i++) {
  66. player.print((i + 1) + ". " + snapshots.get(i).getName());
  67. }
  68. player.print("Use /snap use [snapshot] or /snap use latest.");
  69. } else {
  70. player.printError("No snapshots are available. See console for details.");
  71. // Okay, let's toss some debugging information!
  72. File dir = config.snapshotRepo.getDirectory();
  73. try {
  74. logger.info("WorldEdit found no snapshots: looked in: "
  75. + dir.getCanonicalPath());
  76. } catch (IOException e) {
  77. logger.info("WorldEdit found no snapshots: looked in "
  78. + "(NON-RESOLVABLE PATH - does it exist?): "
  79. + dir.getPath());
  80. }
  81. }
  82. } catch (MissingWorldException ex) {
  83. player.printError("No snapshots were found for this world.");
  84. }
  85. }
  86. @Command(
  87. aliases = { "use" },
  88. usage = "<snapshot>",
  89. desc = "Choose a snapshot to use",
  90. min = 1,
  91. max = 1
  92. )
  93. @CommandPermissions("worldedit.snapshots.restore")
  94. public void use(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
  95. LocalConfiguration config = we.getConfiguration();
  96. if (config.snapshotRepo == null) {
  97. player.printError("Snapshot/backup restore is not configured.");
  98. return;
  99. }
  100. String name = args.getString(0);
  101. // Want the latest snapshot?
  102. if (name.equalsIgnoreCase("latest")) {
  103. try {
  104. Snapshot snapshot = config.snapshotRepo.getDefaultSnapshot(player.getWorld().getName());
  105. if (snapshot != null) {
  106. session.setSnapshot(null);
  107. player.print("Now using newest snapshot.");
  108. } else {
  109. player.printError("No snapshots were found.");
  110. }
  111. } catch (MissingWorldException ex) {
  112. player.printError("No snapshots were found for this world.");
  113. }
  114. } else {
  115. try {
  116. session.setSnapshot(config.snapshotRepo.getSnapshot(name));
  117. player.print("Snapshot set to: " + name);
  118. } catch (InvalidSnapshotException e) {
  119. player.printError("That snapshot does not exist or is not available.");
  120. }
  121. }
  122. }
  123. @Command(
  124. aliases = { "sel" },
  125. usage = "<index>",
  126. desc = "Choose the snapshot based on the list id",
  127. min = 1,
  128. max = 1
  129. )
  130. @CommandPermissions("worldedit.snapshots.restore")
  131. public void sel(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
  132. LocalConfiguration config = we.getConfiguration();
  133. if (config.snapshotRepo == null) {
  134. player.printError("Snapshot/backup restore is not configured.");
  135. return;
  136. }
  137. int index = -1;
  138. try {
  139. index = Integer.parseInt(args.getString(0));
  140. } catch (NumberFormatException e) {
  141. player.printError("Invalid index, " + args.getString(0) + " is not a valid integer.");
  142. return;
  143. }
  144. if (index < 1) {
  145. player.printError("Invalid index, must be equal or higher then 1.");
  146. return;
  147. }
  148. try {
  149. List<Snapshot> snapshots = config.snapshotRepo.getSnapshots(true, player.getWorld().getName());
  150. if (snapshots.size() < index) {
  151. player.printError("Invalid index, must be between 1 and " + snapshots.size() + ".");
  152. return;
  153. }
  154. Snapshot snapshot = snapshots.get(index - 1);
  155. if (snapshot == null) {
  156. player.printError("That snapshot does not exist or is not available.");
  157. return;
  158. }
  159. session.setSnapshot(snapshot);
  160. player.print("Snapshot set to: " + snapshot.getName());
  161. } catch (MissingWorldException e) {
  162. player.printError("No snapshots were found for this world.");
  163. }
  164. }
  165. @Command(
  166. aliases = { "before" },
  167. usage = "<date>",
  168. desc = "Choose the nearest snapshot before a date",
  169. min = 1,
  170. max = -1
  171. )
  172. @CommandPermissions("worldedit.snapshots.restore")
  173. public void before(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
  174. LocalConfiguration config = we.getConfiguration();
  175. if (config.snapshotRepo == null) {
  176. player.printError("Snapshot/backup restore is not configured.");
  177. return;
  178. }
  179. Calendar date = session.detectDate(args.getJoinedStrings(0));
  180. if (date == null) {
  181. player.printError("Could not detect the date inputted.");
  182. } else {
  183. try {
  184. Snapshot snapshot = config.snapshotRepo.getSnapshotBefore(date, player.getWorld().getName());
  185. if (snapshot == null) {
  186. dateFormat.setTimeZone(session.getTimeZone());
  187. player.printError("Couldn't find a snapshot before "
  188. + dateFormat.format(date.getTime()) + ".");
  189. } else {
  190. session.setSnapshot(snapshot);
  191. player.print("Snapshot set to: " + snapshot.getName());
  192. }
  193. } catch (MissingWorldException ex) {
  194. player.printError("No snapshots were found for this world.");
  195. }
  196. }
  197. }
  198. @Command(
  199. aliases = { "after" },
  200. usage = "<date>",
  201. desc = "Choose the nearest snapshot after a date",
  202. min = 1,
  203. max = -1
  204. )
  205. @CommandPermissions("worldedit.snapshots.restore")
  206. public void after(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
  207. LocalConfiguration config = we.getConfiguration();
  208. if (config.snapshotRepo == null) {
  209. player.printError("Snapshot/backup restore is not configured.");
  210. return;
  211. }
  212. Calendar date = session.detectDate(args.getJoinedStrings(0));
  213. if (date == null) {
  214. player.printError("Could not detect the date inputted.");
  215. } else {
  216. try {
  217. Snapshot snapshot = config.snapshotRepo.getSnapshotAfter(date, player.getWorld().getName());
  218. if (snapshot == null) {
  219. dateFormat.setTimeZone(session.getTimeZone());
  220. player.printError("Couldn't find a snapshot after "
  221. + dateFormat.format(date.getTime()) + ".");
  222. } else {
  223. session.setSnapshot(snapshot);
  224. player.print("Snapshot set to: " + snapshot.getName());
  225. }
  226. } catch (MissingWorldException ex) {
  227. player.printError("No snapshots were found for this world.");
  228. }
  229. }
  230. }
  231. }