net.minecraft.inventory.container.Slot Java Examples

The following examples show how to use net.minecraft.inventory.container.Slot. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PacketGhostSlot.java    From MiningGadgets with MIT License 6 votes vote down vote up
public static void handle(PacketGhostSlot msg, Supplier<NetworkEvent.Context> ctx) {
    ctx.get().enqueueWork(() -> {
        ServerPlayerEntity sender = ctx.get().getSender();
        if (sender == null)
            return;

        Container container = sender.openContainer;
        if (container == null)
            return;

        Slot slot = container.inventorySlots.get(msg.slotNumber);
        if (slot instanceof GhostSlot)
            slot.putStack(msg.stack);
    });

    ctx.get().setPacketHandled(true);
}
 
Example #2
Source File: ContainerEnderItemStorage.java    From EnderStorage with MIT License 6 votes vote down vote up
@Override
public ItemStack transferStackInSlot(PlayerEntity par1EntityPlayer, int i) {
    ItemStack itemstack = ItemStack.EMPTY;
    Slot slot = inventorySlots.get(i);

    if (slot != null && slot.getHasStack()) {
        ItemStack itemstack1 = slot.getStack();
        itemstack = itemstack1.copy();

        int chestSlots = EnderItemStorage.sizes[chestInv.getSize()];
        if (i < chestSlots) {
            if (!mergeItemStack(itemstack1, chestSlots, inventorySlots.size(), true)) {
                return ItemStack.EMPTY;
            }
        } else if (!mergeItemStack(itemstack1, 0, chestSlots, false)) {
            return ItemStack.EMPTY;
        }
        if (itemstack1.getCount() == 0) {
            slot.putStack(ItemStack.EMPTY);
        } else {
            slot.onSlotChanged();
        }
    }
    return itemstack;
}
 
Example #3
Source File: DryingRackContainer.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void bindPlayerInventory(PlayerInventory playerInventory, int yOffset)
{
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 9; x++)
        {
            addSlot(new Slot(playerInventory,
                    x + y * 9 + 9,
                    8 + x * 18, yOffset + y * 18));
        }
    }

    for (int x = 0; x < 9; x++)
    {
        addSlot(new Slot(playerInventory, x, 8 + x * 18, yOffset+ 58));
    }
}
 
Example #4
Source File: DryingRackContainer.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex)
{
    boolean transferred = false;
    for (int i = startIndex; i < endIndex; i++)
    {
        Slot slot = this.inventorySlots.get(i);
        ItemStack existing = slot.getStack();

        if (existing.getCount() <= 0 && slot.isItemValid(stack))
        {
            slot.putStack(copyWithSize(stack, 1));
            slot.onSlotChanged();
            stack.grow(-1);
            transferred = true;
            if (stack.getCount() <= 0)
                break;
        }
    }
    return transferred;
}
 
Example #5
Source File: ModificationTableCommands.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void extractButton(ModificationTableContainer container, ServerPlayerEntity player, String upgradeName) {
    Slot laserSlot = container.inventorySlots.get(0);
    ItemStack laser = laserSlot.getStack();

    if (!(laser.getItem() instanceof MiningGadget))
        return;

    if (!UpgradeTools.containsUpgrades(laser))
        return;

    UpgradeTools.getUpgrades(laser).forEach(upgrade -> {
        if( !upgrade.getName().equals(upgradeName) )
            return;

        UpgradeTools.removeUpgrade(laser, upgrade);

        boolean success = player.inventory.addItemStackToInventory(new ItemStack(upgrade.getCard(), 1));
        if (!success) {
            player.dropItem(new ItemStack(upgrade.getCard(), 1), true);
        }

        if (upgrade == Upgrade.THREE_BY_THREE)
            MiningProperties.setRange(laser, 1);

        // Set both max and default range to MIN_RANGE.
        if (upgrade.getBaseName().equals(Upgrade.RANGE_1.getBaseName())) {
            MiningProperties.setBeamRange(laser, MiningProperties.MIN_RANGE);
            MiningProperties.setBeamMaxRange(laser, UpgradeTools.getMaxBeamRange(0));
        }

        if (upgrade.getBaseName().equals(Upgrade.BATTERY_1.getBaseName()))
            laser.getCapability(CapabilityEnergy.ENERGY).ifPresent(e -> ((EnergisedItem) e).updatedMaxEnergy(UpgradeBatteryLevels.BATTERY.getPower()));
    });
}
 
Example #6
Source File: FilterContainer.java    From MiningGadgets with MIT License 5 votes vote down vote up
@Override
public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
    ItemStack itemstack = ItemStack.EMPTY;
    Slot slot = this.inventorySlots.get(index);

    if (slot != null && slot.getHasStack()) {
        ItemStack currentStack = slot.getStack();

        // Stop our items at the very least :P
        if( currentStack.getItem() instanceof MiningGadget || currentStack.getItem() instanceof UpgradeCard)
            return itemstack;

        if( currentStack.isEmpty() )
            return itemstack;

        // Find the first empty slot number
        int slotNumber = -1;
        for( int i = 0; i <= 26; i ++ ) {
            if( this.inventorySlots.get(i).getStack().isEmpty() ) {
                slotNumber = i;
                break;
            }
        }

        if( slotNumber == -1 )
            return itemstack;

        this.inventorySlots.get(slotNumber).putStack(currentStack.copy().split(1));
    }

    return itemstack;
}
 
Example #7
Source File: ContainerEnderItemStorage.java    From EnderStorage with MIT License 5 votes vote down vote up
public ContainerEnderItemStorage(int windowId, PlayerInventory playerInv, EnderItemStorage chestInv) {
    super(ModContent.containerItemStorage, windowId);
    this.chestInv = chestInv;
    chestInv.openInventory();

    switch (chestInv.getSize()) {
        case 0:
            for (int row = 0; row < 3; ++row) {
                for (int col = 0; col < 3; ++col) {
                    addSlot(new Slot(chestInv, col + row * 3, 62 + col * 18, 17 + row * 18));
                }
            }
            addPlayerSlots(playerInv, 84);
            break;
        case 1:
            for (int row = 0; row < 3; ++row) {
                for (int col = 0; col < 9; ++col) {
                    addSlot(new Slot(chestInv, col + row * 9, 8 + col * 18, 18 + row * 18));
                }
            }
            addPlayerSlots(playerInv, 85);
            break;
        case 2:
            for (int row = 0; row < 6; ++row) {
                for (int col = 0; col < 9; ++col) {
                    addSlot(new Slot(chestInv, col + row * 9, 8 + col * 18, 18 + row * 18));
                }
            }
            addPlayerSlots(playerInv, 140);
            break;
    }

}
 
Example #8
Source File: ContainerEnderItemStorage.java    From EnderStorage with MIT License 5 votes vote down vote up
private void addPlayerSlots(IInventory invplayer, int yOffset) {
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 9; col++) {
            addSlot(new Slot(invplayer, col + row * 9 + 9, 8 + col * 18, yOffset + row * 18));
        }
    }

    for (int col = 0; col < 9; col++) {
        addSlot(new Slot(invplayer, col, 8 + col * 18, yOffset + 58));
    }
}
 
Example #9
Source File: SawmillContainer.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void bindPlayerInventory(PlayerInventory playerInventory)
{
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 9; ++j)
        {
            this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
        }
    }

    for (int k = 0; k < 9; ++k)
    {
        this.addSlot(new Slot(playerInventory, k, 8 + k * 18, 142));
    }
}
 
Example #10
Source File: ModificationTableContainer.java    From MiningGadgets with MIT License 4 votes vote down vote up
@Override
public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
    ItemStack itemstack = ItemStack.EMPTY;
    Slot slot = this.inventorySlots.get(index);
    if (slot != null && slot.getHasStack()) {
        ItemStack stack = slot.getStack();
        itemstack = stack.copy();
        if (index == 0) {
            if (!this.mergeItemStack(stack, 1, this.getInventory().size(), true)) {
                return ItemStack.EMPTY;
            }
            slot.onSlotChange(stack, itemstack);
        } else {
            if (stack.getItem() instanceof MiningGadget) {
                if (!this.mergeItemStack(stack, 0, 1, false)) {
                    return ItemStack.EMPTY;
                }
            } else if (stack.getItem() instanceof UpgradeCard) {
                // Push the item right into the modification table.
                if( ModificationTableCommands.insertButton(this, stack) ) {
                    int maxSize = Math.min(slot.getSlotStackLimit(), stack.getMaxStackSize());
                    int remove = maxSize - itemstack.getCount();
                    stack.shrink(remove == 0 ? 1 : remove);
                    updateUpgradeCache(0);
                }
                else
                    return ItemStack.EMPTY;
            } else if (index < 29) {
                if (!this.mergeItemStack(stack, 29, 38, false)) {
                    return ItemStack.EMPTY;
                }
            } else if (index < 38 && !this.mergeItemStack(stack, 1, 29, false)) {
                return ItemStack.EMPTY;
            }
        }

        if (stack.isEmpty()) {
            slot.putStack(ItemStack.EMPTY);
        } else {
            slot.onSlotChanged();
        }

        if (stack.getCount() == itemstack.getCount()) {
            return ItemStack.EMPTY;
        }

        slot.onTake(playerIn, stack);
    }

    return itemstack;
}
 
Example #11
Source File: ModificationTableCommands.java    From MiningGadgets with MIT License 4 votes vote down vote up
public static boolean insertButton(ModificationTableContainer container, ItemStack upgrade) {
    Slot laserSlot = container.inventorySlots.get(0);
    ItemStack laser = laserSlot.getStack();

    if (laser.getItem() instanceof MiningGadget && upgrade.getItem() instanceof UpgradeCard) {
        Upgrade card = ((UpgradeCard) upgrade.getItem()).getUpgrade();
        if (card == Upgrade.EMPTY)
            return false; //Don't allow inserting empty cards.

        List<Upgrade> upgrades = UpgradeTools.getUpgrades(laser);

        // Fortune has to be done slightly differently as it requires us to check
        // against all fortune tiers and not just it's existence.
        boolean hasFortune = UpgradeTools.containsUpgradeFromList(upgrades, Upgrade.FORTUNE_1);
        boolean hasSilk = UpgradeTools.containsUpgradeFromList(upgrades, Upgrade.SILK);

        // Did we just insert a Range upgrade?
        if (card.getBaseName().equals(Upgrade.RANGE_1.getBaseName())) {
            // Always reset the range regardless if it's bigger or smaller
            // We set max range on the gadget so we don't have to check if an upgrade exists.
            MiningProperties.setBeamRange(laser, UpgradeTools.getMaxBeamRange(card.getTier()));
            MiningProperties.setBeamMaxRange(laser, UpgradeTools.getMaxBeamRange(card.getTier()));
        }

        if (UpgradeTools.containsUpgrade(laser, card))
            return false;

        if (hasFortune && card.getBaseName().equals(Upgrade.SILK.getBaseName()) || hasSilk && card.getBaseName().equals(Upgrade.FORTUNE_1.getBaseName()))
            ((UpgradeCard) upgrade.getItem()).getUpgrade().setEnabled(false);

        MiningGadget.applyUpgrade(laser, (UpgradeCard) upgrade.getItem());

        // Did we just insert a battery upgrade?
        if(card.getBaseName().equals(Upgrade.BATTERY_1.getBaseName())) {
            UpgradeBatteryLevels.getBatteryByLevel(card.getTier()).ifPresent(power -> {
                laser.getCapability(CapabilityEnergy.ENERGY).ifPresent(e -> ((EnergisedItem) e).updatedMaxEnergy(power.getPower()));
            });
        }

        return true;
    }

    return false;
}
 
Example #12
Source File: SawmillScreen.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void handleMouseClick(Slot slotIn, int slotId, int mouseButton, ClickType type)
{
    super.handleMouseClick(slotIn, slotId, mouseButton, type);
    this.recipeGui.slotClicked(slotIn);
}
 
Example #13
Source File: SawmillContainer.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public ItemStack transferStackInSlot(PlayerEntity playerIn, int index)
{
    ItemStack itemstack = ItemStack.EMPTY;
    Slot slot = this.inventorySlots.get(index);

    if (slot != null && slot.getHasStack())
    {
        ItemStack itemstack1 = slot.getStack();
        itemstack = itemstack1.copy();

        if (index == 2)
        {
            if (!this.mergeItemStack(itemstack1, 3, 39, true))
            {
                return ItemStack.EMPTY;
            }

            slot.onSlotChange(itemstack1, itemstack);
        }
        else if (index != 1 && index != 0)
        {
            if (ChoppingRecipe.getRecipe(playerIn.world, itemstack1)
                    .isPresent())
            {
                if (!this.mergeItemStack(itemstack1, 0, 1, false))
                {
                    return ItemStack.EMPTY;
                }
            }
            else if (AbstractFurnaceTileEntity.isFuel(itemstack1))
            {
                if (!this.mergeItemStack(itemstack1, 1, 2, false))
                {
                    return ItemStack.EMPTY;
                }
            }
            else if (index >= 3 && index < 30)
            {
                if (!this.mergeItemStack(itemstack1, 30, 39, false))
                {
                    return ItemStack.EMPTY;
                }
            }
            else if (index >= 30 && index < 39 && !this.mergeItemStack(itemstack1, 3, 30, false))
            {
                return ItemStack.EMPTY;
            }
        }
        else if (!this.mergeItemStack(itemstack1, 3, 39, false))
        {
            return ItemStack.EMPTY;
        }

        if (itemstack1.isEmpty())
        {
            slot.putStack(ItemStack.EMPTY);
        }
        else
        {
            slot.onSlotChanged();
        }

        if (itemstack1.getCount() == itemstack.getCount())
        {
            return ItemStack.EMPTY;
        }

        slot.onTake(playerIn, itemstack1);
    }

    return itemstack;
}
 
Example #14
Source File: DryingRackContainer.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public ItemStack transferStackInSlot(PlayerEntity player, int slotIndex)
{
    Slot slot = this.inventorySlots.get(slotIndex);
    if (slot == null || !slot.getHasStack())
    {
        return ItemStack.EMPTY;
    }

    ItemStack stack = slot.getStack();
    ItemStack stackCopy = stack.copy();

    int startIndex;
    int endIndex;

    if (slotIndex < 4)
    {
        startIndex = 4;
        endIndex = startIndex + 4 * 9;

        if (!mergeItemStack(stack, startIndex, endIndex, false))
        {
            return ItemStack.EMPTY;
        }
    }
    else
    {
        startIndex = 0;
        endIndex = startIndex + 4;

        if (!mergeItemStack(stack, startIndex, endIndex))
        {
            return ItemStack.EMPTY;
        }
    }

    if (stack.getCount() == 0)
    {
        slot.putStack(ItemStack.EMPTY);
    }
    else
    {
        slot.onSlotChanged();
    }

    if (stack.getCount() == stackCopy.getCount())
    {
        return ItemStack.EMPTY;
    }

    slot.onTake(player, stack);
    return stackCopy;
}