Search in sources :

Example 16 with Lens

use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.

the class KeyValueMatcherQuery method execute.

@Override
public Inventory execute(final Inventory inventory, final InventoryAdapter adapter) {
    final Fabric fabric = adapter.inventoryAdapter$getFabric();
    final Lens lens = adapter.inventoryAdapter$getRootLens();
    if (this.matches(lens, null, inventory)) {
        return lens.getAdapter(fabric, inventory);
    }
    return this.toResult(inventory, fabric, this.reduce(fabric, lens, this.search(inventory, lens)));
}
Also used : DelegatingLens(org.spongepowered.common.inventory.lens.impl.DelegatingLens) Lens(org.spongepowered.common.inventory.lens.Lens) Fabric(org.spongepowered.common.inventory.fabric.Fabric)

Example 17 with Lens

use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.

the class KeyValueMatcherQuery method depthLaterSearch.

private Map<Lens, Integer> depthLaterSearch(final Inventory inventory, final Lens lens) {
    final Map<Lens, Integer> matches = new LinkedHashMap<>();
    // Search for any match with the direct children
    for (final Lens child : lens.getChildren()) {
        if (child == null) {
            continue;
        }
        if (this.matches(child, lens, inventory)) {
            matches.put(child, 0);
        }
    }
    // If no match was found go one layer deeper
    if (matches.isEmpty()) {
        for (final Lens child : lens.getChildren()) {
            if (child == null) {
                continue;
            }
            if (!child.getChildren().isEmpty()) {
                matches.putAll(this.depthLaterSearch(inventory, child));
            }
        }
    }
    this.delegateOffset(lens, matches);
    return matches;
}
Also used : DelegatingLens(org.spongepowered.common.inventory.lens.impl.DelegatingLens) Lens(org.spongepowered.common.inventory.lens.Lens) LinkedHashMap(java.util.LinkedHashMap)

Example 18 with Lens

use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.

the class KeyValueMatcherQuery method search.

private Map<Lens, Integer> search(final Inventory inventory, final Lens lens) {
    if (inventory instanceof AbstractContainerMenu) {
        final Map<Lens, Integer> matches = new LinkedHashMap<>();
        // Search for Container Slot properties
        for (final net.minecraft.world.inventory.Slot slot : ((AbstractContainerMenu) inventory).slots) {
            if (this.matches(null, null, (Inventory) slot)) {
                matches.put(((InventoryAdapter) slot).inventoryAdapter$getRootLens(), 0);
            }
        }
        if (!matches.isEmpty()) {
            return matches;
        }
        // Search for Container Viewed inventory properties
        final Set<Inventory> viewedInventories = new HashSet<>();
        for (final Slot slot : inventory.slots()) {
            viewedInventories.add(slot.viewedSlot().parent());
        }
        // TODO does this work?
        for (final Inventory viewedInventory : viewedInventories) {
            if (this.matches(null, null, viewedInventory)) {
                matches.put(((InventoryAdapter) viewedInventory).inventoryAdapter$getRootLens(), 0);
            }
        }
        if (!matches.isEmpty()) {
            this.delegateOffset(lens, matches);
            return matches;
        }
    }
    return this.depthLaterSearch(inventory, lens);
}
Also used : AbstractContainerMenu(net.minecraft.world.inventory.AbstractContainerMenu) Slot(org.spongepowered.api.item.inventory.Slot) DelegatingLens(org.spongepowered.common.inventory.lens.impl.DelegatingLens) Lens(org.spongepowered.common.inventory.lens.Lens) Inventory(org.spongepowered.api.item.inventory.Inventory) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet)

Example 19 with Lens

use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.

the class SpongeDepthQuery method execute.

public Inventory execute(Inventory inventory, InventoryAdapter adapter) {
    final Fabric fabric = adapter.inventoryAdapter$getFabric();
    final Lens lens = adapter.inventoryAdapter$getRootLens();
    if (this.matches(lens, null, inventory)) {
        return lens.getAdapter(fabric, inventory);
    }
    return this.toResult(inventory, fabric, this.reduce(fabric, lens, this.depthFirstSearch(inventory, lens)));
}
Also used : DelegatingLens(org.spongepowered.common.inventory.lens.impl.DelegatingLens) Lens(org.spongepowered.common.inventory.lens.Lens) Fabric(org.spongepowered.common.inventory.fabric.Fabric)

Example 20 with Lens

use of org.spongepowered.common.inventory.lens.Lens 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

Lens (org.spongepowered.common.inventory.lens.Lens)22 SlotLens (org.spongepowered.common.inventory.lens.slots.SlotLens)10 DelegatingLens (org.spongepowered.common.inventory.lens.impl.DelegatingLens)9 InventoryAdapter (org.spongepowered.common.inventory.adapter.InventoryAdapter)8 Fabric (org.spongepowered.common.inventory.fabric.Fabric)8 PlayerInventoryLens (org.spongepowered.common.inventory.lens.impl.minecraft.PlayerInventoryLens)7 LinkedHashMap (java.util.LinkedHashMap)5 CraftingInventoryLens (org.spongepowered.common.inventory.lens.impl.comp.CraftingInventoryLens)5 PrimaryPlayerInventoryLens (org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens)5 SingleGridLens (org.spongepowered.common.inventory.lens.impl.minecraft.SingleGridLens)5 ContainerLens (org.spongepowered.common.inventory.lens.impl.minecraft.container.ContainerLens)5 EquipmentInventoryAdapter (org.spongepowered.common.inventory.adapter.impl.comp.EquipmentInventoryAdapter)4 PrimaryPlayerInventoryAdapter (org.spongepowered.common.inventory.adapter.impl.comp.PrimaryPlayerInventoryAdapter)4 BasicSlotLens (org.spongepowered.common.inventory.lens.impl.slot.BasicSlotLens)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 CraftingContainer (net.minecraft.world.inventory.CraftingContainer)3 CompoundLens (org.spongepowered.common.inventory.lens.impl.CompoundLens)3 HashMap (java.util.HashMap)2 List (java.util.List)2