codechicken.lib.inventory.InventoryUtils Java Examples

The following examples show how to use codechicken.lib.inventory.InventoryUtils. 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: FluidUtils.java    From CodeChickenCore with MIT License 6 votes vote down vote up
public static boolean fillTankWithContainer(IFluidHandler tank, EntityPlayer player) {
    ItemStack stack = player.getCurrentEquippedItem();
    FluidStack liquid = FluidContainerRegistry.getFluidForFilledItem(stack);

    if (liquid == null)
        return false;

    if (tank.fill(null, liquid, false) != liquid.amount && !player.capabilities.isCreativeMode)
        return false;

    tank.fill(null, liquid, true);

    if (!player.capabilities.isCreativeMode)
        InventoryUtils.consumeItem(player.inventory, player.inventory.currentItem);

    player.inventoryContainer.detectAndSendChanges();
    return true;
}
 
Example #2
Source File: TileCraftingGrid.java    From Translocators with MIT License 6 votes vote down vote up
public void activate(int subHit, EntityPlayer player) {
    ItemStack held = player.inventory.getCurrentItem();
    if (held == null) {
        if (items[subHit] != null)
            giveOrDropItem(items[subHit], player);
        items[subHit] = null;
    } else {
        if (!InventoryUtils.areStacksIdentical(held, items[subHit])) {
            ItemStack old = items[subHit];
            items[subHit] = InventoryUtils.copyStack(held, 1);
            player.inventory.decrStackSize(player.inventory.currentItem, 1);

            if (old != null)
                giveOrDropItem(old, player);
        }
    }

    timeout = 2400;
    worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
    markDirty();
}
 
Example #3
Source File: ContainerPotionCreator.java    From NotEnoughItems with MIT License 6 votes vote down vote up
@Override
public ItemStack slotClick(ContainerExtended container, EntityPlayer player, int button, int modifier) {
    ItemStack held = player.inventory.getItemStack();
    if (button == 0 && modifier == 1) {
        NEIClientUtils.cheatItem(getStack(), button, -1);
    } else if (button == 1) {
        putStack(null);
    } else if (held != null) {
        if (isItemValid(held)) {
            putStack(InventoryUtils.copyStack(held, 1));
            player.inventory.setItemStack(null);
        }
    } else if (getHasStack())
        player.inventory.setItemStack(getStack());

    return null;
}
 
Example #4
Source File: NEIServerUtils.java    From NotEnoughItems with MIT License 6 votes vote down vote up
public static void givePlayerItem(EntityPlayerMP player, ItemStack stack, boolean infinite, boolean doGive) {
    if (stack.getItem() == null) {
        player.addChatComponentMessage(setColour(new ChatComponentTranslation("nei.chat.give.noitem"), EnumChatFormatting.WHITE));
        return;
    }

    int given = stack.stackSize;
    if(doGive) {
        if (infinite)
            player.inventory.addItemStackToInventory(stack);
        else
            given -= InventoryUtils.insertItem(player.inventory, stack, false);
    }

    sendNotice(player, new ChatComponentTranslation("commands.give.success", stack.getChatComponent(), infinite ? "\u221E" : Integer.toString(given), player.getCommandSenderName()), "notify-item");
    player.openContainer.detectAndSendChanges();
}
 
Example #5
Source File: EnderStorageRecipe.java    From EnderStorage with MIT License 6 votes vote down vote up
public ItemStack getCraftingResult(InventoryCrafting ic) {
    for (int row = 0; row < 2; row++) {
        if (!offsetMatchesDyes(ic, 0, row))
            continue;

        ItemStack freqowner = ic.getStackInRowAndColumn(1, row + 1);
        int freq = freqowner.getItemDamage() & 0xFFF;

        int colour1 = recolour(0, row, freq, ic);
        int colour2 = recolour(1, row, freq, ic);
        int colour3 = recolour(2, row, freq, ic);

        ItemStack result = InventoryUtils.copyStack(freqowner, 1);
        result.setItemDamage(EnderStorageManager.getFreqFromColours(colour3, colour2, colour1) | freqowner.getItemDamage() & 0xF000);
        return result;
    }
    return null;
}
 
Example #6
Source File: TileItemTranslocator.java    From Translocators with MIT License 6 votes vote down vote up
private boolean isSatsified(ItemAttachment ia, InventoryRange access)
{
    boolean filterSet = false;
    for(ItemStack filter : ia.filters)
        if(filter != null)
        {
            filterSet = true;
            if(ia.regulate)
            {
                if(countMatchingStacks(access, filter, !ia.a_eject) < filterCount(ia, filter))
                    return false;
            }
            else
            {
                if(InventoryUtils.getInsertibleQuantity(access, filter) > 0)
                    return false;
            }
        }
    
    return filterSet || !hasEmptySpace(access);
}
 
Example #7
Source File: NEIServerUtils.java    From NotEnoughItems with MIT License 6 votes vote down vote up
public static void givePlayerItem(EntityPlayerMP player, ItemStack stack, boolean infinite, boolean doGive) {
    if (stack.getItem() == null) {
        player.sendMessage(setColour(new TextComponentTranslation("nei.chat.give.noitem"), TextFormatting.WHITE));
        return;
    }

    int given = stack.getCount();
    if (doGive) {
        if (infinite) {
            player.inventory.addItemStackToInventory(stack);
        } else {
            given -= InventoryUtils.insertItem(player.inventory, stack, false);
        }
    }

    sendNotice(player, new TextComponentTranslation("commands.give.success", stack.getTextComponent(), infinite ? "\u221E" : Integer.toString(given), player.getName()), "notify-item");
    player.openContainer.detectAndSendChanges();
}
 
Example #8
Source File: DefaultOverlayHandler.java    From NotEnoughItems with MIT License 5 votes vote down vote up
private List<IngredientDistribution> assignIngredients(List<PositionedStack> ingredients, List<DistributedIngred> ingredStacks)
{
    ArrayList<IngredientDistribution> assignedIngredients = new ArrayList<IngredientDistribution>();
    for(PositionedStack posstack : ingredients)//assign what we need and have
    {
        DistributedIngred biggestIngred = null;
        ItemStack permutation = null;
        int biggestSize = 0;
        for(ItemStack pstack : posstack.items)
        {
            for(int j = 0; j < ingredStacks.size(); j++)
            {
                DistributedIngred istack = ingredStacks.get(j);
                if(!InventoryUtils.canStack(pstack, istack.stack) || istack.invAmount-istack.distributed < pstack.stackSize)
                    continue;
                
                int relsize = (istack.invAmount-istack.invAmount/istack.recipeAmount*istack.distributed)/pstack.stackSize;
                if(relsize > biggestSize)
                {
                    biggestSize = relsize;
                    biggestIngred = istack;
                    permutation = pstack;
                    break;
                }
            }
        }
        
        if(biggestIngred == null)//not enough ingreds
            return null;

        biggestIngred.distributed+=permutation.stackSize;
        assignedIngredients.add(new IngredientDistribution(biggestIngred, permutation));
    }
    
    return assignedIngredients;
}
 
Example #9
Source File: EnderItemStorage.java    From EnderStorage with MIT License 5 votes vote down vote up
public ItemStack decrStackSize(int slot, int size)
{
    synchronized(this)
    {
        return InventoryUtils.decrStackSize(this, slot, size);
    }
}
 
Example #10
Source File: TileItemTranslocator.java    From Translocators with MIT License 5 votes vote down vote up
private void spreadOutput(ItemStack move, int src, boolean rspass, InventoryRange[] attached)
{
    if(move.stackSize == 0)
        return;
    
    int outputCount = 0;
    int[] outputQuantities = new int[6];
    for(int i = 0; i < 6; i++)
    {
        ItemAttachment ia = (ItemAttachment) attachments[i];
        if(ia != null && !ia.a_eject && ia.redstone == rspass)
        {
            outputQuantities[i] = insertAmount(move, ia, attached[i]);
            if(outputQuantities[i] > 0)
                outputCount++;
        }
    }
    
    for(int dst = 0; dst < 6 && move.stackSize > 0; dst++)
    {
        int qty = outputQuantities[dst];
        if(qty <= 0)
            continue;
        
        qty = Math.min(qty, move.stackSize/outputCount + worldObj.rand.nextInt(move.stackSize%outputCount+1));
        outputCount--;
        
        if(qty == 0)
            continue;

        InventoryRange range = attached[dst];
        ItemStack add = InventoryUtils.copyStack(move, qty);
        InventoryUtils.insertItem(range, add, false);
        move.stackSize-=qty;
        
        sendTransferPacket(src, dst, add);
    }
}
 
Example #11
Source File: NEIServerConfig.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public static void saveBannedItems() {
    File file = new File(saveDir, "banneditems.cfg");
    try {
        if(!file.exists())
            file.createNewFile();

        PrintWriter p = new PrintWriter(file);
        p.println("#Saved in this format for external editing. The format isn't that hard to figure out. If you think you're up to it, modify it here!");

        for (ItemStackMap.Entry<Set<String>> entry : bannedItems.entries()) {
            NBTTagCompound key = InventoryUtils.savePersistant(entry.key, new NBTTagCompound());
            key.removeTag("Count");
            if(key.getByte("Damage") == 0) key.removeTag("Damage");

            p.print(key.toString());
            p.print("=[");
            int i = 0;
            for (String s : entry.value) {
                if(i++ != 0) p.print(", ");
                p.print(s);
            }
            p.println("]");
        }
        p.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #12
Source File: TileItemTranslocator.java    From Translocators with MIT License 5 votes vote down vote up
@Override
public void readFromNBT(NBTTagCompound tag)
{
    super.readFromNBT(tag);
    if(tag.hasKey("items"))
        for(Attachment a : attachments)
            if(a != null)
                InventoryUtils.readItemStacksFromTag(((ItemAttachment)a).filters, tag.getTagList("items", 10));
}
 
Example #13
Source File: TileItemTranslocator.java    From Translocators with MIT License 5 votes vote down vote up
@Override
public NBTTagCompound write(NBTTagCompound tag)
{
    tag.setBoolean("regulate", regulate);
    tag.setBoolean("signal", signal);
    tag.setBoolean("powering", a_powering);
    tag.setTag("filters", InventoryUtils.writeItemStacksToTag(filters, 65536));
    return super.write(tag);
}
 
Example #14
Source File: EnderItemStorage.java    From EnderStorage with MIT License 5 votes vote down vote up
public ItemStack getStackInSlotOnClosing(int slot)
{
    synchronized(this)
    {
        return InventoryUtils.getStackInSlotOnClosing(this, slot);
    }
}
 
Example #15
Source File: DefaultOverlayHandler.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public DistributedIngred findIngred(List<DistributedIngred> ingredStacks, ItemStack pstack)
{
    for(DistributedIngred istack : ingredStacks)
        if(InventoryUtils.canStack(pstack, istack.stack))
            return istack;
    return null;
}
 
Example #16
Source File: NEICPH.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public static void sendOpenPotionWindow() {
    ItemStack[] potionStore = new ItemStack[9];
    InventoryUtils.readItemStacksFromTag(potionStore, NEIClientConfig.global.nbt.getCompoundTag("potionStore").getTagList("items", 10));
    PacketCustom packet = new PacketCustom(channel, 24);
    for (ItemStack stack : potionStore)
        packet.writeItemStack(stack);
    packet.sendToServer();
}
 
Example #17
Source File: TileItemTranslocator.java    From Translocators with MIT License 5 votes vote down vote up
@Override
public void read(NBTTagCompound tag)
{
    super.read(tag);
    regulate = tag.getBoolean("regulate");
    signal = tag.getBoolean("signal");
    a_powering = tag.getBoolean("powering");
    InventoryUtils.readItemStacksFromTag(filters, tag.getTagList("filters", 10));
}
 
Example #18
Source File: ItemPanelDumper.java    From NotEnoughItems with MIT License 5 votes vote down vote up
@Override
public Iterable<String[]> dump(int mode) {
    LinkedList<String[]> list = new LinkedList<String[]>();
    for (ItemStack stack : ItemPanel.items)
        list.add(new String[]{
                Item.itemRegistry.getNameForObject(stack.getItem()).toString(),
                Integer.toString(Item.getIdFromItem(stack.getItem())),
                Integer.toString(InventoryUtils.actualDamage(stack)),
                stack.getTagCompound() == null ? "false" : "true",
                EnumChatFormatting.getTextWithoutFormattingCodes(GuiContainerManager.itemDisplayNameShort(stack))
        });

    return list;
}
 
Example #19
Source File: ContainerPotionCreator.java    From NotEnoughItems with MIT License 5 votes vote down vote up
@Override
public void onContainerClosed(EntityPlayer player) {
    super.onContainerClosed(player);

    if (!player.worldObj.isRemote)
        InventoryUtils.dropOnClose(player, potionInv);
}
 
Example #20
Source File: SlotDummy.java    From CodeChickenCore with MIT License 5 votes vote down vote up
@Override
public void putStack(ItemStack stack)
{
    if(stack != null && stack.stackSize > stackLimit)
        stack = InventoryUtils.copyStack(stack, stackLimit);
    super.putStack(stack);
}
 
Example #21
Source File: NEIClientPacketHandler.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public static void sendOpenPotionWindow() {
    ItemStack[] potionStore = new ItemStack[9];
    ArrayUtils.fillArray(potionStore, ItemStack.EMPTY);
    InventoryUtils.readItemStacksFromTag(potionStore, NEIClientConfig.global.nbt.getCompoundTag("potionStore").getTagList("items", 10));
    PacketCustom packet = new PacketCustom(channel, 24);
    for (ItemStack stack : potionStore) {
        packet.writeItemStack(stack);
    }
    packet.sendToServer();
}
 
Example #22
Source File: EnderItemStorage.java    From EnderStorage with MIT License 5 votes vote down vote up
public CompoundNBT saveToTag() {
    if (size != EnderStorageConfig.storageSize && open == 0) {
        alignSize();
    }

    CompoundNBT compound = new CompoundNBT();
    compound.put("Items", InventoryUtils.writeItemStacksToTag(items));
    compound.putByte("size", (byte) size);

    return compound;
}
 
Example #23
Source File: EnderItemStorage.java    From EnderStorage with MIT License 5 votes vote down vote up
public void loadFromTag(CompoundNBT tag) {
    size = tag.getByte("size");
    empty();
    InventoryUtils.readItemStacksFromTag(items, tag.getList("Items", 10));
    if (size != EnderStorageConfig.storageSize) {
        alignSize();
    }
}
 
Example #24
Source File: DefaultOverlayHandler.java    From NotEnoughItems with MIT License 5 votes vote down vote up
private List<IngredientDistribution> assignIngredients(List<PositionedStack> ingredients, List<DistributedIngred> ingredStacks) {
    ArrayList<IngredientDistribution> assignedIngredients = new ArrayList<>();
    for (PositionedStack posstack : ingredients)//assign what we need and have
    {
        DistributedIngred biggestIngred = null;
        ItemStack permutation = null;
        int biggestSize = 0;
        for (ItemStack pstack : posstack.items) {
            for (int j = 0; j < ingredStacks.size(); j++) {
                DistributedIngred istack = ingredStacks.get(j);
                if (!InventoryUtils.canStack(pstack, istack.stack) || istack.invAmount - istack.distributed < pstack.getCount()) {
                    continue;
                }

                int relsize = (istack.invAmount - istack.invAmount / istack.recipeAmount * istack.distributed) / pstack.getCount();
                if (relsize > biggestSize) {
                    biggestSize = relsize;
                    biggestIngred = istack;
                    permutation = pstack;
                    break;
                }
            }
        }

        if (biggestIngred == null)//not enough ingreds
        {
            return null;
        }

        biggestIngred.distributed += permutation.getCount();
        assignedIngredients.add(new IngredientDistribution(biggestIngred, permutation));
    }

    return assignedIngredients;
}
 
Example #25
Source File: DefaultOverlayHandler.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public DistributedIngred findIngred(List<DistributedIngred> ingredStacks, ItemStack pstack) {
    for (DistributedIngred istack : ingredStacks) {
        if (InventoryUtils.canStack(pstack, istack.stack)) {
            return istack;
        }
    }
    return null;
}
 
Example #26
Source File: NEIServerConfig.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public static void saveBannedItems() {
    File file = new File(saveDir, "banneditems.cfg");
    try {
        if (!file.exists()) {
            file.createNewFile();
        }

        PrintWriter p = new PrintWriter(file);
        p.println("#Saved in this format for external editing. The format isn't that hard to figure out. If you think you're up to it, modify it here!");

        for (ItemStackMap.Entry<Set<String>> entry : bannedItems.entries()) {
            NBTTagCompound key = InventoryUtils.savePersistant(entry.key, new NBTTagCompound());
            key.removeTag("Count");
            if (key.getByte("Damage") == 0) {
                key.removeTag("Damage");
            }

            p.print(key.toString());
            p.print("=[");
            int i = 0;
            for (String s : entry.value) {
                if (i++ != 0) {
                    p.print(", ");
                }
                p.print(s);
            }
            p.println("]");
        }
        p.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #27
Source File: PlayerSave.java    From NotEnoughItems with MIT License 5 votes vote down vote up
private void loadCreativeInv() {
    creativeInv = new ItemStack[54];
    ArrayUtils.fillArray(creativeInv, ItemStack.EMPTY);
    NBTTagList itemList = nbt.getTagList("creativeitems", 10);
    if (itemList != null) {
        InventoryUtils.readItemStacksFromTag(creativeInv, itemList);
    }
}
 
Example #28
Source File: TileItemTranslocator.java    From Translocators with MIT License 5 votes vote down vote up
private int insertAmount(ItemStack stack, ItemAttachment ia, InventoryRange range)
{
    int filter = filterCount(ia, stack);
    if(filter == 0)
        return 0;
    
    int fit = InventoryUtils.getInsertibleQuantity(range, stack);
    if(fit == 0)
        return 0;

    if(ia.regulate && filter > 0)
        fit = Math.min(fit, filter-countMatchingStacks(range, stack, true));
    
    return fit > 0 ? fit : 0;
}
 
Example #29
Source File: ContainerPotionCreator.java    From NotEnoughItems with MIT License 5 votes vote down vote up
@Override
public void onContainerClosed(EntityPlayer player) {
    super.onContainerClosed(player);

    if (!player.world.isRemote) {
        InventoryUtils.dropOnClose(player, potionInv);
    }
}
 
Example #30
Source File: ItemPanelDumper.java    From NotEnoughItems with MIT License 5 votes vote down vote up
@Override
public Iterable<String[]> dump(int mode) {
    LinkedList<String[]> list = new LinkedList<>();
    for (ItemStack stack : ItemPanel.items) {
        list.add(new String[] { Item.REGISTRY.getNameForObject(stack.getItem()).toString(), Integer.toString(Item.getIdFromItem(stack.getItem())), Integer.toString(InventoryUtils.actualDamage(stack)), stack.getTagCompound() == null ? "false" : "true", TextFormatting.getTextWithoutFormattingCodes(GuiHelper.itemDisplayNameShort(stack)) });
    }

    return list;
}