Skip to content

Commit

Permalink
Merge eebccd3 into 947c750
Browse files Browse the repository at this point in the history
  • Loading branch information
Querz committed Dec 17, 2018
2 parents 947c750 + eebccd3 commit 7e5ab93
Show file tree
Hide file tree
Showing 33 changed files with 411 additions and 411 deletions.
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ ct.put("byte", new ByteTag((byte) 1));
ct.put("double", new DoubleTag(1.234));
ct.putString("string", "stringValue");
```
An example on how to use `ListTag`:
An example how to use a `ListTag`:
```java
ListTag<FloatTag> fl = new ListTag<>();
ListTag<FloatTag> fl = new ListTag<>(FloatTag.class);

fl.add(new FloatTag(1.234f);
fl.addFloat(5.678f);
Expand All @@ -56,7 +56,7 @@ NBTUtil.writeTag(tag, "filename.dat");

Example usage:
```java
Tag tag = NBTUtil.readTag("filename.dat");
Tag<?> tag = NBTUtil.readTag("filename.dat");
```
#### Playing Minecraft?
Each tag can be converted into a JSON-like NBT String used in Minecraft commands.
Expand All @@ -66,15 +66,20 @@ Example usage:
CompoundTag c = new CompoundTag();
c.putByte("blah", (byte) 5);
c.putString("foo", "bär");
String s = c.toTagString();
System.out.println(c.toTagString()); // {blah:5b,foo:"bär"}

ListTag<StringTag> s = new ListTag<>(StringTag.class);
s.addString("test");
s.add(new StringTag("text"));
c.add("list", s);
System.out.println(c.toTagString()); // {blah:5b,foo:"bär",list:[test,text]}

//output: {blah:5b,foo:"bär"}
```
There are also some tools to read, change and write MCA files.
There is also a tool to read, change and write MCA files.

Here are some examples:
```java
//This changes the InhabitedTime field of the chunk at x=68, z=81 to 0
// This changes the InhabitedTime field of the chunk at x=68, z=81 to 0
MCAFile mcaFile = MCAUtil.readMCAFile("r.2.2.mca");
Chunk chunk = mcaFile.getChunk(68, 81);
chunk.setInhabitedTime(0);
Expand All @@ -84,13 +89,13 @@ There is also an optimized api to retrieve and set block information (BlockState

Example:
```java
//Retrieves block information from the MCA file
// Retrieves block information from the MCA file
CompoundTag blockState = mcaFile.getBlockStateAt(1090, 25, 1301);

//Retrieves block information from a single chunk
// Retrieves block information from a single chunk
CompoundTag blockState = chunk.getBlockStateAt(2, 25, 5);

//Set block information
// Set block information
CompoundTag stone = new CompoundTag();
stone.putString("Name", "minecraft:stone");
mcaFile.setBlockStateAt(1090, 25, 1301, stone, false);
Expand All @@ -115,7 +120,7 @@ There are 4 example classes in `net.querz.nbt.custom` that show how to implement
| [CharTag](src/main/java/net/querz/nbt/custom/CharTag.java) | 110 | `Character` (char) tag. |
| [StructTag](src/main/java/net/querz/nbt/custom/StructTag.java) | 120 | Similar to the `ListTag`, but with the ability to store multiple types. |
To be able to use a custom tag with deserialization, a `Supplier` must be registered at runtime alongside its id with `TagFactory.registerCustomTag()`. The `Supplier` can be anything that returns a new instance of this custom tag. Here is an example using the custom tags no-args constructor:
To be able to use a custom tag with deserialization, a `Supplier` and the custom tag class must be registered at runtime alongside its id with `TagFactory.registerCustomTag()`. The `Supplier` can be anything that returns a new instance of this custom tag. Here is an example using the custom tags no-args constructor:
```java
TagFactory.registerCustomTag(90, ObjectTag::new);
TagFactory.registerCustomTag(90, ObjectTag::new, ObjectTag.class);
```
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/ByteArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ public ByteArrayTag(byte[] value) {
super(value);
}

@Override
public byte getID() {
return 7;
}

@Override
protected byte[] getEmptyValue() {
return new byte[0];
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/ByteTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ public ByteTag(boolean value) {
super((byte) (value ? 1 : 0));
}

@Override
public byte getID() {
return 1;
}

@Override
protected Byte getEmptyValue() {
return 0;
Expand Down
69 changes: 32 additions & 37 deletions src/main/java/net/querz/nbt/CompoundTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,20 @@
import java.util.*;
import java.util.function.BiConsumer;

public class CompoundTag extends Tag<Map<String, Tag>> implements Iterable<Map.Entry<String, Tag>> {
public class CompoundTag extends Tag<Map<String, Tag<?>>> implements Iterable<Map.Entry<String, Tag<?>>> {

public CompoundTag() {}

@Override
public byte getID() {
return 10;
}

@Override
protected Map<String, Tag> getEmptyValue() {
protected Map<String, Tag<?>> getEmptyValue() {
return new HashMap<>(8);
}

public int size() {
return getValue().size();
}

public Tag remove(String key) {
public Tag<?> remove(String key) {
return getValue().remove(key);
}

Expand All @@ -36,40 +31,40 @@ public boolean containsKey(String key) {
return getValue().containsKey(key);
}

public boolean containsValue(Tag value) {
public boolean containsValue(Tag<?> value) {
return getValue().containsValue(value);
}

public Collection<Tag> values() {
public Collection<Tag<?>> values() {
return getValue().values();
}

public Set<String> keySet() {
return getValue().keySet();
}

public Set<Map.Entry<String, Tag>> entrySet() {
public Set<Map.Entry<String, Tag<?>>> entrySet() {
return new NonNullEntrySet<>(getValue().entrySet());
}

@Override
public Iterator<Map.Entry<String, Tag>> iterator() {
public Iterator<Map.Entry<String, Tag<?>>> iterator() {
return entrySet().iterator();
}

public void forEach(BiConsumer<String, Tag> action) {
public void forEach(BiConsumer<String, Tag<?>> action) {
getValue().forEach(action);
}

public <C extends Tag> C get(String key, Class<C> type) {
Tag t = getValue().get(key);
public <C extends Tag<?>> C get(String key, Class<C> type) {
Tag<?> t = getValue().get(key);
if (t != null) {
return type.cast(t);
}
return null;
}

public Tag get(String key) {
public Tag<?> get(String key) {
return getValue().get(key);
}

Expand Down Expand Up @@ -122,7 +117,7 @@ public CompoundTag getCompoundTag(String key) {
}

public boolean getBoolean(String key) {
Tag t = get(key);
Tag<?> t = get(key);
return t instanceof ByteTag && ((ByteTag) t).asByte() > 0;
}

Expand Down Expand Up @@ -176,57 +171,57 @@ public long[] getLongArray(String key) {
return t == null ? new LongArrayTag().getEmptyValue() : t.getValue();
}

public Tag put(String key, Tag tag) {
public Tag<?> put(String key, Tag<?> tag) {
return getValue().put(checkNull(key), checkNull(tag));
}

public Tag putBoolean(String key, boolean value) {
public Tag<?> putBoolean(String key, boolean value) {
return put(key, new ByteTag(value));
}

public Tag putByte(String key, byte value) {
public Tag<?> putByte(String key, byte value) {
return put(key, new ByteTag(value));
}

public Tag putShort(String key, short value) {
public Tag<?> putShort(String key, short value) {
return put(key, new ShortTag(value));
}

public Tag putInt(String key, int value) {
public Tag<?> putInt(String key, int value) {
return put(key, new IntTag(value));
}

public Tag putLong(String key, long value) {
public Tag<?> putLong(String key, long value) {
return put(key, new LongTag(value));
}

public Tag putFloat(String key, float value) {
public Tag<?> putFloat(String key, float value) {
return put(key, new FloatTag(value));
}

public Tag putDouble(String key, double value) {
public Tag<?> putDouble(String key, double value) {
return put(key, new DoubleTag(value));
}

public Tag putString(String key, String value) {
public Tag<?> putString(String key, String value) {
return put(key, new StringTag(checkNull(value)));
}

public Tag putByteArray(String key, byte[] value) {
public Tag<?> putByteArray(String key, byte[] value) {
return put(key, new ByteArrayTag(checkNull(value)));
}

public Tag putIntArray(String key, int[] value) {
public Tag<?> putIntArray(String key, int[] value) {
return put(key, new IntArrayTag(checkNull(value)));
}

public Tag putLongArray(String key, long[] value) {
public Tag<?> putLongArray(String key, long[] value) {
return put(key, new LongArrayTag(checkNull(value)));
}

@Override
public void serializeValue(DataOutputStream dos, int depth) throws IOException {
for (Map.Entry<String, Tag> e : getValue().entrySet()) {
for (Map.Entry<String, Tag<?>> e : getValue().entrySet()) {
dos.writeByte(e.getValue().getID());
dos.writeUTF(e.getKey());
e.getValue().serializeValue(dos, incrementDepth(depth));
Expand All @@ -237,7 +232,7 @@ public void serializeValue(DataOutputStream dos, int depth) throws IOException {
@Override
public void deserializeValue(DataInputStream dis, int depth) throws IOException {
for (int id = dis.readByte() & 0xFF; id != 0; id = dis.readByte() & 0xFF) {
Tag tag = TagFactory.fromID(id);
Tag<?> tag = TagFactory.fromID(id);
String name = dis.readUTF();
tag.deserializeValue(dis, incrementDepth(depth));
put(name, tag);
Expand All @@ -248,7 +243,7 @@ public void deserializeValue(DataInputStream dis, int depth) throws IOException
public String valueToString(int depth) {
StringBuilder sb = new StringBuilder("{");
boolean first = true;
for (Map.Entry<String, Tag> e : getValue().entrySet()) {
for (Map.Entry<String, Tag<?>> e : getValue().entrySet()) {
sb.append(first ? "" : ",")
.append(escapeString(e.getKey(), false)).append(":")
.append(e.getValue().toString(incrementDepth(depth)));
Expand All @@ -262,7 +257,7 @@ public String valueToString(int depth) {
public String valueToTagString(int depth) {
StringBuilder sb = new StringBuilder("{");
boolean first = true;
for (Map.Entry<String, Tag> e : getValue().entrySet()) {
for (Map.Entry<String, Tag<?>> e : getValue().entrySet()) {
sb.append(first ? "" : ",")
.append(escapeString(e.getKey(), true)).append(":")
.append(e.getValue().valueToTagString(incrementDepth(depth)));
Expand All @@ -280,8 +275,8 @@ public boolean equals(Object other) {
if (!super.equals(other) || size() != ((CompoundTag) other).size()) {
return false;
}
for (Map.Entry<String, Tag> e : getValue().entrySet()) {
Tag v;
for (Map.Entry<String, Tag<?>> e : getValue().entrySet()) {
Tag<?> v;
if ((v = ((CompoundTag) other).get(e.getKey())) == null || !e.getValue().equals(v)) {
return false;
}
Expand All @@ -295,7 +290,7 @@ public int hashCode() {
}

@Override
public int compareTo(Tag<Map<String, Tag>> o) {
public int compareTo(Tag<Map<String, Tag<?>>> o) {
if (!(o instanceof CompoundTag)) {
return 0;
}
Expand All @@ -305,7 +300,7 @@ public int compareTo(Tag<Map<String, Tag>> o) {
@Override
public CompoundTag clone() {
CompoundTag copy = new CompoundTag();
for (Map.Entry<String, Tag> e : getValue().entrySet()) {
for (Map.Entry<String, Tag<?>> e : getValue().entrySet()) {
copy.put(e.getKey(), e.getValue().clone());
}
return copy;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/DoubleTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public DoubleTag(double value) {
super(value);
}

@Override
public byte getID() {
return 6;
}

@Override
protected Double getEmptyValue() {
return 0.0d;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/EndTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ public final class EndTag extends Tag<Void> {

private EndTag() {}

@Override
public byte getID() {
return 0;
}

@Override
protected Void getEmptyValue() {
return null;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/FloatTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public FloatTag(float value) {
super(value);
}

@Override
public byte getID() {
return 5;
}

@Override
protected Float getEmptyValue() {
return 0.0f;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/IntArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ public IntArrayTag(int[] value) {
super(value);
}

@Override
public byte getID() {
return 11;
}

@Override
protected int[] getEmptyValue() {
return new int[0];
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/querz/nbt/IntTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public IntTag(int value) {
super(value);
}

@Override
public byte getID() {
return 3;
}

@Override
protected Integer getEmptyValue() {
return 0;
Expand Down
Loading

0 comments on commit 7e5ab93

Please sign in to comment.