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.0.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderFrish committed Dec 31, 2024
1 parent b4c9fab commit 17e8292
Show file tree
Hide file tree
Showing 13 changed files with 360 additions and 254 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 版本更新 (3.0.0开始算起):

### 4.0.0 - 更新
- 添加了Java Bean转NBT的支持(不稳定)
- 修改了部分代码

### 3.4.0 - 更新
- 移除了kotlin委托
- 正式发布了3.4.0版本
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 = "3.4.0"
version = "4.0.0"

repositories {
mavenCentral()
Expand Down
49 changes: 27 additions & 22 deletions src/main/java/io/github/xiefrish2021/NBT.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.github.xiefrish2021;

import io.github.xiefrish2021.tag.compound.CompoundTag;
import io.github.xiefrish2021.object.ObjectToNbt;
import io.github.xiefrish2021.snbt.SNBTReader;
import io.github.xiefrish2021.tag.compound.CompoundTag;
import io.github.xiefrish2021.util.ReaderUtil;
import io.github.xiefrish2021.util.WriteUtil;

Expand All @@ -12,14 +13,33 @@
*/
@SuppressWarnings("unused")
public final class NBT {
private static NBT instance;
/**
* @param name NBT name.
* @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
public static void writeNamedNBT(String name, Object object, OutputStream out) {
writeNamedNBT(name, new ObjectToNbt(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
public static void writeUnnamedNBT(Object object, OutputStream out) {
writeUnnamedNBT(new ObjectToNbt(object).toNBT(), out);
}

/**
* @param name NBT name.
* @param out NBT file output stream.
* @param tag NBT compound tag.
*/
public void writeNamedNBT(String name, CompoundTag tag, OutputStream out) {
public static void writeNamedNBT(String name, CompoundTag tag, OutputStream out) {
try(DataOutputStream buffer = new DataOutputStream(out)) {
WriteUtil.writeType(tag.type(), buffer);
WriteUtil.writeString(name, buffer);
Expand All @@ -33,14 +53,14 @@ public void writeNamedNBT(String name, CompoundTag tag, OutputStream out) {
* @param out NBT file output stream.
* @param tag NBT compound tag.
*/
public void writeUnnamedNBT(CompoundTag tag, OutputStream out) {
public static void writeUnnamedNBT(CompoundTag tag, OutputStream out) {
writeNamedNBT("", tag, out);
}

/**
* @param tag NBT compound tag.
*/
public String generateSNBT(CompoundTag tag) {
public static String generateSNBT(CompoundTag tag) {
try {
return tag.toString();
} catch (Exception e) {
Expand All @@ -51,7 +71,7 @@ public String generateSNBT(CompoundTag tag) {
/**
* @param in NBT file input stream.
*/
public CompoundTag readUnnamedNBT(InputStream in) {
public static CompoundTag readUnnamedNBT(InputStream in) {
try(DataInputStream buffer = new DataInputStream(in)) {
if (ReaderUtil.readType(buffer) != TagType.COMPOUND) {
throw new NBTException("Invalid nbt.");
Expand All @@ -67,26 +87,11 @@ public CompoundTag readUnnamedNBT(InputStream in) {
/**
* @param snbt SNBT input.
*/
public CompoundTag readUnnamedSNBT(String snbt) {
public static CompoundTag readUnnamedSNBT(String snbt) {
try {
return new SNBTReader(snbt).parserSNBT();
} catch (Exception e) {
throw new NBTException(e);
}
}

/**
* New a NBT instance.
*/
public synchronized static NBT getInstance() {
try {
if (instance == null) {
instance = new NBT();
}

return instance;
} catch (Exception e) {
throw new NBTException(e);
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/io/github/xiefrish2021/Preview.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.xiefrish2021;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.METHOD})
public @interface Preview {
}
109 changes: 109 additions & 0 deletions src/main/java/io/github/xiefrish2021/object/ObjectToNbt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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<ITag> 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;
}
}
4 changes: 0 additions & 4 deletions src/main/java/io/github/xiefrish2021/package-info.java

This file was deleted.

Loading

0 comments on commit 17e8292

Please sign in to comment.