Search in sources :

Example 1 with ImmutableValue

use of org.spongepowered.api.data.value.immutable.ImmutableValue in project SpongeCommon by SpongePowered.

the class MixinWorld_Data method offer.

@Override
public DataTransactionResult offer(int x, int y, int z, DataManipulator<?, ?> manipulator, MergeFunction function) {
    final BlockState blockState = getBlock(x, y, z).withExtendedProperties(new Location<>(this, x, y, z));
    final ImmutableDataManipulator<?, ?> immutableDataManipulator = manipulator.asImmutable();
    if (blockState.supports((Class) immutableDataManipulator.getClass())) {
        final List<ImmutableValue<?>> old = new ArrayList<>(blockState.getValues());
        final BlockState newState = blockState.with(immutableDataManipulator).get();
        old.removeAll(newState.getValues());
        setBlock(x, y, z, newState);
        return DataTransactionResult.successReplaceResult(old, manipulator.getValues());
    }
    return getTileEntity(x, y, z).map(tileEntity -> tileEntity.offer(manipulator, function)).orElseGet(() -> DataTransactionResult.failResult(manipulator.getValues()));
}
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) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) BlockState(org.spongepowered.api.block.BlockState) ArrayList(java.util.ArrayList)

Example 2 with ImmutableValue

use of org.spongepowered.api.data.value.immutable.ImmutableValue in project SpongeCommon by SpongePowered.

the class MixinWorld_Data method getValues.

@Override
public Set<ImmutableValue<?>> getValues(int x, int y, int z) {
    final ImmutableSet.Builder<ImmutableValue<?>> builder = ImmutableSet.builder();
    final BlockState blockState = getBlock(x, y, z).withExtendedProperties(new Location<>(this, x, y, z));
    builder.addAll(blockState.getValues());
    final Optional<TileEntity> tileEntity = getTileEntity(x, y, z);
    if (tileEntity.isPresent()) {
        builder.addAll(tileEntity.get().getValues());
    }
    return builder.build();
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) BlockState(org.spongepowered.api.block.BlockState) ImmutableSet(com.google.common.collect.ImmutableSet)

Example 3 with ImmutableValue

use of org.spongepowered.api.data.value.immutable.ImmutableValue 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 4 with ImmutableValue

use of org.spongepowered.api.data.value.immutable.ImmutableValue 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 5 with ImmutableValue

use of org.spongepowered.api.data.value.immutable.ImmutableValue 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)

Aggregations

ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)10 DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)5 Key (org.spongepowered.api.data.key.Key)5 BaseValue (org.spongepowered.api.data.value.BaseValue)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 DataManipulator (org.spongepowered.api.data.manipulator.DataManipulator)4 ImmutableDataManipulator (org.spongepowered.api.data.manipulator.ImmutableDataManipulator)4 ArrayList (java.util.ArrayList)3 Optional (java.util.Optional)3 IImmutableDataManipulator (org.lanternpowered.server.data.manipulator.immutable.IImmutableDataManipulator)3 BlockState (org.spongepowered.api.block.BlockState)3 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)3 DataHolder (org.spongepowered.api.data.DataHolder)3 Value (org.spongepowered.api.data.value.mutable.Value)3 ImmutableList (com.google.common.collect.ImmutableList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 Set (java.util.Set)2 EnumFacing (net.minecraft.util.EnumFacing)2