Search in sources :

Example 21 with DataTransactionResult

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();
}
Also used : Value(org.spongepowered.api.data.value.mutable.Value) BaseValue(org.spongepowered.api.data.value.BaseValue) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult)

Example 22 with DataTransactionResult

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);
}
Also used : SpongeImpl(org.spongepowered.common.SpongeImpl) Value(org.spongepowered.api.data.value.mutable.Value) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) PropertyStore(org.spongepowered.api.data.property.PropertyStore) Key(org.spongepowered.api.data.key.Key) ArrayList(java.util.ArrayList) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) ImmutableList(com.google.common.collect.ImmutableList) Mixin(org.spongepowered.asm.mixin.Mixin) MergeFunction(org.spongepowered.api.data.merge.MergeFunction) Location(org.spongepowered.api.world.Location) ImmutableSet(com.google.common.collect.ImmutableSet) DirectionFacingProvider(org.spongepowered.common.registry.provider.DirectionFacingProvider) Collection(java.util.Collection) BaseValue(org.spongepowered.api.data.value.BaseValue) EnumFacing(net.minecraft.util.EnumFacing) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) DataHolder(org.spongepowered.api.data.DataHolder) BlockState(org.spongepowered.api.block.BlockState) Direction(org.spongepowered.api.util.Direction) List(java.util.List) DataView(org.spongepowered.api.data.DataView) World(org.spongepowered.api.world.World) Property(org.spongepowered.api.data.Property) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) Optional(java.util.Optional) Collections(java.util.Collections) BlockState(org.spongepowered.api.block.BlockState) Value(org.spongepowered.api.data.value.mutable.Value) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) BaseValue(org.spongepowered.api.data.value.BaseValue) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult)

Example 23 with DataTransactionResult

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();
}
Also used : ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) HashMap(java.util.HashMap) DataHolder(org.spongepowered.api.data.DataHolder) Cause(org.spongepowered.api.event.cause.Cause) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) ChangeDataHolderEvent(org.spongepowered.api.event.data.ChangeDataHolderEvent) LanternKey(org.lanternpowered.server.data.key.LanternKey) Key(org.spongepowered.api.data.key.Key)

Example 24 with DataTransactionResult

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

Example 25 with DataTransactionResult

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();
}
Also used : ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult)

Aggregations

DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)29 ItemStack (org.spongepowered.api.item.inventory.ItemStack)6 DataHolder (org.spongepowered.api.data.DataHolder)4 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BlockState (org.spongepowered.api.block.BlockState)3 BaseValue (org.spongepowered.api.data.value.BaseValue)3 Player (org.spongepowered.api.entity.living.player.Player)3 IMixinCustomDataHolder (org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Optional (java.util.Optional)2 Set (java.util.Set)2 EnumFacing (net.minecraft.util.EnumFacing)2 Sponge (org.spongepowered.api.Sponge)2 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)2 DataView (org.spongepowered.api.data.DataView)2