Search in sources :

Example 1 with GridInventoryAdapter

use of org.spongepowered.common.inventory.adapter.impl.comp.GridInventoryAdapter 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)

Aggregations

EmptyInventoryImpl (org.spongepowered.common.inventory.EmptyInventoryImpl)1 GridInventoryAdapter (org.spongepowered.common.inventory.adapter.impl.comp.GridInventoryAdapter)1 CompoundSlotLensProvider (org.spongepowered.common.inventory.lens.CompoundSlotLensProvider)1 GridInventoryLens (org.spongepowered.common.inventory.lens.impl.comp.GridInventoryLens)1 Vector2i (org.spongepowered.math.vector.Vector2i)1