Search in sources :

Example 1 with SlotAdapter

use of org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter in project SpongeCommon by SpongePowered.

the class CompoundSlotLensProvider method add.

/**
 * Adds all slots from this inventory adapter.
 *
 * @param adapter The adapter
 *
 * @return this provider for chaining
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public CompoundSlotLensProvider add(InventoryAdapter adapter) {
    for (Inventory slot : ((Inventory) adapter).slots()) {
        SlotLens slotLens = ((SlotLens) ((SlotAdapter) slot).inventoryAdapter$getRootLens());
        this.slotList.add(slotLens);
    }
    return this;
}
Also used : SlotAdapter(org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter) Inventory(org.spongepowered.api.item.inventory.Inventory) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens)

Example 2 with SlotAdapter

use of org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter in project SpongeCommon by SpongePowered.

the class AdapterLogic method appendSequential.

public static InventoryTransactionResult appendSequential(Fabric fabric, @Nullable Lens lens, ItemStack stack) {
    if (lens == null) {
        return InventoryTransactionResult.builder().type(Type.FAILURE).reject(ItemStackUtil.cloneDefensive(stack)).build();
    }
    InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(Type.SUCCESS);
    net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
    int maxStackSize = Math.min(lens.getMaxStackSize(fabric), nativeStack.getMaxStackSize());
    int remaining = stack.quantity();
    for (int ord = 0; ord < lens.slotCount() && remaining > 0; ord++) {
        net.minecraft.world.item.ItemStack old = lens.getStack(fabric, ord);
        int push = Math.min(remaining, maxStackSize);
        if (old.isEmpty() && lens.setStack(fabric, ord, ItemStackUtil.cloneDefensiveNative(nativeStack, push))) {
            remaining -= push;
            Slot slot = ((SlotAdapter) lens.getSlotLens(fabric, ord).getAdapter(fabric, null));
            result.transaction(new SlotTransaction(slot, ItemStackUtil.snapshotOf(old), ItemStackUtil.snapshotOf(lens.getStack(fabric, ord))));
        } else if (!old.isEmpty() && ItemStackUtil.compareIgnoreQuantity(old, stack)) {
            ItemStackSnapshot oldSnap = ItemStackUtil.snapshotOf(old);
            // max() accounts for oversized stacks
            push = Math.max(Math.min(maxStackSize - old.getCount(), remaining), 0);
            old.setCount(old.getCount() + push);
            lens.setStack(fabric, ord, old);
            remaining -= push;
            Slot slot = ((SlotAdapter) lens.getSlotLens(fabric, ord).getAdapter(fabric, null));
            result.transaction(new SlotTransaction(slot, oldSnap, ItemStackUtil.snapshotOf(lens.getStack(fabric, ord))));
        }
    }
    if (remaining == stack.quantity()) {
        // No items were consumed
        result.type(Type.FAILURE).reject(ItemStackUtil.cloneDefensive(nativeStack));
    } else {
        stack.setQuantity(remaining);
        fabric.fabric$markDirty();
    }
    return result.build();
}
Also used : Slot(org.spongepowered.api.item.inventory.Slot) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SlotAdapter(org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)

Example 3 with SlotAdapter

use of org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter in project SpongeCommon by SpongePowered.

the class PacketPhaseUtil method handleSlotRestore.

public static void handleSlotRestore(@Nullable final Player player, @Nullable final AbstractContainerMenu containerMenu, final List<SlotTransaction> slotTransactions, final boolean eventCancelled) {
    try (PhaseContext<@NonNull ?> ignored = BlockPhase.State.RESTORING_BLOCKS.createPhaseContext(PhaseTracker.SERVER).buildAndSwitch()) {
        boolean restoredAny = false;
        for (final SlotTransaction slotTransaction : slotTransactions) {
            if ((!slotTransaction.custom().isPresent() && slotTransaction.isValid()) && !eventCancelled) {
                continue;
            }
            restoredAny = true;
            final org.spongepowered.api.item.inventory.Slot slot = slotTransaction.slot();
            final ItemStackSnapshot snapshot = eventCancelled || !slotTransaction.isValid() ? slotTransaction.original() : slotTransaction.custom().get();
            if (containerMenu == null || slot.viewedSlot() == slot) {
                slot.set(snapshot.createStack());
            } else {
                final int slotNumber = ((SlotAdapter) slot).getOrdinal();
                final Slot nmsSlot = containerMenu.getSlot(slotNumber);
                if (nmsSlot != null) {
                    nmsSlot.set(ItemStackUtil.fromSnapshotToNative(snapshot));
                }
            }
        }
        if (restoredAny && player instanceof net.minecraft.server.level.ServerPlayer) {
            if (containerMenu != null) {
                containerMenu.broadcastChanges();
                if (player.containerMenu == containerMenu) {
                    ((net.minecraft.server.level.ServerPlayer) player).refreshContainer(containerMenu);
                }
            } else {
                player.inventoryMenu.broadcastChanges();
            }
        }
    }
}
Also used : SlotAdapter(org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Slot(net.minecraft.world.inventory.Slot)

Example 4 with SlotAdapter

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

Aggregations

SlotAdapter (org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter)4 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)3 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)2 InventoryTransactionResult (org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)2 SlotLens (org.spongepowered.common.inventory.lens.slots.SlotLens)2 Slot (net.minecraft.world.inventory.Slot)1 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)1 Inventory (org.spongepowered.api.item.inventory.Inventory)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1 Slot (org.spongepowered.api.item.inventory.Slot)1