Search in sources :

Example 1 with Processor

use of org.lanternpowered.server.data.processor.Processor in project LanternServer by LanternPowered.

the class ICompositeValueStore method offerNoEvents.

default <E> DataTransactionResult offerNoEvents(Key<? extends BaseValue<E>> key, E element) {
    // Check the local key registration
    final KeyRegistration<?, ?> localKeyRegistration = (KeyRegistration<?, ?>) getValueCollection().get((Key) key).orElse(null);
    if (localKeyRegistration != null) {
        return ((Processor<BaseValue<E>, E>) localKeyRegistration).offerTo(this, element);
    }
    // Check for a global registration
    final Optional<ValueProcessorKeyRegistration> globalRegistration = LanternValueFactory.get().getKeyRegistration((Key) key);
    if (globalRegistration.isPresent()) {
        return ((Processor<BaseValue<E>, E>) globalRegistration.get()).offerTo(this, element);
    }
    // Check if custom data is supported by this container
    if (this instanceof AdditionalContainerHolder) {
        // Check for the custom value containers
        final AdditionalContainerCollection<H> containers = ((AdditionalContainerHolder<H>) this).getAdditionalContainers();
        for (H valueContainer : containers.getAll()) {
            if (valueContainer.supports(key)) {
                if (valueContainer instanceof ICompositeValueStore) {
                    return ((ICompositeValueStore) valueContainer).offerNoEvents(key, element);
                } else if (valueContainer instanceof CompositeValueStore) {
                    return ((CompositeValueStore) valueContainer).offer(key, element);
                } else if (valueContainer instanceof DataManipulator) {
                    final ImmutableValue oldImmutableValue = (ImmutableValue) valueContainer.getValue((Key) key).map(value -> ValueHelper.toImmutable((BaseValue) value)).orElse(null);
                    ((DataManipulator) valueContainer).set(key, element);
                    final ImmutableValue immutableValue = (ImmutableValue) valueContainer.getValue((Key) key).map(value -> ValueHelper.toImmutable((BaseValue) value)).orElse(null);
                    if (oldImmutableValue == null && immutableValue == null) {
                        return DataTransactionResult.successNoData();
                    } else if (oldImmutableValue == null) {
                        return DataTransactionResult.successResult(immutableValue);
                    } else if (immutableValue == null) {
                        return DataTransactionResult.successRemove(oldImmutableValue);
                    } else {
                        return DataTransactionResult.successReplaceResult(immutableValue, oldImmutableValue);
                    }
                } else {
                    // TODO: Support immutable manipulators?
                    return DataTransactionResult.failNoData();
                }
            }
        }
    }
    return DataTransactionResult.failNoData();
}
Also used : Processor(org.lanternpowered.server.data.processor.Processor) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) IImmutableDataManipulator(org.lanternpowered.server.data.manipulator.immutable.IImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) BaseValue(org.spongepowered.api.data.value.BaseValue) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) ValueProcessorKeyRegistration(org.lanternpowered.server.data.processor.ValueProcessorKeyRegistration) CompositeValueStore(org.spongepowered.api.data.value.mutable.CompositeValueStore) ValueProcessorKeyRegistration(org.lanternpowered.server.data.processor.ValueProcessorKeyRegistration) Key(org.spongepowered.api.data.key.Key)

Example 2 with Processor

use of org.lanternpowered.server.data.processor.Processor in project LanternServer by LanternPowered.

the class IValueContainer method getValues.

@SuppressWarnings("unchecked")
@Override
default Set<ImmutableValue<?>> getValues() {
    final ImmutableSet.Builder<ImmutableValue<?>> values = ImmutableSet.builder();
    // Check local registrations
    for (KeyRegistration<?, ?> entry : getValueCollection().getAll()) {
        final Key key = entry.getKey();
        final Optional<BaseValue> optValue = getValue(key);
        optValue.ifPresent(baseValue -> values.add(ValueHelper.toImmutable(baseValue)));
    }
    // Check for global registrations
    for (ValueProcessorKeyRegistration<?, ?> registration : LanternValueFactory.get().getKeyRegistrations()) {
        final Optional<BaseValue> optValue = ((Processor) registration).getValueFrom(this);
        optValue.ifPresent(baseValue -> values.add(ValueHelper.toImmutable(baseValue)));
    }
    // Check if custom data is supported by this container
    if (this instanceof AdditionalContainerHolder) {
        final AdditionalContainerCollection<?> containers = ((AdditionalContainerHolder<?>) this).getAdditionalContainers();
        containers.getAll().forEach(manipulator -> values.addAll(manipulator.getValues()));
    }
    return values.build();
}
Also used : ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) Processor(org.lanternpowered.server.data.processor.Processor) ImmutableSet(com.google.common.collect.ImmutableSet) BaseValue(org.spongepowered.api.data.value.BaseValue) Key(org.spongepowered.api.data.key.Key)

Aggregations

Processor (org.lanternpowered.server.data.processor.Processor)2 Key (org.spongepowered.api.data.key.Key)2 BaseValue (org.spongepowered.api.data.value.BaseValue)2 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 IImmutableDataManipulator (org.lanternpowered.server.data.manipulator.immutable.IImmutableDataManipulator)1 ValueProcessorKeyRegistration (org.lanternpowered.server.data.processor.ValueProcessorKeyRegistration)1 DataManipulator (org.spongepowered.api.data.manipulator.DataManipulator)1 ImmutableDataManipulator (org.spongepowered.api.data.manipulator.ImmutableDataManipulator)1 CompositeValueStore (org.spongepowered.api.data.value.mutable.CompositeValueStore)1