From 54242debaafe978d6d9a93a5174e33036d94aa8b Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Wed, 19 Sep 2012 19:25:11 +0200 Subject: [PATCH] Made structure modifiers more overridable. --- .../protocol/injector/EntityUtilities.java | 2 +- .../injector/PacketFilterManager.java | 2 - .../protocol/reflect/StructureModifier.java | 71 ++++++++++++++----- 3 files changed, 53 insertions(+), 22 deletions(-) diff --git a/ProtocolLib/src/com/comphenix/protocol/injector/EntityUtilities.java b/ProtocolLib/src/com/comphenix/protocol/injector/EntityUtilities.java index d358bcd7..c7c4d82d 100644 --- a/ProtocolLib/src/com/comphenix/protocol/injector/EntityUtilities.java +++ b/ProtocolLib/src/com/comphenix/protocol/injector/EntityUtilities.java @@ -36,7 +36,7 @@ class EntityUtilities { private static Method scanPlayersMethod; public static void updateEntity(Entity entity, List observers) throws FieldAccessException { - + World world = entity.getWorld(); Object worldServer = ((CraftWorld) world).getHandle(); diff --git a/ProtocolLib/src/com/comphenix/protocol/injector/PacketFilterManager.java b/ProtocolLib/src/com/comphenix/protocol/injector/PacketFilterManager.java index cd380899..8b331ddb 100644 --- a/ProtocolLib/src/com/comphenix/protocol/injector/PacketFilterManager.java +++ b/ProtocolLib/src/com/comphenix/protocol/injector/PacketFilterManager.java @@ -343,8 +343,6 @@ public final class PacketFilterManager implements ProtocolManager { @Override public void updateEntity(Entity entity, List observers) throws FieldAccessException { - - EntityUtilities.updateEntity(entity, observers); } diff --git a/ProtocolLib/src/com/comphenix/protocol/reflect/StructureModifier.java b/ProtocolLib/src/com/comphenix/protocol/reflect/StructureModifier.java index 0f9aed1f..11df07b1 100644 --- a/ProtocolLib/src/com/comphenix/protocol/reflect/StructureModifier.java +++ b/ProtocolLib/src/com/comphenix/protocol/reflect/StructureModifier.java @@ -34,21 +34,21 @@ import com.google.common.collect.ImmutableList; public class StructureModifier { // Object and its type - private Class targetType; - private Object target; + protected Class targetType; + protected Object target; // Converter. May be NULL. - private EquivalentConverter converter; + protected EquivalentConverter converter; // The fields to read in order - private Class fieldType; - private List data = new ArrayList(); + protected Class fieldType; + protected List data = new ArrayList(); // Improved default values - private Set defaultFields; + protected Set defaultFields; // Cache of previous types - private Map subtypeCache; + protected Map subtypeCache; /** * Creates a structure modifier. @@ -63,16 +63,20 @@ public class StructureModifier { initialize(targetType, Object.class, fields, defaults, null, new HashMap()); } - private StructureModifier(StructureModifier other, Object target) { - initialize(other.targetType, other.fieldType, other.data, other.defaultFields, other.converter, other.subtypeCache); - this.target = target; - } - private StructureModifier() { // Consumers of this method should call "initialize" } - private void initialize(Class targetType, Class fieldType, + /** + * Initialize every field of this class. + * @param targetType - type of the object we're reading and writing from. + * @param fieldType - the common type of the fields we're modifying. + * @param data - list of fields to modify. + * @param defaultFields - list of fields that will be automatically initialized. + * @param converter - converts between the common field type and the actual type the consumer expects. + * @param subTypeCache - a structure modifier cache. + */ + protected void initialize(Class targetType, Class fieldType, List data, Set defaultFields, EquivalentConverter converter, Map subTypeCache) { this.targetType = targetType; @@ -235,9 +239,7 @@ public class StructureModifier { } // Cache structure modifiers - result = new StructureModifier(); - result.initialize(targetType, fieldType, filtered, defaults, - converter, new HashMap()); + result = withFieldType(fieldType, filtered, defaults, converter); if (fieldType != null) subtypeCache.put(fieldType, result); @@ -296,13 +298,36 @@ public class StructureModifier { return data.size(); } + /** + * Create a new structure modifier for the new field type. + * @param fieldType - common type of each field. + * @param filtered - list of fields after filtering the original modifier. + * @param defaults - list of default values after filtering the original. + * @param converter - the new converter. + * @return A new structure modifier. + */ + protected StructureModifier withFieldType( + Class fieldType, List filtered, + Set defaults, EquivalentConverter converter) { + + StructureModifier result = new StructureModifier(); + result.initialize(targetType, fieldType, filtered, defaults, + converter, new HashMap()); + return result; + } + /** * Retrieves a structure modifier of the same type for a different object target. * @param target - different target of the same type. * @return Structure modifier with the new target. */ public StructureModifier withTarget(Object target) { - return new StructureModifier(this, target); + StructureModifier copy = new StructureModifier(); + + // Create a new instance + copy.initialize(targetType, fieldType, data, defaultFields, converter, subtypeCache); + copy.target = target; + return copy; } /** @@ -312,12 +337,20 @@ public class StructureModifier { */ @SuppressWarnings("unchecked") private StructureModifier withConverter(EquivalentConverter converter) { - StructureModifier copy = new StructureModifier(this, target); + StructureModifier copy = withTarget(target); - copy.converter = converter; + copy.setConverter(converter); return copy; } + /** + * Set the current object converter. Should only be called during construction. + * @param converter - current object converter. + */ + protected void setConverter(EquivalentConverter converter) { + this.converter = converter; + } + /** * Retrieves a list of the fields matching the constraints of this structure modifier. * @return List of fields.