Search in sources :

Example 16 with Key

use of org.spongepowered.api.data.key.Key in project SpongeCommon by SpongePowered.

the class TileEntityCommandDataProcessor method getValues.

@Override
protected Map<Key<?>, ?> getValues(TileEntityCommandBlock entity) {
    CommandBlockBaseLogic logic = entity.getCommandBlockLogic();
    Map<Key<?>, Object> values = Maps.newHashMapWithExpectedSize(4);
    Optional<Text> lastCommandOutput = logic.getLastOutput() != null ? Optional.of(SpongeTexts.toText(logic.getLastOutput())) : Optional.empty();
    values.put(Keys.LAST_COMMAND_OUTPUT, lastCommandOutput);
    values.put(Keys.COMMAND, logic.commandStored);
    values.put(Keys.SUCCESS_COUNT, logic.successCount);
    values.put(Keys.TRACKS_OUTPUT, logic.shouldTrackOutput());
    return values;
}
Also used : CommandBlockBaseLogic(net.minecraft.tileentity.CommandBlockBaseLogic) Text(org.spongepowered.api.text.Text) Key(org.spongepowered.api.data.key.Key)

Example 17 with Key

use of org.spongepowered.api.data.key.Key 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 18 with Key

use of org.spongepowered.api.data.key.Key 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 19 with Key

use of org.spongepowered.api.data.key.Key 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 20 with Key

use of org.spongepowered.api.data.key.Key in project LanternServer by LanternPowered.

the class IValueContainer method getValues.

@SuppressWarnings("unchecked")
@Override
default Set<ImmutableValue<?>> getValues() {
    final ImmutableSet.Builder<ImmutableValue<?>> values = ImmutableSet.builder();
    // Check local registrations
    for (KeyRegistration<?, ?> entry : getValueCollection().getAll()) {
        final Key key = entry.getKey();
        final Optional<BaseValue> optValue = getValue(key);
        optValue.ifPresent(baseValue -> values.add(ValueHelper.toImmutable(baseValue)));
    }
    // Check for global registrations
    for (ValueProcessorKeyRegistration<?, ?> registration : LanternValueFactory.get().getKeyRegistrations()) {
        final Optional<BaseValue> optValue = ((Processor) registration).getValueFrom(this);
        optValue.ifPresent(baseValue -> values.add(ValueHelper.toImmutable(baseValue)));
    }
    // Check if custom data is supported by this container
    if (this instanceof AdditionalContainerHolder) {
        final AdditionalContainerCollection<?> containers = ((AdditionalContainerHolder<?>) this).getAdditionalContainers();
        containers.getAll().forEach(manipulator -> values.addAll(manipulator.getValues()));
    }
    return values.build();
}
Also used : ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) Processor(org.lanternpowered.server.data.processor.Processor) ImmutableSet(com.google.common.collect.ImmutableSet) BaseValue(org.spongepowered.api.data.value.BaseValue) Key(org.spongepowered.api.data.key.Key)

Aggregations

Key (org.spongepowered.api.data.key.Key)28 HashMap (java.util.HashMap)8 Map (java.util.Map)8 ArrayList (java.util.ArrayList)7 BlockState (org.spongepowered.api.block.BlockState)7 BaseValue (org.spongepowered.api.data.value.BaseValue)7 List (java.util.List)6 Optional (java.util.Optional)6 Sponge (org.spongepowered.api.Sponge)5 Text (org.spongepowered.api.text.Text)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 Collection (java.util.Collection)4 BlockType (org.spongepowered.api.block.BlockType)4 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)4 World (org.spongepowered.api.world.World)4 Set (java.util.Set)3 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)3 CommandContext (org.spongepowered.api.command.args.CommandContext)3 CommandElement (org.spongepowered.api.command.args.CommandElement)3 DataView (org.spongepowered.api.data.DataView)3