Skip to content

Commit

Permalink
Actually implement @NBTAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed Mar 1, 2024

Verified

This commit was signed with the committer’s verified signature.
LordGrimmauld Grimmauld
1 parent 3e560f2 commit 455fac5
Showing 2 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
import de.bluecolored.bluenbt.*;
import de.bluecolored.bluenbt.ObjectConstructor;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.lang.reflect.Field;
@@ -88,14 +89,11 @@ public DefaultAdapter(TypeToken<T> type, ObjectConstructor<T> constructor, BlueN
if (nbtName != null) names = nbtName.value();

TypeToken<?> fieldType = TypeToken.get(TypeUtil.resolve(typeToken.getType(), raw, field.getGenericType()));
NBTDeserializer deserializerType = field.getAnnotation(NBTDeserializer.class);
if (deserializerType == null) {
deserializerType = fieldType.getRawType().getAnnotation(NBTDeserializer.class);
}

TypeDeserializer<?> typeDeserializer;
Class<? extends TypeDeserializer<?>> deserializerType = findDeserializerType(field, fieldType.getRawType());
if (deserializerType != null) {
typeDeserializer = typeDeserializerCache.computeIfAbsent(deserializerType.value(), t -> {
typeDeserializer = typeDeserializerCache.computeIfAbsent(deserializerType, t -> {
try {
return t.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
@@ -151,6 +149,22 @@ public T read(NBTReader reader) throws IOException {
}
}

private @Nullable Class<? extends TypeDeserializer<?>> findDeserializerType(Field field, Class<?> type) {
NBTDeserializer fieldDeserializer = field.getAnnotation(NBTDeserializer.class);
if (fieldDeserializer != null) return fieldDeserializer.value();

NBTAdapter fieldAdapter = field.getAnnotation(NBTAdapter.class);
if (fieldAdapter != null) return fieldAdapter.value();

NBTDeserializer typeDeserializer = type.getAnnotation(NBTDeserializer.class);
if (typeDeserializer != null) return typeDeserializer.value();

NBTAdapter typeAdapter = type.getAnnotation(NBTAdapter.class);
if (typeAdapter != null) return typeAdapter.value();

return null;
}

}

private interface FieldAccessor {
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@
import com.google.gson.reflect.TypeToken;
import de.bluecolored.bluenbt.*;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.lang.reflect.Field;
@@ -85,14 +86,11 @@ public DefaultAdapter(TypeToken<T> type, BlueNBT blueNBT) {
if (nbtName != null) names = nbtName.value();

TypeToken<?> fieldType = TypeToken.get(TypeUtil.resolve(typeToken.getType(), raw, field.getGenericType()));
NBTSerializer serializerType = field.getAnnotation(NBTSerializer.class);
if (serializerType == null) {
serializerType = fieldType.getRawType().getAnnotation(NBTSerializer.class);
}

TypeSerializer<?> typeSerializer;
Class<? extends TypeSerializer<?>> serializerType = findSerializerType(field, fieldType.getRawType());
if (serializerType != null) {
typeSerializer = typeSerializerCache.computeIfAbsent(serializerType.value(), t -> {
typeSerializer = typeSerializerCache.computeIfAbsent(serializerType, t -> {
try {
return t.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
@@ -134,6 +132,22 @@ public void write(T value, NBTWriter writer) throws IOException {
}
}

private @Nullable Class<? extends TypeSerializer<?>> findSerializerType(Field field, Class<?> type) {
NBTSerializer fieldSerializer = field.getAnnotation(NBTSerializer.class);
if (fieldSerializer != null) return fieldSerializer.value();

NBTAdapter fieldAdapter = field.getAnnotation(NBTAdapter.class);
if (fieldAdapter != null) return fieldAdapter.value();

NBTSerializer typeSerializer = type.getAnnotation(NBTSerializer.class);
if (typeSerializer != null) return typeSerializer.value();

NBTAdapter typeAdapter = type.getAnnotation(NBTAdapter.class);
if (typeAdapter != null) return typeAdapter.value();

return null;
}

}

private interface FieldAccessor {

0 comments on commit 455fac5

Please sign in to comment.