use of org.spongepowered.api.data.DataTransactionResult in project SpongeCommon by SpongePowered.
the class ValueProcessorDelegate method offerToStore.
@Override
public DataTransactionResult offerToStore(ValueContainer<?> container, E value) {
for (ValueProcessor<E, V> processor : this.processors) {
if (processor.supports(container)) {
final DataTransactionResult result = processor.offerToStore(container, value);
if (!result.getType().equals(DataTransactionResult.Type.FAILURE)) {
return result;
}
}
}
for (ValueProcessor<E, V> processor : this.processors) {
if (processor.supports(container)) {
final Optional<V> optional = processor.getApiValueFromContainer(container);
if (optional.isPresent()) {
V mutable = optional.get();
((Value<E>) mutable).set(value);
return DataTransactionResult.failResult(((Value<E>) mutable).asImmutable());
}
}
}
return DataTransactionResult.failNoData();
}
use of org.spongepowered.api.data.DataTransactionResult in project SpongeCommon by SpongePowered.
the class MixinWorld_Data method offer.
@Override
public <E> DataTransactionResult offer(int x, int y, int z, Key<? extends BaseValue<E>> key, E value) {
final BlockState blockState = getBlock(x, y, z).withExtendedProperties(new Location<>(this, x, y, z));
if (blockState.supports(key)) {
ImmutableValue<E> old = ((Value<E>) getValue(x, y, z, (Key) key).get()).asImmutable();
setBlock(x, y, z, blockState.with(key, value).get());
ImmutableValue<E> newVal = ((Value<E>) getValue(x, y, z, (Key) key).get()).asImmutable();
return DataTransactionResult.successReplaceResult(newVal, old);
}
return getTileEntity(x, y, z).map(tileEntity -> tileEntity.offer(key, value)).orElseGet(DataTransactionResult::failNoData);
}
use of org.spongepowered.api.data.DataTransactionResult in project LanternServer by LanternPowered.
the class CompositeValueStoreHelper method processDataTransactionResult.
protected static DataTransactionResult processDataTransactionResult(ICompositeValueStore store, DataTransactionResult result, BooleanSupplier hasListeners) {
if (!(store instanceof DataHolder) || !result.isSuccessful() || !hasListeners.getAsBoolean()) {
return result;
}
final Cause cause = CauseStack.currentOrEmpty().getCurrentCause();
final ChangeDataHolderEvent.ValueChange event = SpongeEventFactory.createChangeDataHolderEventValueChange(cause, result, (DataHolder) store);
Lantern.getGame().getEventManager().post(event);
// Nothing is allowed to change, revert everything fast
if (event.isCancelled()) {
store.undoFastNoEvents(result);
return DataTransactionResult.failNoData();
}
final DataTransactionResult original = result;
result = event.getEndResult();
// Check if something actually changed
if (result != original) {
final Map<Key<?>, ImmutableValue<?>> success = new HashMap<>();
for (ImmutableValue<?> value : original.getSuccessfulData()) {
success.put(value.getKey(), value);
}
for (ImmutableValue<?> value : result.getSuccessfulData()) {
final ImmutableValue<?> value1 = success.remove(value.getKey());
if (value1 == null || value1.get() != value.get()) {
store.offerNoEvents(value);
}
}
// A previously successful offering got removed, revert this
if (!success.isEmpty()) {
for (ImmutableValue<?> value : original.getReplacedData()) {
if (success.containsKey(value.getKey())) {
store.offerNoEvents(value);
}
}
}
}
return event.getEndResult();
}
use of org.spongepowered.api.data.DataTransactionResult 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();
}
use of org.spongepowered.api.data.DataTransactionResult in project LanternServer by LanternPowered.
the class IDataHolder method copyFromNoEvents.
default DataTransactionResult copyFromNoEvents(DataHolder that, MergeFunction function) {
final Collection<DataManipulator<?, ?>> containers = that.getContainers();
final DataTransactionResult.Builder builder = DataTransactionResult.builder();
boolean success = false;
for (DataManipulator<?, ?> thatContainer : containers) {
final DataManipulator<?, ?> thisContainer = get(thatContainer.getClass()).orElse(null);
final DataManipulator<?, ?> merged = function.merge(thisContainer, thatContainer);
final DataTransactionResult result = offerNoEvents(merged, MergeFunction.IGNORE_ALL);
builder.absorbResult(result);
if (!result.getSuccessfulData().isEmpty()) {
success = true;
}
}
return builder.result(success ? DataTransactionResult.Type.SUCCESS : DataTransactionResult.Type.FAILURE).build();
}
Aggregations