Search in sources :

Example 1 with OrderedInventory

use of org.spongepowered.api.item.inventory.type.OrderedInventory in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method captureTransaction.

/**
 * Captures a transaction
 *
 * @param captureIn the {@link IMixinInventory} to capture the transaction in
 * @param inv the Inventory
 * @param index the affected SlotIndex
 * @param originalStack the original Stack
 */
public static void captureTransaction(IMixinInventory captureIn, Inventory inv, int index, ItemStack originalStack) {
    // TODO make sure we never got null
    if (captureIn == null || inv == null) {
        return;
    }
    Inventory ordered = inv.query(QueryOperationTypes.INVENTORY_TYPE.of(OrderedInventory.class));
    if (ordered instanceof OrderedInventory) {
        Optional<org.spongepowered.api.item.inventory.Slot> slot = ((OrderedInventory) ordered).getSlot(SlotIndex.of(index));
        if (slot.isPresent()) {
            SlotTransaction trans = new SlotTransaction(slot.get(), ItemStackUtil.snapshotOf(originalStack), ItemStackUtil.snapshotOf(slot.get().peek().orElse(org.spongepowered.api.item.inventory.ItemStack.empty())));
            captureIn.getCapturedTransactions().add(trans);
        }
    }
}
Also used : SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) Slot(net.minecraft.inventory.Slot) OrderedInventory(org.spongepowered.api.item.inventory.type.OrderedInventory) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) IMixinInventory(org.spongepowered.common.interfaces.IMixinInventory) OrderedInventory(org.spongepowered.api.item.inventory.type.OrderedInventory) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Inventory(org.spongepowered.api.item.inventory.Inventory) CustomInventory(org.spongepowered.common.item.inventory.custom.CustomInventory) IInventory(net.minecraft.inventory.IInventory)

Example 2 with OrderedInventory

use of org.spongepowered.api.item.inventory.type.OrderedInventory in project LanternServer by LanternPowered.

the class ContainerTileEntityStore method deserialize.

@Override
public void deserialize(T object, DataView dataView) {
    final List<DataView> itemViews = dataView.getViewList(ITEMS).orElse(null);
    if (itemViews != null) {
        dataView.remove(ITEMS);
        final OrderedInventory inventory = (OrderedInventory) object.getInventory();
        final ObjectSerializer<LanternItemStack> itemStackSerializer = ObjectSerializerRegistry.get().get(LanternItemStack.class).get();
        for (DataView itemView : itemViews) {
            final int slot = itemView.getByte(SLOT).get() & 0xff;
            final LanternItemStack itemStack = itemStackSerializer.deserialize(itemView);
            inventory.set(new SlotIndex(slot), itemStack);
        }
    }
    super.deserialize(object, dataView);
}
Also used : DataView(org.spongepowered.api.data.DataView) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) OrderedInventory(org.spongepowered.api.item.inventory.type.OrderedInventory) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 3 with OrderedInventory

use of org.spongepowered.api.item.inventory.type.OrderedInventory in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method captureTransaction.

/**
 * Captures a transaction
 *
 * @param captureIn the {@link IMixinInventory} to capture the transaction in
 * @param inv the Inventory
 * @param index the affected SlotIndex
 * @param transaction the transaction to execute
 * @return the result if the transaction
 */
public static ItemStack captureTransaction(IMixinInventory captureIn, Inventory inv, int index, Supplier<ItemStack> transaction) {
    // TODO make sure we never got null
    if (captureIn == null || inv == null) {
        return transaction.get();
    }
    Inventory ordered = inv.query(QueryOperationTypes.INVENTORY_TYPE.of(OrderedInventory.class));
    if (ordered instanceof OrderedInventory) {
        Optional<org.spongepowered.api.item.inventory.Slot> slot = ((OrderedInventory) ordered).getSlot(SlotIndex.of(index));
        if (slot.isPresent()) {
            ItemStackSnapshot original = slot.get().peek().map(ItemStackUtil::snapshotOf).orElse(ItemStackSnapshot.NONE);
            ItemStack remaining = transaction.get();
            if (remaining.isEmpty()) {
                ItemStackSnapshot replacement = slot.get().peek().map(ItemStackUtil::snapshotOf).orElse(ItemStackSnapshot.NONE);
                captureIn.getCapturedTransactions().add(new SlotTransaction(slot.get(), original, replacement));
            }
            return remaining;
        }
    }
    // else inventory was missing the slot for some reason
    return transaction.get();
}
Also used : SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) Slot(net.minecraft.inventory.Slot) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) OrderedInventory(org.spongepowered.api.item.inventory.type.OrderedInventory) ItemStack(net.minecraft.item.ItemStack) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) IMixinInventory(org.spongepowered.common.interfaces.IMixinInventory) OrderedInventory(org.spongepowered.api.item.inventory.type.OrderedInventory) CraftingInventory(org.spongepowered.api.item.inventory.crafting.CraftingInventory) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Inventory(org.spongepowered.api.item.inventory.Inventory) CustomInventory(org.spongepowered.common.item.inventory.custom.CustomInventory) IInventory(net.minecraft.inventory.IInventory)

Aggregations

OrderedInventory (org.spongepowered.api.item.inventory.type.OrderedInventory)3 IInventory (net.minecraft.inventory.IInventory)2 Slot (net.minecraft.inventory.Slot)2 SPacketSetSlot (net.minecraft.network.play.server.SPacketSetSlot)2 Inventory (org.spongepowered.api.item.inventory.Inventory)2 CraftingInventory (org.spongepowered.api.item.inventory.crafting.CraftingInventory)2 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)2 CarriedInventory (org.spongepowered.api.item.inventory.type.CarriedInventory)2 IMixinInventory (org.spongepowered.common.interfaces.IMixinInventory)2 CustomInventory (org.spongepowered.common.item.inventory.custom.CustomInventory)2 ItemStack (net.minecraft.item.ItemStack)1 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)1 DataView (org.spongepowered.api.data.DataView)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 SlotIndex (org.spongepowered.api.item.inventory.property.SlotIndex)1