Added LongArrayTag support to NBT

This commit is contained in:
Matthew Miller 2018-08-05 13:36:53 +10:00
parent 9494c4445a
commit 334143357a
12 changed files with 135 additions and 7 deletions

View File

@ -317,6 +317,24 @@ public final class CompoundTag extends Tag {
}
}
/**
* Get a {@code long[]} named with the given key.
*
* <p>If the key does not exist or its value is not an long array tag,
* then an empty array will be returned.</p>
*
* @param key the key
* @return an int array
*/
public long[] getLongArray(String key) {
Tag tag = value.get(key);
if (tag instanceof LongArrayTag) {
return ((LongArrayTag) tag).getValue();
} else {
return new long[0];
}
}
/**
* Get a long named with the given key.
*

View File

@ -133,6 +133,18 @@ public class CompoundTagBuilder {
return put(key, new IntTag(value));
}
/**
* Put the given key and value into the compound tag as a
* {@code LongArrayTag}.
*
* @param key they key
* @param value the value
* @return this object
*/
public CompoundTagBuilder putLongArray(String key, long[] value) {
return put(key, new LongArrayTag(value));
}
/**
* Put the given key and value into the compound tag as a
* {@code LongTag}.

View File

@ -0,0 +1,60 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.jnbt;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* The {@code TAG_Long_Array} tag.
*/
public class LongArrayTag extends Tag {
private final long[] value;
/**
* Creates the tag with an empty name.
*
* @param value the value of the tag
*/
public LongArrayTag(long[] value) {
super();
checkNotNull(value);
this.value = value;
}
@Override
public long[] getValue() {
return value;
}
@Override
public String toString() {
StringBuilder hex = new StringBuilder();
for (long b : value) {
String hexDigits = Long.toHexString(b).toUpperCase();
if (hexDigits.length() == 1) {
hex.append("0");
}
hex.append(hexDigits).append(" ");
}
return "TAG_Long_Array(" + hex + ")";
}
}

View File

@ -31,7 +31,7 @@ public final class NBTConstants {
public static final int TYPE_END = 0, TYPE_BYTE = 1, TYPE_SHORT = 2,
TYPE_INT = 3, TYPE_LONG = 4, TYPE_FLOAT = 5, TYPE_DOUBLE = 6,
TYPE_BYTE_ARRAY = 7, TYPE_STRING = 8, TYPE_LIST = 9,
TYPE_COMPOUND = 10, TYPE_INT_ARRAY = 11;
TYPE_COMPOUND = 10, TYPE_INT_ARRAY = 11, TYPE_LONG_ARRAY = 12;
/**
* Default private constructor.
@ -73,6 +73,8 @@ public final class NBTConstants {
return CompoundTag.class;
case TYPE_INT_ARRAY:
return IntArrayTag.class;
case TYPE_LONG_ARRAY:
return LongArrayTag.class;
default:
throw new IllegalArgumentException("Unknown tag type ID of " + id);
}

View File

@ -158,6 +158,13 @@ public final class NBTInputStream implements Closeable {
data[i] = is.readInt();
}
return new IntArrayTag(data);
case NBTConstants.TYPE_LONG_ARRAY:
length = is.readInt();
long[] longData = new long[length];
for (int i = 0; i < length; i++) {
longData[i] = is.readLong();
}
return new LongArrayTag(longData);
default:
throw new IOException("Invalid tag type: " + type + ".");
}

View File

@ -129,6 +129,9 @@ public final class NBTOutputStream implements Closeable {
case NBTConstants.TYPE_INT_ARRAY:
writeIntArrayTagPayload((IntArrayTag) tag);
break;
case NBTConstants.TYPE_LONG_ARRAY:
writeLongArrayTagPayload((LongArrayTag) tag);
break;
default:
throw new IOException("Invalid tag type: " + type + ".");
}
@ -286,6 +289,14 @@ public final class NBTOutputStream implements Closeable {
}
}
private void writeLongArrayTagPayload(LongArrayTag tag) throws IOException {
long[] data = tag.getValue();
os.writeInt(data.length);
for (long aData : data) {
os.writeLong(aData);
}
}
@Override
public void close() throws IOException {
os.close();

View File

@ -69,6 +69,8 @@ public final class NBTUtils {
return "TAG_String";
} else if (clazz.equals(IntArrayTag.class)) {
return "TAG_Int_Array";
} else if (clazz.equals(LongArrayTag.class)) {
return "TAG_Long_Array";
} else {
throw new IllegalArgumentException("Invalid tag classs ("
+ clazz.getName() + ").");
@ -107,6 +109,8 @@ public final class NBTUtils {
return NBTConstants.TYPE_STRING;
} else if (clazz.equals(IntArrayTag.class)) {
return NBTConstants.TYPE_INT_ARRAY;
} else if (clazz.equals(LongArrayTag.class)) {
return NBTConstants.TYPE_LONG_ARRAY;
} else {
throw new IllegalArgumentException("Invalid tag classs ("
+ clazz.getName() + ").");
@ -146,6 +150,8 @@ public final class NBTUtils {
return CompoundTag.class;
case NBTConstants.TYPE_INT_ARRAY:
return IntArrayTag.class;
case NBTConstants.TYPE_LONG_ARRAY:
return LongArrayTag.class;
default:
throw new IllegalArgumentException("Invalid tag type : " + type
+ ".");

View File

@ -89,7 +89,7 @@ public class SchematicCommands {
)
@Deprecated
@CommandPermissions({ "worldedit.clipboard.load", "worldedit.schematic.load" })
public void load(Player player, LocalSession session, @Optional("schematic") String formatName, String filename) throws FilenameException {
public void load(Player player, LocalSession session, @Optional("sponge") String formatName, String filename) throws FilenameException {
LocalConfiguration config = worldEdit.getConfiguration();
File dir = worldEdit.getWorkingDirectoryFile(config.saveDir);

View File

@ -76,7 +76,7 @@ public enum BuiltInClipboardFormat implements ClipboardFormat {
if (!schematic.containsKey("Materials")) {
return false;
}
} catch (IOException e) {
} catch (Exception e) {
return false;
}
return true;
@ -115,7 +115,7 @@ public enum BuiltInClipboardFormat implements ClipboardFormat {
if (!schematic.containsKey("Version")) {
return false;
}
} catch (IOException e) {
} catch (Exception e) {
return false;
}

View File

@ -202,6 +202,12 @@ public class SpongeSchematicReader extends NBTSchematicReader {
handler.updateNBT(state, values);
}
}
values.put("x", new IntTag(pt.getBlockX()));
values.put("y", new IntTag(pt.getBlockY()));
values.put("z", new IntTag(pt.getBlockZ()));
values.put("id", values.get("Id"));
values.remove("Id");
values.remove("Pos");
clipboard.setBlock(pt, new BaseBlock(state, new CompoundTag(values)));
} else {
clipboard.setBlock(pt, state);

View File

@ -117,7 +117,7 @@ public class SpongeSchematicWriter implements ClipboardWriter {
int paletteMax = 0;
Map<String, Integer> palette = new HashMap<>();
List<Tag> tileEntities = new ArrayList<>();
List<CompoundTag> tileEntities = new ArrayList<>();
ByteArrayOutputStream buffer = new ByteArrayOutputStream(width * height * length);
@ -135,6 +135,13 @@ public class SpongeSchematicWriter implements ClipboardWriter {
values.put(entry.getKey(), entry.getValue());
}
values.remove("id"); // Remove 'id' if it exists. We want 'Id'
// Positions are kept in NBT, we don't want that.
values.remove("x");
values.remove("y");
values.remove("z");
values.put("Id", new StringTag(block.getNbtId()));
values.put("Pos", new IntArrayTag(new int[]{
x,
@ -142,8 +149,7 @@ public class SpongeSchematicWriter implements ClipboardWriter {
z
}));
CompoundTag tileEntityTag = new CompoundTag(values);
tileEntities.add(tileEntityTag);
tileEntities.add(new CompoundTag(values));
}
String blockKey = block.toImmutableState().getAsString();