Search in sources :

Example 1 with EmptyInventoryImpl

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

the class SpongeQuery method toResult.

protected Inventory toResult(Inventory inventory, Fabric fabric, Map<Lens, Integer> matches) {
    if (matches.isEmpty()) {
        return new EmptyInventoryImpl(inventory);
    }
    if (matches.size() == 1) {
        final Map.Entry<Lens, Integer> entry = matches.entrySet().iterator().next();
        if (entry.getValue() == 0) {
            return entry.getKey().getAdapter(fabric, inventory);
        }
        final LensRegistrar.BasicSlotLensProvider slotProvider = new LensRegistrar.BasicSlotLensProvider(entry.getKey().slotCount());
        // TODO check correct slotprovider
        final DelegatingLens delegate = new DelegatingLens(entry.getValue(), entry.getKey(), slotProvider);
        return delegate.getAdapter(fabric, inventory);
    }
    final QueryLens lens = new QueryLens(matches, this);
    return lens.getAdapter(fabric, inventory);
}
Also used : LensRegistrar(org.spongepowered.common.inventory.lens.impl.LensRegistrar) EmptyInventoryImpl(org.spongepowered.common.inventory.EmptyInventoryImpl) 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) DelegatingLens(org.spongepowered.common.inventory.lens.impl.DelegatingLens) QueryLens(org.spongepowered.common.inventory.lens.impl.QueryLens)

Example 2 with EmptyInventoryImpl

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

the class GridQuery method execute.

@Override
public Inventory execute(Inventory inventory, InventoryAdapter adapter) {
    if (!(adapter instanceof GridInventoryAdapter)) {
        return new EmptyInventoryImpl(inventory);
    }
    GridInventoryAdapter gridAdapter = (GridInventoryAdapter) adapter;
    Vector2i max = gridAdapter.dimensions();
    if (max.x() < this.offset.x() + this.size.x() && max.y() < this.offset.y() + this.size.y()) {
        // queried grid does not fit inventory
        return new EmptyInventoryImpl(inventory);
    }
    // Get slots for new grid
    CompoundSlotLensProvider slotProvider = new CompoundSlotLensProvider();
    for (int dy = 0; dy < this.size.y(); dy++) {
        for (int dx = 0; dx < this.size.x(); dx++) {
            slotProvider.add(gridAdapter.getSlotLens(this.offset.x() + dx, this.offset.y() + dy));
        }
    }
    // build new grid lens
    GridInventoryLens lens = new GridInventoryLens(0, this.size.x(), this.size.y(), slotProvider);
    return new GridInventoryAdapter(adapter.inventoryAdapter$getFabric(), lens, inventory);
}
Also used : GridInventoryAdapter(org.spongepowered.common.inventory.adapter.impl.comp.GridInventoryAdapter) GridInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.GridInventoryLens) CompoundSlotLensProvider(org.spongepowered.common.inventory.lens.CompoundSlotLensProvider) EmptyInventoryImpl(org.spongepowered.common.inventory.EmptyInventoryImpl) Vector2i(org.spongepowered.math.vector.Vector2i)

Example 3 with EmptyInventoryImpl

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

the class AppendQuery method execute.

@Override
public Inventory execute(Inventory inventory, InventoryAdapter adapter) {
    Inventory result = new EmptyInventoryImpl(inventory);
    if (this.queryList.isEmpty()) {
        return result;
    }
    List<Inventory> results = this.queryList.stream().map(q -> q.execute(inventory)).collect(Collectors.toList());
    return new MultiInventoryAdapter(results);
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) SpongeQuery(org.spongepowered.common.inventory.query.SpongeQuery) Query(org.spongepowered.api.item.inventory.query.Query) Arrays(java.util.Arrays) List(java.util.List) MultiInventoryAdapter(org.spongepowered.common.inventory.adapter.impl.MultiInventoryAdapter) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) EmptyInventoryImpl(org.spongepowered.common.inventory.EmptyInventoryImpl) InventoryAdapter(org.spongepowered.common.inventory.adapter.InventoryAdapter) ArrayList(java.util.ArrayList) EmptyInventoryImpl(org.spongepowered.common.inventory.EmptyInventoryImpl) MultiInventoryAdapter(org.spongepowered.common.inventory.adapter.impl.MultiInventoryAdapter) Inventory(org.spongepowered.api.item.inventory.Inventory)

Aggregations

EmptyInventoryImpl (org.spongepowered.common.inventory.EmptyInventoryImpl)3 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Inventory (org.spongepowered.api.item.inventory.Inventory)1 Query (org.spongepowered.api.item.inventory.query.Query)1 InventoryAdapter (org.spongepowered.common.inventory.adapter.InventoryAdapter)1 MultiInventoryAdapter (org.spongepowered.common.inventory.adapter.impl.MultiInventoryAdapter)1 GridInventoryAdapter (org.spongepowered.common.inventory.adapter.impl.comp.GridInventoryAdapter)1 CompoundSlotLensProvider (org.spongepowered.common.inventory.lens.CompoundSlotLensProvider)1 Lens (org.spongepowered.common.inventory.lens.Lens)1 DelegatingLens (org.spongepowered.common.inventory.lens.impl.DelegatingLens)1 LensRegistrar (org.spongepowered.common.inventory.lens.impl.LensRegistrar)1 QueryLens (org.spongepowered.common.inventory.lens.impl.QueryLens)1 GridInventoryLens (org.spongepowered.common.inventory.lens.impl.comp.GridInventoryLens)1