use of org.lanternpowered.server.data.processor.ValueProcessorKeyRegistration 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();
}
Aggregations