Jump to content

How do I create a creative tab and add vanilla Minecraft items and blocks to it in a certain order?


X66Herobrine66X

Recommended Posts

Trying to recreate the 1.19.3 creative changes in my mod. I know the basics of Java but I usually code with MCreator so if you could walk me through it that'd be great.

I found this link in an old thread, but it looks like it's outdated. Also I don't really understand whether I would have added that code to my existing creative tab class or have it as it's own separate class. Either way I had difficulties figuring out what each thing was.

 

Link to comment
Share on other sites

The default processing asks each item to add its relevant ItemStacks to a list in the order in which the Items were registered.

You override the method that does this in your custom CreativeModeTab.  e.g. (untested code)

    public static CreativeModeTab MY_CREATIVE_MODE_TAB = new CreativeModeTab("whatever") {

        @Override
        public void fillItemList(NonNullList<ItemStack> list) {
            // Default processing fills the list
            super.fillItemList(list);
            // Manipulate the list, e.g. sort by reverse order of rarity
            list.sort(Comparator.comparing(ItemStack::getRarity).reversed());
        }

        @Override
        public ItemStack makeIcon() {
            return Items.DIAMOND.getDefaultInstance();
        }
    };

 

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

15 minutes ago, warjort said:

The default processing asks each item to add its relevant ItemStacks to a list in the order in which the Items were registered.

You override the method that does this in your custom CreativeModeTab.  e.g. (untested code)


    public static CreativeModeTab MY_CREATIVE_MODE_TAB = new CreativeModeTab("whatever") {

        @Override
        public void fillItemList(NonNullList<ItemStack> list) {
            // Default processing
            super.fillItemList(list);
            // Manipulate the list, e.g. sort by reverse order of rarity
            list.sort(Comparator.comparing(ItemStack::getRarity).reversed());
        }

        @Override
        public ItemStack makeIcon() {
            return Items.DIAMOND.getDefaultInstance();
        }
    };

 

Ok that's worked so far, but I'm trying to add specific blocks in a specific order. This is my code so far:
 

package net.angelofcreation.randomshit.item;

import com.google.common.collect.Ordering;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Blocks;
import net.minecraftforge.common.Tags;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class ModCreativeModeTab {

    static Comparator<ItemStack> tabSorter;

    public static final CreativeModeTab BUILDING_BLOCKS = new CreativeModeTab("building_block") {

        @Override
        public void fillItemList(NonNullList<ItemStack> list) {
            // Default processing
            super.fillItemList(list);
            // Manipulate the list, e.g. sort by reverse order of rarity
            list.sort(Comparator.comparing(ItemStack::getRarity).reversed());
        }

        @Override
        public ItemStack makeIcon() {
            return new ItemStack(Blocks.BRICKS);
        }

        void preInit() {
            List<Item> order = Arrays.asList();
            tabSorter = Ordering.explicit(order).onResultOf(ItemStack::getItem);
        }

    };
}

The problem I'm now running into is what to put in Arrays.asList. The link I'm going off of for this section says "List<Item> order = Arrays.asList(myItemA, myItemB, Item.getItemFromBlock(myBlockA), ...);" What would 'myItemA' be? What if it's a vanilla item I want to add?

Link to comment
Share on other sites

38 minutes ago, X66Herobrine66X said:

Ok that's worked so far, but I'm trying to add specific blocks in a specific order. This is my code so far:
 

The problem I'm now running into is what to put in Arrays.asList. The link I'm going off of for this section says "List<Item> order = Arrays.asList(myItemA, myItemB, Item.getItemFromBlock(myBlockA), ...);" What would 'myItemA' be? What if it's a vanilla item I want to add?

'myItemA' would be the item reference you want.

So obsidian would be: Item i = Items.OBSIDIAN;

Edited by MistaOmega
Link to comment
Share on other sites

4 minutes ago, MistaOmega said:

'myItemA' would be the item reference you want.

So obsidian would be: Item i = Items.OBSIDIAN;

Thank you. This is the code that I've come up with:

package net.angelofcreation.randomshit;

import com.google.common.collect.Ordering;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.CreativeModeTab;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class CreativechangesModTabs {

    static Comparator<ItemStack> tabSorter;

    public static CreativeModeTab TAB_BUILDING_BLOCKS;

    public static void load() {
        TAB_BUILDING_BLOCKS = new CreativeModeTab("tabbuilding_blocks") {


            @Override
            public void fillItemList(NonNullList<ItemStack> list) {
                List<Item> order = Arrays.asList(Items.OAK_LOG, Items.OAK_WOOD, Items.STRIPPED_OAK_LOG, Items.STRIPPED_OAK_WOOD);
                tabSorter = Ordering.explicit(order).onResultOf(ItemStack::getItem);
                super.fillItemList(list);
                list.sort(tabSorter);
            }

            @Override
            public ItemStack makeIcon() {
                return new ItemStack(Blocks.BRICKS);
            }
        };
    }
}

Unfortunately, the creative tab does not have any of the 4 items I specified. Do you know why this is and how I can fix it?

Link to comment
Share on other sites

 

5 minutes ago, X66Herobrine66X said:

Thank you. This is the code that I've come up with:

package net.angelofcreation.randomshit;

import com.google.common.collect.Ordering;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.CreativeModeTab;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class CreativechangesModTabs {

    static Comparator<ItemStack> tabSorter;

    public static CreativeModeTab TAB_BUILDING_BLOCKS;

    public static void load() {
        TAB_BUILDING_BLOCKS = new CreativeModeTab("tabbuilding_blocks") {


            @Override
            public void fillItemList(NonNullList<ItemStack> list) {
                List<Item> order = Arrays.asList(Items.OAK_LOG, Items.OAK_WOOD, Items.STRIPPED_OAK_LOG, Items.STRIPPED_OAK_WOOD);
                tabSorter = Ordering.explicit(order).onResultOf(ItemStack::getItem);
                super.fillItemList(list);
                list.sort(tabSorter);
            }

            @Override
            public ItemStack makeIcon() {
                return new ItemStack(Blocks.BRICKS);
            }
        };
    }
}

Unfortunately, the creative tab does not have any of the 4 items I specified. Do you know why this is and how I can fix it?

Because you're not pushing anything into the super.

The super looks like it wants a NonNullList of ItemStacks. So create a list of ItemStacks, you can create an ItemStack with an item via: ItemStack stack = new ItemStack(Items.OBSIDIAN);
Then I'd send that list that you make into the super.fillItemList.

list.sort only serves to modify the order of the list, you need to ensure that what you create and manipulate you send to the super. If you're unsure what super is for, refer to this link here: https://www.w3schools.com/java/ref_keyword_super.asp

Edited by MistaOmega
Link to comment
Share on other sites

12 minutes ago, MistaOmega said:

 

Because you're not pushing anything into the super.

The super looks like it wants a NonNullList of ItemStacks. So create a list of ItemStacks, you can create one via it's constructor. E.G: ItemStack stack = new ItemStack(Items.OBSIDIAN);
Then I'd send that list that you make into the super.fillItemList.

list.sort only serves to modify the order of the list, you need to ensure that what you create and manipulate you send to the super. If you're unsure what super is for, refer to this link here: https://www.w3schools.com/java/ref_keyword_super.asp

Does this look right? It's crashing my game when I click on the tab:
 

package net.angelofcreation.randomshit;

import com.google.common.collect.Ordering;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.CreativeModeTab;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class CreativechangesModTabs {

    static Comparator<ItemStack> tabSorter;

    public static CreativeModeTab TAB_BUILDING_BLOCKS;

    public static void load() {
        TAB_BUILDING_BLOCKS = new CreativeModeTab("tabbuilding_blocks") {


            @Override
            public void fillItemList(NonNullList<ItemStack> list) {
                // Create list of itemstacks
                List<ItemStack> itemList = new ArrayList<ItemStack>();
                
                // Add itemstacks to list
                itemList.add(new ItemStack(Items.OAK_LOG));
                itemList.add(new ItemStack(Items.OAK_WOOD));
                itemList.add(new ItemStack(Items.STRIPPED_OAK_LOG));
                itemList.add(new ItemStack(Items.STRIPPED_OAK_WOOD));

                // set list to itemList
                list = (NonNullList<ItemStack>) itemList;

                // sort list
                List<Item> order = Arrays.asList(Items.OAK_LOG.asItem(), Items.OAK_WOOD.asItem(), Items.STRIPPED_OAK_LOG.asItem(), Items.STRIPPED_OAK_WOOD.asItem());
                tabSorter = Ordering.explicit(order).onResultOf(ItemStack::getItem);
                list.sort(tabSorter);
                
                // add list to super
                super.fillItemList(list);
            }

            @Override
            public ItemStack makeIcon() {
                return new ItemStack(Blocks.BRICKS);
            }
        };
    }
}

 

Link to comment
Share on other sites

Don't do all that new list stuff, all that casting and so on is bound to make an error, also, call your super first

 

                // add list to super
                super.fillItemList(list);

                // Add itemstacks to list
                list.add(new ItemStack(Items.OAK_LOG));
                list.add(new ItemStack(Items.OAK_WOOD));
                list.add(new ItemStack(Items.STRIPPED_OAK_LOG));
                list.add(new ItemStack(Items.STRIPPED_OAK_WOOD));


                // sort list
                List<Item> definedOrder = Arrays.asList(Items.OAK_LOG.asItem(), Items.OAK_WOOD.asItem(), Items.STRIPPED_OAK_LOG.asItem(), Items.STRIPPED_OAK_WOOD.asItem());
                Comparator<ItemStack> tabSorter;tabSorter = Ordering.explicit(definedOrder).onResultOf(ItemStack::getItem);
                list.sort(tabSorter);
                

this should do it, after testing it shows up with what you want

Edited by MistaOmega
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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.