From 50fce3147fa835cdda5c677be759774ec63ea222 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Fri, 17 Aug 2018 00:05:56 +1000 Subject: [PATCH] Add BuiltInClipboardFormat --- .../clipboard/io/BuiltInClipboardFormat.java | 46 +++++++++ .../extent/clipboard/io/ClipboardFormat.java | 31 +++++- .../extent/clipboard/io/ClipboardFormats.java | 96 +++++++++++++++++++ 3 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/BuiltInClipboardFormat.java create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormats.java diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/BuiltInClipboardFormat.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/BuiltInClipboardFormat.java new file mode 100644 index 000000000..e03160c84 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/BuiltInClipboardFormat.java @@ -0,0 +1,46 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * 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 + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.extent.clipboard.io; + +/** + * A collection of supported clipboard formats. + */ +@Deprecated +public class BuiltInClipboardFormat { + public static final ClipboardFormat MCEDIT_SCHEMATIC = ClipboardFormat.SCHEMATIC; + public static final ClipboardFormat SPONGE_SCHEMATIC = ClipboardFormat.SPONGE_SCHEMATIC; + public static final ClipboardFormat STRUCTURE = ClipboardFormat.STRUCTURE; + public static final ClipboardFormat PNG = ClipboardFormat.PNG; + + @Deprecated + public static final ClipboardFormat[] values() { + return ClipboardFormat.values(); + } + + @Deprecated + public static ClipboardFormat valueOf(String value) { + switch (value) { + case "MCEDIT_SCHEMATIC": + value = "SCHEMATIC"; + break; + } + return ClipboardFormat.valueOf(value); + } +} \ No newline at end of file diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormat.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormat.java index a1f629cf5..88dd09aa5 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormat.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormat.java @@ -264,6 +264,35 @@ public enum ClipboardFormat { this.format = format; } + /** + * Returns the name of this format. + * + * @return The name of the format + */ + public String getName() { + return name(); + } + + /** + * Get the file extension this format primarily uses. + * + * @return The primary file extension + */ + public String getPrimaryFileExtension() { + return getExtension(); + } + + /** + * Get the file extensions this format is commonly known to use. This should + * include {@link #getPrimaryFileExtension()}. + * + * @return The file extensions this format might be known by + */ + public Set getFileExtensions() { + return Collections.singleton(getPrimaryFileExtension()); + } + + public URL uploadPublic(final Clipboard clipboard, String category, String user) { // summary // blocks @@ -528,7 +557,7 @@ public enum ClipboardFormat { @Nullable public static ClipboardFormat findByAlias(String alias) { checkNotNull(alias); - return aliasMap.get(alias.toLowerCase().trim()); + return aliasMap.get(alias.toLowerCase(Locale.ENGLISH).trim()); } /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormats.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormats.java new file mode 100644 index 000000000..14e7c93f9 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormats.java @@ -0,0 +1,96 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * 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 + * (at your option) any later version. + * + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.extent.clipboard.io; + +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; + +import javax.annotation.Nullable; +import java.io.File; +import java.util.*; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class ClipboardFormats { + /** + * Find the clipboard format named by the given alias. + * + * @param alias + * the alias + * @return the format, otherwise null if none is matched + */ + @Nullable + public static ClipboardFormat findByAlias(String alias) { + return ClipboardFormat.findByAlias(alias); + } + + /** + * Detect the format of given a file. + * + * @param file + * the file + * @return the format, otherwise null if one cannot be detected + */ + @Nullable + public static ClipboardFormat findByFile(File file) { + checkNotNull(file); + + for (ClipboardFormat format : ClipboardFormat.values()) { + if (format.isFormat(file)) { + return format; + } + } + + return null; + } + + /** + * @return a multimap from a file extension to the potential matching formats. + */ + public static Multimap getFileExtensionMap() { + HashMultimap map = HashMultimap.create(); + for (ClipboardFormat format : ClipboardFormat.values()) { + for (String ext : format.getFileExtensions()) { + map.put(ext, format); + } + } + return map; + } + + public static Collection getAll() { + return Arrays.asList(ClipboardFormat.values()); + } + + /** + * Not public API, only used by SchematicCommands. + * It is not in SchematicCommands because it may rely on internal register calls. + */ + public static String[] getFileExtensionArray() { + List exts = new ArrayList<>(); + HashMultimap map = HashMultimap.create(); + for (ClipboardFormat format : ClipboardFormat.values()) { + exts.addAll(format.getFileExtensions()); + } + return exts.toArray(new String[exts.size()]); + } + + private ClipboardFormats() {} +} \ No newline at end of file