Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Sorting your CreativeTab


diesieben07
 Share

Recommended Posts

Ever wanted to sort the Items in your creative tab in a specific order? I'm going to show you how.

 

Basic Idea

You are going to need a new class that extends CreativeTabs. In that class you override the displayAllRelevantItems method. It is the core method of the tab, it fills the NonNullList<ItemStack> parameter with the ItemStack instances that should be displayed on this particular tab. The idea is that you first call the super method to fill the list and then sort it, using a Comparator:

items.sort(comparator);
 

Making the Comparator

The easiest way to create a Comparator for your Items is by creating a List that specifies the order of your Items. In the simplest way:

List<Item> myItemsInOrder = Arrays.asList(itemA, itemB, itemC);
 

The Guava library, which is included with Minecraft, has a handy method which can sort according to an already sorted List:

Comparator<Item> c = Ordering.explicit(myItemsInOrder);
 

That Comparator will sort any List so that it conforms to the order you've specified.

 

The problem here is that this Comparator sorts a List of Items, the List we want to sort contains ItemStacks though. But thanks to Guava and Java 8's method references we can easily convert it:

Comparator<ItemStack> c = Ordering.explicit(myItemsInOrder).onResultOf(ItemStack::getItem);

 

Finishing up

The Comparator you've created can be reused, you only need to create it once. Then store it in a static field in your Main mod class and use it from your CreativeTabs class.

 

Metadata

Any Sub-ItemStacks that you create in getSubItems (resp. getSubBlocks) will not be reordered, they stay in the order you add them to the List.

 

Example

The following code does not compile. This is intentional, do not simply copy&paste it.

@Mod
class MyMod {

    static Comparator<ItemStack> tabSorter;

    private static final CreativeTabs creativeTab = new CreativeTabs() {

        @Override
        public void displayAllRelevantItems(NonNullList<ItemStack> items) {
            super.displayAllRelevantItems(items);
            items.sort(tabSorter);
        }
    }

    void preInit() {
        // create items, blocks, etc.
        List<Item> order = Arrays.asList(myItemA, myItemB, Item.getItemFromBlock(myBlockA), ...);
        tabSorter = Ordering.explicit(order).onResultOf(ItemStack::getItem);
    }
}

     

Edited by diesieben07
update for new forum formatting, new Minecraft version and Java 8
  • Thanks 1
Link to comment
Share on other sites

  • 1 year later...
  • 2 weeks later...

Now i found another way to sort my creative-tab:

    @Override
    public void fill(NonNullList<ItemStack> itemStacks)
    {
        List<Item> items = Arrays.asList(your_item_1, your_item_2, ...);
        
        itemStacks.clear();
        
        for (Item item : items)
        {
            if(item.getCreativeTabs().contains(YOUR_ITEMGROUP))
            {
                itemStacks.add(new ItemStack(item));
            }
        }
    }

You have to fill the items-list with your registered items (i think, many modders have the references in an item-list-class) in the right order.

Where i placed "YOUR_ITEMGROUP" you have to insert your own creativetab/itemgroup.

 

Every time, you add a new item to your mod, you have to set it to your itemgroup and add it to the list here in the position, where you want to have it.

Edited by Drachenbauer
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.