diff --git a/src/main/java/de/bluecolored/bluenbt/adapter/DefaultDeserializerFactory.java b/src/main/java/de/bluecolored/bluenbt/adapter/DefaultDeserializerFactory.java index 8bf6a08..1fa0a23 100644 --- a/src/main/java/de/bluecolored/bluenbt/adapter/DefaultDeserializerFactory.java +++ b/src/main/java/de/bluecolored/bluenbt/adapter/DefaultDeserializerFactory.java @@ -87,8 +87,10 @@ public DefaultAdapter(TypeToken type, ObjectConstructor constructor, BlueN Class raw; while (typeToken != null && (raw = typeToken.getRawType()) != Object.class) { for (Field field : raw.getDeclaredFields()) { - if (Modifier.isStatic(field.getModifiers())) + int modifiers = field.getModifiers(); + if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) continue; + field.setAccessible(true); String[] names = new String[]{ field.getName() }; @@ -102,6 +104,12 @@ public DefaultAdapter(TypeToken type, ObjectConstructor constructor, BlueN if (deserializerType != null) { typeDeserializer = typeDeserializerCache.computeIfAbsent(deserializerType, t -> { try { + // try BlueNBT constructor + try { + return t.getDeclaredConstructor(BlueNBT.class).newInstance(blueNBT); + } catch (NoSuchMethodException ignore) {} + + // use no-args constructor return t.getDeclaredConstructor().newInstance(); } catch (Exception ex) { throw new RuntimeException("Failed to create Instance of TypeDeserializer!", ex); diff --git a/src/main/java/de/bluecolored/bluenbt/adapter/DefaultSerializerFactory.java b/src/main/java/de/bluecolored/bluenbt/adapter/DefaultSerializerFactory.java index 038d59b..ef73402 100644 --- a/src/main/java/de/bluecolored/bluenbt/adapter/DefaultSerializerFactory.java +++ b/src/main/java/de/bluecolored/bluenbt/adapter/DefaultSerializerFactory.java @@ -78,8 +78,10 @@ public DefaultAdapter(TypeToken type, BlueNBT blueNBT) { Class raw; while (typeToken != null && (raw = typeToken.getRawType()) != Object.class) { for (Field field : raw.getDeclaredFields()) { - if (Modifier.isStatic(field.getModifiers())) + int modifiers = field.getModifiers(); + if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers)) continue; + field.setAccessible(true); String[] names = new String[]{ field.getName() }; @@ -93,6 +95,12 @@ public DefaultAdapter(TypeToken type, BlueNBT blueNBT) { if (serializerType != null) { typeSerializer = typeSerializerCache.computeIfAbsent(serializerType, t -> { try { + // try BlueNBT constructor + try { + return t.getDeclaredConstructor(BlueNBT.class).newInstance(blueNBT); + } catch (NoSuchMethodException ignore) {} + + // use no-args constructor return t.getDeclaredConstructor().newInstance(); } catch (Exception ex) { throw new RuntimeException("Failed to create Instance of TypeSerializer!", ex);