Search in sources :

Example 1 with CompositeValueStore

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

the class CompositeValueStoreHelper method offerFast.

protected static <H extends ValueContainer<?>> boolean offerFast(ICompositeValueStore<?, H> store, Iterable<H> valueContainers, MergeFunction function) {
    // Leave this, the compiler complains
    final CompositeValueStore store1 = store;
    if (store1 instanceof DataHolder) {
        final Set<Key<?>> keys = new HashSet<>();
        for (H valueContainer : valueContainers) {
            keys.addAll(valueContainer.getKeys());
        }
        final boolean hasListeners = hasListeners(store, keys);
        if (hasListeners) {
            return offer(store, valueContainers, function, () -> true).isSuccessful();
        }
    }
    return store.offerFastNoEvents(valueContainers, function);
}
Also used : DataHolder(org.spongepowered.api.data.DataHolder) CompositeValueStore(org.spongepowered.api.data.value.mutable.CompositeValueStore) LanternKey(org.lanternpowered.server.data.key.LanternKey) Key(org.spongepowered.api.data.key.Key) HashSet(java.util.HashSet)

Example 2 with CompositeValueStore

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

the class DataRegistrar method setupRegistrations.

public static void setupRegistrations(LanternGame game) {
    Copyable.register(ImmutableMap.class, map -> map);
    Copyable.register(ImmutableList.class, list -> list);
    Copyable.register(ImmutableSet.class, set -> set);
    Copyable.register(List.class, ArrayList::new);
    Copyable.register(Set.class, HashSet::new);
    Copyable.register(Map.class, HashMap::new);
    final PropertyRegistry propertyRegistry = game.getPropertyRegistry();
    // Block property stores
    propertyRegistry.register(SkyLuminanceProperty.class, new SkyLuminancePropertyStore());
    propertyRegistry.register(GroundLuminanceProperty.class, new GroundLuminancePropertyStore());
    // Entity property stores
    propertyRegistry.register(DominantHandProperty.class, new DominantHandPropertyStore());
    // Item property stores
    propertyRegistry.register(SmeltableProperty.class, new SmeltablePropertyStore());
    propertyRegistry.register(BurningFuelProperty.class, new BurningFuelPropertyStore());
    final LanternDataManager dataManager = game.getDataManager();
    // Register the data type serializers
    DataTypeSerializers.registerSerializers(dataManager);
    // Register the data serializers
    DataTranslators.registerSerializers(dataManager);
    // Register the data builders
    dataManager.registerBuilder(PatternLayer.class, new LanternPatternLayer.Builder(game));
    dataManager.registerBuilder(Text.class, new TextConfigSerializer());
    dataManager.registerBuilder(BookView.class, new BookViewDataBuilder());
    dataManager.registerBuilder(PotionEffect.class, new LanternPotionEffectBuilder());
    dataManager.registerBuilder(RespawnLocation.class, new RespawnLocation.Builder());
    dataManager.registerBuilder(Enchantment.class, new LanternEnchantmentBuilder());
    final LanternValueFactory valueFactory = LanternValueFactory.get();
    valueFactory.registerKey(Keys.CONNECTED_DIRECTIONS).add(builder -> builder.applicableTester(valueContainer -> valueContainer.supports(Keys.CONNECTED_WEST) || valueContainer.supports(Keys.CONNECTED_EAST) || valueContainer.supports(Keys.CONNECTED_NORTH) || valueContainer.supports(Keys.CONNECTED_SOUTH)).retrieveHandler((valueContainer, key) -> {
        final Set<Direction> directions = new HashSet<>();
        if (valueContainer.get(Keys.CONNECTED_WEST).orElse(false)) {
            directions.add(Direction.WEST);
        }
        if (valueContainer.get(Keys.CONNECTED_EAST).orElse(false)) {
            directions.add(Direction.EAST);
        }
        if (valueContainer.get(Keys.CONNECTED_SOUTH).orElse(false)) {
            directions.add(Direction.SOUTH);
        }
        if (valueContainer.get(Keys.CONNECTED_NORTH).orElse(false)) {
            directions.add(Direction.NORTH);
        }
        return Optional.of(directions);
    }).offerHandler((valueContainer, key, directions) -> {
        if (valueContainer instanceof ICompositeValueStore) {
            final ICompositeValueStore store = (ICompositeValueStore) valueContainer;
            final DataTransactionResult.Builder resultBuilder = DataTransactionResult.builder();
            resultBuilder.absorbResult(store.offerNoEvents(Keys.CONNECTED_WEST, directions.contains(Direction.WEST)));
            resultBuilder.absorbResult(store.offerNoEvents(Keys.CONNECTED_EAST, directions.contains(Direction.EAST)));
            resultBuilder.absorbResult(store.offerNoEvents(Keys.CONNECTED_SOUTH, directions.contains(Direction.SOUTH)));
            resultBuilder.absorbResult(store.offerNoEvents(Keys.CONNECTED_NORTH, directions.contains(Direction.NORTH)));
            return resultBuilder.result(DataTransactionResult.Type.SUCCESS).build();
        }
        return DataTransactionResult.successNoData();
    }).failAlwaysRemoveHandler());
    valueFactory.registerKey(Keys.WIRE_ATTACHMENTS).add(builder -> builder.applicableTester(valueContainer -> valueContainer.supports(Keys.WIRE_ATTACHMENT_WEST) || valueContainer.supports(Keys.WIRE_ATTACHMENT_EAST) || valueContainer.supports(Keys.WIRE_ATTACHMENT_NORTH) || valueContainer.supports(Keys.WIRE_ATTACHMENT_SOUTH)).retrieveHandler((valueContainer, key) -> {
        final Map<Direction, WireAttachmentType> attachments = new HashMap<>();
        valueContainer.get(Keys.WIRE_ATTACHMENT_WEST).ifPresent(type -> attachments.put(Direction.WEST, type));
        valueContainer.get(Keys.WIRE_ATTACHMENT_EAST).ifPresent(type -> attachments.put(Direction.EAST, type));
        valueContainer.get(Keys.WIRE_ATTACHMENT_SOUTH).ifPresent(type -> attachments.put(Direction.SOUTH, type));
        valueContainer.get(Keys.WIRE_ATTACHMENT_NORTH).ifPresent(type -> attachments.put(Direction.NORTH, type));
        return Optional.of(attachments);
    }).offerHandler((key, valueContainer, attachments) -> {
        if (valueContainer instanceof ICompositeValueStore) {
            final ICompositeValueStore store = (ICompositeValueStore) valueContainer;
            final DataTransactionResult.Builder resultBuilder = DataTransactionResult.builder();
            WireAttachmentType type = attachments.get(Direction.WEST);
            resultBuilder.absorbResult(store.offerNoEvents(Keys.CONNECTED_WEST, type == null ? WireAttachmentTypes.NONE : type));
            type = attachments.get(Direction.EAST);
            resultBuilder.absorbResult(store.offerNoEvents(Keys.CONNECTED_EAST, type == null ? WireAttachmentTypes.NONE : type));
            type = attachments.get(Direction.SOUTH);
            resultBuilder.absorbResult(store.offerNoEvents(Keys.CONNECTED_SOUTH, type == null ? WireAttachmentTypes.NONE : type));
            type = attachments.get(Direction.NORTH);
            resultBuilder.absorbResult(store.offerNoEvents(Keys.CONNECTED_NORTH, type == null ? WireAttachmentTypes.NONE : type));
            return resultBuilder.result(DataTransactionResult.Type.SUCCESS).build();
        }
        return DataTransactionResult.successNoData();
    }).failAlwaysRemoveHandler());
    valueFactory.registerKey(Keys.BODY_ROTATIONS).add(builder -> builder.applicableTester(valueContainer -> valueContainer.supports(Keys.RIGHT_ARM_ROTATION) || valueContainer.supports(Keys.LEFT_ARM_ROTATION) || valueContainer.supports(Keys.RIGHT_LEG_ROTATION) || valueContainer.supports(Keys.LEFT_LEG_ROTATION) || valueContainer.supports(Keys.HEAD_ROTATION) || valueContainer.supports(Keys.CHEST_ROTATION)).retrieveHandler((valueContainer, key) -> {
        final Map<BodyPart, Vector3d> rotations = new HashMap<>();
        valueContainer.get(Keys.RIGHT_ARM_ROTATION).ifPresent(type -> rotations.put(BodyParts.RIGHT_ARM, type));
        valueContainer.get(Keys.RIGHT_LEG_ROTATION).ifPresent(type -> rotations.put(BodyParts.RIGHT_LEG, type));
        valueContainer.get(Keys.LEFT_ARM_ROTATION).ifPresent(type -> rotations.put(BodyParts.LEFT_ARM, type));
        valueContainer.get(Keys.LEFT_LEG_ROTATION).ifPresent(type -> rotations.put(BodyParts.LEFT_LEG, type));
        valueContainer.get(Keys.HEAD_ROTATION).ifPresent(type -> rotations.put(BodyParts.HEAD, type));
        valueContainer.get(Keys.CHEST_ROTATION).ifPresent(type -> rotations.put(BodyParts.CHEST, type));
        return Optional.of(rotations);
    }).offerHandler((key, valueContainer, rotations) -> {
        if (valueContainer instanceof CompositeValueStore) {
            final ICompositeValueStore store = (ICompositeValueStore) valueContainer;
            final DataTransactionResult.Builder resultBuilder = DataTransactionResult.builder();
            Vector3d rot;
            if ((rot = rotations.get(BodyParts.RIGHT_ARM)) != null) {
                resultBuilder.absorbResult(store.offerNoEvents(Keys.RIGHT_ARM_ROTATION, rot));
            }
            if ((rot = rotations.get(BodyParts.RIGHT_LEG)) != null) {
                resultBuilder.absorbResult(store.offerNoEvents(Keys.RIGHT_LEG_ROTATION, rot));
            }
            if ((rot = rotations.get(BodyParts.LEFT_ARM)) != null) {
                resultBuilder.absorbResult(store.offerNoEvents(Keys.LEFT_ARM_ROTATION, rot));
            }
            if ((rot = rotations.get(BodyParts.LEFT_LEG)) != null) {
                resultBuilder.absorbResult(store.offerNoEvents(Keys.LEFT_LEG_ROTATION, rot));
            }
            if ((rot = rotations.get(BodyParts.HEAD)) != null) {
                resultBuilder.absorbResult(store.offerNoEvents(Keys.HEAD_ROTATION, rot));
            }
            if ((rot = rotations.get(BodyParts.CHEST)) != null) {
                resultBuilder.absorbResult(store.offerNoEvents(Keys.CHEST_ROTATION, rot));
            }
            return resultBuilder.result(DataTransactionResult.Type.SUCCESS).build();
        }
        return DataTransactionResult.successNoData();
    }).failAlwaysRemoveHandler());
    DataManipulatorRegistry.get();
}
Also used : DataManipulatorRegistry(org.lanternpowered.server.data.manipulator.DataManipulatorRegistry) Copyable(org.lanternpowered.server.util.copy.Copyable) SkyLuminanceProperty(org.spongepowered.api.data.property.block.SkyLuminanceProperty) Keys(org.spongepowered.api.data.key.Keys) PropertyRegistry(org.spongepowered.api.data.property.PropertyRegistry) BodyParts(org.spongepowered.api.data.type.BodyParts) Vector3d(com.flowpowered.math.vector.Vector3d) HashMap(java.util.HashMap) LanternValueFactory(org.lanternpowered.server.data.value.LanternValueFactory) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) GroundLuminanceProperty(org.spongepowered.api.data.property.block.GroundLuminanceProperty) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BookView(org.spongepowered.api.text.BookView) ImmutableList(com.google.common.collect.ImmutableList) DominantHandPropertyStore(org.lanternpowered.server.data.property.entity.DominantHandPropertyStore) Text(org.spongepowered.api.text.Text) TextConfigSerializer(org.spongepowered.api.text.serializer.TextConfigSerializer) SmeltableProperty(org.spongepowered.api.data.property.item.SmeltableProperty) WireAttachmentTypes(org.spongepowered.api.data.type.WireAttachmentTypes) Map(java.util.Map) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) PatternLayer(org.spongepowered.api.data.meta.PatternLayer) DataTranslators(org.lanternpowered.server.data.persistence.DataTranslators) WireAttachmentType(org.spongepowered.api.data.type.WireAttachmentType) GroundLuminancePropertyStore(org.lanternpowered.server.data.property.block.GroundLuminancePropertyStore) BookViewDataBuilder(org.spongepowered.api.text.serializer.BookViewDataBuilder) CompositeValueStore(org.spongepowered.api.data.value.mutable.CompositeValueStore) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) BurningFuelPropertyStore(org.lanternpowered.server.data.property.item.BurningFuelPropertyStore) Set(java.util.Set) RespawnLocation(org.spongepowered.api.util.RespawnLocation) LanternGame(org.lanternpowered.server.game.LanternGame) SkyLuminancePropertyStore(org.lanternpowered.server.data.property.block.SkyLuminancePropertyStore) LanternPotionEffectBuilder(org.lanternpowered.server.effect.potion.LanternPotionEffectBuilder) BodyPart(org.spongepowered.api.data.type.BodyPart) Direction(org.spongepowered.api.util.Direction) List(java.util.List) LanternEnchantmentBuilder(org.lanternpowered.server.item.enchantment.LanternEnchantmentBuilder) LanternPatternLayer(org.lanternpowered.server.data.meta.LanternPatternLayer) SmeltablePropertyStore(org.lanternpowered.server.data.property.item.SmeltablePropertyStore) DataTypeSerializers(org.lanternpowered.server.data.persistence.DataTypeSerializers) DominantHandProperty(org.spongepowered.api.data.property.entity.DominantHandProperty) Optional(java.util.Optional) BurningFuelProperty(org.spongepowered.api.data.property.item.BurningFuelProperty) DominantHandPropertyStore(org.lanternpowered.server.data.property.entity.DominantHandPropertyStore) HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashMap(java.util.HashMap) WireAttachmentType(org.spongepowered.api.data.type.WireAttachmentType) BookViewDataBuilder(org.spongepowered.api.text.serializer.BookViewDataBuilder) LanternEnchantmentBuilder(org.lanternpowered.server.item.enchantment.LanternEnchantmentBuilder) ArrayList(java.util.ArrayList) LanternPatternLayer(org.lanternpowered.server.data.meta.LanternPatternLayer) CompositeValueStore(org.spongepowered.api.data.value.mutable.CompositeValueStore) PropertyRegistry(org.spongepowered.api.data.property.PropertyRegistry) HashSet(java.util.HashSet) GroundLuminancePropertyStore(org.lanternpowered.server.data.property.block.GroundLuminancePropertyStore) RespawnLocation(org.spongepowered.api.util.RespawnLocation) TextConfigSerializer(org.spongepowered.api.text.serializer.TextConfigSerializer) SmeltablePropertyStore(org.lanternpowered.server.data.property.item.SmeltablePropertyStore) Vector3d(com.flowpowered.math.vector.Vector3d) BurningFuelPropertyStore(org.lanternpowered.server.data.property.item.BurningFuelPropertyStore) LanternPotionEffectBuilder(org.lanternpowered.server.effect.potion.LanternPotionEffectBuilder) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) SkyLuminancePropertyStore(org.lanternpowered.server.data.property.block.SkyLuminancePropertyStore) LanternValueFactory(org.lanternpowered.server.data.value.LanternValueFactory) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with CompositeValueStore

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

the class DataHelper method deserializeRawRegisteredKeyData.

public static void deserializeRawRegisteredKeyData(DataView dataView, IValueContainer valueContainer) throws InvalidDataException {
    dataView = dataView.getView(DataQueries.DATA_VALUES).orElse(null);
    if (dataView == null) {
        return;
    }
    final ValueCollection valueCollection = valueContainer.getValueCollection();
    final LanternDataManager dataManager = Lantern.getGame().getDataManager();
    final DataTypeSerializerContext context = dataManager.getTypeSerializerContext();
    for (KeyRegistration<?, ?> registration : valueCollection.getAll()) {
        final Key<?> key = registration.getKey();
        if (!(registration instanceof Element) || key == LanternKeys.FAILED_DATA_MANIPULATORS || key == LanternKeys.FAILED_DATA_VALUES) {
            continue;
        }
        final Optional<Object> data = dataView.get(key.getQuery());
        if (!data.isPresent()) {
            continue;
        }
        dataView.remove(key.getQuery());
        final TypeToken<?> typeToken = key.getElementToken();
        final DataTypeSerializer typeSerializer = dataManager.getTypeSerializer(typeToken).orElseThrow(() -> new IllegalStateException("Wasn't able to find a type serializer for the element type: " + typeToken.toString()));
        ((Element) registration).set(typeSerializer.deserialize(typeToken, context, data.get()));
    }
    if (valueContainer instanceof CompositeValueStore) {
        final CompositeValueStore store = (CompositeValueStore) valueContainer;
        for (Map.Entry<DataQuery, Object> entry : dataView.getValues(false).entrySet()) {
            final Key<?> key = KeyRegistryModule.get().getByQuery(entry.getKey()).orElse(null);
            if (key == null) {
                continue;
            }
            final TypeToken<?> typeToken = key.getElementToken();
            final DataTypeSerializer typeSerializer = dataManager.getTypeSerializer(typeToken).orElseThrow(() -> new IllegalStateException("Wasn't able to find a type serializer for the element type: " + typeToken.toString()));
            store.offer(key, typeSerializer.deserialize(typeToken, context, entry.getValue()));
            dataView.remove(entry.getKey());
        }
    }
    if (!dataView.isEmpty()) {
        // Should be safe to cast, at least if nobody touches this key
        Element<DataView> holder = valueCollection.getElement(LanternKeys.FAILED_DATA_VALUES).orElse(null);
        if (holder == null) {
            holder = valueCollection.register(LanternKeys.FAILED_DATA_VALUES, null);
        }
        holder.set(dataView);
    }
}
Also used : DataTypeSerializerContext(org.lanternpowered.server.data.persistence.DataTypeSerializerContext) Element(org.lanternpowered.server.data.element.Element) DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) CompositeValueStore(org.spongepowered.api.data.value.mutable.CompositeValueStore) Map(java.util.Map)

Example 4 with CompositeValueStore

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

Aggregations

CompositeValueStore (org.spongepowered.api.data.value.mutable.CompositeValueStore)4 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Key (org.spongepowered.api.data.key.Key)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Element (org.lanternpowered.server.data.element.Element)1 LanternKey (org.lanternpowered.server.data.key.LanternKey)1 DataManipulatorRegistry (org.lanternpowered.server.data.manipulator.DataManipulatorRegistry)1 IImmutableDataManipulator (org.lanternpowered.server.data.manipulator.immutable.IImmutableDataManipulator)1 LanternPatternLayer (org.lanternpowered.server.data.meta.LanternPatternLayer)1 DataTranslators (org.lanternpowered.server.data.persistence.DataTranslators)1 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)1