Search in sources :

Example 1 with BaseValue

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

the class MixinStateImplementation method cycleValue.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public BlockState cycleValue(Key<? extends BaseValue<? extends Cycleable<?>>> key) {
    if (supports(key)) {
        final Cycleable value = (Cycleable) get((Key) key).get();
        final Cycleable next = value.cycleNext();
        return with((Key<? extends BaseValue<Object>>) (Key<?>) key, next).get();
    }
    throw new IllegalArgumentException("Used an invalid cyclable key! Check with supports in the future!");
}
Also used : BaseValue(org.spongepowered.api.data.value.BaseValue) Cycleable(org.spongepowered.api.util.Cycleable) Key(org.spongepowered.api.data.key.Key)

Example 2 with BaseValue

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

the class MixinDataHolder method offer.

@Override
public <E> DataTransactionResult offer(Key<? extends BaseValue<E>> key, E value) {
    TimingsManager.DATA_GROUP_HANDLER.startTimingIfSync();
    SpongeTimings.dataOfferKey.startTimingIfSync();
    final Optional<ValueProcessor<E, ? extends BaseValue<E>>> optional = DataUtil.getBaseValueProcessor(key);
    if (optional.isPresent()) {
        final DataTransactionResult result = optional.get().offerToStore(this, value);
        SpongeTimings.dataOfferKey.stopTimingIfSync();
        TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
        return result;
    } else if (this instanceof IMixinCustomDataHolder) {
        final DataTransactionResult result = ((IMixinCustomDataHolder) this).offerCustom(key, value);
        SpongeTimings.dataOfferKey.stopTimingIfSync();
        TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
        return result;
    }
    SpongeTimings.dataOfferKey.stopTimingIfSync();
    TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
    return DataTransactionResult.failNoData();
}
Also used : BaseValue(org.spongepowered.api.data.value.BaseValue) ValueProcessor(org.spongepowered.common.data.ValueProcessor) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)

Example 3 with BaseValue

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

use of org.spongepowered.api.data.value.BaseValue 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();
}
Also used : Processor(org.lanternpowered.server.data.processor.Processor) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) IImmutableDataManipulator(org.lanternpowered.server.data.manipulator.immutable.IImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) BaseValue(org.spongepowered.api.data.value.BaseValue) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) ValueProcessorKeyRegistration(org.lanternpowered.server.data.processor.ValueProcessorKeyRegistration) CompositeValueStore(org.spongepowered.api.data.value.mutable.CompositeValueStore) ValueProcessorKeyRegistration(org.lanternpowered.server.data.processor.ValueProcessorKeyRegistration) Key(org.spongepowered.api.data.key.Key)

Example 5 with BaseValue

use of org.spongepowered.api.data.value.BaseValue in project LanternServer by LanternPowered.

the class IImmutableValueHolder method getImmutableValueFor.

/**
 * Attempts to get a {@link ImmutableValue} for the given
 * {@link Key}. {@link Optional#empty()} will be returned
 * when it fails.
 *
 * @param key The key
 * @return The immutable value, if success
 */
default <E, R extends ImmutableValue<E>> Optional<R> getImmutableValueFor(Key<? extends BaseValue<E>> key) {
    checkNotNull(key, "key");
    final ImmutableContainerCache cache = getContainerCache();
    if (cache != null) {
        Object value = cache.values.get(key);
        if (value != null) {
            return value == ImmutableContainerCache.NONE ? Optional.empty() : Optional.of((R) value);
        }
    }
    Optional optValue = getRawValueFor((Key) key);
    if (optValue.isPresent() && !(optValue.get() instanceof ImmutableValue)) {
        optValue = Optional.of(((Value) optValue.get()).asImmutable());
    }
    if (cache != null) {
        cache.values.put(key, optValue.orElse(ImmutableContainerCache.NONE));
    }
    return Optional.empty();
}
Also used : ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) Optional(java.util.Optional) Value(org.spongepowered.api.data.value.mutable.Value) BaseValue(org.spongepowered.api.data.value.BaseValue) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue)

Aggregations

BaseValue (org.spongepowered.api.data.value.BaseValue)8 Key (org.spongepowered.api.data.key.Key)6 Optional (java.util.Optional)4 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Sponge (org.spongepowered.api.Sponge)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 DataScanner (io.github.nucleuspowered.nucleus.internal.DataScanner)2 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)2 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)2 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)2 PermissionInformation (io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation)2 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Processor (org.lanternpowered.server.data.processor.Processor)2 BlockState (org.spongepowered.api.block.BlockState)2 CommandResult (org.spongepowered.api.command.CommandResult)2