Search in sources :

Example 6 with InventoryTransactionResult

use of org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult in project SpongeCommon by SpongePowered.

the class SlotAdapter method set.

@Override
public InventoryTransactionResult set(final ItemStack stack) {
    final InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(InventoryTransactionResult.Type.SUCCESS);
    final net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
    final net.minecraft.world.item.ItemStack old = this.slot.getStack(this.inventoryAdapter$getFabric());
    ItemStackSnapshot oldSnap = ItemStackUtil.snapshotOf(old);
    if (stack.isEmpty()) {
        // NONE item will clear the slot
        this.clear();
        SlotTransaction trans = new SlotTransaction(this, oldSnap, ItemStackSnapshot.empty());
        return result.transaction(trans).build();
    }
    int remaining = stack.quantity();
    final int push = Math.min(remaining, this.slot.getMaxStackSize(this.inventoryAdapter$getFabric()));
    net.minecraft.world.item.ItemStack newStack = ItemStackUtil.cloneDefensiveNative(nativeStack, push);
    if (this.slot.setStack(this.inventoryAdapter$getFabric(), newStack)) {
        result.transaction(new SlotTransaction(this, oldSnap, ItemStackUtil.snapshotOf(newStack)));
        remaining -= push;
    }
    if (remaining > 0) {
        result.reject(ItemStackUtil.cloneDefensive(nativeStack, remaining));
    }
    return result.build();
}
Also used : ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)

Example 7 with InventoryTransactionResult

use of org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult in project SpongeCommon by SpongePowered.

the class SlotAdapter method offer.

@Override
public InventoryTransactionResult offer(final ItemStack... stacks) {
    final InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(InventoryTransactionResult.Type.SUCCESS);
    for (ItemStack stack : stacks) {
        final net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
        final int maxStackSize = this.slot.getMaxStackSize(this.inventoryAdapter$getFabric());
        int remaining = stack.quantity();
        final net.minecraft.world.item.ItemStack old = this.slot.getStack(this.inventoryAdapter$getFabric());
        ItemStackSnapshot oldStack = ItemStackUtil.snapshotOf(old);
        ItemStackSnapshot newStack = oldStack;
        int push = Math.min(remaining, maxStackSize);
        if (old.isEmpty() && this.slot.setStack(this.inventoryAdapter$getFabric(), ItemStackUtil.cloneDefensiveNative(nativeStack, push))) {
            remaining -= push;
            newStack = ItemStackUtil.snapshotOf(stack);
        } else if (!old.isEmpty() && ItemStackUtil.compareIgnoreQuantity(old, stack)) {
            this.inventoryAdapter$getFabric().fabric$markDirty();
            // max() accounts for oversized stacks
            push = Math.max(Math.min(maxStackSize - old.getCount(), remaining), 0);
            old.setCount(old.getCount() + push);
            remaining -= push;
            newStack = ItemStackUtil.snapshotOf(old);
        }
        result.transaction(new SlotTransaction(this, oldStack, newStack));
        if (remaining == stack.quantity()) {
            // No items were consumed
            result.reject(ItemStackUtil.cloneDefensive(nativeStack));
            result.type(InventoryTransactionResult.Type.FAILURE);
        }
    }
    return result.build();
}
Also used : ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)

Example 8 with InventoryTransactionResult

use of org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult in project SpongeCommon by SpongePowered.

the class AdapterLogic method insertStack.

private static InventoryTransactionResult insertStack(Fabric fabric, Lens lens, ItemStack stack) {
    InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(Type.SUCCESS);
    net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
    if (stack.isEmpty() && lens.slotCount() == 1) {
        final net.minecraft.world.item.ItemStack old = lens.getStack(fabric, 0);
        final ItemStackSnapshot oldSnap = ItemStackUtil.snapshotOf(old);
        lens.setStack(fabric, 0, nativeStack);
        final SlotTransaction trans = new SlotTransaction((Slot) lens.getAdapter(fabric, null), oldSnap, ItemStackSnapshot.empty());
        result.transaction(trans);
        return result.build();
    }
    int maxStackSize = Math.min(lens.getMaxStackSize(fabric), nativeStack.getMaxStackSize());
    int remaining = stack.quantity();
    for (int ord = 0; ord < lens.slotCount() && remaining > 0; ord++) {
        final net.minecraft.world.item.ItemStack old = lens.getStack(fabric, ord);
        final ItemStackSnapshot oldSnap = ItemStackUtil.snapshotOf(old);
        final int push = Math.min(remaining, maxStackSize);
        final net.minecraft.world.item.ItemStack newStack = ItemStackUtil.cloneDefensiveNative(nativeStack, push);
        if (lens.setStack(fabric, ord, newStack)) {
            remaining -= push;
            final Slot slot = lens.getSlotLens(fabric, ord).getAdapter(fabric, null);
            final SlotTransaction trans = new SlotTransaction(slot, oldSnap, ItemStackUtil.snapshotOf(lens.getStack(fabric, ord)));
            result.transaction(trans);
        }
    }
    if (remaining > 0) {
        result.reject(ItemStackUtil.cloneDefensive(nativeStack, remaining));
    }
    fabric.fabric$markDirty();
    return result.build();
}
Also used : ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Slot(org.spongepowered.api.item.inventory.Slot) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)

Example 9 with InventoryTransactionResult

use of org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult 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 10 with InventoryTransactionResult

use of org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult in project SpongeCommon by SpongePowered.

the class SlotMixin_Inventory_API method set.

@Override
public InventoryTransactionResult set(ItemStack stack) {
    final InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(InventoryTransactionResult.Type.SUCCESS);
    final net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
    final net.minecraft.world.item.ItemStack old = this.shadow$getItem();
    ItemStackSnapshot oldSnap = ItemStackUtil.snapshotOf(old);
    int remaining = stack.quantity();
    final int push = Math.min(remaining, this.shadow$getMaxStackSize());
    net.minecraft.world.item.ItemStack newStack = ItemStackUtil.cloneDefensiveNative(nativeStack, push);
    this.shadow$set(newStack);
    result.transaction(new SlotTransaction(this, oldSnap, ItemStackUtil.snapshotOf(newStack)));
    remaining -= push;
    if (remaining > 0) {
        result.reject(ItemStackUtil.cloneDefensive(nativeStack, remaining));
    }
    return result.build();
}
Also used : ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)

Aggregations

InventoryTransactionResult (org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)10 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)7 Inventory (org.spongepowered.api.item.inventory.Inventory)5 ItemStack (org.spongepowered.api.item.inventory.ItemStack)5 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)5 Slot (org.spongepowered.api.item.inventory.Slot)2 Clause (com.skelril.nitro.Clause)1 ItemDropper (com.skelril.nitro.item.ItemDropper)1 NucleusKitEvent (io.github.nucleuspowered.nucleus.api.events.NucleusKitEvent)1 KitRedeemException (io.github.nucleuspowered.nucleus.api.exceptions.KitRedeemException)1 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)1 KitUserDataModule (io.github.nucleuspowered.nucleus.modules.kit.datamodules.KitUserDataModule)1 KitEvent (io.github.nucleuspowered.nucleus.modules.kit.events.KitEvent)1 Instant (java.time.Instant)1 Optional (java.util.Optional)1 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)1 IEquipmentInventory (org.lanternpowered.server.inventory.IEquipmentInventory)1 CommandResult (org.spongepowered.api.command.CommandResult)1 User (org.spongepowered.api.entity.living.player.User)1 Cause (org.spongepowered.api.event.cause.Cause)1