Major command changes that don't work yet.

This commit is contained in:
MattBDev
2019-07-05 20:46:48 -04:00
parent ffc2092d93
commit 8108d0a936
399 changed files with 13558 additions and 7985 deletions

View File

@ -181,6 +181,18 @@ public class CompoundTagBuilder {
return put(key, new StringTag(value));
}
/**
* Remove the given key from the compound tag. Does nothing if the key doesn't exist.
*
* @param key the key
* @return this object
*/
public CompoundTagBuilder remove(String key) {
checkNotNull(key);
entries.remove(key);
return this;
}
/**
* Put all the entries from the given map into this map.
*

View File

@ -1,10 +1,9 @@
package com.sk89q.jnbt;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import net.jpountz.lz4.LZ4BlockInputStream;
public abstract class CompressedCompoundTag<T> extends CompoundTag {
private T in;
@ -20,7 +19,7 @@ public abstract class CompressedCompoundTag<T> extends CompoundTag {
return super.getValue();
}
public abstract DataInputStream adapt(T src) throws IOException;
public abstract LZ4BlockInputStream adapt(T src) throws IOException;
public T getSource() {
return in;

View File

@ -2,15 +2,12 @@ package com.sk89q.jnbt;
import com.boydti.fawe.object.io.FastByteArrayOutputStream;
import com.boydti.fawe.object.io.FastByteArraysInputStream;
import com.boydti.fawe.object.io.PGZIPOutputStream;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.SpongeSchematicWriter;
import net.jpountz.lz4.LZ4BlockInputStream;
import net.jpountz.lz4.LZ4BlockOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
public class CompressedSchematicTag extends CompressedCompoundTag<Clipboard> {
public CompressedSchematicTag(Clipboard holder) {
@ -18,7 +15,7 @@ public class CompressedSchematicTag extends CompressedCompoundTag<Clipboard> {
}
@Override
public DataInputStream adapt(Clipboard src) throws IOException {
public LZ4BlockInputStream adapt(Clipboard src) throws IOException {
FastByteArrayOutputStream blocksOut = new FastByteArrayOutputStream();
try (LZ4BlockOutputStream lz4out = new LZ4BlockOutputStream(blocksOut)) {
NBTOutputStream nbtOut = new NBTOutputStream(lz4out);
@ -27,6 +24,6 @@ public class CompressedSchematicTag extends CompressedCompoundTag<Clipboard> {
throw new RuntimeException(e);
}
FastByteArraysInputStream in = new FastByteArraysInputStream(blocksOut.toByteArrays());
return new DataInputStream(new LZ4BlockInputStream(in));
return new LZ4BlockInputStream(in);
}
}

View File

@ -21,6 +21,8 @@ package com.sk89q.jnbt;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Locale;
/**
* The {@code TAG_Int_Array} tag.
*/
@ -48,7 +50,7 @@ public final class IntArrayTag extends Tag {
public String toString() {
StringBuilder hex = new StringBuilder();
for (int b : value) {
String hexDigits = Integer.toHexString(b).toUpperCase();
String hexDigits = Integer.toHexString(b).toUpperCase(Locale.ROOT);
if (hexDigits.length() == 1) {
hex.append("0");
}

View File

@ -21,6 +21,8 @@ package com.sk89q.jnbt;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Locale;
/**
* The {@code TAG_Long_Array} tag.
*/
@ -48,7 +50,7 @@ public class LongArrayTag extends Tag {
public String toString() {
StringBuilder hex = new StringBuilder();
for (long b : value) {
String hexDigits = Long.toHexString(b).toUpperCase();
String hexDigits = Long.toHexString(b).toUpperCase(Locale.ROOT);
if (hexDigits.length() == 1) {
hex.append("0");
}

View File

@ -152,39 +152,21 @@ public final class NBTInputStream implements Closeable {
if (reader instanceof NBTStreamer.ByteReader) {
NBTStreamer.ByteReader byteReader = (NBTStreamer.ByteReader) reader;
int i = 0;
if (is instanceof InputStream) {
DataInputStream dis = is;
if (length > 1024) {
if (buf == null) {
buf = new byte[1024];
}
int left = length;
for (; left > 1024; left -= 1024) {
dis.readFully(buf);
for (byte b : buf) {
byteReader.run(i++, b & 0xFF);
}
DataInputStream dis = is;
if (length > 1024) {
if (buf == null) {
buf = new byte[1024];
}
int left = length;
for (; left > 1024; left -= 1024) {
dis.readFully(buf);
for (byte b : buf) {
byteReader.run(i++, b & 0xFF);
}
}
for (; i < length; i++) {
byteReader.run(i, dis.read());
}
} else {
if (length > 1024) {
if (buf == null) {
buf = new byte[1024];
}
int left = length;
for (; left > 1024; left -= 1024) {
is.readFully(buf);
for (byte b : buf) {
byteReader.run(i++, b & 0xFF);
}
}
}
for (; i < length; i++) {
byteReader.run(i, is.readByte() & 0xFF);
}
}
for (; i < length; i++) {
byteReader.run(i, dis.read());
}
} else if (reader instanceof NBTStreamer.LazyReader) {
reader.accept(length, is);
@ -410,88 +392,6 @@ public final class NBTInputStream implements Closeable {
}
}
public Object readDataPayload(int type, int depth) throws IOException {
switch (type) {
case NBTConstants.TYPE_END:
if (depth == 0) {
throw new IOException(
"TAG_End found without a TAG_Compound/TAG_List tag preceding it.");
} else {
return null;
}
case NBTConstants.TYPE_BYTE:
return is.readByte();
case NBTConstants.TYPE_SHORT:
return is.readShort();
case NBTConstants.TYPE_INT:
return is.readInt();
case NBTConstants.TYPE_LONG:
return is.readLong();
case NBTConstants.TYPE_FLOAT:
return is.readFloat();
case NBTConstants.TYPE_DOUBLE:
return is.readDouble();
case NBTConstants.TYPE_BYTE_ARRAY:
int length = is.readInt();
byte[] bytes = new byte[length];
is.readFully(bytes);
return bytes;
case NBTConstants.TYPE_STRING:
length = is.readShort();
bytes = new byte[length];
is.readFully(bytes);
return new String(bytes, NBTConstants.CHARSET);
case NBTConstants.TYPE_LIST:
int childType = is.readByte();
if (childType == NBTConstants.TYPE_LIST) {
childType = NBTConstants.TYPE_COMPOUND;
}
length = is.readInt();
ArrayList<Object> list = new ArrayList<>();
for (int i = 0; i < length; ++i) {
Object obj = readDataPayload(childType, depth + 1);
if (obj == null) {
throw new IOException("TAG_End not permitted in a list.");
}
list.add(obj);
}
return list;
case NBTConstants.TYPE_COMPOUND:
Map<String, Object> map = new HashMap<>();
while (true) {
int newType = is.readByte();
String name = readNamedTagName(newType);
Object data = readDataPayload(newType, depth + 1);
if (data == null) {
break;
} else {
map.put(name, data);
}
}
return map;
case NBTConstants.TYPE_INT_ARRAY: {
length = is.readInt();
int[] data = new int[length];
for (int i = 0; i < length; i++) {
data[i] = is.readInt();
}
return data;
}
case NBTConstants.TYPE_LONG_ARRAY: {
length = is.readInt();
long[] data = new long[length];
for (int i = 0; i < length; i++) {
data[i] = is.readLong();
}
return data;
}
default:
throw new IOException("Invalid tag type: " + type + ".");
}
}
/**
* Reads the payload of a tag given the type.
*
@ -560,22 +460,20 @@ public final class NBTInputStream implements Closeable {
}
return new CompoundTag(tagMap);
case NBTConstants.TYPE_INT_ARRAY: {
case NBTConstants.TYPE_INT_ARRAY:
length = is.readInt();
int[] data = new int[length];
for (int i = 0; i < length; i++) {
data[i] = is.readInt();
}
return new IntArrayTag(data);
}
case NBTConstants.TYPE_LONG_ARRAY: {
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 + ".");
}
@ -583,13 +481,7 @@ public final class NBTInputStream implements Closeable {
@Override
public void close() throws IOException {
if (is instanceof AutoCloseable) {
try {
((AutoCloseable) is).close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
is.close();
}
}

View File

@ -19,8 +19,6 @@
package com.sk89q.jnbt;
import com.boydti.fawe.object.io.LittleEndianOutputStream;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Closeable;
@ -45,7 +43,7 @@ public final class NBTOutputStream extends OutputStream implements Closeable, Da
/**
* The output stream.
*/
private DataOutput os;
private final DataOutputStream os;
/**
* Creates a new {@code NBTOutputStream}, which will write data to the
@ -60,23 +58,10 @@ public final class NBTOutputStream extends OutputStream implements Closeable, Da
this.os = new DataOutputStream(os);
}
public NBTOutputStream(DataOutput os) throws IOException {
this.os = os;
}
public DataOutput getOutputStream() {
return os;
}
/**
* Use a little endian output stream
*/
public void setLittleEndian() {
if (!(os instanceof LittleEndianOutputStream)) {
this.os = new LittleEndianOutputStream((OutputStream) os);
}
}
/**
* Writes a tag.
*
@ -159,7 +144,7 @@ public final class NBTOutputStream extends OutputStream implements Closeable, Da
writeNamedEmptyList(name, NBTConstants.TYPE_COMPOUND);
}
public void writeNamedEmptyList(String name, int type) throws IOException {
private void writeNamedEmptyList(String name, int type) throws IOException {
writeNamedTagName(name, NBTConstants.TYPE_LIST);
os.writeByte(type);
os.writeInt(0);
@ -420,7 +405,7 @@ public final class NBTOutputStream extends OutputStream implements Closeable, Da
@Override
public void close() throws IOException {
if (os instanceof Closeable) ((Closeable) os).close();
os.close();
}
@Override
@ -500,6 +485,6 @@ public final class NBTOutputStream extends OutputStream implements Closeable, Da
*/
@Override
public void flush() throws IOException {
if (os instanceof Flushable) ((Flushable) os).flush();
((Flushable) os).flush();
}
}