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

Commit

Permalink
Update to 4.1.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderFrish committed Dec 31, 2024
1 parent 17e8292 commit 5b59d77
Show file tree
Hide file tree
Showing 26 changed files with 191 additions and 281 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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的支持(不稳定)
- 修改了部分代码
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "io.github.xiefrish2021"
version = "4.0.0"
version = "4.1.0"

repositories {
mavenCentral()
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/io/github/xiefrish2021/NBT.java
Original file line number Diff line number Diff line change
@@ -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.*;

Expand All @@ -19,19 +21,19 @@ 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);
}

/**
* @param out NBT file output stream.
* @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);
}

/**
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/io/github/xiefrish2021/Preview.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.xiefrish2021;
package io.github.xiefrish2021.core;

public class NBTException extends RuntimeException {
public NBTException(String message) {
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/io/github/xiefrish2021/core/ObjectNBTMapper.java
Original file line number Diff line number Diff line change
@@ -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();
};
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -254,7 +255,7 @@ private ITag readListTag() {
if (!canRead()) {
throw new NBTException("Unexpected end of tag.");
} else {
ListTag<ITag> listtag = new ListTag<>();
ListTag listtag = new ListTag();
TagType tagtype = null;

while(peek() != ']') {
Expand Down
109 changes: 0 additions & 109 deletions src/main/java/io/github/xiefrish2021/object/ObjectToNbt.java

This file was deleted.

19 changes: 3 additions & 16 deletions src/main/java/io/github/xiefrish2021/tag/ByteTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/io/github/xiefrish2021/tag/DoubleTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 3 additions & 15 deletions src/main/java/io/github/xiefrish2021/tag/FloatTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 5b59d77

Please sign in to comment.