Search in sources :

Example 1 with IDataManipulatorBase

use of org.lanternpowered.server.data.manipulator.IDataManipulatorBase in project LanternServer by LanternPowered.

the class ICompositeValueStore method offerFastNoEvents.

default boolean offerFastNoEvents(H valueContainer, MergeFunction function) {
    if (valueContainer instanceof IDataManipulatorBase) {
        // Offer all the default key values as long if they are supported
        final Optional<DataManipulatorRegistration> optRegistration = DataManipulatorRegistry.get().getByMutable(((IDataManipulatorBase) valueContainer).getMutableType());
        if (optRegistration.isPresent()) {
            if (function == MergeFunction.FORCE_NOTHING) {
                return true;
            } else if (function != MergeFunction.IGNORE_ALL) {
                ValueContainer old = DataHelper.create(this, optRegistration.get());
                if (valueContainer instanceof IImmutableDataManipulator && old != null) {
                    old = ((DataManipulator) old).asImmutable();
                }
                valueContainer = (H) function.merge(old, valueContainer);
            }
            boolean success = false;
            for (ImmutableValue value : valueContainer.getValues()) {
                if (offerFast(value)) {
                    success = true;
                    break;
                }
            }
            return success;
        }
    }
    if (this instanceof AdditionalContainerHolder) {
        final AdditionalContainerCollection<H> containers = ((AdditionalContainerHolder<H>) this).getAdditionalContainers();
        final Class key = valueContainer.getClass();
        final H old = (H) containers.get(key).orElse(null);
        final H merged = function.merge(old, valueContainer);
        containers.offer(merged);
        return true;
    }
    return false;
}
Also used : ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) IImmutableDataManipulator(org.lanternpowered.server.data.manipulator.immutable.IImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) IImmutableDataManipulator(org.lanternpowered.server.data.manipulator.immutable.IImmutableDataManipulator) ValueContainer(org.spongepowered.api.data.value.ValueContainer) IDataManipulatorBase(org.lanternpowered.server.data.manipulator.IDataManipulatorBase) DataManipulatorRegistration(org.lanternpowered.server.data.manipulator.DataManipulatorRegistration)

Example 2 with IDataManipulatorBase

use of org.lanternpowered.server.data.manipulator.IDataManipulatorBase in project LanternServer by LanternPowered.

the class ICompositeValueStore method offerNoEvents.

default DataTransactionResult offerNoEvents(H valueContainer, MergeFunction function) {
    if (valueContainer instanceof IDataManipulatorBase) {
        // Offer all the default key values as long if they are supported
        final Optional<DataManipulatorRegistration> optRegistration = DataManipulatorRegistry.get().getByMutable(((IDataManipulatorBase) valueContainer).getMutableType());
        if (optRegistration.isPresent()) {
            if (function != MergeFunction.IGNORE_ALL) {
                ValueContainer old = DataHelper.create(this, optRegistration.get());
                if (valueContainer instanceof IImmutableDataManipulator && old != null) {
                    old = ((DataManipulator) old).asImmutable();
                }
                valueContainer = (H) function.merge(old, valueContainer);
            }
            final DataTransactionResult.Builder builder = DataTransactionResult.builder();
            boolean success = false;
            for (ImmutableValue value : valueContainer.getValues()) {
                final DataTransactionResult result = offerNoEvents(value);
                if (result.isSuccessful()) {
                    builder.success(value);
                    builder.replace(result.getReplacedData());
                    success = true;
                } else {
                    builder.reject(value);
                }
            }
            if (success) {
                builder.result(DataTransactionResult.Type.SUCCESS);
            } else {
                builder.result(DataTransactionResult.Type.FAILURE);
            }
            return builder.build();
        }
    }
    if (this instanceof AdditionalContainerHolder) {
        final AdditionalContainerCollection<H> containers = ((AdditionalContainerHolder<H>) this).getAdditionalContainers();
        final Class key = valueContainer.getClass();
        final H old = (H) containers.get(key).orElse(null);
        final H merged = function.merge(old, valueContainer);
        containers.offer(merged);
        final DataTransactionResult.Builder builder = DataTransactionResult.builder().result(DataTransactionResult.Type.SUCCESS);
        builder.success(merged.getValues());
        if (old != null) {
            builder.replace(old.getValues());
        }
        return builder.build();
    }
    return DataTransactionResult.failNoData();
}
Also used : IImmutableDataManipulator(org.lanternpowered.server.data.manipulator.immutable.IImmutableDataManipulator) DataManipulatorRegistration(org.lanternpowered.server.data.manipulator.DataManipulatorRegistration) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) ValueContainer(org.spongepowered.api.data.value.ValueContainer) IDataManipulatorBase(org.lanternpowered.server.data.manipulator.IDataManipulatorBase)

Aggregations

DataManipulatorRegistration (org.lanternpowered.server.data.manipulator.DataManipulatorRegistration)2 IDataManipulatorBase (org.lanternpowered.server.data.manipulator.IDataManipulatorBase)2 IImmutableDataManipulator (org.lanternpowered.server.data.manipulator.immutable.IImmutableDataManipulator)2 ValueContainer (org.spongepowered.api.data.value.ValueContainer)2 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)2 DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)1 DataManipulator (org.spongepowered.api.data.manipulator.DataManipulator)1 ImmutableDataManipulator (org.spongepowered.api.data.manipulator.ImmutableDataManipulator)1