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

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

https://gitlab.com/Skull3x/WorldEdit
Java | 353 lines | 281 code | 39 blank | 33 comment | 41 complexity | 29476bfcdc9c4d3dbf11e827819efffe 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.CommandException;
  23. import com.sk89q.minecraft.util.commands.CommandPermissions;
  24. import com.sk89q.worldedit.EditSession;
  25. import com.sk89q.worldedit.LocalConfiguration;
  26. import com.sk89q.worldedit.LocalSession;
  27. import com.sk89q.worldedit.WorldEdit;
  28. import com.sk89q.worldedit.WorldEditException;
  29. import com.sk89q.worldedit.entity.Player;
  30. import com.sk89q.worldedit.extension.platform.Actor;
  31. import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
  32. import com.sk89q.worldedit.extent.clipboard.Clipboard;
  33. import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
  34. import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
  35. import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
  36. import com.sk89q.worldedit.function.operation.Operations;
  37. import com.sk89q.worldedit.math.transform.Transform;
  38. import com.sk89q.worldedit.session.ClipboardHolder;
  39. import com.sk89q.worldedit.util.command.binding.Switch;
  40. import com.sk89q.worldedit.util.command.parametric.Optional;
  41. import com.sk89q.worldedit.util.io.Closer;
  42. import com.sk89q.worldedit.util.io.file.FilenameException;
  43. import com.sk89q.worldedit.world.registry.WorldData;
  44. import java.io.BufferedInputStream;
  45. import java.io.BufferedOutputStream;
  46. import java.io.File;
  47. import java.io.FileInputStream;
  48. import java.io.FileOutputStream;
  49. import java.io.IOException;
  50. import java.util.ArrayList;
  51. import java.util.Arrays;
  52. import java.util.Comparator;
  53. import java.util.List;
  54. import java.util.logging.Level;
  55. import java.util.logging.Logger;
  56. import java.util.regex.Pattern;
  57. import static com.google.common.base.Preconditions.checkNotNull;
  58. /**
  59. * Commands that work with schematic files.
  60. */
  61. public class SchematicCommands {
  62. /**
  63. * 9 schematics per page fits in the MC chat window.
  64. */
  65. private static final int SCHEMATICS_PER_PAGE = 9;
  66. private static final Logger log = Logger.getLogger(SchematicCommands.class.getCanonicalName());
  67. private final WorldEdit worldEdit;
  68. /**
  69. * Create a new instance.
  70. *
  71. * @param worldEdit reference to WorldEdit
  72. */
  73. public SchematicCommands(WorldEdit worldEdit) {
  74. checkNotNull(worldEdit);
  75. this.worldEdit = worldEdit;
  76. }
  77. @Command(
  78. aliases = { "load" },
  79. usage = "[<format>] <filename>",
  80. desc = "Load a schematic into your clipboard",
  81. min = 1, max = 2
  82. )
  83. @Deprecated
  84. @CommandPermissions({ "worldedit.clipboard.load", "worldedit.schematic.load" })
  85. public void load(Player player, LocalSession session, @Optional("schematic") String formatName, String filename) throws FilenameException {
  86. LocalConfiguration config = worldEdit.getConfiguration();
  87. File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
  88. File f = worldEdit.getSafeOpenFile(player, dir, filename, "schematic", "schematic");
  89. if (!f.exists()) {
  90. player.printError("Schematic " + filename + " does not exist!");
  91. return;
  92. }
  93. ClipboardFormat format = ClipboardFormat.findByAlias(formatName);
  94. if (format == null) {
  95. player.printError("Unknown schematic format: " + formatName);
  96. return;
  97. }
  98. Closer closer = Closer.create();
  99. try {
  100. FileInputStream fis = closer.register(new FileInputStream(f));
  101. BufferedInputStream bis = closer.register(new BufferedInputStream(fis));
  102. ClipboardReader reader = format.getReader(bis);
  103. WorldData worldData = player.getWorld().getWorldData();
  104. Clipboard clipboard = reader.read(player.getWorld().getWorldData());
  105. session.setClipboard(new ClipboardHolder(clipboard, worldData));
  106. log.info(player.getName() + " loaded " + f.getCanonicalPath());
  107. player.print(filename + " loaded. Paste it with //paste");
  108. } catch (IOException e) {
  109. player.printError("Schematic could not read or it does not exist: " + e.getMessage());
  110. log.log(Level.WARNING, "Failed to load a saved clipboard", e);
  111. } finally {
  112. try {
  113. closer.close();
  114. } catch (IOException ignored) {
  115. }
  116. }
  117. }
  118. @Command(
  119. aliases = { "save" },
  120. usage = "[<format>] <filename>",
  121. desc = "Save a schematic into your clipboard",
  122. min = 1, max = 2
  123. )
  124. @Deprecated
  125. @CommandPermissions({ "worldedit.clipboard.save", "worldedit.schematic.save" })
  126. public void save(Player player, LocalSession session, @Optional("schematic") String formatName, String filename) throws CommandException, WorldEditException {
  127. LocalConfiguration config = worldEdit.getConfiguration();
  128. File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
  129. File f = worldEdit.getSafeSaveFile(player, dir, filename, "schematic", "schematic");
  130. ClipboardFormat format = ClipboardFormat.findByAlias(formatName);
  131. if (format == null) {
  132. player.printError("Unknown schematic format: " + formatName);
  133. return;
  134. }
  135. ClipboardHolder holder = session.getClipboard();
  136. Clipboard clipboard = holder.getClipboard();
  137. Transform transform = holder.getTransform();
  138. Clipboard target;
  139. // If we have a transform, bake it into the copy
  140. if (!transform.isIdentity()) {
  141. FlattenedClipboardTransform result = FlattenedClipboardTransform.transform(clipboard, transform, holder.getWorldData());
  142. target = new BlockArrayClipboard(result.getTransformedRegion());
  143. target.setOrigin(clipboard.getOrigin());
  144. Operations.completeLegacy(result.copyTo(target));
  145. } else {
  146. target = clipboard;
  147. }
  148. Closer closer = Closer.create();
  149. try {
  150. // Create parent directories
  151. File parent = f.getParentFile();
  152. if (parent != null && !parent.exists()) {
  153. if (!parent.mkdirs()) {
  154. throw new CommandException("Could not create folder for schematics!");
  155. }
  156. }
  157. FileOutputStream fos = closer.register(new FileOutputStream(f));
  158. BufferedOutputStream bos = closer.register(new BufferedOutputStream(fos));
  159. ClipboardWriter writer = closer.register(format.getWriter(bos));
  160. writer.write(target, holder.getWorldData());
  161. log.info(player.getName() + " saved " + f.getCanonicalPath());
  162. player.print(filename + " saved.");
  163. } catch (IOException e) {
  164. player.printError("Schematic could not written: " + e.getMessage());
  165. log.log(Level.WARNING, "Failed to write a saved clipboard", e);
  166. } finally {
  167. try {
  168. closer.close();
  169. } catch (IOException ignored) {
  170. }
  171. }
  172. }
  173. @Command(
  174. aliases = { "delete", "d" },
  175. usage = "<filename>",
  176. desc = "Delete a saved schematic",
  177. help = "Delete a schematic from the schematic list",
  178. min = 1,
  179. max = 1
  180. )
  181. @CommandPermissions("worldedit.schematic.delete")
  182. public void delete(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
  183. LocalConfiguration config = worldEdit.getConfiguration();
  184. String filename = args.getString(0);
  185. File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);
  186. File f = worldEdit.getSafeSaveFile(player, dir, filename, "schematic", "schematic");
  187. if (!f.exists()) {
  188. player.printError("Schematic " + filename + " does not exist!");
  189. return;
  190. }
  191. if (!f.delete()) {
  192. player.printError("Deletion of " + filename + " failed! Maybe it is read-only.");
  193. return;
  194. }
  195. player.print(filename + " has been deleted.");
  196. }
  197. @Command(
  198. aliases = {"formats", "listformats", "f"},
  199. desc = "List available formats",
  200. max = 0
  201. )
  202. @CommandPermissions("worldedit.schematic.formats")
  203. public void formats(Actor actor) throws WorldEditException {
  204. actor.print("Available clipboard formats (Name: Lookup names)");
  205. StringBuilder builder;
  206. boolean first = true;
  207. for (ClipboardFormat format : ClipboardFormat.values()) {
  208. builder = new StringBuilder();
  209. builder.append(format.name()).append(": ");
  210. for (String lookupName : format.getAliases()) {
  211. if (!first) {
  212. builder.append(", ");
  213. }
  214. builder.append(lookupName);
  215. first = false;
  216. }
  217. first = true;
  218. actor.print(builder.toString());
  219. }
  220. }
  221. @Command(
  222. aliases = {"list", "all", "ls"},
  223. desc = "List saved schematics",
  224. min = 0,
  225. max = 1,
  226. flags = "dnp",
  227. help = "List all schematics in the schematics directory\n" +
  228. " -d sorts by date, oldest first\n" +
  229. " -n sorts by date, newest first\n" +
  230. " -p <page> prints the requested page\n"
  231. )
  232. @CommandPermissions("worldedit.schematic.list")
  233. public void list(Actor actor, CommandContext args, @Switch('p') @Optional("1") int page) throws WorldEditException {
  234. File dir = worldEdit.getWorkingDirectoryFile(worldEdit.getConfiguration().saveDir);
  235. List<File> fileList = allFiles(dir);
  236. if (fileList.isEmpty()) {
  237. actor.printError("No schematics found.");
  238. return;
  239. }
  240. File[] files = new File[fileList.size()];
  241. fileList.toArray(files);
  242. int pageCount = files.length / SCHEMATICS_PER_PAGE + 1;
  243. if (page < 1) {
  244. actor.printError("Page must be at least 1");
  245. return;
  246. }
  247. if (page > pageCount) {
  248. actor.printError("Page must be less than " + (pageCount + 1));
  249. return;
  250. }
  251. final int sortType = args.hasFlag('d') ? -1 : args.hasFlag('n') ? 1 : 0;
  252. // cleanup file list
  253. Arrays.sort(files, new Comparator<File>(){
  254. @Override
  255. public int compare(File f1, File f2) {
  256. // http://stackoverflow.com/questions/203030/best-way-to-list-files-in-java-sorted-by-date-modified
  257. int res;
  258. if (sortType == 0) { // use name by default
  259. int p = f1.getParent().compareTo(f2.getParent());
  260. if (p == 0) { // same parent, compare names
  261. res = f1.getName().compareTo(f2.getName());
  262. } else { // different parent, sort by that
  263. res = p;
  264. }
  265. } else {
  266. res = Long.valueOf(f1.lastModified()).compareTo(f2.lastModified()); // use date if there is a flag
  267. if (sortType == 1) res = -res; // flip date for newest first instead of oldest first
  268. }
  269. return res;
  270. }
  271. });
  272. List<String> schematics = listFiles(worldEdit.getConfiguration().saveDir, files);
  273. int offset = (page - 1) * SCHEMATICS_PER_PAGE;
  274. actor.print("Available schematics (Filename: Format) [" + page + "/" + pageCount + "]:");
  275. StringBuilder build = new StringBuilder();
  276. int limit = Math.min(offset + SCHEMATICS_PER_PAGE, schematics.size());
  277. for (int i = offset; i < limit;) {
  278. build.append(schematics.get(i));
  279. if (++i != limit) {
  280. build.append("\n");
  281. }
  282. }
  283. actor.print(build.toString());
  284. }
  285. private List<File> allFiles(File root) {
  286. File[] files = root.listFiles();
  287. if (files == null) return null;
  288. List<File> fileList = new ArrayList<File>();
  289. for (File f : files) {
  290. if (f.isDirectory()) {
  291. List<File> subFiles = allFiles(f);
  292. if (subFiles == null) continue; // empty subdir
  293. fileList.addAll(subFiles);
  294. } else {
  295. fileList.add(f);
  296. }
  297. }
  298. return fileList;
  299. }
  300. private List<String> listFiles(String prefix, File[] files) {
  301. if (prefix == null) prefix = "";
  302. List<String> result = new ArrayList<String>();
  303. for (File file : files) {
  304. StringBuilder build = new StringBuilder();
  305. build.append("\u00a72");
  306. ClipboardFormat format = ClipboardFormat.findByFile(file);
  307. boolean inRoot = file.getParentFile().getName().equals(prefix);
  308. build.append(inRoot ? file.getName() : file.getPath().split(Pattern.quote(prefix + File.separator))[1])
  309. .append(": ").append(format == null ? "Unknown" : format.name());
  310. result.add(build.toString());
  311. }
  312. return result;
  313. }
  314. }