Package com.comphenix.protocol.reflect
Class StructureModifier<T>
java.lang.Object
com.comphenix.protocol.reflect.StructureModifier<T>
- Type Parameters:
T- Type of the fields to retrieve.
Provides list-oriented access to the fields of a Minecraft packet.
Implemented by using reflection. Use a CompiledStructureModifier, if speed is essential.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected List<FieldAccessor> protected EquivalentConverter<T> protected booleanprotected Map<FieldAccessor, Integer> protected Class<?> protected Map<Class<?>, StructureModifier<?>> protected Objectprotected Class<?> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConsumers of this method should call "initialize".StructureModifier(Class<?> targetType) Creates a structure modifier.StructureModifier(Class<?> targetType, Class<?> superclassExclude, boolean requireDefault) Creates a structure modifier. -
Method Summary
Modifier and TypeMethodDescriptionprotected FieldAccessorfindFieldAccessor(int fieldIndex) getField(int fieldIndex) Retrieve a field by index.Retrieves a list of the fields matching the constraints of this structure modifier.Class<?> Retrieves the common type of each field.Retrieves the object we're currently modifying.Class<?> Retrieves the type of the object we're modifying.Retrieve every value stored in the fields of the current type.protected voidinitialize(StructureModifier<T> other) Initialize using the same field types.protected voidinitialize(Class<?> targetType, Class<?> fieldType, List<FieldAccessor> data, Map<FieldAccessor, Integer> defaultFields, EquivalentConverter<T> converter, Map<Class<?>, StructureModifier<?>> subTypeCache) Initialize every field of this class.modify(int fieldIndex, UnaryOperator<T> select) Correctly modifies the value of a field.optionRead(int fieldIndex) Reads the value of a field only if it exists.read(int fieldIndex) Reads the value of a field given its index.readSafely(int fieldIndex) Reads the value of a field only if it exists.protected voidsetConverter(EquivalentConverter<T> converter) Set the current object converter.intsize()Retrieve the number of readable types.toString()protected <V> StructureModifier<V> withFieldType(Class<?> fieldType, List<FieldAccessor> filtered, Map<FieldAccessor, Integer> defaults) Create a new structure modifier for the new field type.protected <V> StructureModifier<V> withFieldType(Class<?> fieldType, List<FieldAccessor> filtered, Map<FieldAccessor, Integer> defaults, EquivalentConverter<V> converter) Create a new structure modifier for the new field type.<R> StructureModifier<R> withParamType(Class<?> fieldType, EquivalentConverter<R> converter, Class<?>... paramTypes) Retrieves a structure modifier that only reads and writes fields of a given type.withTarget(Object target) Retrieves a structure modifier of the same type for a different object target.<R> StructureModifier<R> Retrieves a structure modifier that only reads and writes fields of a given type.<R> StructureModifier<R> withType(Class<?> fieldType, EquivalentConverter<R> converter) Retrieves a structure modifier that only reads and writes fields of a given type.Writes the value of a field given its index.Sets all non-primitive fields to a more fitting default value.writeSafely(int fieldIndex, T value) Writes the value of a given field IF and ONLY if it exists.
-
Field Details
-
target
-
targetType
-
fieldType
-
accessors
-
converter
-
defaultFields
-
subtypeCache
-
customConvertHandling
protected boolean customConvertHandling
-
-
Constructor Details
-
StructureModifier
Creates a structure modifier.- Parameters:
targetType- - the structure to modify.
-
StructureModifier
Creates a structure modifier.- Parameters:
targetType- - the structure to modify.superclassExclude- - a superclass to exclude.requireDefault- - whether we will be using writeDefaults()
-
StructureModifier
protected StructureModifier()Consumers of this method should call "initialize".
-
-
Method Details
-
initialize
Initialize using the same field types.- Parameters:
other- - information to set.
-
initialize
protected void initialize(Class<?> targetType, Class<?> fieldType, List<FieldAccessor> data, Map<FieldAccessor, Integer> defaultFields, EquivalentConverter<T> converter, Map<Class<?>, StructureModifier<?>> subTypeCache) Initialize every field of this class.- Parameters:
targetType- - type of the object we're reading and writing from.fieldType- - the common type of the fields we're modifying.data- - list of fields to modify.defaultFields- - list of fields that will be automatically initialized.converter- - converts between the common field type and the actual type the consumer expects.subTypeCache- - a structure modifier cache.
-
read
Reads the value of a field given its index.Note: This method is prone to exceptions (there are currently 5 total throw statements). It is recommended that you use
readSafely(int), which returnsnullif the field doesn't exist, instead of throwing an exception.- Parameters:
fieldIndex- - index of the field.- Returns:
- Value of the field.
- Throws:
FieldAccessException- if the given field index is out of bounds.IllegalStateException- if this modifier has no target set.
-
readSafely
Reads the value of a field only if it exists. If the field does not exist,nullis returned.As its name implies, this method is a much safer alternative to
read(int). In addition to throwing less exceptions and thereby causing less console spam, this method makes providing backwards compatiblity signficiantly easier, as shown below:BlockPosition position = packet.getBlockPositionModifier().readSafely(0); if (position != null) { // Handle 1.8+ } else { // Handle 1.7- }- Parameters:
fieldIndex- - index of the field.- Returns:
- Value of the field, or NULL if it doesn't exist.
- Throws:
IllegalStateException- if this modifier has no target set.FieldAccessException
-
optionRead
Reads the value of a field only if it exists. If the field does not exist, an emptyOptionalis returned.This method has the same functionality as
readSafely(int), but enforces null checks by way of an Optional. It will eventually become the preferred method of reading fields.- Parameters:
fieldIndex- index of the field- Returns:
- An optional that may contain the value of the field
- See Also:
-
write
Writes the value of a field given its index.- Parameters:
fieldIndex- - index of the field.value- - new value of the field.- Returns:
- This structure modifier - for chaining.
- Throws:
FieldAccessException- The field doesn't exist, or it cannot be accessed under the current security contraints.
-
writeSafely
Writes the value of a given field IF and ONLY if it exists.- Parameters:
fieldIndex- - index of the potential field.value- - new value of the field.- Returns:
- This structure modifer - for chaining.
- Throws:
FieldAccessException- The field cannot be accessed under the current security contraints.
-
modify
public StructureModifier<T> modify(int fieldIndex, UnaryOperator<T> select) throws FieldAccessException Correctly modifies the value of a field.- Parameters:
fieldIndex- - index of the field to modify.select- - the function that modifies the field value.- Returns:
- This structure modifier - for chaining.
- Throws:
FieldAccessException- The field cannot be accessed under the current security contraints.
-
findFieldAccessor
-
writeDefaults
Sets all non-primitive fields to a more fitting default value. SeeDefaultInstances.getDefault(Class).- Returns:
- The current structure modifier - for chaining.
- Throws:
FieldAccessException- If we're unable to write to the fields due to a security limitation.
-
getFieldType
Retrieves the common type of each field.- Returns:
- Common type of each field.
-
getTargetType
Retrieves the type of the object we're modifying.- Returns:
- Type of the object.
-
getTarget
Retrieves the object we're currently modifying.- Returns:
- Object we're modifying.
-
size
public int size()Retrieve the number of readable types.- Returns:
- Readable types.
-
getFields
Retrieves a list of the fields matching the constraints of this structure modifier.- Returns:
- List of fields.
-
getField
Retrieve a field by index.- Parameters:
fieldIndex- - index of the field to retrieve.- Returns:
- The field represented with the given index.
- Throws:
IllegalArgumentException- If no field with the given index can be found.
-
getValues
Retrieve every value stored in the fields of the current type.- Returns:
- Every field value.
- Throws:
FieldAccessException- Unable to access one or all of the fields
-
withType
Retrieves a structure modifier that only reads and writes fields of a given type.- Type Parameters:
R- Type- Parameters:
fieldType- - the type, or supertype, of every field to modify.- Returns:
- A structure modifier for fields of this type.
-
withType
Retrieves a structure modifier that only reads and writes fields of a given type.- Type Parameters:
R- Type- Parameters:
fieldType- - the type, or supertype, of every field to modify.converter- - converts objects into the given type.- Returns:
- A structure modifier for fields of this type.
-
withParamType
public <R> StructureModifier<R> withParamType(Class<?> fieldType, EquivalentConverter<R> converter, Class<?>... paramTypes) Retrieves a structure modifier that only reads and writes fields of a given type.- Type Parameters:
R- Type- Parameters:
fieldType- - the type, or supertype, of every field to modify.converter- - converts objects into the given type.paramTypes- - field type parameters- Returns:
- A structure modifier for fields of this type.
-
withFieldType
protected <V> StructureModifier<V> withFieldType(Class<?> fieldType, List<FieldAccessor> filtered, Map<FieldAccessor, Integer> defaults) Create a new structure modifier for the new field type.- Type Parameters:
V- Type- Parameters:
fieldType- - common type of each field.filtered- - list of fields after filtering the original modifier.defaults- - list of default values after filtering the original.- Returns:
- A new structure modifier.
-
withFieldType
protected <V> StructureModifier<V> withFieldType(Class<?> fieldType, List<FieldAccessor> filtered, Map<FieldAccessor, Integer> defaults, EquivalentConverter<V> converter) Create a new structure modifier for the new field type.- Type Parameters:
V- Type- Parameters:
fieldType- - common type of each field.filtered- - list of fields after filtering the original modifier.defaults- - list of default values after filtering the original.converter- - the new converter, or NULL.- Returns:
- A new structure modifier.
-
withTarget
Retrieves a structure modifier of the same type for a different object target.- Parameters:
target- - different target of the same type.- Returns:
- Structure modifier with the new target.
-
setConverter
Set the current object converter. Should only be called during construction.- Parameters:
converter- - current object converter.
-
toString
-