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);
}
}
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;
}
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();
}
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;
}
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();
}
Aggregations