Skip to content

Commit 11247f1

Browse files
committed
Return an empty modifier if a class does not exist
Addresses #336
1 parent f093c91 commit 11247f1

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

modules/API/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,17 @@ public StructureModifier<TField> writeDefaults() throws FieldAccessException {
453453
*/
454454
@SuppressWarnings("unchecked")
455455
public <T> StructureModifier<T> withType(Class fieldType, EquivalentConverter<T> converter) {
456+
if (fieldType == null) {
457+
// It's not supported in this version, so return an empty modifier
458+
return new StructureModifier<T>() {
459+
@Override
460+
public T read(int index) { return null; }
461+
462+
@Override
463+
public StructureModifier<T> write(int index, T value) { return this; }
464+
};
465+
}
466+
456467
StructureModifier<T> result = subtypeCache.get(fieldType);
457468

458469
// Do we need to update the cache?
@@ -462,7 +473,7 @@ public <T> StructureModifier<T> withType(Class fieldType, EquivalentConverter<T>
462473
int index = 0;
463474

464475
for (Field field : data) {
465-
if (fieldType != null && fieldType.isAssignableFrom(field.getType())) {
476+
if (fieldType.isAssignableFrom(field.getType())) {
466477
filtered.add(field);
467478

468479
// Don't use the original index
@@ -476,14 +487,12 @@ public <T> StructureModifier<T> withType(Class fieldType, EquivalentConverter<T>
476487

477488
// Cache structure modifiers
478489
result = withFieldType(fieldType, filtered, defaults);
479-
480-
if (fieldType != null) {
481-
subtypeCache.put(fieldType, result);
490+
491+
subtypeCache.put(fieldType, result);
482492

483-
// Automatically compile the structure modifier
484-
if (useStructureCompiler && BackgroundCompiler.getInstance() != null)
485-
BackgroundCompiler.getInstance().scheduleCompilation(subtypeCache, fieldType);
486-
}
493+
// Automatically compile the structure modifier
494+
if (useStructureCompiler && BackgroundCompiler.getInstance() != null)
495+
BackgroundCompiler.getInstance().scheduleCompilation(subtypeCache, fieldType);
487496
}
488497

489498
// Add the target too

0 commit comments

Comments
 (0)