Search in sources :

Example 11 with ItemStackKey

use of gregtech.api.util.ItemStackKey in project GregTech by GregTechCE.

the class MultiblockInfoRecipeWrapper method gatherBlockDrops.

private static void gatherBlockDrops(World world, Map<BlockPos, BlockInfo> blocks, Set<ItemStackKey> drops, Map<ItemStackKey, PartInfo> partsMap) {
    NonNullList<ItemStack> dropsList = NonNullList.create();
    for (Entry<BlockPos, BlockInfo> entry : blocks.entrySet()) {
        BlockPos pos = entry.getKey();
        IBlockState blockState = world.getBlockState(pos);
        NonNullList<ItemStack> blockDrops = NonNullList.create();
        blockState.getBlock().getDrops(blockDrops, world, pos, blockState, 0);
        dropsList.addAll(blockDrops);
        for (ItemStack itemStack : blockDrops) {
            ItemStackKey itemStackKey = new ItemStackKey(itemStack);
            PartInfo partInfo = partsMap.get(itemStackKey);
            if (partInfo == null) {
                partInfo = new PartInfo(itemStackKey, entry.getValue());
                partsMap.put(itemStackKey, partInfo);
            }
            ++partInfo.amount;
        }
    }
    for (ItemStack itemStack : dropsList) {
        drops.add(new ItemStackKey(itemStack));
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) BlockInfo(gregtech.api.util.BlockInfo) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) ItemStackKey(gregtech.api.util.ItemStackKey)

Example 12 with ItemStackKey

use of gregtech.api.util.ItemStackKey in project GregTech by GregTechCE.

the class ItemListGridWidget method readUpdateInfo.

@Override
public void readUpdateInfo(int id, PacketBuffer buffer) {
    super.readUpdateInfo(id, buffer);
    if (id == 2) {
        int slotsToAdd = buffer.readVarInt();
        modifySlotRows(slotsToAdd);
    }
    if (id == 3) {
        try {
            int itemsRemoved = buffer.readVarInt();
            for (int i = 0; i < itemsRemoved; i++) {
                ItemStackKey itemStack = new ItemStackKey(buffer.readItemStack());
                this.displayItemList.removeIf(it -> it.getItemStackKey().equals(itemStack));
            }
            int itemsChanged = buffer.readVarInt();
            for (int i = 0; i < itemsChanged; i++) {
                ItemStackKey itemStack = new ItemStackKey(buffer.readItemStack());
                int newTotalAmount = buffer.readVarInt();
                SimpleItemInfo itemInfo = displayItemList.stream().filter(it -> it.getItemStackKey().equals(itemStack)).findAny().orElse(null);
                if (itemInfo == null) {
                    itemInfo = new SimpleItemInfo(itemStack);
                    this.displayItemList.add(itemInfo);
                }
                itemInfo.setTotalItemAmount(newTotalAmount);
            }
            this.displayItemList.sort(comparator);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : SimpleItemInfo(gregtech.common.inventory.SimpleItemInfo) IOException(java.io.IOException) ItemStackKey(gregtech.api.util.ItemStackKey)

Example 13 with ItemStackKey

use of gregtech.api.util.ItemStackKey in project GregTech by GregTechCE.

the class ItemListGridWidget method checkItemListForChanges.

private void checkItemListForChanges() {
    Iterator<ItemStackKey> iterator = cachedItemList.keySet().iterator();
    while (iterator.hasNext()) {
        ItemStackKey itemStack = iterator.next();
        if (!itemList.hasItemStored(itemStack)) {
            iterator.remove();
            itemsRemoved.add(itemStack);
        }
    }
    for (ItemStackKey itemStack : itemList.getStoredItems()) {
        IItemInfo itemInfo = itemList.getItemInfo(itemStack);
        if (itemInfo == null)
            continue;
        if (!cachedItemList.containsKey(itemStack)) {
            SimpleItemInfo lookupInfo = new SimpleItemInfo(itemStack);
            int totalAmount = itemInfo.getTotalItemAmount();
            if (totalAmount == 0) {
                itemsRemoved.add(itemStack);
            } else {
                lookupInfo.setTotalItemAmount(totalAmount);
                cachedItemList.put(itemStack, lookupInfo);
                itemsChanged.add(lookupInfo);
            }
        } else {
            SimpleItemInfo cachedItemInfo = cachedItemList.get(itemStack);
            if (cachedItemInfo.getTotalItemAmount() != itemInfo.getTotalItemAmount()) {
                cachedItemInfo.setTotalItemAmount(itemInfo.getTotalItemAmount());
                itemsChanged.add(cachedItemInfo);
            }
        }
    }
}
Also used : IItemInfo(gregtech.common.inventory.IItemInfo) SimpleItemInfo(gregtech.common.inventory.SimpleItemInfo) ItemStackKey(gregtech.api.util.ItemStackKey)

Example 14 with ItemStackKey

use of gregtech.api.util.ItemStackKey in project GregTech by GregTechCE.

the class ItemListGridWidget method detectAndSendChanges.

@Override
public void detectAndSendChanges() {
    super.detectAndSendChanges();
    if (itemList == null)
        return;
    int amountOfItemTypes = itemList.getStoredItems().size();
    int slotRowsRequired = Math.max(slotAmountY, (int) Math.ceil(amountOfItemTypes / (slotAmountX * 1.0)));
    if (slotRowsAmount != slotRowsRequired) {
        int slotsToAdd = slotRowsRequired - slotRowsAmount;
        this.slotRowsAmount = slotRowsRequired;
        writeUpdateInfo(2, buf -> buf.writeVarInt(slotsToAdd));
        modifySlotRows(slotsToAdd);
    }
    this.itemsChanged.clear();
    this.itemsRemoved.clear();
    checkItemListForChanges();
    if (!itemsChanged.isEmpty() || !itemsRemoved.isEmpty()) {
        writeUpdateInfo(3, buf -> {
            buf.writeVarInt(itemsRemoved.size());
            for (ItemStackKey itemStackKey : itemsRemoved) {
                buf.writeItemStack(itemStackKey.getItemStackRaw());
            }
            buf.writeVarInt(itemsChanged.size());
            for (SimpleItemInfo itemInfo : itemsChanged) {
                buf.writeItemStack(itemInfo.getItemStackKey().getItemStackRaw());
                buf.writeVarInt(itemInfo.getTotalItemAmount());
            }
        });
    }
}
Also used : SimpleItemInfo(gregtech.common.inventory.SimpleItemInfo) ItemStackKey(gregtech.api.util.ItemStackKey)

Example 15 with ItemStackKey

use of gregtech.api.util.ItemStackKey in project GregTech by GregTechCE.

the class ItemListSlotWidget method insertHeldItemStack.

// returns true if something actually happened
private boolean insertHeldItemStack(int button, boolean isClient) {
    InventoryPlayer inventory = gui.entityPlayer.inventory;
    int amountToInsert = button == 1 ? 1 : Integer.MAX_VALUE;
    if (!inventory.getItemStack().isEmpty()) {
        if (!isClient) {
            // on server, we lookup item list to see how much we can actually insert
            ItemStack heldItemStack = inventory.getItemStack();
            IItemList itemList = gridWidget.getItemList();
            int amountInserted = itemList.insertItem(new ItemStackKey(heldItemStack), Math.min(heldItemStack.getCount(), amountToInsert), false, InsertMode.LOWEST_PRIORITY);
            heldItemStack.shrink(amountInserted);
            uiAccess.sendHeldItemUpdate();
            gui.entityPlayer.openContainer.detectAndSendChanges();
            return amountInserted > 0;
        } else {
            // on client we assume we can insert full stack into the network
            inventory.getItemStack().shrink(amountToInsert);
            return true;
        }
    }
    return false;
}
Also used : InventoryPlayer(net.minecraft.entity.player.InventoryPlayer) IItemList(gregtech.common.inventory.IItemList) ItemStack(net.minecraft.item.ItemStack) ItemStackKey(gregtech.api.util.ItemStackKey)

Aggregations

ItemStackKey (gregtech.api.util.ItemStackKey)16 ItemStack (net.minecraft.item.ItemStack)10 SimpleItemInfo (gregtech.common.inventory.SimpleItemInfo)3 BlockInfo (gregtech.api.util.BlockInfo)2 IItemInfo (gregtech.common.inventory.IItemInfo)2 IOException (java.io.IOException)2 BlockPos (net.minecraft.util.math.BlockPos)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 WorldSceneRenderer (gregtech.api.render.scene.WorldSceneRenderer)1 IItemList (gregtech.common.inventory.IItemList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 IBlockState (net.minecraft.block.state.IBlockState)1 InventoryPlayer (net.minecraft.entity.player.InventoryPlayer)1 InventoryCrafting (net.minecraft.inventory.InventoryCrafting)1