Search in sources :

Example 6 with SlotLens

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

the class SlotMixin_Lens_Inventory method lensGeneratorBridge$generateLens.

@Override
public Lens lensGeneratorBridge$generateLens(SlotLensProvider slotLensProvider) {
    try {
        final Lens rootLens = ((InventoryAdapter) this.container).inventoryAdapter$getRootLens();
        SlotLens lens = rootLens.getSlotLens(this.inventoryAdapter$getFabric(), this.slot);
        if (lens != null) {
            return lens;
        }
    } catch (Exception ignored) {
    }
    // this works as a fallback but removes Inventory Property Support completely
    return new BasicSlotLens(0);
}
Also used : InventoryAdapter(org.spongepowered.common.inventory.adapter.InventoryAdapter) BasicSlotLens(org.spongepowered.common.inventory.lens.impl.slot.BasicSlotLens) Lens(org.spongepowered.common.inventory.lens.Lens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) BasicSlotLens(org.spongepowered.common.inventory.lens.impl.slot.BasicSlotLens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) BasicSlotLens(org.spongepowered.common.inventory.lens.impl.slot.BasicSlotLens)

Example 7 with SlotLens

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

the class AdapterLogic method pollSequential.

public static InventoryTransactionResult.Poll pollSequential(Fabric fabric, @Nullable Lens lens, @Nullable Integer limit) {
    if (lens == null || lens.getSlots(fabric).size() <= 0) {
        return InventoryTransactionResult.builder().type(Type.NO_SLOT).poll(ItemStackSnapshot.empty()).build();
    }
    InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(Type.SUCCESS);
    // used when polling from multiple slots
    ItemStack removedType = null;
    int totalPolled = 0;
    for (SlotLens slot : lens.getSlots(fabric)) {
        net.minecraft.world.item.ItemStack stack = slot.getStack(fabric);
        // Only remove one type of item
        if (stack.isEmpty() || (removedType != null && !ItemStackUtil.compareIgnoreQuantity(removedType, stack))) {
            continue;
        }
        // Poll up to limit items OR entire stack when no limit is set
        int pollCount = limit != null ? Math.min(stack.getCount(), limit) : stack.getCount();
        net.minecraft.world.item.ItemStack newStack = net.minecraft.world.item.ItemStack.EMPTY;
        if (pollCount < stack.getCount()) {
            // is stack not removed completely?
            newStack = stack.copy();
            newStack.setCount(newStack.getCount() - pollCount);
        }
        if (slot.setStack(fabric, newStack)) {
            // Set new stack
            // TODO parent??
            SlotAdapter slotAdapter = (SlotAdapter) slot.getAdapter(fabric, null);
            result.transaction(new SlotTransaction(slotAdapter, ItemStackUtil.snapshotOf(stack), ItemStackUtil.snapshotOf(newStack)));
            if (removedType == null) {
                // set removed type when first removing
                removedType = ItemStackUtil.cloneDefensive(stack, 1);
            }
            if (limit == null) {
                totalPolled = pollCount;
                // no limit only polls the first non-empty slot
                break;
            }
            // remove amount polled from slot
            limit -= pollCount;
            totalPolled += pollCount;
        }
        if (limit != null && limit <= 0) {
            // polled all items requested
            break;
        }
    }
    if (removedType != null) {
        // mark dirty if items were removed
        fabric.fabric$markDirty();
    }
    if (limit != null && limit > 0) {
        // not all items requested could be polled
        result.type(Type.FAILURE);
    }
    if (removedType == null) {
        removedType = ItemStack.empty();
    } else {
        removedType.setQuantity(totalPolled);
    }
    return result.poll(removedType.createSnapshot()).build();
}
Also used : SlotAdapter(org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter) ItemStack(org.spongepowered.api.item.inventory.ItemStack) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens)

Example 8 with SlotLens

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

the class LivingEntityMixin_Inventory method impl$getSpongeSlot.

@SuppressWarnings("ConstantConditions")
protected Slot impl$getSpongeSlot(final EquipmentSlot equipmentSlot) {
    final EquipmentType equipmentType = (EquipmentType) (Object) equipmentSlot;
    if (this instanceof InventoryBridge) {
        final InventoryAdapter adapter = ((InventoryBridge) this).bridge$getAdapter();
        final Lens lens = adapter.inventoryAdapter$getRootLens();
        if (lens instanceof EquipmentInventoryLens) {
            final SlotLens slotLens = ((EquipmentInventoryLens) lens).getSlotLens(equipmentType);
            return slotLens.getAdapter(adapter.inventoryAdapter$getFabric(), (Inventory) adapter);
        }
        throw new IllegalStateException("Expected EquipmentInventoryLens for " + this.getClass().getName() + " Inventory but found: " + lens.getClass().getName());
    }
    throw new IllegalStateException("Living Entity has no InventoryAdapter: " + this.getClass().getName());
}
Also used : InventoryAdapter(org.spongepowered.common.inventory.adapter.InventoryAdapter) InventoryBridge(org.spongepowered.common.bridge.world.inventory.InventoryBridge) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) Lens(org.spongepowered.common.inventory.lens.Lens) EquipmentInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.EquipmentInventoryLens) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) EquipmentInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.EquipmentInventoryLens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens)

Example 9 with SlotLens

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

the class Inventory2DLens method init.

private void init(SlotLensProvider slots) {
    for (int y = 0, slot = this.base; y < this.height; y++) {
        for (int x = 0; x < this.width; x++, slot += this.stride) {
            SlotLens slotLens = slots.getSlotLens(slot);
            this.addChild(slotLens, KeyValuePair.of(Keys.SLOT_POSITION, new Vector2i(this.xBase + x, this.yBase + y)));
        }
    }
}
Also used : Vector2i(org.spongepowered.math.vector.Vector2i) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens)

Example 10 with SlotLens

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

the class ItemStackQuery method matches.

@Override
public boolean matches(Lens lens, Lens parent, Inventory inventory) {
    if (lens instanceof SlotLens) {
        Fabric fabric = ((InventoryBridge) inventory).bridge$getAdapter().inventoryAdapter$getFabric();
        ItemStack stack = ItemStackUtil.fromNative(((SlotLens) lens).getStack(fabric));
        if (stack == null) {
            return false;
        }
        return this.matches(stack, this.arg);
    }
    return false;
}
Also used : ItemStack(org.spongepowered.api.item.inventory.ItemStack) Fabric(org.spongepowered.common.inventory.fabric.Fabric) 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