Search in sources :

Example 6 with ImmutableValue

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

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

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

Example 9 with ImmutableValue

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

Example 10 with ImmutableValue

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

the class TestInventoryPlugin method onInit.

@Listener
public void onInit(GameInitializationEvent event) {
    this.myArchetype = InventoryArchetype.builder().with(InventoryArchetypes.MENU_ROW).title(Text.of("My Fancy Title")).property(InventoryDimension.of(9, 1)).property(new GuiIdProperty(GuiIds.CHEST)).build("inventory_test:test", "Test Inventory");
    Sponge.getCommandManager().register(this, CommandSpec.builder().executor((src, args) -> {
        if (!(src instanceof Player)) {
            throw new CommandException(t("Only players may use this command."));
        }
        final Inventory inventory = Inventory.builder().of(this.myArchetype).withCarrier((Carrier) src).build(this);
        System.out.println(inventory.getClass().getName());
        final ItemStack itemStack = ItemStack.of(ItemTypes.LOG, 64);
        itemStack.offer(Keys.TREE_TYPE, TreeTypes.JUNGLE);
        inventory.offer(itemStack);
        ((Player) src).openInventory(inventory);
        return CommandResult.success();
    }).build(), "test-a-inv");
    Keys.COAL_TYPE.registerEvent(ItemStack.class, event1 -> {
        final DataTransactionResult result = event1.getEndResult();
        final List<ImmutableValue<?>> newSuccessfulData = new ArrayList<>(result.getSuccessfulData());
        Iterator<ImmutableValue<?>> it = newSuccessfulData.iterator();
        while (it.hasNext()) {
            final ImmutableValue<?> value = it.next();
            if (value.getKey() == Keys.COAL_TYPE) {
                System.out.println("Changed coal type to: " + ((CoalType) value.get()).getId() + ", but this not allowed");
                it.remove();
                break;
            }
        }
        final List<ImmutableValue<?>> newReplacedData = new ArrayList<>(result.getReplacedData());
        it = newSuccessfulData.iterator();
        while (it.hasNext()) {
            final ImmutableValue<?> value = it.next();
            if (value.getKey() == Keys.COAL_TYPE) {
                it.remove();
                break;
            }
        }
        event1.proposeChanges(DataTransactionResult.builder().result(result.getType()).reject(result.getRejectedData()).replace(newReplacedData).success(newSuccessfulData).build());
    });
}
Also used : GuiIdProperty(org.spongepowered.api.item.inventory.property.GuiIdProperty) Player(org.spongepowered.api.entity.living.player.Player) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) ArrayList(java.util.ArrayList) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) CommandException(org.spongepowered.api.command.CommandException) Carrier(org.spongepowered.api.item.inventory.Carrier) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) Listener(org.spongepowered.api.event.Listener)

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