From 11247f1c012e4083059828b8384635d633ac1eb8 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Tue, 23 May 2017 18:52:28 -0400 Subject: [PATCH] Return an empty modifier if a class does not exist Addresses #336 --- .../protocol/reflect/StructureModifier.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/modules/API/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java b/modules/API/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java index 2744fbe5..37902ae5 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java +++ b/modules/API/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java @@ -453,6 +453,17 @@ public class StructureModifier { */ @SuppressWarnings("unchecked") public StructureModifier withType(Class fieldType, EquivalentConverter converter) { + if (fieldType == null) { + // It's not supported in this version, so return an empty modifier + return new StructureModifier() { + @Override + public T read(int index) { return null; } + + @Override + public StructureModifier write(int index, T value) { return this; } + }; + } + StructureModifier result = subtypeCache.get(fieldType); // Do we need to update the cache? @@ -462,7 +473,7 @@ public class StructureModifier { int index = 0; for (Field field : data) { - if (fieldType != null && fieldType.isAssignableFrom(field.getType())) { + if (fieldType.isAssignableFrom(field.getType())) { filtered.add(field); // Don't use the original index @@ -476,14 +487,12 @@ public class StructureModifier { // Cache structure modifiers result = withFieldType(fieldType, filtered, defaults); - - if (fieldType != null) { - subtypeCache.put(fieldType, result); + + subtypeCache.put(fieldType, result); - // Automatically compile the structure modifier - if (useStructureCompiler && BackgroundCompiler.getInstance() != null) - BackgroundCompiler.getInstance().scheduleCompilation(subtypeCache, fieldType); - } + // Automatically compile the structure modifier + if (useStructureCompiler && BackgroundCompiler.getInstance() != null) + BackgroundCompiler.getInstance().scheduleCompilation(subtypeCache, fieldType); } // Add the target too