Search in sources :

Example 1 with HotBarSlot

use of mekanism.common.inventory.container.slot.HotBarSlot 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 HotBarSlot

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

the class ModuleTweakerContainer method addInventorySlots.

@Override
protected void addInventorySlots(@Nonnull PlayerInventory inv) {
    int armorInventorySize = inv.armor.size();
    for (int index = 0; index < armorInventorySize; index++) {
        EquipmentSlotType slotType = EnumUtils.EQUIPMENT_SLOT_TYPES[2 + armorInventorySize - index - 1];
        addSlot(new ArmorSlot(inv, 36 + slotType.ordinal() - 2, 8, 8 + index * 18, slotType) {

            @Override
            public boolean mayPickup(@Nonnull PlayerEntity player) {
                return false;
            }

            @Override
            public boolean mayPlace(@Nonnull ItemStack stack) {
                return false;
            }
        });
    }
    for (int slotY = 0; slotY < PlayerInventory.getSelectionSize(); slotY++) {
        addSlot(new HotBarSlot(inv, slotY, 43 + slotY * 18, 161) {

            @Override
            public boolean mayPickup(@Nonnull PlayerEntity player) {
                return false;
            }

            @Override
            public boolean mayPlace(@Nonnull ItemStack stack) {
                return false;
            }
        });
    }
    addSlot(new OffhandSlot(inv, 40, 8, 16 + 18 * 4) {

        @Override
        public boolean mayPickup(@Nonnull PlayerEntity player) {
            return false;
        }

        @Override
        public boolean mayPlace(@Nonnull ItemStack stack) {
            return false;
        }
    });
}
Also used : OffhandSlot(mekanism.common.inventory.container.slot.OffhandSlot) EquipmentSlotType(net.minecraft.inventory.EquipmentSlotType) ArmorSlot(mekanism.common.inventory.container.slot.ArmorSlot) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) HotBarSlot(mekanism.common.inventory.container.slot.HotBarSlot)

Aggregations

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