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.
This commit is contained in:
wizjany 2019-03-08 16:00:42 -05:00
parent eebba8e324
commit 7c89ece96e
5 changed files with 38 additions and 20 deletions

View File

@ -160,7 +160,10 @@ public class WorldEditPlugin extends JavaPlugin implements TabCompleter {
} }
// Entity // Entity
for (org.bukkit.entity.EntityType entityType : org.bukkit.entity.EntityType.values()) { 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 // Tags
try { try {

View File

@ -67,6 +67,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -90,11 +91,11 @@ import javax.script.ScriptException;
* method {@link WorldEdit#getInstance()}, which is shared among all * method {@link WorldEdit#getInstance()}, which is shared among all
* platforms within the same classloader hierarchy.</p> * platforms within the same classloader hierarchy.</p>
*/ */
public class WorldEdit { public final class WorldEdit {
public static final Logger logger = Logger.getLogger(WorldEdit.class.getCanonicalName()); 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 static String version;
private final EventBus eventBus = new EventBus(); private final EventBus eventBus = new EventBus();
@ -274,14 +275,15 @@ public class WorldEdit {
} }
} else { } else {
List<String> exts = extensions == null ? ImmutableList.of(defaultExt) : Lists.asList(defaultExt, extensions); List<String> exts = extensions == null ? ImmutableList.of(defaultExt) : Lists.asList(defaultExt, extensions);
return getSafeFileWithExtensions(dir, filename, exts, isSave); f = getSafeFileWithExtensions(dir, filename, exts, isSave);
} }
try { try {
String filePath = f.getCanonicalPath(); String filePath = f.getCanonicalPath();
String dirPath = dir.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, throw new FilenameResolutionException(filename,
"Path is outside allowable root"); "Path is outside allowable root");
} }
@ -301,7 +303,7 @@ public class WorldEdit {
} }
} }
File result = null; File result = null;
for (Iterator<String> iter = exts.iterator(); iter.hasNext() && (result == null || !result.exists());) { for (Iterator<String> iter = exts.iterator(); iter.hasNext() && (result == null || (!isSave && !result.exists()));) {
result = getSafeFileWithExtension(dir, filename, iter.next()); result = getSafeFileWithExtension(dir, filename, iter.next());
} }
if (result == null) { if (result == null) {
@ -311,9 +313,13 @@ public class WorldEdit {
} }
private File getSafeFileWithExtension(File dir, String filename, String extension) { private File getSafeFileWithExtension(File dir, String filename, String extension) {
if (extension != null && filename.lastIndexOf('.') == -1) { if (extension != null) {
int dot = filename.lastIndexOf('.');
if (dot < 0 || !filename.substring(dot).equalsIgnoreCase(extension))
{
filename += "." + extension; filename += "." + extension;
} }
}
if (!checkFilename(filename)) { if (!checkFilename(filename)) {
return null; return null;
@ -618,7 +624,7 @@ public class WorldEdit {
byte[] data = new byte[in.available()]; byte[] data = new byte[in.available()];
in.readFully(data); in.readFully(data);
in.close(); in.close();
script = new String(data, 0, data.length, "utf-8"); script = new String(data, 0, data.length, StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
player.printError("Script read error: " + e.getMessage()); player.printError("Script read error: " + e.getMessage());
return; return;

View File

@ -21,6 +21,8 @@ package com.sk89q.worldedit.command;
import static com.google.common.base.Preconditions.checkNotNull; 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.Command;
import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException; import com.sk89q.minecraft.util.commands.CommandException;
@ -196,7 +198,7 @@ public class SchematicCommands {
String filename = args.getString(0); String filename = args.getString(0);
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir); 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()) { if (!f.exists()) {
player.printError("Schematic " + filename + " does not exist!"); player.printError("Schematic " + filename + " does not exist!");
@ -209,6 +211,11 @@ public class SchematicCommands {
} }
player.print(filename + " has been deleted."); 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( @Command(
@ -245,7 +252,8 @@ public class SchematicCommands {
help = "List all schematics in the schematics directory\n" + help = "List all schematics in the schematics directory\n" +
" -d sorts by date, oldest first\n" + " -d sorts by date, oldest first\n" +
" -n sorts by date, newest first\n" + " -n sorts by date, newest first\n" +
" -p <page> prints the requested page\n" " -p <page> prints the requested page\n" +
"Note: Format is not thoroughly verified until loading."
) )
@CommandPermissions("worldedit.schematic.list") @CommandPermissions("worldedit.schematic.list")
public void list(Actor actor, CommandContext args, @Switch('p') @Optional("1") int page) throws WorldEditException { 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(); StringBuilder build = new StringBuilder();
build.append("\u00a72"); build.append("\u00a72");
ClipboardFormat format = ClipboardFormats.findByFile(file); //ClipboardFormat format = ClipboardFormats.findByFile(file);
Multimap<String, ClipboardFormat> exts = ClipboardFormats.getFileExtensionMap();
ClipboardFormat format = exts.get(Files.getFileExtension(file.getName()))
.stream().findFirst().orElse(null);
boolean inRoot = file.getParentFile().getName().equals(prefix); boolean inRoot = file.getParentFile().getName().equals(prefix);
build.append(inRoot ? file.getName() : file.getPath().split(Pattern.quote(prefix + File.separator))[1]) 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()); result.add(build.toString());
} }
return result; return result;

View File

@ -46,9 +46,7 @@ public abstract class NBTSchematicReader implements ClipboardReader {
} }
@Nullable @Nullable
protected static <T extends Tag> T getTag(CompoundTag tag, Class<T> expected, String key) { protected static <T extends Tag> T getTag(Map<String, Tag> items, String key, Class<T> expected) {
Map<String, Tag> items = tag.getValue();
if (!items.containsKey(key)) { if (!items.containsKey(key)) {
return null; return null;
} }

View File

@ -88,16 +88,16 @@ public class SpongeSchematicReader extends NBTSchematicReader {
int version = requireTag(schematic, "Version", IntTag.class).getValue(); int version = requireTag(schematic, "Version", IntTag.class).getValue();
switch (version) { switch (version) {
case 1: case 1:
return readVersion1(schematic); return readVersion1(schematicTag);
default: default:
throw new IOException("This schematic version is currently not supported"); throw new IOException("This schematic version is currently not supported");
} }
} }
private Clipboard readVersion1(Map<String, Tag> schematic) throws IOException { private Clipboard readVersion1(CompoundTag schematicTag) throws IOException {
BlockVector3 origin; BlockVector3 origin;
Region region; Region region;
Map<String, Tag> schematic = schematicTag.getValue();
Map<String, Tag> metadata = requireTag(schematic, "Metadata", CompoundTag.class).getValue(); Map<String, Tag> metadata = requireTag(schematic, "Metadata", CompoundTag.class).getValue();
int width = requireTag(schematic, "Width", ShortTag.class).getValue(); int width = requireTag(schematic, "Width", ShortTag.class).getValue();
@ -143,7 +143,7 @@ public class SpongeSchematicReader extends NBTSchematicReader {
try { try {
state = WorldEdit.getInstance().getBlockFactory().parseFromInput(palettePart, parserContext).toImmutableState(); state = WorldEdit.getInstance().getBlockFactory().parseFromInput(palettePart, parserContext).toImmutableState();
} catch (InputParseException e) { } 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); palette.put(id, state);
} }