Search in sources :

Example 11 with Key

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

the class VanillaDataStore method serialize.

@Override
@SuppressWarnings(value = { "unchecked", "rawtypes" })
public DataView serialize(DataManipulator dataManipulator, DataView view) {
    for (Map.Entry<Key<?>, Tuple<BiConsumer<DataView, ?>, Function<DataView, Optional<?>>>> entry : this.queriesByKey.entrySet()) {
        final BiConsumer serializer = entry.getValue().first();
        dataManipulator.get((Key) entry.getKey()).ifPresent(value -> serializer.accept(view, value));
    }
    return view;
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) Optional(java.util.Optional) Map(java.util.Map) Key(org.spongepowered.api.data.Key) Tuple(org.spongepowered.api.util.Tuple) BiConsumer(java.util.function.BiConsumer)

Example 12 with Key

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

the class SpongeKeyBuilder method build0.

@Override
public Key<V> build0() {
    Objects.requireNonNull(this.valueType, "The value type must be set");
    Objects.requireNonNull(this.elementType, "The element type must be set");
    BiPredicate<? super E, ? super E> includesTester = this.includesTester;
    if (includesTester == null) {
        includesTester = (e, e2) -> false;
    }
    Comparator<? super E> comparator = this.comparator;
    if (comparator == null) {
        if (Comparable.class.isAssignableFrom(GenericTypeReflector.erase(this.elementType))) {
            // noinspection unchecked
            comparator = Comparator.comparing(o -> ((Comparable) o));
        } else {
            comparator = (o1, o2) -> {
                if (o1.equals(o2))
                    return 0;
                // There could be collisions, but yeah, what can you do about that..
                if (o1.hashCode() > o2.hashCode())
                    return 1;
                return -1;
            };
        }
    }
    Supplier<E> defaultValueSupplier = () -> null;
    final Class<?> rawType = GenericTypeReflector.erase(this.valueType);
    if (ListValue.class.isAssignableFrom(rawType)) {
        defaultValueSupplier = () -> (E) new ArrayList();
    } else if (SetValue.class.isAssignableFrom(rawType)) {
        defaultValueSupplier = () -> (E) new HashSet();
    } else if (WeightedCollectionValue.class.isAssignableFrom(rawType)) {
        defaultValueSupplier = () -> (E) new WeightedTable();
    } else if (MapValue.class.isAssignableFrom(rawType)) {
        defaultValueSupplier = () -> (E) new HashMap<>();
    }
    final SpongeKey<Value<E>, E> key = new SpongeKey<>(this.key, this.valueType, this.elementType, comparator, includesTester, defaultValueSupplier);
    KeyProvider.INSTANCE.register(this.key, (Key<Value<?>>) (Object) key);
    return (Key<V>) key;
}
Also used : KeyProvider(org.spongepowered.common.registry.provider.KeyProvider) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) Value(org.spongepowered.api.data.value.Value) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) WeightedCollectionValue(org.spongepowered.api.data.value.WeightedCollectionValue) AbstractResourceKeyedBuilder(org.spongepowered.common.util.AbstractResourceKeyedBuilder) Map(java.util.Map) Nullable(org.checkerframework.checker.nullness.qual.Nullable) TypeFactory(io.leangen.geantyref.TypeFactory) ListValue(org.spongepowered.api.data.value.ListValue) Set(java.util.Set) TypeToken(io.leangen.geantyref.TypeToken) SetValue(org.spongepowered.api.data.value.SetValue) Key(org.spongepowered.api.data.Key) Objects(java.util.Objects) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) Preconditions(com.google.common.base.Preconditions) GenericTypeReflector(io.leangen.geantyref.GenericTypeReflector) MapValue(org.spongepowered.api.data.value.MapValue) Comparator(java.util.Comparator) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) ArrayList(java.util.ArrayList) MapValue(org.spongepowered.api.data.value.MapValue) Value(org.spongepowered.api.data.value.Value) WeightedCollectionValue(org.spongepowered.api.data.value.WeightedCollectionValue) ListValue(org.spongepowered.api.data.value.ListValue) SetValue(org.spongepowered.api.data.value.SetValue) MapValue(org.spongepowered.api.data.value.MapValue) SetValue(org.spongepowered.api.data.value.SetValue) Key(org.spongepowered.api.data.Key) HashSet(java.util.HashSet)

Example 13 with Key

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

the class SpongeQuery method reduce.

protected Map<Lens, Integer> reduce(Fabric fabric, Lens lens, Map<Lens, Integer> matches) {
    if (matches.isEmpty()) {
        return Collections.emptyMap();
    }
    // Check if all matches are the direct children of this lens
    List<Lens> lensSlots = lens.getChildren();
    if (lensSlots.size() == matches.size() && matches.keySet().containsAll(lensSlots)) {
        // return parent lens instead of constructing a new for the query result
        matches.clear();
        matches.put(lens, 0);
        return matches;
    }
    // Remove duplicate slot-lenses
    Map<SlotLens, Map<Key<?>, Object>> lenses = new LinkedHashMap<>();
    Map<Lens, Integer> toRemove = new HashMap<>();
    for (Map.Entry<Lens, Integer> entry : matches.entrySet()) {
        final Lens slotLens = entry.getKey();
        if (slotLens.slotCount() == 1) {
            // Remove Lens with one slot
            toRemove.put(slotLens, matches.get(slotLens));
            // Find SlotLens for that Lens
            final SlotLens sl = slotLens.getSlotLens(fabric, 0);
            final Lens parent = slotLens.getParent();
            final Map<Key<?>, Object> dataAt = parent == null ? Collections.emptyMap() : parent.getDataFor(slotLens);
            // Collect all data for the SlotLens
            lenses.computeIfAbsent(sl, k -> new HashMap<>()).putAll(dataAt);
        }
    }
    // remove all single-slot lenses
    matches.keySet().removeAll(toRemove.keySet());
    for (Map.Entry<SlotLens, Map<Key<?>, Object>> entry : lenses.entrySet()) {
        final Map<Key<?>, Object> data = entry.getValue();
        if (data.isEmpty()) {
            // add back slot-lenses
            matches.put(entry.getKey(), toRemove.getOrDefault(entry.getKey(), 0));
        } else {
            // with data if found
            final QueriedSlotLens delegatingSlotLens = new QueriedSlotLens(entry.getKey(), data);
            matches.put(delegatingSlotLens, toRemove.getOrDefault(entry.getKey(), 0));
        }
    }
    return matches;
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) DelegatingLens(org.spongepowered.common.inventory.lens.impl.DelegatingLens) QueriedSlotLens(org.spongepowered.common.inventory.lens.impl.slot.QueriedSlotLens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) HashMap(java.util.HashMap) InventoryBridge(org.spongepowered.common.bridge.world.inventory.InventoryBridge) Fabric(org.spongepowered.common.inventory.fabric.Fabric) QueryLens(org.spongepowered.common.inventory.lens.impl.QueryLens) Key(org.spongepowered.api.data.Key) EmptyInventoryImpl(org.spongepowered.common.inventory.EmptyInventoryImpl) LensRegistrar(org.spongepowered.common.inventory.lens.impl.LensRegistrar) LinkedHashMap(java.util.LinkedHashMap) Query(org.spongepowered.api.item.inventory.query.Query) List(java.util.List) Map(java.util.Map) Lens(org.spongepowered.common.inventory.lens.Lens) DelegatingSlotLens(org.spongepowered.common.inventory.lens.impl.slot.DelegatingSlotLens) Collections(java.util.Collections) InventoryAdapter(org.spongepowered.common.inventory.adapter.InventoryAdapter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) QueriedSlotLens(org.spongepowered.common.inventory.lens.impl.slot.QueriedSlotLens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) DelegatingSlotLens(org.spongepowered.common.inventory.lens.impl.slot.DelegatingSlotLens) LinkedHashMap(java.util.LinkedHashMap) DelegatingLens(org.spongepowered.common.inventory.lens.impl.DelegatingLens) QueriedSlotLens(org.spongepowered.common.inventory.lens.impl.slot.QueriedSlotLens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) QueryLens(org.spongepowered.common.inventory.lens.impl.QueryLens) Lens(org.spongepowered.common.inventory.lens.Lens) DelegatingSlotLens(org.spongepowered.common.inventory.lens.impl.slot.DelegatingSlotLens) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Key(org.spongepowered.api.data.Key) QueriedSlotLens(org.spongepowered.common.inventory.lens.impl.slot.QueriedSlotLens)

Aggregations

Key (org.spongepowered.api.data.Key)13 ResourceKey (org.spongepowered.api.ResourceKey)5 Value (org.spongepowered.api.data.value.Value)5 List (java.util.List)4 Map (java.util.Map)4 GenericTypeReflector (io.leangen.geantyref.GenericTypeReflector)3 TypeToken (io.leangen.geantyref.TypeToken)3 Field (java.lang.reflect.Field)3 Type (java.lang.reflect.Type)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Optional (java.util.Optional)3 BiConsumer (java.util.function.BiConsumer)3 Nullable (org.checkerframework.checker.nullness.qual.Nullable)3 Label (org.objectweb.asm.Label)3 DataView (org.spongepowered.api.data.persistence.DataView)3 ValueContainer (org.spongepowered.api.data.value.ValueContainer)3 Tuple (org.spongepowered.api.util.Tuple)3 ImmutableList (com.google.common.collect.ImmutableList)2 ParameterizedType (java.lang.reflect.ParameterizedType)2