Search in sources :

Example 16 with DataManipulator

use of org.spongepowered.api.data.manipulator.DataManipulator in project LanternServer by LanternPowered.

the class DataHelper method deserializeRawContainerData.

public static void deserializeRawContainerData(DataView dataView, IValueContainer valueContainer, DataQuery query) throws InvalidDataException {
    final List<DataView> dataViews = dataView.getViewList(query).orElse(null);
    if (dataViews == null) {
        return;
    }
    if (!(valueContainer instanceof AdditionalContainerHolder)) {
        getLogger().warn("{} is not a AdditionalContainerHolder, but data manipulators were found.", valueContainer);
        return;
    }
    final ValueCollection valueCollection = valueContainer.getValueCollection();
    final AdditionalContainerCollection<ValueContainer<?>> containers = ((AdditionalContainerHolder) valueContainer).getAdditionalContainers();
    final List<DataView> failedData = new ArrayList<>();
    final LanternDataManager dataManager = Lantern.getGame().getDataManager();
    for (DataView view : dataViews) {
        Optional<DataRegistration> optRegistration;
        String id;
        if (view.contains(DataQueries.MANIPULATOR_ID)) {
            id = view.getString(DataQueries.MANIPULATOR_ID).get();
            optRegistration = DataManipulatorRegistryModule.get().getById(id);
        } else if (view.contains(DATA_CLASS)) {
            id = view.getString(DATA_CLASS).get();
            optRegistration = dataManager.getLegacyRegistration(id);
        } else {
            getLogger().warn("Manipulator with missing id.");
            continue;
        }
        final Optional<DataView> manipulatorView = view.getView(DataQueries.MANIPULATOR_DATA);
        if (manipulatorView.isPresent()) {
            getLogger().warn("Missing manipulator data for id: {}", id);
        }
        if (optRegistration.isPresent()) {
            try {
                final Optional<DataManipulator> optManipulator = optRegistration.get().getDataManipulatorBuilder().build(manipulatorView.get());
                if (optManipulator.isPresent()) {
                    containers.offer(optManipulator.get());
                }
            } catch (InvalidDataException e) {
                getLogger().error("Could not deserialize " + id + "! Don't worry though, we'll try to deserialize the rest of the data.", e);
            }
        } else {
            getLogger().warn("Missing DataRegistration for ID: " + id + ". Don't worry, the data will be kept safe.");
            failedData.add(view);
        }
    }
    if (!failedData.isEmpty()) {
        // Should be safe to cast, at least if nobody touches this key
        Element<List<DataView>> holder = valueCollection.getElement(LanternKeys.FAILED_DATA_MANIPULATORS).orElse(null);
        if (holder == null) {
            holder = valueCollection.register(LanternKeys.FAILED_DATA_MANIPULATORS, null);
        }
        holder.set(failedData);
    }
}
Also used : IDataManipulator(org.lanternpowered.server.data.manipulator.mutable.IDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) ArrayList(java.util.ArrayList) DataView(org.spongepowered.api.data.DataView) DataRegistration(org.spongepowered.api.data.DataRegistration) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ValueContainer(org.spongepowered.api.data.value.ValueContainer)

Example 17 with DataManipulator

use of org.spongepowered.api.data.manipulator.DataManipulator 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 18 with DataManipulator

use of org.spongepowered.api.data.manipulator.DataManipulator 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 19 with DataManipulator

use of org.spongepowered.api.data.manipulator.DataManipulator in project LanternServer by LanternPowered.

the class IDataHolder method removeFast.

// TODO: Support event? Would require special handling to restore the container
@Override
default boolean removeFast(Class<? extends DataManipulator<?, ?>> containerClass) {
    checkNotNull(containerClass, "containerClass");
    // You cannot remove default data manipulators?
    final Optional optRegistration = DataManipulatorRegistry.get().getBy(containerClass);
    if (optRegistration.isPresent()) {
        return false;
    }
    if (this instanceof AdditionalContainerHolder) {
        final AdditionalContainerCollection<DataManipulator<?, ?>> containers = ((AdditionalContainerHolder<DataManipulator<?, ?>>) this).getAdditionalContainers();
        final Optional<DataManipulator<?, ?>> old = containers.remove(containerClass);
        if (old.isPresent()) {
            return true;
        }
    }
    return false;
}
Also used : Optional(java.util.Optional) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator)

Example 20 with DataManipulator

use of org.spongepowered.api.data.manipulator.DataManipulator in project LanternServer by LanternPowered.

the class IDataHolder method get.

@Override
default <T extends DataManipulator<?, ?>> Optional<T> get(Class<T> containerClass) {
    checkNotNull(containerClass, "containerClass");
    // Check default registrations
    final Optional<DataManipulatorRegistration> optRegistration = DataManipulatorRegistry.get().getBy(containerClass);
    if (optRegistration.isPresent()) {
        final DataManipulator manipulator = DataHelper.create(this, optRegistration.get());
        return manipulator == null ? Optional.empty() : Optional.of((T) (ImmutableDataManipulator.class.isAssignableFrom(containerClass) ? manipulator.asImmutable() : manipulator));
    }
    // Try the additional containers if they are supported
    if (this instanceof AdditionalContainerHolder) {
        final AdditionalContainerCollection<DataManipulator<?, ?>> containers = ((AdditionalContainerHolder<DataManipulator<?, ?>>) this).getAdditionalContainers();
        return containers.get(containerClass);
    }
    return Optional.empty();
}
Also used : ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) DataManipulatorRegistration(org.lanternpowered.server.data.manipulator.DataManipulatorRegistration) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator)

Aggregations

DataManipulator (org.spongepowered.api.data.manipulator.DataManipulator)21 ImmutableDataManipulator (org.spongepowered.api.data.manipulator.ImmutableDataManipulator)13 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)8 DataView (org.spongepowered.api.data.DataView)7 ImmutableList (com.google.common.collect.ImmutableList)6 IMixinCustomDataHolder (org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)6 Optional (java.util.Optional)5 DataContainer (org.spongepowered.api.data.DataContainer)5 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)4 SerializedDataTransaction (org.spongepowered.common.data.persistence.SerializedDataTransaction)4 ArrayList (java.util.ArrayList)3 NBTTagList (net.minecraft.nbt.NBTTagList)3 DataManipulatorRegistration (org.lanternpowered.server.data.manipulator.DataManipulatorRegistration)3 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)3 World (org.spongepowered.api.world.World)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Collection (java.util.Collection)2 List (java.util.List)2 IImmutableDataManipulator (org.lanternpowered.server.data.manipulator.immutable.IImmutableDataManipulator)2 DataRegistration (org.spongepowered.api.data.DataRegistration)2