diff --git a/.gitignore b/.gitignore index 409eeab..fcbc7bc 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ out/ ### VS Code ### .vscode/ + +### kotlin ### +.kotlin/ diff --git a/CHANGES.md b/CHANGES.md new file mode 100644 index 0000000..d4f7295 --- /dev/null +++ b/CHANGES.md @@ -0,0 +1,22 @@ +# 版本更新 (3.0.0开始算起): + +### 3.3.0 - 更新 + - 修改了部分代码 + - `get(String)Lio/github/xiefrish2021/api/ITag`方法修改成 `get(String)Lio/github/xiefrish2021/compound/NBTElement` + - 添加了一些kotlin成分:如委托,语法糖 + +#### 3.2.0 - 更新 + - 删除或合并了一些不必要的接口 + - `NBT.newInstance()`用法更改为 `NBT.getInstance()` + +#### 3.1.0 - 更新 + - 修改了SNBTReader里面readArray的会报unchecked的某条代码 + - 把Compound,Array等等的接口迁移到了`xyz.frish2021.nbt.api`包 + - 修改了NBT类的基本用法由`new NBT()` 修改成`NBT.newInstance()` 并且添加了单例模式 + - `NBT.newInstance()` 添加了synchronized关键字以防止多次初始化影响线程安全 + - 修复了生成IntArray和ByteArray的SNBT却生成成了List的SNBT。 + +#### 3.0.0 - 更新 + - 重写了库的代码 + - 允许通过Compound实例生成SNBT + - diff --git a/README.md b/README.md index 1c8e8b5..04fbead 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,7 @@ NBT(全称:二进制命名标签(`N`amed`B`inary `T`ags))\ 是Minecraft游戏存档及一些游戏数据的存储格式。\ -作者:Frish2021 - -## (0) 3.2.0版本 - 更新内容 - - 删除或合并了一些不必要的接口 - - `NBT.newInstance()`用法更改为 `NBT.getInstance()` +版本更新:[CHANGES.md](CHANGES.md) ## (1) 用法 Maven @@ -92,19 +88,53 @@ public class NBTest { ### 读操作(NBT) #### 源码 +java + ```java public class NBTest { public static void main(String[] args) { NBT nbt = new NBT.getInstance(); - File output = new File("你要读取的NBT文件"); - - CompoundTag compound = nbt.readUnnamedNBT(output); - System.out.println(compound.getString("test")); - System.out.println(compound.getCompound("test1").getInt("test3")); + + try (InputStream is = new FileInputStream("你要读取的NBT文件")) { + CompoundTag compound = nbt.readUnnamedNBT(is); + System.out.println(compound.getString("test")); + System.out.println(compound.getCompound("test1").getInt("test3")); + } catch (Exception e) { + throw new RuntimeException(e); + } } } ``` +kotlin (通过`compound[key]`方法) + +```kotlin +fun main() { + val nbt = NBT.getInstance() + val compound = nbt.readUnnamedNBT(FileOutputStream("你要读取的NBT文件")) + + println(compound["test"].asString()); + println(compound["test1"].asCompound()["test3"].asInt()); +} +``` + +kotlin (通过委托方法) + +```kotlin +fun main() { + val nbt = NBT.getInstance() + val compound = nbt.readUnnamedNBT(FileOutputStream("你要读取的NBT文件")) + + val test: NBTElement by compound + + val test1: NBTElement by compound + val test3: NBTElement by test1.asCompound() + + println(test.asString()); + println(test3.asInt()); +} +``` + 被读取的NBT文件类容 ```nbtt "": { @@ -125,6 +155,7 @@ Frish2021 ### 读操作(SNBT) #### 源码 +java ```java public class NBTest { public static void main(String[] args) { @@ -136,14 +167,17 @@ public class NBTest { } ``` +kotlin 同理 + #### 效果 控制台输出 ``` Frish2021 ``` -### 新特性 - 生成SNBT +### 生成SNBT #### 源码 +java ```java public class NBTest { public static void main(String[] args) { @@ -161,6 +195,7 @@ public class NBTest { } } ``` +kotlin同理 #### 效果 控制台输出 @@ -174,17 +209,6 @@ public class NBTest { 邮件:`1573880184@qq.com` 如果有BUG,请发布Issues. -## (3) 历史更新 (3.0.0开始算起): -#### 3.0.0 - 更新 - - 重写了库的代码 - - 允许通过Compound实例生成SNBT -#### 3.1.0 - 更新 - - 修改了SNBTReader里面readArray的会报unchecked的某条代码 - - 把Compound,Array等等的接口迁移到了`xyz.frish2021.nbt.api`包 - - 修改了NBT类的基本用法由`new NBT()` 修改成`NBT.newInstance()` 并且添加了单例模式 - - `NBT.newInstance()` 添加了synchronized关键字以防止多次初始化影响线程安全 - - 修复了生成IntArray和ByteArray的SNBT却生成成了List的SNBT。 - -## (4) 最后 +## (3) 最后 该NBT库的其他用法就靠你自己发掘把 :)\ 那些比如IntArray,ByteArray,List可以自己看test模块或者源码。 diff --git a/build.gradle.kts b/build.gradle.kts index dbed9c0..3517b45 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,11 +1,12 @@ plugins { id("java") id("maven-publish") + kotlin("jvm") version "2.0.0" id("com.github.johnrengelman.shadow") version "8.1.1" } group = "io.github.xiefrish2021" -version = "3.2.0" +version = "3.3.0" repositories { mavenCentral() diff --git a/settings.gradle.kts b/settings.gradle.kts index 9bc65df..af9be77 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,2 +1,10 @@ +pluginManagement { + repositories { + gradlePluginPortal() + mavenCentral() + mavenLocal() + } +} + rootProject.name = "NBT" diff --git a/src/main/java/io/github/xiefrish2021/api/Compound.java b/src/main/java/io/github/xiefrish2021/api/Compound.java index 05e1d39..33d1a7e 100644 --- a/src/main/java/io/github/xiefrish2021/api/Compound.java +++ b/src/main/java/io/github/xiefrish2021/api/Compound.java @@ -1,12 +1,8 @@ package io.github.xiefrish2021.api; -import io.github.xiefrish2021.array.ByteArrayTag; -import io.github.xiefrish2021.array.IntArrayTag; -import io.github.xiefrish2021.array.LongArrayTag; -import io.github.xiefrish2021.compound.CompoundTag; -import io.github.xiefrish2021.primitive.PrimitiveTag; +import io.github.xiefrish2021.compound.Getter; -public interface Compound extends Iterable, ITag { +public interface Compound extends Iterable, ITag, Getter { Compound put(String key, ITag value); Compound putAll(Compound compound); @@ -29,36 +25,6 @@ public interface Compound extends Iterable, ITag { int size(); - ITag get(String key); - - ITag get(String key, ITag defaultValue); - - PrimitiveTag getPrimitive(String key); - - String getString(String key); - - int getInt(String key); - - float getFloat(String key); - - double getDouble(String key); - - byte getByte(String key); - - long getLong(String key); - - short getShort(String key); - - List getList(String key); - - ByteArrayTag getByteArray(String key); - - IntArrayTag getIntArray(String key); - - LongArrayTag getLongArray(String key); - - CompoundTag getCompound(String key); - interface Entry { String key(); diff --git a/src/main/java/io/github/xiefrish2021/compound/CompoundTag.java b/src/main/java/io/github/xiefrish2021/compound/CompoundTag.java index 53e6845..bc210f2 100644 --- a/src/main/java/io/github/xiefrish2021/compound/CompoundTag.java +++ b/src/main/java/io/github/xiefrish2021/compound/CompoundTag.java @@ -1,15 +1,11 @@ package io.github.xiefrish2021.compound; -import io.github.xiefrish2021.primitive.number.*; +import kotlin.reflect.KProperty; import org.jetbrains.annotations.NotNull; import io.github.xiefrish2021.api.Compound; -import io.github.xiefrish2021.array.ByteArrayTag; -import io.github.xiefrish2021.array.IntArrayTag; -import io.github.xiefrish2021.array.LongArrayTag; -import io.github.xiefrish2021.primitive.PrimitiveTag; -import io.github.xiefrish2021.primitive.StringTag; import io.github.xiefrish2021.api.ITag; import io.github.xiefrish2021.tag.TagType; +import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.function.Consumer; @@ -76,13 +72,13 @@ public ITag value() { } @Override - public Compound put(String key, ITag value) { + public @NotNull Compound put(@NotNull String key, @NotNull ITag value) { compounds.put(key, value); return this; } @Override - public Compound putAll(Compound compound) { + public @NotNull Compound putAll(@NotNull Compound compound) { for (Entry entry : this) { if (compound.containsKey(entry.key())) { replace(entry.key(), entry.value()); @@ -96,7 +92,7 @@ public Compound putAll(Compound compound) { @Override public Compound remove(String key) { - this.remove(key, get(key)); + this.remove(key, get(key).asTag()); return this; } @@ -114,85 +110,18 @@ public Compound replace(String key, ITag oldValue, ITag newValue) { @Override public Compound replace(String key, ITag newValue) { - this.replace(key, get(key), newValue); + this.replace(key, get(key).asTag(), newValue); return this; } @Override - public ITag get(String key) { - return compounds.get(key); + public @NotNull NBTElement get(@NotNull String key) { + return new NBTElement(compounds.get(key)); } @Override - public ITag get(String key, ITag defaultValue) { - return compounds.getOrDefault(key, defaultValue); - } - - @Override - @SuppressWarnings("unchecked") - public PrimitiveTag getPrimitive(String key) { - return (PrimitiveTag) get(key); - } - - @Override - public String getString(String key) { - return ((StringTag) get(key)).value(); - } - - @Override - public int getInt(String key) { - return ((IntTag) get(key)).value(); - } - - @Override - public float getFloat(String key) { - return ((FloatTag) get(key)).value(); - } - - @Override - public double getDouble(String key) { - return ((DoubleTag) get(key)).value(); - } - - @Override - public byte getByte(String key) { - return ((ByteTag) get(key)).value(); - } - - @Override - public long getLong(String key) { - return ((LongTag) get(key)).value(); - } - - @Override - public short getShort(String key) { - return ((ShortTag) get(key)).value(); - } - - @Override - @SuppressWarnings("unchecked") - public io.github.xiefrish2021.api.List getList(String key) { - return ((io.github.xiefrish2021.api.List) get(key)); - } - - @Override - public ByteArrayTag getByteArray(String key) { - return (ByteArrayTag) get(key); - } - - @Override - public IntArrayTag getIntArray(String key) { - return (IntArrayTag) get(key); - } - - @Override - public LongArrayTag getLongArray(String key) { - return (LongArrayTag) get(key); - } - - @Override - public CompoundTag getCompound(String key) { - return ((CompoundTag) get(key)); + public @NotNull NBTElement get(@NotNull String key, @NotNull ITag defaultValue) { + return new NBTElement(compounds.getOrDefault(key, defaultValue)); } @Override @@ -245,4 +174,9 @@ public String toString() { return builder.toString(); } + + @Override + public @NotNull NBTElement getValue(@Nullable Void nothing, @NotNull KProperty property) { + return get(property.getName()); + } } diff --git a/src/main/java/io/github/xiefrish2021/tag/TagType.java b/src/main/java/io/github/xiefrish2021/tag/TagType.java index ea0ff0c..457e590 100644 --- a/src/main/java/io/github/xiefrish2021/tag/TagType.java +++ b/src/main/java/io/github/xiefrish2021/tag/TagType.java @@ -14,8 +14,4 @@ public enum TagType { COMPOUND, INT_ARRAY, LONG_ARRAY; - - public int id() { - return ordinal(); - } } diff --git a/src/main/kotlin/io/github/xiefrish2021/compound/Getter.kt b/src/main/kotlin/io/github/xiefrish2021/compound/Getter.kt new file mode 100644 index 0000000..477c9ca --- /dev/null +++ b/src/main/kotlin/io/github/xiefrish2021/compound/Getter.kt @@ -0,0 +1,12 @@ +package io.github.xiefrish2021.compound + +import io.github.xiefrish2021.api.ITag +import kotlin.reflect.KProperty + +interface Getter { + operator fun get(key: String): NBTElement + + fun get(key: String, defaultValue: ITag): NBTElement + + operator fun getValue(nothing: Nothing?, property: KProperty<*>): NBTElement +} diff --git a/src/main/kotlin/io/github/xiefrish2021/compound/NBTElement.kt b/src/main/kotlin/io/github/xiefrish2021/compound/NBTElement.kt new file mode 100644 index 0000000..f5f4120 --- /dev/null +++ b/src/main/kotlin/io/github/xiefrish2021/compound/NBTElement.kt @@ -0,0 +1,122 @@ +package io.github.xiefrish2021.compound + +import io.github.xiefrish2021.api.ITag +import io.github.xiefrish2021.api.List +import io.github.xiefrish2021.array.ByteArrayTag +import io.github.xiefrish2021.array.IntArrayTag +import io.github.xiefrish2021.array.LongArrayTag +import io.github.xiefrish2021.list.ListTag +import io.github.xiefrish2021.primitive.PrimitiveTag +import io.github.xiefrish2021.primitive.StringTag +import io.github.xiefrish2021.primitive.number.* + +@Suppress("unused") +class NBTElement(val tag: ITag) { + fun asPrimitive(): PrimitiveTag<*> { + if (tag is PrimitiveTag<*>) { + return tag + } + + return null!! + } + + fun asString(): String { + if (tag is StringTag) { + return tag.value() + } + + return null!! + } + + fun asInt(): Int { + if (tag is IntTag) { + return tag.value() + } + + return null!! + } + + fun asFloat(): Float { + if (tag is FloatTag) { + return tag.value() + } + + return null!! + } + + fun asDouble(): Double { + if (tag is DoubleTag) { + return tag.value() + } + + return null!! + } + + fun asByte(): Byte { + if (tag is ByteTag) { + return tag.value() + } + + return null!! + } + + fun asLong(): Long { + if (tag is LongTag) { + return tag.value() + } + + return null!! + } + + fun asShort(): Short { + if (tag is ShortTag) { + return tag.value() + } + + return null!! + } + + fun asList(): List<*> { + if (tag is ListTag<*>) { + return tag + } + + return null!! + } + + fun asByteArray(): ByteArrayTag { + if (tag is ByteArrayTag) { + return tag + } + + return null!! + } + + fun asIntArray(): IntArrayTag { + if (tag is IntArrayTag) { + return tag + } + + return null!! + } + + fun asLongArray(): LongArrayTag { + if (tag is LongArrayTag) { + return tag + } + + return null!! + } + + fun asCompound(): CompoundTag { + if (tag is CompoundTag) { + return tag + } + + return null!! + } + + fun asTag(): ITag { + return tag + } +} diff --git a/src/test/java/me/coderfrish/test/ItrorTets.java b/src/test/java/me/coderfrish/test/ItrorTets.java deleted file mode 100644 index 106d612..0000000 --- a/src/test/java/me/coderfrish/test/ItrorTets.java +++ /dev/null @@ -1,35 +0,0 @@ -package me.coderfrish.test; - -import java.util.Iterator; - -public class ItrorTets { - public static void main(String[] args) { - int[] ints = {0, 50, 60, 70, 90}; - Itor itor = new Itor(ints); - - while (itor.hasNext()) { - System.out.println(itor.next()); - } - } - - public static class Itor implements Iterator { - private final int[] array; - private int index = 0; - - public Itor(int[] array) { - this.array = array; - } - - @Override - public boolean hasNext() { - return index < array.length; - } - - @Override - public Integer next() { - Integer value = array[index]; - index++; - return value; - } - } -} diff --git a/src/test/kotlin/me/coderfrish/test/KotlinTest.kt b/src/test/kotlin/me/coderfrish/test/KotlinTest.kt new file mode 100644 index 0000000..4246c52 --- /dev/null +++ b/src/test/kotlin/me/coderfrish/test/KotlinTest.kt @@ -0,0 +1,22 @@ +package me.coderfrish.test + +import io.github.xiefrish2021.api.NBT +import io.github.xiefrish2021.compound.CompoundTag +import io.github.xiefrish2021.compound.NBTElement +import io.github.xiefrish2021.primitive.StringTag +import java.io.FileInputStream +import java.io.FileOutputStream + +fun main() { + val nbt = NBT.getInstance() +// val compound = CompoundTag() + +// compound.put("hello", StringTag("Frish2021")) +// nbt.writeUnnamedNBT(compound, FileOutputStream("D:\\NBT\\src\\test\\resources\\test1.nbt")) + + val compound = nbt.readUnnamedNBT(FileInputStream("D:\\NBT\\src\\test\\resources\\test1.nbt")) +// println(compound["hello"].asString()) + val hello: NBTElement by compound + println(hello.asString()) +// println(hello) +} diff --git a/src/test/resources/test1.nbt b/src/test/resources/test1.nbt new file mode 100644 index 0000000..d896499 Binary files /dev/null and b/src/test/resources/test1.nbt differ