Skip to content

Commit

Permalink
Support tag search
Browse files Browse the repository at this point in the history
  • Loading branch information
pop4959 committed Feb 4, 2024
1 parent a2e8736 commit 62c437d
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 0 deletions.
12 changes: 12 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/ByteArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@ public void read(final DataInput input) throws IOException {
input.readFully(value);
}

@Override
public void skip(final DataInput input) throws IOException {
final int size = input.readInt();
input.skipBytes(size);
}

@Override
public void write(final DataOutput output) throws IOException {
output.writeInt(value.length);
output.write(value);
}

@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
skip(input);
return null;
}

@Override
public byte type() {
return TagType.BYTE_ARRAY;
Expand Down
11 changes: 11 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/ByteTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ public void read(final DataInput input) throws IOException {
this.value = input.readByte();
}

@Override
public void skip(final DataInput input) throws IOException {
input.skipBytes(1);
}

@Override
public void write(final DataOutput output) throws IOException {
output.writeByte(value);
}

@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
skip(input);
return null;
}

@Override
public byte type() {
return TagType.BYTE;
Expand Down
17 changes: 17 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/CompoundTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public void read(final DataInput input) throws IOException {
}
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public void skip(final DataInput input) throws IOException {
while (TagType.END != Tag.pass(input)) ;
}

@Override
public void write(final DataOutput output) throws IOException {
for (final Tag tag : value.values()) {
Expand All @@ -36,6 +42,17 @@ public void write(final DataOutput output) throws IOException {
output.writeByte(TagType.END);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
Tag tag;
while ((tag = Tag.find(input, type, name)) == null) ;
if (TagType.END == tag.type()) {
return null;
}
return tag;
}

@Override
public byte type() {
return TagType.COMPOUND;
Expand Down
11 changes: 11 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/DoubleTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ public void read(final DataInput input) throws IOException {
this.value = input.readDouble();
}

@Override
public void skip(final DataInput input) throws IOException {
input.skipBytes(8);
}

@Override
public void write(final DataOutput output) throws IOException {
output.writeDouble(value);
}

@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
skip(input);
return null;
}

@Override
public byte type() {
return TagType.DOUBLE;
Expand Down
11 changes: 11 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/EndTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,22 @@ public void read(final DataInput input) throws IOException {
// No data
}

@Override
public void skip(final DataInput input) throws IOException {
// No data
}

@Override
public void write(final DataOutput output) throws IOException {
// No data
}

@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
skip(input);
return null;
}

@Override
public byte type() {
return TagType.END;
Expand Down
11 changes: 11 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/FloatTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ public void read(final DataInput input) throws IOException {
this.value = input.readFloat();
}

@Override
public void skip(final DataInput input) throws IOException {
input.skipBytes(4);
}

@Override
public void write(final DataOutput output) throws IOException {
output.writeFloat(value);
}

@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
skip(input);
return null;
}

@Override
public byte type() {
return TagType.FLOAT;
Expand Down
12 changes: 12 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/IntArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public void read(final DataInput input) throws IOException {
}
}

@Override
public void skip(final DataInput input) throws IOException {
final int size = input.readInt();
input.skipBytes(4 * size);
}

@Override
public void write(final DataOutput output) throws IOException {
final int size = value.length;
Expand All @@ -35,6 +41,12 @@ public void write(final DataOutput output) throws IOException {
}
}

@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
skip(input);
return null;
}

@Override
public byte type() {
return TagType.INT_ARRAY;
Expand Down
11 changes: 11 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/IntTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ public void read(final DataInput input) throws IOException {
this.value = input.readInt();
}

@Override
public void skip(final DataInput input) throws IOException {
input.skipBytes(4);
}

@Override
public void write(final DataOutput output) throws IOException {
output.writeInt(value);
}

@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
skip(input);
return null;
}

@Override
public byte type() {
return TagType.INT;
Expand Down
16 changes: 16 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/ListTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public void read(final DataInput input) throws IOException {
}
}

@Override
public void skip(final DataInput input) throws IOException {
this.type = input.readByte();
final int size = input.readInt();
final Tag tag = Tag.create(type, "");
for (int i = 0; i < size; ++i) {
tag.skip(input);
}
}

@Override
public void write(final DataOutput output) throws IOException {
output.writeByte(type);
Expand All @@ -42,6 +52,12 @@ public void write(final DataOutput output) throws IOException {
}
}

@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
skip(input);
return null;
}

@Override
public byte type() {
return TagType.LIST;
Expand Down
12 changes: 12 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/LongArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public void read(final DataInput input) throws IOException {
}
}

@Override
public void skip(final DataInput input) throws IOException {
final int size = input.readInt();
input.skipBytes(8 * size);
}

@Override
public void write(final DataOutput output) throws IOException {
final int size = value.length;
Expand All @@ -35,6 +41,12 @@ public void write(final DataOutput output) throws IOException {
}
}

@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
skip(input);
return null;
}

@Override
public byte type() {
return TagType.LONG_ARRAY;
Expand Down
11 changes: 11 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/LongTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ public void read(final DataInput input) throws IOException {
this.value = input.readLong();
}

@Override
public void skip(final DataInput input) throws IOException {
input.skipBytes(8);
}

@Override
public void write(final DataOutput output) throws IOException {
output.writeLong(value);
}

@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
skip(input);
return null;
}

@Override
public byte type() {
return TagType.LONG;
Expand Down
11 changes: 11 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/ShortTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ public void read(final DataInput input) throws IOException {
this.value = input.readShort();
}

@Override
public void skip(final DataInput input) throws IOException {
input.skipBytes(2);
}

@Override
public void write(final DataOutput output) throws IOException {
output.writeShort(value);
}

@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
skip(input);
return null;
}

@Override
public byte type() {
return TagType.SHORT;
Expand Down
12 changes: 12 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/StringTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@ public void read(final DataInput input) throws IOException {
this.value = input.readUTF();
}

@Override
public void skip(final DataInput input) throws IOException {
final int size = input.readUnsignedShort();
input.skipBytes(size);
}

@Override
public void write(final DataOutput output) throws IOException {
output.writeUTF(value);
}

@Override
public Tag search(final DataInput input, final byte type, final String name) throws IOException {
skip(input);
return null;
}

@Override
public byte type() {
return TagType.STRING;
Expand Down
29 changes: 29 additions & 0 deletions nbt/src/main/java/org/popcraft/chunky/nbt/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public static Tag load(final DataInput input) throws IOException {
return tag;
}

public static byte pass(final DataInput input) throws IOException {
final byte type = input.readByte();
if (TagType.END == type) {
return type;
}
final int size = input.readUnsignedShort();
input.skipBytes(size);
create(type, "").skip(input);
return type;
}

public static void save(final DataOutput output, final Tag tag) throws IOException {
final byte type = tag.type();
output.writeByte(type);
Expand All @@ -33,6 +44,20 @@ public static void save(final DataOutput output, final Tag tag) throws IOExcepti
tag.write(output);
}

public static Tag find(final DataInput input, final byte type, final String name) throws IOException {
final byte t = input.readByte();
if (TagType.END == t) {
return new EndTag();
}
final String n = input.readUTF();
final Tag tag = create(t, n);
if (type == t && name.equals(n)) {
tag.read(input);
return tag;
}
return tag.search(input, type, name);
}

public static Tag create(final byte type, final String name) {
return switch (type) {
case TagType.END -> new EndTag();
Expand All @@ -58,8 +83,12 @@ public String name() {

abstract void read(final DataInput input) throws IOException;

abstract void skip(final DataInput input) throws IOException;

abstract void write(final DataOutput output) throws IOException;

abstract Tag search(final DataInput input, final byte type, final String name) throws IOException;

abstract byte type();

abstract String typeName();
Expand Down

0 comments on commit 62c437d

Please sign in to comment.