Search in sources :

Example 1 with MainInventorySlot

use of mekanism.common.inventory.container.slot.MainInventorySlot in project Mekanism by mekanism.

the class MekanismContainer method quickMoveStack.

/**
 * {@inheritDoc}
 *
 * @return The contents in this slot AFTER transferring items away.
 */
@Nonnull
@Override
public ItemStack quickMoveStack(@Nonnull PlayerEntity player, int slotID) {
    Slot currentSlot = slots.get(slotID);
    if (currentSlot == null || !currentSlot.hasItem()) {
        return ItemStack.EMPTY;
    }
    SelectedWindowData selectedWindow = player.level.isClientSide ? getSelectedWindow() : getSelectedWindow(player.getUUID());
    if (currentSlot instanceof IInsertableSlot && !((IInsertableSlot) currentSlot).exists(selectedWindow)) {
        return ItemStack.EMPTY;
    }
    ItemStack slotStack = currentSlot.getItem();
    ItemStack stackToInsert = slotStack;
    if (currentSlot instanceof InventoryContainerSlot) {
        // Insert into stacks that already contain an item in the order hot bar -> main inventory
        stackToInsert = insertItem(armorSlots, stackToInsert, true, selectedWindow);
        stackToInsert = insertItem(hotBarSlots, stackToInsert, true, selectedWindow);
        stackToInsert = insertItem(mainInventorySlots, stackToInsert, true, selectedWindow);
        // If we still have any left then input into the empty stacks in the order of main inventory -> hot bar
        // Note: Even though we are doing the main inventory, we still need to do both, ignoring empty then not instead of
        // just directly inserting into the main inventory, in case there are empty slots before the one we can stack with
        stackToInsert = insertItem(armorSlots, stackToInsert, false, selectedWindow);
        stackToInsert = insertItem(hotBarSlots, stackToInsert, false, selectedWindow);
        stackToInsert = insertItem(mainInventorySlots, stackToInsert, false, selectedWindow);
    } else {
        // We are in the main inventory or the hot bar
        // Start by trying to insert it into the tile's inventory slots, first attempting to stack with other items
        stackToInsert = insertItem(inventoryContainerSlots, stackToInsert, true, selectedWindow);
        if (slotStack.getCount() == stackToInsert.getCount()) {
            // Then as long as if we still have the same number of items (failed to insert), try to insert it into the tile's inventory slots allowing for empty items
            stackToInsert = insertItem(inventoryContainerSlots, stackToInsert, false, selectedWindow);
            if (slotStack.getCount() == stackToInsert.getCount()) {
                // Else if we failed to do that also, try transferring to armor inventory, main inventory or the hot bar, depending on which one we currently are in
                if (currentSlot instanceof ArmorSlot || currentSlot instanceof OffhandSlot) {
                    stackToInsert = insertItem(hotBarSlots, stackToInsert, true, selectedWindow);
                    stackToInsert = insertItem(mainInventorySlots, stackToInsert, true, selectedWindow);
                    stackToInsert = insertItem(hotBarSlots, stackToInsert, false, selectedWindow);
                    stackToInsert = insertItem(mainInventorySlots, stackToInsert, false, selectedWindow);
                } else if (currentSlot instanceof MainInventorySlot) {
                    stackToInsert = insertItem(armorSlots, stackToInsert, false, selectedWindow);
                    stackToInsert = insertItem(hotBarSlots, stackToInsert, selectedWindow);
                } else if (currentSlot instanceof HotBarSlot) {
                    stackToInsert = insertItem(armorSlots, stackToInsert, false, selectedWindow);
                    stackToInsert = insertItem(mainInventorySlots, stackToInsert, selectedWindow);
                } else {
                // TODO: Should we add a warning message so we can find out if we ever end up here. (Given we should never end up here anyways)
                }
            }
        }
    }
    if (stackToInsert.getCount() == slotStack.getCount()) {
        // If nothing changed then return that fact
        return ItemStack.EMPTY;
    }
    // Otherwise, decrease the stack by the amount we inserted, and return it as a new stack for what is now in the slot
    return transferSuccess(currentSlot, player, slotStack, stackToInsert);
}
Also used : OffhandSlot(mekanism.common.inventory.container.slot.OffhandSlot) ArmorSlot(mekanism.common.inventory.container.slot.ArmorSlot) OffhandSlot(mekanism.common.inventory.container.slot.OffhandSlot) ArmorSlot(mekanism.common.inventory.container.slot.ArmorSlot) MainInventorySlot(mekanism.common.inventory.container.slot.MainInventorySlot) HotBarSlot(mekanism.common.inventory.container.slot.HotBarSlot) Slot(net.minecraft.inventory.container.Slot) IInsertableSlot(mekanism.common.inventory.container.slot.IInsertableSlot) InventoryContainerSlot(mekanism.common.inventory.container.slot.InventoryContainerSlot) InventoryContainerSlot(mekanism.common.inventory.container.slot.InventoryContainerSlot) IInsertableSlot(mekanism.common.inventory.container.slot.IInsertableSlot) MainInventorySlot(mekanism.common.inventory.container.slot.MainInventorySlot) ItemStack(net.minecraft.item.ItemStack) SyncableItemStack(mekanism.common.inventory.container.sync.SyncableItemStack) HotBarSlot(mekanism.common.inventory.container.slot.HotBarSlot) Nonnull(javax.annotation.Nonnull)

Example 2 with MainInventorySlot

use of mekanism.common.inventory.container.slot.MainInventorySlot in project Mekanism by mekanism.

the class MekanismContainer method addInventorySlots.

protected void addInventorySlots(@Nonnull PlayerInventory inv) {
    if (this instanceof IEmptyContainer) {
        // Don't include the player's inventory slots
        return;
    }
    int yOffset = getInventoryYOffset();
    int xOffset = getInventoryXOffset();
    for (int slotY = 0; slotY < 3; slotY++) {
        for (int slotX = 0; slotX < 9; slotX++) {
            addSlot(new MainInventorySlot(inv, PlayerInventory.getSelectionSize() + slotX + slotY * 9, xOffset + slotX * 18, yOffset + slotY * 18));
        }
    }
    yOffset += 58;
    for (int slotX = 0; slotX < PlayerInventory.getSelectionSize(); slotX++) {
        addSlot(createHotBarSlot(inv, slotX, xOffset + slotX * 18, yOffset));
    }
}
Also used : MainInventorySlot(mekanism.common.inventory.container.slot.MainInventorySlot)

Aggregations

MainInventorySlot (mekanism.common.inventory.container.slot.MainInventorySlot)2 Nonnull (javax.annotation.Nonnull)1 ArmorSlot (mekanism.common.inventory.container.slot.ArmorSlot)1 HotBarSlot (mekanism.common.inventory.container.slot.HotBarSlot)1 IInsertableSlot (mekanism.common.inventory.container.slot.IInsertableSlot)1 InventoryContainerSlot (mekanism.common.inventory.container.slot.InventoryContainerSlot)1 OffhandSlot (mekanism.common.inventory.container.slot.OffhandSlot)1 SyncableItemStack (mekanism.common.inventory.container.sync.SyncableItemStack)1 Slot (net.minecraft.inventory.container.Slot)1 ItemStack (net.minecraft.item.ItemStack)1