Solved Inventory Sorter

Discussion in 'Plugin Development' started by Smeary_Subset, Sep 4, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    Smeary_Subset

    I'm trying to make an inventory sorter plugin that allows players to sort a chest's inventory by looking at it and typing /sort. I'm aware there are plugins that already do this, however I am determined to make my own.

    Right now, I'm having a real hard time with the algorithm. Currently, I have gotten as far as being able to group together materials of the same type, however I'm having trouble stacking them. And, of course, items that contain more data than just their type (such as items with a durability) are also something I'm having a hard time with.

    Any ideas on how to solve this problem? I'm not focused on getting the most efficient algorithm. Just a decent one that works.
     
  2. Offline

    CraftCreeper6

    @Smeary_Subset
    You can use ItemStack.isSimilar() to check if ItemStacks are close to one another.

    An example:
    For a game I made a while ago, I used this to 'sort' the inventories, it's not as complex as a Minecraft one since my items didn't have metadata, but the concept is similar.
    It's C# so you'll have to convert it too.
    Code:
        public void SortInventory()
        {
            Dictionary<string, int> count = new Dictionary<string, int>();
            foreach (int i in this.GetCurrentMachine().inventoryContents.Keys) // i is slot number
            {
                    if (this.GetCurrentMachine().inventoryContents[i] != null) // null check
                    {
                        Item item = this.GetCurrentMachine().inventoryContents[i].GetItem(); // get the item  at the given slot
                        if (count.ContainsKey(item.GetName())) // if the count dictionary contains the item type already...
                        {
                            int c = count[item.GetName()]; // get the current count
                            c += this.GetCurrentMachine().inventoryContents[i].GetStackSize(); // increase it by the stack amount
                            count[item.GetName()] = c; // set the new value
                        }
                        else
                        {
                            count.Add(item.GetName(), this.GetCurrentMachine().inventoryContents[i].GetStackSize()); // add the count to the dictionary
                        }
                    }
            }
    
            this.GetCurrentMachine().inventoryContents.Clear(); // clear the inventory so you can readd the sorted items
    
            // you should sort the items here
    
            foreach (string i in count.Keys) // loop the ordered item types
            {
                Item item = register.FindItem(i); // get the item that it represents
                for (int x = 0; x < count[i]; x++) // loop
                {
                    this.GetCurrentMachine().AddItem(item, 1); // add each item individually, Inventory#addItem() will sort stack sizes out for you
                }
            }
    
            this.UpdateUI();
        }
    You can sort the "count" dictionary before doing anything else, sort by item type, metadata, etc...
     
    Last edited: Sep 5, 2022
  3. Offline

    Smeary_Subset

    I ended up giving up on making the items stack because it wasn't crucial and wanted to focus on other things.
     
Thread Status:
Not open for further replies.

Share This Page