Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

Commit

Permalink
Update to 3.3.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderFrish committed Dec 15, 2024
1 parent b906438 commit 4970d95
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 180 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ out/

### VS Code ###
.vscode/

### kotlin ###
.kotlin/
22 changes: 22 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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<V extends ITag>等等的接口迁移到了`xyz.frish2021.nbt.api`
- 修改了NBT类的基本用法由`new NBT()` 修改成`NBT.newInstance()` 并且添加了单例模式
- `NBT.newInstance()` 添加了synchronized关键字以防止多次初始化影响线程安全
- 修复了生成IntArray和ByteArray的SNBT却生成成了List的SNBT。

#### 3.0.0 - 更新
- 重写了库的代码
- 允许通过Compound实例生成SNBT
-
70 changes: 47 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
"": {
Expand All @@ -125,6 +155,7 @@ Frish2021

### 读操作(SNBT)
#### 源码
java
```java
public class NBTest {
public static void main(String[] args) {
Expand All @@ -136,14 +167,17 @@ public class NBTest {
}
```

kotlin 同理

#### 效果
控制台输出
```
Frish2021
```

### 新特性 - 生成SNBT
### 生成SNBT
#### 源码
java
```java
public class NBTest {
public static void main(String[] args) {
Expand All @@ -161,6 +195,7 @@ public class NBTest {
}
}
```
kotlin同理

#### 效果
控制台输出
Expand All @@ -174,17 +209,6 @@ public class NBTest {
邮件:`[email protected]`
如果有BUG,请发布Issues.

## (3) 历史更新 (3.0.0开始算起):
#### 3.0.0 - 更新
- 重写了库的代码
- 允许通过Compound实例生成SNBT
#### 3.1.0 - 更新
- 修改了SNBTReader里面readArray的会报unchecked的某条代码
- 把Compound,Array<V extends ITag>等等的接口迁移到了`xyz.frish2021.nbt.api`
- 修改了NBT类的基本用法由`new NBT()` 修改成`NBT.newInstance()` 并且添加了单例模式
- `NBT.newInstance()` 添加了synchronized关键字以防止多次初始化影响线程安全
- 修复了生成IntArray和ByteArray的SNBT却生成成了List的SNBT。

## (4) 最后
## (3) 最后
该NBT库的其他用法就靠你自己发掘把 :)\
那些比如IntArray,ByteArray,List可以自己看test模块或者源码。
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
8 changes: 8 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
mavenLocal()
}
}

rootProject.name = "NBT"

38 changes: 2 additions & 36 deletions src/main/java/io/github/xiefrish2021/api/Compound.java
Original file line number Diff line number Diff line change
@@ -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<Compound.Entry>, ITag {
public interface Compound extends Iterable<Compound.Entry>, ITag, Getter {
Compound put(String key, ITag value);

Compound putAll(Compound compound);
Expand All @@ -29,36 +25,6 @@ public interface Compound extends Iterable<Compound.Entry>, ITag {

int size();

ITag get(String key);

ITag get(String key, ITag defaultValue);

<V> PrimitiveTag<V> 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);

<V extends ITag> List<V> getList(String key);

ByteArrayTag getByteArray(String key);

IntArrayTag getIntArray(String key);

LongArrayTag getLongArray(String key);

CompoundTag getCompound(String key);

interface Entry {
String key();

Expand Down
96 changes: 15 additions & 81 deletions src/main/java/io/github/xiefrish2021/compound/CompoundTag.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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;
}

Expand All @@ -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 <V> PrimitiveTag<V> getPrimitive(String key) {
return (PrimitiveTag<V>) 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 <V extends ITag> io.github.xiefrish2021.api.List<V> getList(String key) {
return ((io.github.xiefrish2021.api.List<V>) 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
Expand Down Expand Up @@ -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());
}
}
4 changes: 0 additions & 4 deletions src/main/java/io/github/xiefrish2021/tag/TagType.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@ public enum TagType {
COMPOUND,
INT_ARRAY,
LONG_ARRAY;

public int id() {
return ordinal();
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/io/github/xiefrish2021/compound/Getter.kt
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 4970d95

Please sign in to comment.