Skip to content

Commit

Permalink
Merge 98924e6 into 8cf846f
Browse files Browse the repository at this point in the history
  • Loading branch information
Querz committed Dec 9, 2018
2 parents 8cf846f + 98924e6 commit fecbc0b
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 72 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,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, it needs to have a public no-args constructor and its id and class must be registered during runtime with `TagFactory.registerCustomTag()`.
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:
```java
TagFactory.registerCustomTag(90, ObjectTag::new);
```
17 changes: 7 additions & 10 deletions src/main/java/net/querz/nbt/TagFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

public class TagFactory {

private static Map<Integer, Class<? extends Tag>> customTags = new HashMap<>();
private static Map<Integer, Supplier<? extends Tag<?>>> customTags = new HashMap<>();

private TagFactory() {}

Expand Down Expand Up @@ -38,19 +39,15 @@ public static Tag fromID(int id) {
case 12:
return new LongArrayTag();
default:
Class<?> clazz = customTags.get(id);
if (clazz != null) {
try {
return (Tag) clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException("error instantiating custom tag: " + e.getMessage());
}
Supplier<? extends Tag<?>> factory = customTags.get(id);
if (factory != null) {
return factory.get();
}
throw new IllegalArgumentException("unknown Tag id: " + id);
}
}

public static void registerCustomTag(int id, Class<? extends Tag> clazz) {
public static void registerCustomTag(int id, Supplier<? extends Tag<?>> factory) {
if (id < 0) {
throw new IllegalArgumentException("id cannot be negative");
}
Expand All @@ -63,7 +60,7 @@ public static void registerCustomTag(int id, Class<? extends Tag> clazz) {
if (customTags.containsKey(id)) {
throw new IllegalArgumentException("custom tag already registered");
}
customTags.put(id, clazz);
customTags.put(id, factory);
}

public static void unregisterCustomTag(int id) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/custom/CharTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class CharTag extends Tag<Character> {

public static void register() {
TagFactory.registerCustomTag(110, CharTag.class);
TagFactory.registerCustomTag(110, CharTag::new);
}

public CharTag() {}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/custom/ObjectTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class ObjectTag<T extends Serializable> extends Tag<T> {

public static void register() {
TagFactory.registerCustomTag(90, ObjectTag.class);
TagFactory.registerCustomTag(90, ObjectTag::new);
}

public ObjectTag() {}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/custom/ShortArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class ShortArrayTag extends ArrayTag<short[]> {

public static void register() {
TagFactory.registerCustomTag(100, ShortArrayTag.class);
TagFactory.registerCustomTag(100, ShortArrayTag::new);
}

public ShortArrayTag() {}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/nbt/custom/StructTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class StructTag extends Tag<List<Tag>> implements Iterable<Tag> {

public static void register() {
TagFactory.registerCustomTag(120, StructTag.class);
TagFactory.registerCustomTag(120, StructTag::new);
}

public StructTag() {}
Expand Down
59 changes: 3 additions & 56 deletions src/test/java/net/querz/nbt/TagFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import net.querz.nbt.custom.CharTag;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class TagFactoryTest extends NBTTestCase {

public void testFromID() {
Expand All @@ -23,14 +19,12 @@ public void testFromID() {
assertEquals(IntArrayTag.class, TagFactory.fromID(11).getClass());
assertEquals(LongArrayTag.class, TagFactory.fromID(12).getClass());
assertThrowsRuntimeException(() -> TagFactory.fromID(-1), IllegalArgumentException.class);
TagFactory.registerCustomTag(127, InvalidCustomTag.class);
assertThrowsRuntimeException(() -> TagFactory.fromID(127), RuntimeException.class);
}

public void testRegisterCustomTag() {
assertThrowsRuntimeException(() -> TagFactory.registerCustomTag(-1, CharTag.class), IllegalArgumentException.class);
assertThrowsRuntimeException(() -> TagFactory.registerCustomTag(12, CharTag.class), IllegalArgumentException.class);
assertThrowsRuntimeException(() -> TagFactory.registerCustomTag(128, CharTag.class), IllegalArgumentException.class);
assertThrowsRuntimeException(() -> TagFactory.registerCustomTag(-1, CharTag::new), IllegalArgumentException.class);
assertThrowsRuntimeException(() -> TagFactory.registerCustomTag(12, CharTag::new), IllegalArgumentException.class);
assertThrowsRuntimeException(() -> TagFactory.registerCustomTag(128, CharTag::new), IllegalArgumentException.class);
CharTag.register();
assertThrowsRuntimeException(CharTag::register, IllegalArgumentException.class);
}
Expand All @@ -39,51 +33,4 @@ public void testUnregisterCustomTag() {
CharTag.register();
assertThrowsNoRuntimeException(() -> TagFactory.unregisterCustomTag(new CharTag().getID()));
}

public class InvalidCustomTag extends Tag<Void> {

public InvalidCustomTag(InvalidCustomTag invalidConstructorParam) {

}

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

@Override
protected Void getEmptyValue() {
return null;
}

@Override
public void serializeValue(DataOutputStream dos, int depth) throws IOException {

}

@Override
public void deserializeValue(DataInputStream dis, int depth) throws IOException {

}

@Override
public String valueToString(int depth) {
return null;
}

@Override
public String valueToTagString(int depth) {
return null;
}

@Override
protected InvalidCustomTag clone() {
return null;
}

@Override
public int compareTo(Tag<Void> o) {
return 0;
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/net/querz/nbt/custom/ObjectTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void testCompareTo() {
}

public void testUnknownObject() {
TagFactory.registerCustomTag(90, ObjectTag.class);
TagFactory.registerCustomTag(90, ObjectTag::new);
assertThrowsException(() -> NBTUtil.readTag(getResourceFile("unknown_object_tag.dat")), IOException.class);
}

Expand Down

0 comments on commit fecbc0b

Please sign in to comment.