Amazon Ad

Wednesday, February 1, 2017

Items

Hello, my name is Alex and I am going to show you how to create a mod for minecraft forge 1.11.2. There's not a lot of tutorials on 1.11 so I am creating one for those that want to get started.

WARNING: IF YOU DO NOT KNOW ANYTHING ABOUT JAVA, PLEASE LEARN THE BASICS BEFORE ATTEMPTING TO FOLLOW ANY MODDING TUTORIALS!!!!!


In this tutorial, we will be creating an item. Go ahead and create a new package called "com.<yourname>.tut.handlers". Within that, create a class called "ItemHandler". This is where we will initialize all of our Items.

Within this class we will create four methods, as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
 public static void init(){
  
 }
 
 public static void register(){
  
 }
 
 public static void registerRenders(){
  
 }
 
 public static void registerRender(Item item){
  
 }

"init()" is where we will initialize all of our items. "register()" is where we will register our items. "registerRenders()" is where we will be registering our rendering so that forge knows where to find the models and textures. "registerRender(Item item)" is how we will be telling Forge to look for our models and textures.

Within the "registerRender(Item item)" method, we will need to type in the following line of code:

1
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(item.getRegistryName(), "inventory"));

This just tells Minecraft to look in a specific namespace, being ours, rather than its own. Next we will create the field necessary for creating our item.

1
public static Item tutItem;

Now to initialize it in our "init" method:

1
tutItem = new ItemTutItem("tut_item", CreativeTabs.MATERIALS);

What we have done is we have created an item called "tutItem" and we initialized it to a new "ItemTutItem" which is a class we have not created yet. We gave it the arguments of a string "tutItem" which is the registry name and unlocalized name. The second argument is the creative tab our item will be in.

Now let's create a new package called "com.<yourname>.tut.items" and create a new class called "ModItem". In this we will create some constructors that will automatically set some configurations for our items. There will be a constructor that takes in a stacksize integer as well as a string and a creative tab. This class will also need to extend to "Item" class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 public ModItem(String name, CreativeTabs tab){
  setUnlocalizedName(name);
  setRegistryName(name);
  setCreativeTab(tab);
 }
 
 public ModItem(String name, CreativeTabs tab, int stacksize){
  setUnlocalizedName(name);
  setRegistryName(name);
  setCreativeTab(tab);
  setMaxStackSize(stacksize);
 }

The two different constructors will be used by its constituent classes. One constructor is a basic constructor, and the second constructor is for items that have a different max stack size (i.e.: snowballs have a max size of 16).

Now create the "ItemTutItem" class and extend it to "ModItems". Create the constructor for the class as it shows below:

1
2
3
 public ItemTutItem(String name, CreativeTabs tab) {
  super(name, tab);
 }

We are now finished with this class. Go back to your "ItemHandler" class and register your item in your "register" method.

1
GameRegistry.register(tutItem);

Register your rendering context in "registerRenders" method:

1
registerRender(tutItem);

Now we have to define the model files. In your "src/main/resources" folder, create a package called "assets.<modid>", so in our case it would be "assets.tut". Within this we will create another package called "models.item". Within this create a new Untitled Text File. Go into your Referenced Libraries > forgesrc.jar > assets > minecraft > models > item and look for a generic item file. We will use "diamond.json". Copy and paste everything into our newly created Untitled Text File. Now we gotta change something in this file.

Change:

1
"layer0": "items/diamond"

to

1
"layer0": "tut:items/tut_item"

This is looking for our texture. It looks for it in "assets.<modid>.textures.items". "tutItem" is our texture name. Save this and call it "tut_item.json". So let's go ahead and create a texture package called "assets.tut.textures.items". I will create a Paint.NET tutorial in time. Once you have created the texture for your item, save the item in your "textures.items" package.

MAKE SURE YOU IMPORT ALL YOUR FILES!!!!!
MAKE SURE YOU DO NOT CAMELCASE YOUR JSON FILES!!!!

We have to initialize the methods in our proxy class. So navigate to your "CommonProxy" and put in your "preInit" method the following code:


1
2
ItemHandler.init();
ItemHandler.register();

Then in your "ClientProxy" type this:


1
2
3
 public void init() {
  ItemHandler.registerRenders();
 }

These lines of code registers, initializes, and renders the items that we create, and these are very important lines of code.

Save and run the project. The end result for me is this:


4 comments:

  1. Great tutorial, but a lot of contradicting names may hurt copy+pasters. i.e the difference between ItemTutitem and ItemTutItem. If you need any further "idiot-proof" reading, I'd be very happy to help as I go along your tutorial.

    ReplyDelete
  2. Huh. I've never thought of making an item handler. I'm new to modding though, so I'd bet it's pretty common.
    Sadly, you've made one great mistake:
    Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(item.getRegistryName(), "inventory"));

    That's a deprecated way of registering items.
    You're supposed to do it like this:

    @SubscribeEvent
    public void registerBlocks(RegistryEvent.Register event) {
    event.getRegistry().registerAll(block1, block2, ...);
    }

    ReplyDelete
  3. " This class will also need to extend to "Item" class."

    -There is no such class as "Item"

    "Now create the "ItemTutItem" class and extend it to "ModItems". Create the constructor for the class as it shows below:"

    -There is no such class as "ModItems"

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete