Search in sources :

Example 11 with SlotLens

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

Example 12 with SlotLens

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

the class AbstractLens method getSlotLens.

@Override
@Nullable
public SlotLens getSlotLens(Fabric fabric, int ordinal) {
    if (ordinal < 0 || ordinal > this.maxOrdinal) {
        return null;
    }
    int offset = 0;
    for (final Lens child : this.spanningChildren) {
        int count = child.slotCount();
        if (ordinal < offset + count) {
            return child.getSlotLens(fabric, ordinal - offset);
        }
        offset += count;
    }
    return null;
}
Also used : QueriedSlotLens(org.spongepowered.common.inventory.lens.impl.slot.QueriedSlotLens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) Lens(org.spongepowered.common.inventory.lens.Lens) DelegatingSlotLens(org.spongepowered.common.inventory.lens.impl.slot.DelegatingSlotLens) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 13 with SlotLens

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

the class AbstractContainerMenuMixin_Adapter_Inventory method inventoryAdapter$getSlot.

@Override
public Optional<org.spongepowered.api.item.inventory.Slot> inventoryAdapter$getSlot(int ordinal) {
    org.spongepowered.api.item.inventory.Slot slot = this.impl$slots.get(ordinal);
    if (slot == null) {
        Lens rootLens = this.inventoryAdapter$getRootLens();
        SlotLens slotLens = rootLens.getSlotLens(this.inventoryAdapter$getFabric(), ordinal);
        if (slotLens == null) {
            return Optional.empty();
        }
        slot = slotLens.getAdapter(this.inventoryAdapter$getFabric(), ((Inventory) this));
        this.impl$slots.put(ordinal, slot);
    }
    return Optional.of(slot);
}
Also used : SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) Lens(org.spongepowered.common.inventory.lens.Lens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens)

Aggregations

SlotLens (org.spongepowered.common.inventory.lens.slots.SlotLens)13 Lens (org.spongepowered.common.inventory.lens.Lens)6 EquipmentType (org.spongepowered.api.item.inventory.equipment.EquipmentType)5 InventoryAdapter (org.spongepowered.common.inventory.adapter.InventoryAdapter)4 LinkedHashMap (java.util.LinkedHashMap)3 Fabric (org.spongepowered.common.inventory.fabric.Fabric)3 EquipmentInventoryLens (org.spongepowered.common.inventory.lens.impl.comp.EquipmentInventoryLens)3 Map (java.util.Map)2 EquipmentSlot (net.minecraft.world.entity.EquipmentSlot)2 Inventory (org.spongepowered.api.item.inventory.Inventory)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 InventoryBridge (org.spongepowered.common.bridge.world.inventory.InventoryBridge)2 SlotAdapter (org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter)2 DelegatingSlotLens (org.spongepowered.common.inventory.lens.impl.slot.DelegatingSlotLens)2 QueriedSlotLens (org.spongepowered.common.inventory.lens.impl.slot.QueriedSlotLens)2 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 Key (org.spongepowered.api.data.Key)1