Search in sources :

Example 1 with DataManipulatorRegistration

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

the class IDataHolder method supports.

@Override
default boolean supports(Class<? extends DataManipulator<?, ?>> containerClass) {
    checkNotNull(containerClass, "containerClass");
    // Offer all the default key values as long if they are supported
    final Optional<DataManipulatorRegistration> optRegistration = DataManipulatorRegistry.get().getBy(containerClass);
    if (optRegistration.isPresent()) {
        final DataManipulatorRegistration registration = optRegistration.get();
        for (Key key : (Set<Key>) registration.getRequiredKeys()) {
            if (!supports(key)) {
                return false;
            }
        }
        return true;
    }
    // Support all the additional manipulators
    return this instanceof AdditionalContainerHolder;
}
Also used : Set(java.util.Set) DataManipulatorRegistration(org.lanternpowered.server.data.manipulator.DataManipulatorRegistration) Key(org.spongepowered.api.data.key.Key)

Example 2 with DataManipulatorRegistration

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

the class IDataHolder method getContainers.

@Override
default Collection<DataManipulator<?, ?>> getContainers() {
    final ImmutableList.Builder<DataManipulator<?, ?>> builder = ImmutableList.builder();
    for (DataManipulatorRegistration registration : DataManipulatorRegistry.get().getAll()) {
        final DataManipulator manipulator = DataHelper.create(this, registration);
        if (manipulator != null) {
            builder.add(manipulator);
        }
    }
    // Try the additional manipulators if they are supported
    if (this instanceof AdditionalContainerHolder) {
        final AdditionalContainerCollection<DataManipulator<?, ?>> containers = ((AdditionalContainerHolder<DataManipulator<?, ?>>) this).getAdditionalContainers();
        containers.getAll().forEach(manipulator -> builder.add(manipulator.copy()));
    }
    return builder.build();
}
Also used : ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) ImmutableList(com.google.common.collect.ImmutableList) DataManipulatorRegistration(org.lanternpowered.server.data.manipulator.DataManipulatorRegistration)

Example 3 with DataManipulatorRegistration

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

the class IImmutableDataHolderBase method getContainers.

@Override
default List<ImmutableDataManipulator<?, ?>> getContainers() {
    final ImmutableList.Builder<ImmutableDataManipulator<?, ?>> builder = ImmutableList.builder();
    for (DataManipulatorRegistration registration : DataManipulatorRegistry.get().getAll()) {
        final ImmutableDataManipulator manipulator = (ImmutableDataManipulator) ImmutableContainerCache.get(this, registration, registration.getImmutableManipulatorClass()).orElse(null);
        if (manipulator != null) {
            builder.add(manipulator);
        }
    }
    if (this instanceof AdditionalContainerHolder) {
        final AdditionalContainerCollection<ImmutableDataManipulator<?, ?>> manipulators = ((AdditionalContainerHolder<ImmutableDataManipulator<?, ?>>) this).getAdditionalContainers();
        manipulators.getAll().forEach(builder::add);
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) DataManipulatorRegistration(org.lanternpowered.server.data.manipulator.DataManipulatorRegistration) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator)

Example 4 with DataManipulatorRegistration

use of org.lanternpowered.server.data.manipulator.DataManipulatorRegistration 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 5 with DataManipulatorRegistration

use of org.lanternpowered.server.data.manipulator.DataManipulatorRegistration 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)6 ImmutableDataManipulator (org.spongepowered.api.data.manipulator.ImmutableDataManipulator)4 DataManipulator (org.spongepowered.api.data.manipulator.DataManipulator)3 ImmutableList (com.google.common.collect.ImmutableList)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 Set (java.util.Set)1 DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)1 Key (org.spongepowered.api.data.key.Key)1