Skip to content

Commit 9d904a5

Browse files
author
CoolLoong
committed
add putRecordToNBT
1 parent d7d727b commit 9d904a5

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/main/java/org/cloudburstmc/nbt/NbtUtils.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.cloudburstmc.nbt;
22

3+
import org.cloudburstmc.nbt.annotation.NBT;
34
import org.cloudburstmc.nbt.util.stream.LittleEndianDataInputStream;
45
import org.cloudburstmc.nbt.util.stream.LittleEndianDataOutputStream;
56
import org.cloudburstmc.nbt.util.stream.NetworkDataInputStream;
67
import org.cloudburstmc.nbt.util.stream.NetworkDataOutputStream;
78

89
import java.io.*;
10+
import java.lang.reflect.InvocationTargetException;
11+
import java.lang.reflect.Method;
912
import java.util.Arrays;
1013
import java.util.StringJoiner;
1114
import java.util.zip.GZIPInputStream;
@@ -140,4 +143,30 @@ public static String printHexBinary(byte[] data) {
140143
}
141144
return r.toString();
142145
}
146+
147+
/**
148+
* Write each {@link java.lang.reflect.RecordComponent RecordComponent} from record that be marked {@link NBT} to the specified nbtMap
149+
*
150+
* @param record the record
151+
* @param nbtMap the nbtmap
152+
* @return result NBT
153+
*/
154+
public static NbtMap putRecordToNBT(Record record, NbtMap nbtMap) {
155+
Class<? extends Record> clazz = record.getClass();
156+
NBT annotation = clazz.getAnnotation(NBT.class);
157+
if (annotation == null) {
158+
throw new IllegalArgumentException("This record does not use @NBT annotation!");
159+
}
160+
NbtMapBuilder builder = NbtMapBuilder.from(nbtMap);
161+
for (var c : clazz.getRecordComponents()) {
162+
String name = c.getName();
163+
Method accessor = c.getAccessor();
164+
try {
165+
builder.put(name, accessor.invoke(record));
166+
} catch (IllegalAccessException | InvocationTargetException e) {
167+
throw new RuntimeException(e);
168+
}
169+
}
170+
return builder.build();
171+
}
143172
}

src/test/java/org/cloudburstmc/nbt/NbtTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ record Data(int a, int b, int c) {
6868
{"a":1,"b":2,"c":3}""");
6969
}
7070

71+
@Test
72+
void testPutRecordToNBT() {
73+
@NBT
74+
record Data(int a, int b, int c) {
75+
}
76+
NbtMapBuilder builder = new NbtMapBuilder();
77+
builder.putString("test", "test nbt");
78+
NbtMap build = builder.build();
79+
Assertions.assertEquals(NbtUtils.putRecordToNBT(new Data(1, 2, 3), build).toSNBT(), """
80+
{"test":"test nbt","a":1,"b":2,"c":3}""");
81+
}
82+
7183
@Test
7284
@DisplayName("Network Encoding Test")
7385
void networkTest() {
@@ -92,7 +104,7 @@ void networkTest() {
92104
void biomeDefinitionsTest() {
93105
InputStream stream = NbtTest.class.getClassLoader().getResourceAsStream("biome_definitions.nbt");
94106
try (NBTInputStream in = NbtUtils.createNetworkReader(stream)) {
95-
Object o = in.readTag();
107+
NbtMap o = (NbtMap) in.readTag();
96108
} catch (Exception e) {
97109
e.printStackTrace();
98110
throw new AssertionError("Error whilst decoding tag", e);

0 commit comments

Comments
 (0)