Skip to content

Commit

Permalink
Fix random Java 8 limitations
Browse files Browse the repository at this point in the history
  • Loading branch information
HoldYourWaffle committed Aug 27, 2024
1 parent a749187 commit b2dc6c8
Show file tree
Hide file tree
Showing 18 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/ByteArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void setValue(byte[] value) {
this.value = value;
}

public static final TagReader<ByteArrayTag> READER = new TagReader<>() {
public static final TagReader<ByteArrayTag> READER = new TagReader<ByteArrayTag>() {

@Override
public ByteArrayTag read(DataInput in, int depth) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/ByteTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public String toString() {
return value + "b";
}

public static final TagReader<ByteTag> READER = new TagReader<>() {
public static final TagReader<ByteTag> READER = new TagReader<ByteTag>() {

@Override
public ByteTag read(DataInput in, int depth) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/CompoundTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ public Set<Entry<String, Tag>> entrySet() {
return value.entrySet();
}

public static final TagReader<CompoundTag> READER = new TagReader<>() {
public static final TagReader<CompoundTag> READER = new TagReader<CompoundTag>() {

@Override
public CompoundTag read(DataInput in, int depth) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/DoubleTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public String toString() {
return value + "d";
}

public static final TagReader<DoubleTag> READER = new TagReader<>() {
public static final TagReader<DoubleTag> READER = new TagReader<DoubleTag>() {

@Override
public DoubleTag read(DataInput in, int depth) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/EndTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void accept(TagVisitor visitor) {
visitor.visit(this);
}

public static final TagReader<EndTag> READER = new TagReader<>() {
public static final TagReader<EndTag> READER = new TagReader<EndTag>() {

@Override
public EndTag read(DataInput in, int depth) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/FloatTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public String toString() {
return value + "f";
}

public static final TagReader<FloatTag> READER = new TagReader<>() {
public static final TagReader<FloatTag> READER = new TagReader<FloatTag>() {

@Override
public FloatTag read(DataInput in, int depth) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/IntArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void setValue(int[] value) {
this.value = value;
}

public static final TagReader<IntArrayTag> READER = new TagReader<>() {
public static final TagReader<IntArrayTag> READER = new TagReader<IntArrayTag>() {

@Override
public IntArrayTag read(DataInput in, int depth) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/IntTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public String toString() {
return String.valueOf(value);
}

public static final TagReader<IntTag> READER = new TagReader<>() {
public static final TagReader<IntTag> READER = new TagReader<IntTag>() {

@Override
public IntTag read(DataInput in, int depth) throws IOException {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/querz/nbt/ListTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public boolean equals(Object other) {
if (this == other) {
return true;
} else {
return other instanceof ListTag otherList && value.equals(otherList.value);
return other instanceof ListTag && value.equals(((ListTag) other).value);
}
}

Expand Down Expand Up @@ -453,7 +453,7 @@ public T remove(int index) {

}

public static final TagReader<ListTag> READER = new TagReader<>() {
public static final TagReader<ListTag> READER = new TagReader<ListTag>() {

@Override
public ListTag read(DataInput in, int depth) throws IOException {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/querz/nbt/LongArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void setValue(long[] value) {
this.value = value;
}

public static final TagReader<LongArrayTag> READER = new TagReader<>() {
public static final TagReader<LongArrayTag> READER = new TagReader<LongArrayTag>() {

@Override
public LongArrayTag read(DataInput in, int depth) throws IOException {
Expand All @@ -137,4 +137,4 @@ public void skip(DataInput in) throws IOException {
in.skipBytes(in.readInt() * 8);
}
};
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/LongTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public String toString() {
return value + "L";
}

public static final TagReader<LongTag> READER = new TagReader<>() {
public static final TagReader<LongTag> READER = new TagReader<LongTag>() {

@Override
public LongTag read(DataInput in, int depth) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/ShortTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public String toString() {
return value + "s";
}

public static final TagReader<ShortTag> READER = new TagReader<>() {
public static final TagReader<ShortTag> READER = new TagReader<ShortTag>() {

@Override
public ShortTag read(DataInput in, int depth) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/StringTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public String getValue() {
return value;
}

public static final TagReader<StringTag> READER = new TagReader<>() {
public static final TagReader<StringTag> READER = new TagReader<StringTag>() {

@Override
public StringTag read(DataInput in, int depth) throws IOException {
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/net/querz/nbt/io/snbt/SNBTReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ public Tag read(String s) throws ParseException {
}

public Tag read(InputStream in) throws IOException {
String s = new String(in.readAllBytes(), StandardCharsets.UTF_8);
// InputStream.readAllBytes is only available in Java 9 and beyond
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int length; (length = in.read(buffer)) != -1; ) {
result.write(buffer, 0, length);
}
String s = result.toString(StandardCharsets.UTF_8.name());

return new SNBTParser(s).parse(ignoreTrailing);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/querz/nbt/io/snbt/SNBTWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public SNBTWriter indent(String indent) {
}

public void write(OutputStream out, Tag tag) throws IOException {
PrintWriter writer = new PrintWriter(out, false, StandardCharsets.UTF_8);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8), false);
tag.accept(new SNBTWriterTagVisitor(writer, indent));
writer.flush();
}
Expand All @@ -28,7 +28,7 @@ public String toString(Tag tag) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
write(baos, tag);
return baos.toString(StandardCharsets.UTF_8);
return baos.toString(StandardCharsets.UTF_8.name());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/net/querz/nbt/io/snbt/SNBTWriterTagVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private SNBTWriterTagVisitor(Writer writer, String indent, int depth) {
}

// region primitives

@Override
public void visit(ByteTag t) {
writeNumber(t);
Expand Down Expand Up @@ -82,7 +82,7 @@ public void visit(StringTag t) {
// endregion

// region arrays

@Override
public void visit(ByteArrayTag t) {
writeArray(t, "B");
Expand Down Expand Up @@ -113,7 +113,7 @@ private void writeArray(CollectionTag<? extends NumberTag> t, String prefix) {
throw new UncheckedIOException(ex);
}
}

// endregion

// region hierarchies
Expand Down Expand Up @@ -175,7 +175,11 @@ private void writeSpacing() throws IOException {
}

private void writeIndent(int depth) throws IOException {
writer.write(indent.repeat(depth));
StringBuilder indent = new StringBuilder();
for (int i = 0; i < depth; i++) {
indent.append(this.indent);
}
writer.write(indent.toString());
}

// endregion
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/net/querz/nbt/io/stream/TagSelector.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package net.querz.nbt.io.stream;

import net.querz.nbt.TagReader;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand All @@ -16,15 +19,15 @@ public TagSelector(List<String> path, String name, TagReader<?> type) {
}

public TagSelector(String name, TagReader<?> reader) {
this(List.of(), name, reader);
this(Collections.emptyList(), name, reader);
}

public TagSelector(String p1, String name, TagReader<?> reader) {
this(List.of(p1), name, reader);
this(Collections.singletonList(p1), name, reader);
}

public TagSelector(String p1, String p2, String name, TagReader<?> reader) {
this(List.of(p1, p2), name, reader);
this(Arrays.asList(p1, p2), name, reader);
}

public List<String> path() {
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/net/querz/nbt/TestNBTUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.zip.GZIPInputStream;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -41,7 +42,7 @@ public void testParse() throws IOException {
}

try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(raw))) {
t = NBTUtil.parseStream(dis, new TagSelector(List.of("Data", "Player", "Attributes"), "Name", StringTag.READER));
t = NBTUtil.parseStream(dis, new TagSelector(Arrays.asList("Data", "Player", "Attributes"), "Name", StringTag.READER));

System.out.println(NBTUtil.toSNBT(t, "\t"));
}
Expand Down Expand Up @@ -106,7 +107,7 @@ public void testBlah() throws IOException {







Expand Down

0 comments on commit b2dc6c8

Please sign in to comment.