From 5b59d77537098b546ca3194a1f658b3b5479ccf9 Mon Sep 17 00:00:00 2001 From: xuyin <1573880184@qq.com> Date: Wed, 1 Jan 2025 01:22:47 +0800 Subject: [PATCH] Update to 4.1.0. --- CHANGES.md | 6 + build.gradle.kts | 2 +- src/main/java/io/github/xiefrish2021/NBT.java | 14 ++- .../java/io/github/xiefrish2021/Preview.java | 9 -- .../xiefrish2021/{ => core}/NBTException.java | 2 +- .../xiefrish2021/core/ObjectNBTMapper.java | 91 +++++++++++++++ .../{snbt => core}/SNBTReader.java | 7 +- .../xiefrish2021/object/ObjectToNbt.java | 109 ------------------ .../io/github/xiefrish2021/tag/ByteTag.java | 19 +-- .../io/github/xiefrish2021/tag/DoubleTag.java | 19 +-- .../io/github/xiefrish2021/tag/FloatTag.java | 18 +-- .../io/github/xiefrish2021/tag/IntTag.java | 19 +-- .../io/github/xiefrish2021/tag/LongTag.java | 19 +-- .../io/github/xiefrish2021/tag/ShortTag.java | 20 +--- .../io/github/xiefrish2021/tag/StringTag.java | 19 +-- .../xiefrish2021/tag/array/ByteArrayTag.java | 7 +- .../xiefrish2021/tag/array/IntArrayTag.java | 3 +- .../xiefrish2021/tag/array/LongArrayTag.java | 2 +- .../xiefrish2021/tag/compound/NBTElement.java | 7 +- .../github/xiefrish2021/tag/list/ListTag.java | 35 +++--- .../github/xiefrish2021/util/ReaderUtil.java | 13 ++- .../github/xiefrish2021/util/WriteUtil.java | 9 +- src/test/java/me/coderfrish/test/NBTest.java | 11 +- .../me/coderfrish/test/bean/TestBean1.java | 2 +- .../me/coderfrish/test/bean/TestBean2.java | 10 ++ src/test/resources/test.nbt | Bin 0 -> 56 bytes 26 files changed, 191 insertions(+), 281 deletions(-) delete mode 100644 src/main/java/io/github/xiefrish2021/Preview.java rename src/main/java/io/github/xiefrish2021/{ => core}/NBTException.java (88%) create mode 100644 src/main/java/io/github/xiefrish2021/core/ObjectNBTMapper.java rename src/main/java/io/github/xiefrish2021/{snbt => core}/SNBTReader.java (98%) delete mode 100644 src/main/java/io/github/xiefrish2021/object/ObjectToNbt.java create mode 100644 src/test/java/me/coderfrish/test/bean/TestBean2.java diff --git a/CHANGES.md b/CHANGES.md index 2b7b938..5bec2a1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,11 @@ # 版本更新 (3.0.0开始算起): +### 4.1.0 - 更新 +- 移除了4.0.0不稳定的Java Bean转NBT的支持 +- 重写了新的Java Bean转NBT的支持(基本稳定) +- 标签类修从普通的Class类改成了Record类 +- 修改了部分代码 + ### 4.0.0 - 更新 - 添加了Java Bean转NBT的支持(不稳定) - 修改了部分代码 diff --git a/build.gradle.kts b/build.gradle.kts index d19a925..12d5143 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "io.github.xiefrish2021" -version = "4.0.0" +version = "4.1.0" repositories { mavenCentral() diff --git a/src/main/java/io/github/xiefrish2021/NBT.java b/src/main/java/io/github/xiefrish2021/NBT.java index ac281cc..d1d2525 100644 --- a/src/main/java/io/github/xiefrish2021/NBT.java +++ b/src/main/java/io/github/xiefrish2021/NBT.java @@ -1,10 +1,12 @@ package io.github.xiefrish2021; -import io.github.xiefrish2021.object.ObjectToNbt; -import io.github.xiefrish2021.snbt.SNBTReader; +import io.github.xiefrish2021.core.NBTException; +import io.github.xiefrish2021.core.ObjectNBTMapper; +import io.github.xiefrish2021.core.SNBTReader; import io.github.xiefrish2021.tag.compound.CompoundTag; import io.github.xiefrish2021.util.ReaderUtil; import io.github.xiefrish2021.util.WriteUtil; +import org.jetbrains.annotations.ApiStatus; import java.io.*; @@ -19,9 +21,9 @@ public final class NBT { * @param object Java Bean. * The authors of this method cannot guarantee its stability and may be removed in the future. */ - @Preview + @ApiStatus.Experimental public static void writeNamedNBT(String name, Object object, OutputStream out) { - writeNamedNBT(name, new ObjectToNbt(object).toNBT(), out); + writeNamedNBT(name, new ObjectNBTMapper(object).toNBT(), out); } /** @@ -29,9 +31,9 @@ public static void writeNamedNBT(String name, Object object, OutputStream out) { * @param object Java Bean. * The authors of this method cannot guarantee its stability and may be removed in the future. */ - @Preview + @ApiStatus.Experimental public static void writeUnnamedNBT(Object object, OutputStream out) { - writeUnnamedNBT(new ObjectToNbt(object).toNBT(), out); + writeUnnamedNBT(new ObjectNBTMapper(object).toNBT(), out); } /** diff --git a/src/main/java/io/github/xiefrish2021/Preview.java b/src/main/java/io/github/xiefrish2021/Preview.java deleted file mode 100644 index c21b8b1..0000000 --- a/src/main/java/io/github/xiefrish2021/Preview.java +++ /dev/null @@ -1,9 +0,0 @@ -package io.github.xiefrish2021; - -import java.lang.annotation.*; - -@Documented -@Retention(RetentionPolicy.CLASS) -@Target({ElementType.METHOD}) -public @interface Preview { -} diff --git a/src/main/java/io/github/xiefrish2021/NBTException.java b/src/main/java/io/github/xiefrish2021/core/NBTException.java similarity index 88% rename from src/main/java/io/github/xiefrish2021/NBTException.java rename to src/main/java/io/github/xiefrish2021/core/NBTException.java index f12f80f..d28abbd 100644 --- a/src/main/java/io/github/xiefrish2021/NBTException.java +++ b/src/main/java/io/github/xiefrish2021/core/NBTException.java @@ -1,4 +1,4 @@ -package io.github.xiefrish2021; +package io.github.xiefrish2021.core; public class NBTException extends RuntimeException { public NBTException(String message) { diff --git a/src/main/java/io/github/xiefrish2021/core/ObjectNBTMapper.java b/src/main/java/io/github/xiefrish2021/core/ObjectNBTMapper.java new file mode 100644 index 0000000..544f8ea --- /dev/null +++ b/src/main/java/io/github/xiefrish2021/core/ObjectNBTMapper.java @@ -0,0 +1,91 @@ +package io.github.xiefrish2021.core; + +import io.github.xiefrish2021.ITag; +import io.github.xiefrish2021.tag.*; +import io.github.xiefrish2021.tag.array.ByteArrayTag; +import io.github.xiefrish2021.tag.array.IntArrayTag; +import io.github.xiefrish2021.tag.array.LongArrayTag; +import io.github.xiefrish2021.tag.compound.CompoundTag; +import io.github.xiefrish2021.tag.list.ListTag; +import org.jetbrains.annotations.ApiStatus; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.lang.reflect.RecordComponent; +import java.util.Collection; +import java.util.Map; + +@ApiStatus.Internal +public class ObjectNBTMapper { + private final Object object; + + public ObjectNBTMapper(Object object) { + this.object = object; + } + + public CompoundTag toNBT() { + CompoundTag entries = new CompoundTag(); + Class objectRef = object.getClass(); + try { + if (objectRef.isRecord()) { + for (RecordComponent component : objectRef.getRecordComponents()) { + String key = component.getName(); + Object invoked = objectRef.getMethod(key).invoke(object); + if (invoked != null) { + entries.put(key, writePrimitives(invoked)); + } + } + } else if (isPlainClass(objectRef)){ + for (Field field : objectRef.getFields()) { + String key = field.getName(); + Object invoked = objectRef.getField(key).get(object); + + if (invoked != null) { + entries.put(key, writePrimitives(invoked)); + } + } + } else { + throw new NBTException("This feature can only support simple Java classes and Java record classes."); + } + } catch (Exception e) { + throw new NBTException(e); + } + + return entries; + } + + private static boolean isPlainClass(Class clazz) { + return !clazz.isAnnotation() && !clazz.isInterface() && !clazz.isEnum() && !Modifier.isAbstract(clazz.getModifiers()); + } + + private static ITag writePrimitives(Object object) { + return switch (object) { + case String string -> new StringTag(string); + case Integer integer -> new IntTag(integer); + case Float f -> new FloatTag(f); + case Long l -> new LongTag(l); + case Double d -> new DoubleTag(d); + case Short s -> new ShortTag(s); + case Byte b -> new ByteTag(b); + case byte[] array -> new ByteArrayTag(array); + case int[] array -> new IntArrayTag(array); + case long[] array -> new LongArrayTag(array); + case ITag tag -> tag; + case Collection collection -> { + ListTag listTag = new ListTag(); + for (Object o : collection) { + listTag.add(writePrimitives(o)); + } + + yield listTag; + } + case Boolean bool -> { + if (bool) { + yield new ByteTag((byte) 1); + } yield new ByteTag((byte) 0); + } + case Map ignored -> throw new NBTException("Map type is not supported."); + case Object o -> new ObjectNBTMapper(o).toNBT(); + }; + } +} diff --git a/src/main/java/io/github/xiefrish2021/snbt/SNBTReader.java b/src/main/java/io/github/xiefrish2021/core/SNBTReader.java similarity index 98% rename from src/main/java/io/github/xiefrish2021/snbt/SNBTReader.java rename to src/main/java/io/github/xiefrish2021/core/SNBTReader.java index 1cd9a5b..d3d9e52 100644 --- a/src/main/java/io/github/xiefrish2021/snbt/SNBTReader.java +++ b/src/main/java/io/github/xiefrish2021/core/SNBTReader.java @@ -1,7 +1,6 @@ -package io.github.xiefrish2021.snbt; +package io.github.xiefrish2021.core; import io.github.xiefrish2021.ITag; -import io.github.xiefrish2021.NBTException; import io.github.xiefrish2021.TagType; import io.github.xiefrish2021.tag.ByteTag; import io.github.xiefrish2021.tag.IntTag; @@ -14,10 +13,12 @@ import io.github.xiefrish2021.tag.list.ListTag; import io.github.xiefrish2021.util.CommonUtil; import io.github.xiefrish2021.util.ReaderUtil; +import org.jetbrains.annotations.ApiStatus; import java.util.ArrayList; import java.util.List; +@ApiStatus.Internal public class SNBTReader { private final String snbt; private int readerIndex = 0; @@ -254,7 +255,7 @@ private ITag readListTag() { if (!canRead()) { throw new NBTException("Unexpected end of tag."); } else { - ListTag listtag = new ListTag<>(); + ListTag listtag = new ListTag(); TagType tagtype = null; while(peek() != ']') { diff --git a/src/main/java/io/github/xiefrish2021/object/ObjectToNbt.java b/src/main/java/io/github/xiefrish2021/object/ObjectToNbt.java deleted file mode 100644 index 0ff581d..0000000 --- a/src/main/java/io/github/xiefrish2021/object/ObjectToNbt.java +++ /dev/null @@ -1,109 +0,0 @@ -package io.github.xiefrish2021.object; - -import io.github.xiefrish2021.ITag; -import io.github.xiefrish2021.NBTException; -import io.github.xiefrish2021.tag.*; -import io.github.xiefrish2021.tag.array.ByteArrayTag; -import io.github.xiefrish2021.tag.array.IntArrayTag; -import io.github.xiefrish2021.tag.array.LongArrayTag; -import io.github.xiefrish2021.tag.compound.CompoundTag; -import io.github.xiefrish2021.tag.list.ListTag; - -import java.lang.reflect.Field; -import java.lang.reflect.RecordComponent; -import java.util.Collection; - -public class ObjectToNbt { - private final Object object; - private final CompoundTag entries = new CompoundTag(); - - public ObjectToNbt(Object object) { - this.object = object; - this.serialization(); - } - - private void serialization() { - Class ref = object.getClass(); - if (ref.isRecord()) { - for (RecordComponent component : ref.getRecordComponents()) { - try { - String key = component.getName(); - Object invoke = ref.getMethod(key).invoke(object); - if (invoke != null) { - ITag value = readValue(invoke); - - entries.put(key, value); - } - } catch (Exception e) { - throw new NBTException(e); - } - } - } else { - for (Field field : ref.getFields()) { - try { - Object o = field.get(object); - if (o != null) { - String key = field.getName(); - ITag value = readValue(o); - - entries.put(key, value); - } - } catch (Exception e) { - throw new NBTException(e); - } - } - } - } - - private ITag readValue(Object value) { - switch (value) { - case String string -> { - return new StringTag(string); - } - case Integer integer -> { - return new IntTag(integer); - } - case Float floats -> { - return new FloatTag(floats); - } - case Double doubles -> { - return new DoubleTag(doubles); - } - case Byte bytes -> { - return new ByteTag(bytes); - } - case Short shorts -> { - return new ShortTag(shorts); - } - case Long longs -> { - return new LongTag(longs); - } - case int[] ints -> { - return new IntArrayTag(ints); - } - case byte[] bytes -> { - return new ByteArrayTag(bytes); - } - case long[] longs -> { - return new LongArrayTag(longs); - } - case Collection collection -> { - ListTag tags = new ListTag<>(); - collection.forEach(values -> { - tags.add(readValue(values)); - }); - return tags; - } - case ITag tag -> { - return tag; - } - default -> { - return new ObjectToNbt(object).toNBT(); - } - } - } - - public CompoundTag toNBT() { - return entries; - } -} diff --git a/src/main/java/io/github/xiefrish2021/tag/ByteTag.java b/src/main/java/io/github/xiefrish2021/tag/ByteTag.java index 7b46ef1..66976e8 100644 --- a/src/main/java/io/github/xiefrish2021/tag/ByteTag.java +++ b/src/main/java/io/github/xiefrish2021/tag/ByteTag.java @@ -2,26 +2,13 @@ import io.github.xiefrish2021.ITag; import io.github.xiefrish2021.TagType; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; -public class ByteTag implements ITag { - private Byte value; - - public ByteTag(Byte value) { +public record ByteTag(Byte value) implements ITag { + public ByteTag { if (value == null) { throw new NBTException("The primitive type cannot be null."); } - - this.value = value; - } - - public ByteTag value(Byte value) { - this.value = value; - return this; - } - - public Byte value() { - return value; } @Override diff --git a/src/main/java/io/github/xiefrish2021/tag/DoubleTag.java b/src/main/java/io/github/xiefrish2021/tag/DoubleTag.java index f87694f..cd2936f 100644 --- a/src/main/java/io/github/xiefrish2021/tag/DoubleTag.java +++ b/src/main/java/io/github/xiefrish2021/tag/DoubleTag.java @@ -2,26 +2,13 @@ import io.github.xiefrish2021.ITag; import io.github.xiefrish2021.TagType; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; -public class DoubleTag implements ITag { - private Double value; - - public DoubleTag(Double value) { +public record DoubleTag(Double value) implements ITag { + public DoubleTag { if (value == null) { throw new NBTException("The primitive type cannot be null."); } - - this.value = value; - } - - public DoubleTag value(Double value) { - this.value = value; - return this; - } - - public Double value() { - return value; } @Override diff --git a/src/main/java/io/github/xiefrish2021/tag/FloatTag.java b/src/main/java/io/github/xiefrish2021/tag/FloatTag.java index 0291c66..4e80763 100644 --- a/src/main/java/io/github/xiefrish2021/tag/FloatTag.java +++ b/src/main/java/io/github/xiefrish2021/tag/FloatTag.java @@ -2,27 +2,15 @@ import io.github.xiefrish2021.ITag; import io.github.xiefrish2021.TagType; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; -public class FloatTag implements ITag { - private Float value; - - public FloatTag(Float value) { +public record FloatTag(Float value) implements ITag { + public FloatTag { if (value == null) { throw new NBTException("The primitive type cannot be null."); } - - this.value = value; } - public FloatTag value(Float value) { - this.value = value; - return this; - } - - public Float value() { - return value; - } @Override public TagType type() { return TagType.FLOAT; diff --git a/src/main/java/io/github/xiefrish2021/tag/IntTag.java b/src/main/java/io/github/xiefrish2021/tag/IntTag.java index 252083e..b3b2447 100644 --- a/src/main/java/io/github/xiefrish2021/tag/IntTag.java +++ b/src/main/java/io/github/xiefrish2021/tag/IntTag.java @@ -2,26 +2,13 @@ import io.github.xiefrish2021.ITag; import io.github.xiefrish2021.TagType; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; -public class IntTag implements ITag { - private Integer value; - - public IntTag(Integer value) { +public record IntTag(Integer value) implements ITag { + public IntTag { if (value == null) { throw new NBTException("The primitive type cannot be null."); } - - this.value = value; - } - - public IntTag value(Integer value) { - this.value = value; - return this; - } - - public Integer value() { - return value; } @Override diff --git a/src/main/java/io/github/xiefrish2021/tag/LongTag.java b/src/main/java/io/github/xiefrish2021/tag/LongTag.java index 8d24c5c..d3f55b3 100644 --- a/src/main/java/io/github/xiefrish2021/tag/LongTag.java +++ b/src/main/java/io/github/xiefrish2021/tag/LongTag.java @@ -2,26 +2,13 @@ import io.github.xiefrish2021.ITag; import io.github.xiefrish2021.TagType; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; -public class LongTag implements ITag { - private Long value; - - public LongTag(Long value) { +public record LongTag(Long value) implements ITag { + public LongTag { if (value == null) { throw new NBTException("The primitive type cannot be null."); } - - this.value = value; - } - - public LongTag value(Long value) { - this.value = value; - return this; - } - - public Long value() { - return value; } @Override diff --git a/src/main/java/io/github/xiefrish2021/tag/ShortTag.java b/src/main/java/io/github/xiefrish2021/tag/ShortTag.java index 7199230..e319ffa 100644 --- a/src/main/java/io/github/xiefrish2021/tag/ShortTag.java +++ b/src/main/java/io/github/xiefrish2021/tag/ShortTag.java @@ -2,27 +2,13 @@ import io.github.xiefrish2021.ITag; import io.github.xiefrish2021.TagType; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; -@SuppressWarnings("all") -public class ShortTag implements ITag { - private Short value; - - public ShortTag(Short value) { +public record ShortTag(Short value) implements ITag { + public ShortTag { if (value == null) { throw new NBTException("The primitive type cannot be null."); } - - this.value = value; - } - - public ShortTag value(Short value) { - this.value = value; - return this; - } - - public Short value() { - return value; } @Override diff --git a/src/main/java/io/github/xiefrish2021/tag/StringTag.java b/src/main/java/io/github/xiefrish2021/tag/StringTag.java index ab70bbb..9315221 100644 --- a/src/main/java/io/github/xiefrish2021/tag/StringTag.java +++ b/src/main/java/io/github/xiefrish2021/tag/StringTag.java @@ -2,27 +2,14 @@ import io.github.xiefrish2021.ITag; import io.github.xiefrish2021.TagType; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; -@SuppressWarnings("all") -public class StringTag implements ITag { - private String value; - - public StringTag(String value) { +public record StringTag(String value) implements ITag { + public StringTag { if (value == null) { throw new NBTException("The primitive type cannot be null."); } - this.value = value; - } - - public StringTag value(String value) { - this.value = value; - return this; - } - - public String value() { - return value; } @Override diff --git a/src/main/java/io/github/xiefrish2021/tag/array/ByteArrayTag.java b/src/main/java/io/github/xiefrish2021/tag/array/ByteArrayTag.java index b961641..f8df571 100644 --- a/src/main/java/io/github/xiefrish2021/tag/array/ByteArrayTag.java +++ b/src/main/java/io/github/xiefrish2021/tag/array/ByteArrayTag.java @@ -1,17 +1,12 @@ package io.github.xiefrish2021.tag.array; import io.github.xiefrish2021.ITag; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; import io.github.xiefrish2021.TagType; -import io.github.xiefrish2021.util.CommonUtil; import org.jetbrains.annotations.NotNull; -import java.util.Arrays; import java.util.Iterator; -import java.util.List; -import java.util.Spliterator; import java.util.function.Consumer; -import java.util.stream.Stream; public class ByteArrayTag implements ITag, Iterable { diff --git a/src/main/java/io/github/xiefrish2021/tag/array/IntArrayTag.java b/src/main/java/io/github/xiefrish2021/tag/array/IntArrayTag.java index a4f10d8..38ed492 100644 --- a/src/main/java/io/github/xiefrish2021/tag/array/IntArrayTag.java +++ b/src/main/java/io/github/xiefrish2021/tag/array/IntArrayTag.java @@ -1,9 +1,8 @@ package io.github.xiefrish2021.tag.array; import io.github.xiefrish2021.ITag; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; import io.github.xiefrish2021.TagType; -import io.github.xiefrish2021.util.CommonUtil; import org.jetbrains.annotations.NotNull; import java.util.Iterator; diff --git a/src/main/java/io/github/xiefrish2021/tag/array/LongArrayTag.java b/src/main/java/io/github/xiefrish2021/tag/array/LongArrayTag.java index b27a3d0..b386861 100644 --- a/src/main/java/io/github/xiefrish2021/tag/array/LongArrayTag.java +++ b/src/main/java/io/github/xiefrish2021/tag/array/LongArrayTag.java @@ -1,7 +1,7 @@ package io.github.xiefrish2021.tag.array; import io.github.xiefrish2021.ITag; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; import io.github.xiefrish2021.TagType; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/io/github/xiefrish2021/tag/compound/NBTElement.java b/src/main/java/io/github/xiefrish2021/tag/compound/NBTElement.java index aec09c3..ad00a9f 100644 --- a/src/main/java/io/github/xiefrish2021/tag/compound/NBTElement.java +++ b/src/main/java/io/github/xiefrish2021/tag/compound/NBTElement.java @@ -1,6 +1,6 @@ package io.github.xiefrish2021.tag.compound; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; import io.github.xiefrish2021.tag.array.ByteArrayTag; import io.github.xiefrish2021.tag.array.IntArrayTag; import io.github.xiefrish2021.tag.array.LongArrayTag; @@ -9,10 +9,9 @@ import io.github.xiefrish2021.ITag; public record NBTElement(ITag tag) { - @SuppressWarnings({"unchecked", "unused"}) - public ListTag getAsList( Class type) { + public ListTag getAsList() { try { - return (ListTag) tag; + return (ListTag) tag; } catch (ClassCastException exception) { throw new NBTException("Value is not a list"); } diff --git a/src/main/java/io/github/xiefrish2021/tag/list/ListTag.java b/src/main/java/io/github/xiefrish2021/tag/list/ListTag.java index 210ad37..e403a7e 100644 --- a/src/main/java/io/github/xiefrish2021/tag/list/ListTag.java +++ b/src/main/java/io/github/xiefrish2021/tag/list/ListTag.java @@ -1,38 +1,39 @@ package io.github.xiefrish2021.tag.list; import org.jetbrains.annotations.NotNull; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; import io.github.xiefrish2021.ITag; import io.github.xiefrish2021.TagType; import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import java.util.Spliterator; import java.util.function.Consumer; -public class ListTag implements Iterable, ITag { - private final java.util.List list = new ArrayList<>(); +public class ListTag implements Iterable, ITag { + private final List list = new ArrayList<>(); - public ListTag add(V value) { + public ListTag add(ITag value) { list.add(value); return this; } - public ListTag addAll(ListTag value) { - for (V v : value) { + public ListTag addAll(ListTag value) { + for (ITag v : value) { list.add(v); } return this; } - public ListTag remove(V value) { + public ListTag remove(ITag value) { list.remove(value); return this; } - public V get(int index) { + public ITag get(int index) { return list.get(index); } @@ -44,11 +45,11 @@ public boolean isEmpty() { return list.isEmpty(); } - public boolean contains(V value) { + public boolean contains(ITag value) { return list.contains(value); } - public V getFirst() { + public ITag getFirst() { if (list.isEmpty()) { throw new NBTException("This list is empty."); } @@ -56,29 +57,29 @@ public V getFirst() { return get(0); } - public ListTag set(int index, V value) { + public ListTag set(int index, ITag value) { list.set(index, value); return this; } - public V getLast() { + public ITag getLast() { return get(size()); } @Override - public @NotNull Iterator iterator() { + public @NotNull Iterator iterator() { return list.iterator(); } @Override - public void forEach(Consumer action) { - for (V v : this) { + public void forEach(Consumer action) { + for (ITag v : this) { action.accept(v); } } @Override - public Spliterator spliterator() { + public Spliterator spliterator() { return list.spliterator(); } @@ -92,7 +93,7 @@ public String toString() { StringBuilder builder = new StringBuilder(); builder.append("["); int i = 0; - for (V v : this) { + for (ITag v : this) { builder.append(v.toString()); if (i < list.size() - 1) { builder.append(", "); diff --git a/src/main/java/io/github/xiefrish2021/util/ReaderUtil.java b/src/main/java/io/github/xiefrish2021/util/ReaderUtil.java index aa65375..bc9cbcf 100644 --- a/src/main/java/io/github/xiefrish2021/util/ReaderUtil.java +++ b/src/main/java/io/github/xiefrish2021/util/ReaderUtil.java @@ -1,11 +1,12 @@ package io.github.xiefrish2021.util; import io.github.xiefrish2021.TagType; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; import io.github.xiefrish2021.tag.*; import io.github.xiefrish2021.tag.array.ByteArrayTag; import io.github.xiefrish2021.tag.array.IntArrayTag; import io.github.xiefrish2021.tag.array.LongArrayTag; +import io.github.xiefrish2021.tag.compound.NBTElement; import org.jetbrains.annotations.NotNull; import io.github.xiefrish2021.tag.compound.CompoundTag; import io.github.xiefrish2021.tag.list.ListTag; @@ -13,6 +14,10 @@ import java.io.DataInput; import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Collection; +import java.util.Collections; +import java.util.List; import java.util.regex.Pattern; public class ReaderUtil { @@ -118,11 +123,11 @@ private static ITag readValue(DataInput in, TagType type) throws IOException { }; } - private static @NotNull ListTag readList(DataInput in) throws IOException { - ListTag list = new ListTag<>(); + private static @NotNull ListTag readList(DataInput in) throws IOException { + ListTag list = new ListTag(); TagType type0 = readType(in); if (type0 == TagType.END) { - return new ListTag<>(); + return new ListTag(); } int length = in.readInt(); diff --git a/src/main/java/io/github/xiefrish2021/util/WriteUtil.java b/src/main/java/io/github/xiefrish2021/util/WriteUtil.java index 0e101ff..de58dfc 100644 --- a/src/main/java/io/github/xiefrish2021/util/WriteUtil.java +++ b/src/main/java/io/github/xiefrish2021/util/WriteUtil.java @@ -1,10 +1,11 @@ package io.github.xiefrish2021.util; import io.github.xiefrish2021.TagType; +import io.github.xiefrish2021.core.ObjectNBTMapper; import io.github.xiefrish2021.tag.array.ByteArrayTag; import io.github.xiefrish2021.tag.array.IntArrayTag; import io.github.xiefrish2021.tag.array.LongArrayTag; -import io.github.xiefrish2021.NBTException; +import io.github.xiefrish2021.core.NBTException; import io.github.xiefrish2021.tag.*; import io.github.xiefrish2021.ITag; import io.github.xiefrish2021.tag.compound.CompoundTag; @@ -34,10 +35,10 @@ public static void writeCompound(CompoundTag compound, DataOutput output) throws writeType(TagType.END, output); } - private static void writeList(ListTag list, DataOutput output) throws Exception { + private static void writeList(ListTag list, DataOutput output) throws Exception { writeType(list.getFirst().type(), output); output.writeInt(list.size()); - for (V tag : list) { + for (ITag tag : list) { writeValue(tag, output); } } @@ -45,7 +46,7 @@ private static void writeList(ListTag list, DataOutput outpu private static void writeValue(ITag tag, DataOutput output) throws Exception { switch (tag) { case CompoundTag compound -> writeCompound(compound, output); - case ListTag list -> { + case ListTag list -> { if (list.isEmpty()) { writeType(TagType.END, output); output.writeInt(0); diff --git a/src/test/java/me/coderfrish/test/NBTest.java b/src/test/java/me/coderfrish/test/NBTest.java index 801a324..78414ab 100644 --- a/src/test/java/me/coderfrish/test/NBTest.java +++ b/src/test/java/me/coderfrish/test/NBTest.java @@ -1,5 +1,6 @@ package me.coderfrish.test; +import io.github.xiefrish2021.core.ObjectNBTMapper; import io.github.xiefrish2021.tag.IntTag; import io.github.xiefrish2021.tag.array.ByteArrayTag; import io.github.xiefrish2021.tag.compound.CompoundTag; @@ -7,10 +8,12 @@ import io.github.xiefrish2021.NBT; import me.coderfrish.test.bean.TestBean; import me.coderfrish.test.bean.TestBean1; +import me.coderfrish.test.bean.TestBean2; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; public class NBTest { public static void main(String[] args) { @@ -25,11 +28,17 @@ public static void test() { // tag.put("sfsdfs", new IntTag(4654)); // tag.put("he", new ByteArrayTag("sss".getBytes(StandardCharsets.UTF_8))); + TestBean2 testBean2 = new TestBean2(); + testBean2.age = 10; + testBean2.name = "test"; + testBean2.phone = 416546; + testBean2.isGuangdong = true; try { - NBT.writeNamedNBT("ss", new TestBean("Frish", 15, 114514, new TestBean1("Coderfrish", 15, 666)), new FileOutputStream("D:\\NBT\\src\\test\\resources\\test.nbt")); + NBT.writeNamedNBT("ss", testBean2, new FileOutputStream("D:\\NBT\\src\\test\\resources\\test.nbt")); } catch (FileNotFoundException e) { throw new RuntimeException(e); } + // // tag.put("test1", tag1); // diff --git a/src/test/java/me/coderfrish/test/bean/TestBean1.java b/src/test/java/me/coderfrish/test/bean/TestBean1.java index d503908..08a857c 100644 --- a/src/test/java/me/coderfrish/test/bean/TestBean1.java +++ b/src/test/java/me/coderfrish/test/bean/TestBean1.java @@ -1,4 +1,4 @@ package me.coderfrish.test.bean; -public record TestBean1(String name, int age, int phone) { +public record TestBean1(String name, int age, int phone, TestBean1 bean) { } diff --git a/src/test/java/me/coderfrish/test/bean/TestBean2.java b/src/test/java/me/coderfrish/test/bean/TestBean2.java new file mode 100644 index 0000000..c84fd3c --- /dev/null +++ b/src/test/java/me/coderfrish/test/bean/TestBean2.java @@ -0,0 +1,10 @@ +package me.coderfrish.test.bean; + +import java.util.List; + +public class TestBean2 { + public String name; + public int age; + public int phone; + public boolean isGuangdong; +} diff --git a/src/test/resources/test.nbt b/src/test/resources/test.nbt index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..9485be030352973430eb77552467bf2d1ed57bd5 100644 GIT binary patch literal 56 zcmd;LU@9)=U|`8h%uQurDM>9ZVP;@XOiyKCVBlhAU@geV&r4-si&kP};La>|FHOu# LPsz_qXJh~XP#+A* literal 0 HcmV?d00001