Added a command to list schematics and the abilitiy to autodetect schematic format

This commit is contained in:
zml2008 2012-03-28 12:30:36 -07:00
parent 7812dd6a09
commit 33752eb058
5 changed files with 101 additions and 10 deletions

View File

@ -223,7 +223,7 @@ public abstract class CommandsManager<T> {
String previous = helpMessages.put(key, helpMessage);
if (previous != null && !previous.replaceAll("^/[^ ]+ ", "").equals(helpMessage.replaceAll("^/[^ ]+ ", ""))) {
helpMessages.put(key, previous+"\n\n"+helpMessage);
helpMessages.put(key, previous + "\n\n" + helpMessage);
}
}

View File

@ -47,9 +47,9 @@ public class SchematicCommands {
@Command(
aliases = { "load" },
usage = "<format> <filename>",
usage = "[format] <filename>",
desc = "Load a schematic into your clipboard",
min = 2,
min = 1,
max = 2
)
@CommandPermissions("worldedit.clipboard.load")
@ -57,15 +57,28 @@ public class SchematicCommands {
EditSession editSession) throws WorldEditException {
LocalConfiguration config = we.getConfiguration();
SchematicFormat format = SchematicFormat.getFormat(args.getString(0));
String fileName;
String formatName;
if (args.argsLength() == 1) {
formatName = null;
fileName = args.getString(0);
} else {
formatName = args.getString(0);
fileName = args.getString(1);
}
File dir = we.getWorkingDirectoryFile(config.saveDir);
File f = we.getSafeOpenFile(player, dir, fileName, "schematic", "schematic");
SchematicFormat format = formatName == null ? null : SchematicFormat.getFormat(formatName);
if (format == null) {
player.printError("Unknown schematic format: " + args.getString(0));
return;
format = SchematicFormat.getFormat(f);
}
String filename = args.getString(1);
File dir = we.getWorkingDirectoryFile(config.saveDir);
File f = we.getSafeOpenFile(player, dir, filename, "schematic", "schematic");
if (format == null) {
player.printError("Unknown schematic format: " + formatName);
return;
}
try {
String filePath = f.getCanonicalPath();
@ -76,7 +89,7 @@ public class SchematicCommands {
} else {
session.setClipboard(format.load(f));
WorldEdit.logger.info(player.getName() + " loaded " + filePath);
player.print(filename + " loaded. Paste it with //paste");
player.print(fileName + " loaded. Paste it with //paste");
}
} catch (DataException e) {
player.printError("Load error: " + e.getMessage());
@ -156,4 +169,28 @@ public class SchematicCommands {
player.print(builder.toString());
}
}
@Command(
aliases = {"list", "all"},
desc = "List available schematics",
max = 0
)
public void list(CommandContext args, LocalSession session, LocalPlayer player,
EditSession editSession) throws WorldEditException {
File dir = we.getWorkingDirectoryFile(we.getConfiguration().saveDir);
player.print("Available schematics (Filename - Format)");
StringBuilder builder;
for (File file : dir.listFiles()) {
if (!file.isFile()) {
continue;
}
SchematicFormat format = SchematicFormat.getFormat(file);
if (format == null) {
continue;
}
player.print(file.getName() + ": " + format.getName());
}
}
}

View File

@ -22,6 +22,7 @@ import com.sk89q.jnbt.ByteArrayTag;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.IntTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.NBTConstants;
import com.sk89q.jnbt.NBTInputStream;
import com.sk89q.jnbt.NBTOutputStream;
import com.sk89q.jnbt.ShortTag;
@ -34,6 +35,7 @@ import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.TileEntityBlock;
import com.sk89q.worldedit.data.DataException;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@ -244,6 +246,31 @@ public class MCEditSchematicFormat extends SchematicFormat {
stream.close();
}
@Override
public boolean isOfFormat(File file) {
DataInputStream str = null;
try {
str = new DataInputStream(new GZIPInputStream(new FileInputStream(file)));
if ((str.readByte() & 0xFF) != NBTConstants.TYPE_COMPOUND) {
return false;
}
byte[] nameBytes = new byte[str.readShort() & 0xFFFF];
str.readFully(nameBytes);
String name = new String(nameBytes, NBTConstants.CHARSET);
return name.equals("Schematic");
} catch (IOException e) {
return false;
} finally {
if (str != null) {
try {
str.close();
} catch (IOException ignore) {
// blargh
}
}
}
}
/**
* Get child tag of a NBT structure.
*

View File

@ -45,6 +45,8 @@ import java.util.Set;
*/
public abstract class SchematicFormat {
private static final Map<String, SchematicFormat> SCHEMATIC_FORMATS = new HashMap<String, SchematicFormat>();
// Built-in schematic formats
public static final SchematicFormat MCEDIT = new MCEditSchematicFormat();
public static Set<SchematicFormat> getFormats() {
@ -55,6 +57,19 @@ public abstract class SchematicFormat {
return SCHEMATIC_FORMATS.get(lookupName.toLowerCase());
}
public static SchematicFormat getFormat(File file) {
if (!file.isFile()) {
return null;
}
for (SchematicFormat format : SCHEMATIC_FORMATS.values()) {
if (format.isOfFormat(file)) {
return format;
}
}
return null;
}
private final String name;
private final String[] lookupNames;
@ -137,4 +152,6 @@ public abstract class SchematicFormat {
* @throws DataException If the clipboard has data which cannot be stored
*/
public abstract void save(CuboidClipboard clipboard, File file) throws IOException, DataException;
public abstract boolean isOfFormat(File file);
}

View File

@ -34,4 +34,14 @@ public class SpoutBiomeType extends com.sk89q.worldedit.BiomeType {
public BiomeType getSpoutBiome() {
return type;
}
@Override
public boolean equals(Object other) {
return other instanceof SpoutBiomeType && ((SpoutBiomeType)other).type.equals(this.type);
}
@Override
public int hashCode() {
return 31 + type.hashCode();
}
}