From 7c89ece96e3ce5f6ad574c0dd6d0af0610072f2c Mon Sep 17 00:00:00 2001 From: wizjany Date: Fri, 8 Mar 2019 16:00:42 -0500 Subject: [PATCH] Few tweaks to schematic loading and error fixes. * Not all EntityTypes in Bukkit have the correct enum name. * Don't read entire schematic files to list. Go off file extension only. (Reading in files is more accurate, but slow.) * Enforce extensions. (Due to the above, while you can technically make a schematic called 'test.txt', it's better that we save it as 'test.txt.schem'.) * Fix a few minor warnings. --- .../worldedit/bukkit/WorldEditPlugin.java | 5 ++++- .../java/com/sk89q/worldedit/WorldEdit.java | 22 ++++++++++++------- .../worldedit/command/SchematicCommands.java | 19 ++++++++++++---- .../clipboard/io/NBTSchematicReader.java | 4 +--- .../clipboard/io/SpongeSchematicReader.java | 8 +++---- 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index 640e07338..d09578472 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -160,7 +160,10 @@ public class WorldEditPlugin extends JavaPlugin implements TabCompleter { } // Entity for (org.bukkit.entity.EntityType entityType : org.bukkit.entity.EntityType.values()) { - EntityType.REGISTRY.register("minecraft:" + entityType.name().toLowerCase(), new EntityType("minecraft:" + entityType.name().toLowerCase())); + String mcid = entityType.getName(); + if (mcid != null) { + EntityType.REGISTRY.register("minecraft:" + mcid.toLowerCase(), new EntityType("minecraft:" + mcid.toLowerCase())); + } } // Tags try { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java b/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java index 3b3130180..d9a9c6ddc 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -67,6 +67,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -90,11 +91,11 @@ import javax.script.ScriptException; * method {@link WorldEdit#getInstance()}, which is shared among all * platforms within the same classloader hierarchy.

*/ -public class WorldEdit { +public final class WorldEdit { public static final Logger logger = Logger.getLogger(WorldEdit.class.getCanonicalName()); - private final static WorldEdit instance = new WorldEdit(); + private static final WorldEdit instance = new WorldEdit(); private static String version; private final EventBus eventBus = new EventBus(); @@ -274,14 +275,15 @@ public class WorldEdit { } } else { List exts = extensions == null ? ImmutableList.of(defaultExt) : Lists.asList(defaultExt, extensions); - return getSafeFileWithExtensions(dir, filename, exts, isSave); + f = getSafeFileWithExtensions(dir, filename, exts, isSave); } try { String filePath = f.getCanonicalPath(); String dirPath = dir.getCanonicalPath(); - if (!filePath.substring(0, dirPath.length()).equals(dirPath) && !getConfiguration().allowSymlinks) { + if ((filePath.length() < dirPath.length() || !filePath.substring(0, dirPath.length()).equals(dirPath)) + && !getConfiguration().allowSymlinks) { throw new FilenameResolutionException(filename, "Path is outside allowable root"); } @@ -301,7 +303,7 @@ public class WorldEdit { } } File result = null; - for (Iterator iter = exts.iterator(); iter.hasNext() && (result == null || !result.exists());) { + for (Iterator iter = exts.iterator(); iter.hasNext() && (result == null || (!isSave && !result.exists()));) { result = getSafeFileWithExtension(dir, filename, iter.next()); } if (result == null) { @@ -311,8 +313,12 @@ public class WorldEdit { } private File getSafeFileWithExtension(File dir, String filename, String extension) { - if (extension != null && filename.lastIndexOf('.') == -1) { - filename += "." + extension; + if (extension != null) { + int dot = filename.lastIndexOf('.'); + if (dot < 0 || !filename.substring(dot).equalsIgnoreCase(extension)) + { + filename += "." + extension; + } } if (!checkFilename(filename)) { @@ -618,7 +624,7 @@ public class WorldEdit { byte[] data = new byte[in.available()]; in.readFully(data); in.close(); - script = new String(data, 0, data.length, "utf-8"); + script = new String(data, 0, data.length, StandardCharsets.UTF_8); } catch (IOException e) { player.printError("Script read error: " + e.getMessage()); return; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java index bb5ecba27..cd7da8081 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java @@ -21,6 +21,8 @@ package com.sk89q.worldedit.command; import static com.google.common.base.Preconditions.checkNotNull; +import com.google.common.collect.Multimap; +import com.google.common.io.Files; import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandException; @@ -196,7 +198,7 @@ public class SchematicCommands { String filename = args.getString(0); File dir = worldEdit.getWorkingDirectoryFile(config.saveDir); - File f = worldEdit.getSafeSaveFile(player, dir, filename, "schematic", "schematic"); + File f = worldEdit.getSafeOpenFile(player, dir, filename, "schematic", ClipboardFormats.getFileExtensionArray()); if (!f.exists()) { player.printError("Schematic " + filename + " does not exist!"); @@ -209,6 +211,11 @@ public class SchematicCommands { } player.print(filename + " has been deleted."); + try { + log.info(player.getName() + " deleted " + f.getCanonicalPath()); + } catch (IOException e) { + log.info(player.getName() + " deleted " + f.getAbsolutePath()); + } } @Command( @@ -245,7 +252,8 @@ public class SchematicCommands { help = "List all schematics in the schematics directory\n" + " -d sorts by date, oldest first\n" + " -n sorts by date, newest first\n" + - " -p prints the requested page\n" + " -p prints the requested page\n" + + "Note: Format is not thoroughly verified until loading." ) @CommandPermissions("worldedit.schematic.list") public void list(Actor actor, CommandContext args, @Switch('p') @Optional("1") int page) throws WorldEditException { @@ -328,10 +336,13 @@ public class SchematicCommands { StringBuilder build = new StringBuilder(); build.append("\u00a72"); - ClipboardFormat format = ClipboardFormats.findByFile(file); + //ClipboardFormat format = ClipboardFormats.findByFile(file); + Multimap exts = ClipboardFormats.getFileExtensionMap(); + ClipboardFormat format = exts.get(Files.getFileExtension(file.getName())) + .stream().findFirst().orElse(null); boolean inRoot = file.getParentFile().getName().equals(prefix); build.append(inRoot ? file.getName() : file.getPath().split(Pattern.quote(prefix + File.separator))[1]) - .append(": ").append(format == null ? "Unknown" : format.getName()); + .append(": ").append(format == null ? "Unknown" : format.getName() + "*"); result.add(build.toString()); } return result; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/NBTSchematicReader.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/NBTSchematicReader.java index 424dffcc6..12f1f1b41 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/NBTSchematicReader.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/NBTSchematicReader.java @@ -46,9 +46,7 @@ public abstract class NBTSchematicReader implements ClipboardReader { } @Nullable - protected static T getTag(CompoundTag tag, Class expected, String key) { - Map items = tag.getValue(); - + protected static T getTag(Map items, String key, Class expected) { if (!items.containsKey(key)) { return null; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SpongeSchematicReader.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SpongeSchematicReader.java index 42dfea813..9714fc0de 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SpongeSchematicReader.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SpongeSchematicReader.java @@ -88,16 +88,16 @@ public class SpongeSchematicReader extends NBTSchematicReader { int version = requireTag(schematic, "Version", IntTag.class).getValue(); switch (version) { case 1: - return readVersion1(schematic); + return readVersion1(schematicTag); default: throw new IOException("This schematic version is currently not supported"); } } - private Clipboard readVersion1(Map schematic) throws IOException { + private Clipboard readVersion1(CompoundTag schematicTag) throws IOException { BlockVector3 origin; Region region; - + Map schematic = schematicTag.getValue(); Map metadata = requireTag(schematic, "Metadata", CompoundTag.class).getValue(); int width = requireTag(schematic, "Width", ShortTag.class).getValue(); @@ -143,7 +143,7 @@ public class SpongeSchematicReader extends NBTSchematicReader { try { state = WorldEdit.getInstance().getBlockFactory().parseFromInput(palettePart, parserContext).toImmutableState(); } catch (InputParseException e) { - throw new IOException("Invalid BlockState in schematic: " + palettePart + ". Are you missing a mod of using a schematic made in a newer version of Minecraft?"); + throw new IOException("Invalid BlockState in schematic: " + palettePart + ". Are you missing a mod or using a schematic made in a newer version of Minecraft?"); } palette.put(id, state); }