/** * Obtains the value of the field from a object which is an instance of the owning type. * This method is checked to conform to the generic parameters of the FieldMetadata * * @param from The object to obtain the value of this field from * @return The value of the field */ public U getValueChecked(T from) { return getValue(from); }
/** * For types that need to be copied (e.g. Vector3f) for safe usage, this method will create a new copy of a field from an object. * Otherwise it behaves the same as getValue * * @param from The object to copy the field from * @return A safe to use copy of the value of this field in the given object */ public U getCopyOfValue(Object from) { return copyStrategy.copy(getValue(from)); }
@Override public T get() { Component comp = worldConfig.getProperties().get(label); return fieldMetadata.getValue(comp); }
@Override public T get() { Component comp = worldConfig.getProperties().get(label); return fieldMetadata.getValue(comp); }
@Override public PersistedData serialize(T value, SerializationContext context) { if (value == null) { return context.createNull(); } Map<String, PersistedData> mappedData = Maps.newLinkedHashMap(); for (Map.Entry<FieldMetadata<T, ?>, TypeHandler<?>> entry : mappedFields.entrySet()) { Object val = entry.getKey().getValue(value); if (val != null) { TypeHandler handler = entry.getValue(); PersistedData fieldValue = handler.serialize(val, context); if (fieldValue != null) { mappedData.put(entry.getKey().getName(), fieldValue); } } } return context.create(mappedData); }
private <T extends Component> void updateComponent(EntityRef blockEntity, ComponentMetadata<T> metadata, T targetComponent) { T currentComp = blockEntity.getComponent(metadata.getType()); if (currentComp != null) { boolean changed = false; for (FieldMetadata<T, ?> field : metadata.getFields()) { Object newVal = field.getValue(targetComponent); if (!Objects.equal(field.getValue(currentComp), newVal)) { field.setValue(currentComp, newVal); changed = true; } } if (changed) { blockEntity.saveComponent(currentComp); } } }
/** * Serializes a field of a provided container * * @param field The metadata for the field to serialize * @param container The object containing the field * @param context The current serialization context * @return The serialized value of the field */ @SuppressWarnings("unchecked") public PersistedData serialize(FieldMetadata<?, ?> field, Object container, SerializationContext context) { Object rawValue = field.getValue(container); if (rawValue != null) { TypeHandler handler = getHandlerFor(field); if (handler != null) { return handler.serialize(rawValue, context); } } return context.createNull(); }