Minecraft 1.12 modding with forge – 5 – custom ore block

Hello everyone,

Today we are going to create a custom oreblock. This block can be set to drop a different material, like diamond. And fortune will have effect on it. This tutorial will also show how to make a block that can drop multiple item types.

I created another Item and called it tutorialDust this is the Item my custom ore will drop. If you don’t know how to make an Item look at this tutorial. The model file is the same as in the tutorial but I used this texture: .
Lets get started!

If you only want one type of item follow this:

First you should create a new class in the blocks package. I called it BlockOre, make it extend BlockBasic.
Create 3 variables:

  1. An Item, I will call it itemToDrop. This will be assigned with the item it is gonna drop, or be null if it is gonna drop itself.
  2. An int, I will call it minDropAmount. This will be the minimum amount it is gonna drop.
  3. Another int, I will call it maxDropAmount. This will be the maximumt amount it is gonna drop. If you don’t use the constructor with the maxDropAmount, the ore will always drop the miminum amount.

Now create a constructor and call the super constructor. This constructor takes in the same as the BlockBasic class plus:

  • An Item, this is the item it is gonna drop.
  • An integer, this is the minimum amount it is gonna drop.
  • And finally another integer, this is the maximum amount it is gonna drop.

You could make more then 1 constructor. One that only takes the name and material, one that also takes an Item to drop, one that takes an integer for the (minimum)amount to drop and lastly one that takes another integer for the maximum amount to drop. In the last constructor you can assign the variables we created and call the super constructor. All of the other constructors you can call the last constructor with some default values.

  • For the constructors without maxDropAmount I will default it to 1.
  • For the constructors without minDropAmount I will default it to 1.
  • For the constructors without itemToDrop I will default it to null, we will block itself when it is null.

This is my class now:

Now you should Override 3 methods, make sure you import the right random object (import java.util.Random):

  1.  getItemDropped this will return the Item this block drops. It takes in an IBlockState, an random object and an integer.
  2. quantityDropped this will return the amount of items it drops without fortune. This takes in an random object.
  3. quantityDroppedWithBonus this will return the amount of items dropped with fortune. This takes in an integer for the fortune level and an random object.

In the getItemDropped dropped method you should check if the toDrop variable is null and return the ItemBlock object of the block, else you can just return the toDrop variable. This is a nice one line version:

return toDrop==null?Item.getItemFromBlock(this):toDrop; 

You could ofcourse also use an if/else statement.

The quantityDropped method will return a value between minDropAmount and maxDropAmount. If you don’t know how to do that here is a one line way:

return this.minDropAmount + random.nextInt(this.maxDropAmount - this.minDropAmount + 1); 

I also added a check to make sure minDropAmount is smaller then maxDropAmount.

The last method quantityDroppedWithBonus will check if fortune is used, if it isn’t it will call and retun quantityDropped. If it does have fortune it checks if getItemDropped isn’t dropping the block itself, if it is it also just calls and returns quantityDropped (This is because it would be possible to duplicate the block by using fortune, then placing and repeating that). It then calculates a multiplier, it will call the method quantityDropped multiply it with the calculated multiplier and return that. You could write your own fortune handling logic in this method but this is the vanilla way.

Now in the ModBlocks class create a new Block variable, I will call it tutorialOre. Instantiate this variable in the init method and pass it your values, when importing BlockOre make sure you import your custom class and not minecraft’s ore class. I will name it tutorial_ore use the Material.ROCK value. I also pass it the Item I created for this, you can do that by using ModItems.variablename. My ore will also drop between 1 and 5 items (without fortune). Then you can do the same as in the last tutorial, give it some custom behaviour like hardness and a creativetab and register it. In that tutorial it is also explained how you can give it a texture and a name, so I will not explain that here.

I used the same models as in that tutorial and this is the texture I used: 

With the class we just created it should be possible to create any ore you want!

If you want multiple types of items follow this:

First you should create a new class in the blocks package. I called it BlockOreMultiple, make it extend BlockBasic. This class will not be as abstract as the BlockOre class for one type of item. This is because every item could have a different drop chance and adding that to the constructor would complicate things to much.

Now create a constructor and call the super constructor. This constructor takes in the same as the BlockBasic class.

Now override the getDrops method, this method returns an ArrayList<ItemStack> and takes an IBlockAccess (the world), a BlockPos, an IBlockState and an Integer (the fortune level).

This is my class now:

In the getDrops method create an ArrayList<ItemStack> and instantiate it, I will call it drops.
Now you can add the items you want the ore to drop by adding an ItemStack to the ArrayList.
And at the end of the method return this ArrayList. While creating the ItemStack you can specify the amount of the item the ore is gonna drop. You can randomize it or just set a static number.
With this line you can make it drop the ModItems.tutorialDust Item with a random amount between 2 and 6.

drops.add(new ItemStack(ModItems.tutorialDust, RANDOM.nextInt(4)+2));

Keep adding items in this way until you have added all of the items you want your ore to drop.
You can also add an item depending on other values, just to show this I will make the ore drop a diamond when RANDOM.nextFloat() is greater then 0.5.
This is the class complete:

Now in the ModBlocks class create a new Block variable, I will call it tutorialOreMultiple. Instantiate this variable in the init method and pass it your values. I will name it tutorial_ore_multiple use the Material.ROCK value.  Then you can do the same as in the last tutorial, give it some custom behaviour like hardness and a creativetab and register it. In that tutorial it is also explained how you can give it a texture and a name, so I will not explain that here.

In the next tutorial we will add crafting recipes! As always if you have any question don’t hesitate to ask in the comments! Also if you saw any errors in the tutorial please let me know so I can fix it!

~suppergerrie2

Posted in Forge tutorial, Forge Tutorial 1.12.

28 Comments

  1. Hello, I’m looking at this and can find anywhere where you actually set what item is dropped. What am I missing?

    • This depends on which of the two versions you use, if you use the version for a single item, then just pass in the item you want it to drop in the constructor. Like here

      If you use the version where it drops multiple items you need to do it in the getDrops method you override. Like here

  2. Thanks for this tutorial! I just have the following problem: My custom ore block only drops itself. It seems that my BlockOreMultiple class isn’t connected to the rest of the mod since it makes no difference if the class is there or not. Where do I have to tell the mod that this class is there?

    • Heyo!

      Please post your code on github so I can take a look, you dont need to “connect” a class, when you write it and reference it in your mod it is automatically compiled and used. (It would crash else :P)
      Make sure you are instantiating the right class (new BlockOreMultiple instead of new BlockOre)

      ~suppergerrie2

      • Your reply already helped me. I had defined the new block as “new BlockBasic” instead of “new BlockOreMultiple”. That was the “missing link” to the BlockOreMultiple class I was talking about. Now it works fine, thanks a bunch!

    • the issue would seem to be that ‘this’ will drop the ore block, but if you do a ‘return this.toDrop’ you will get the dust. had this issue recently.

      • this refers to the object (in this case the block) itself, this.toDrop references the toDrop variable in the block.

  3. Hey, just thought you might want to know that the @Override “getItemDropped” part is not very clear. I had to guess and figure out what to write, ultimately come to this:

    @Override

    public Item getItemDropped(IBlockState state, Random random, int i) {
    return toDrop==null?Item.getItemFromBlock(this):toDrop;
    }

    I’m so glad it worked haha

    • Hello, what about it is not very clear? I looked at it but I’m not sure what I could change to make it more clear. ~suppergerrie2

  4. Hello, sorry to bother you, but I’m a bit confused. I would like the ore to drop a set quantity of items always, without any variation, what should I do?

    • Hello, if you want your ore to drop multiple items but always the same just do the multiple ore part of the tutorial but remove the random. ~suppergerrie2

  5. how do you implement this when you have multiple ores? like how can you specify what ore is going to get what drop? i’m new to this so i’m sorry if it’s plainly obvious.

    • This depends on how complex the ores are. If you want multiple ores that just drop 1 item you can create multiple BlockOre variables and instantiate them with the item you want: oreBlock = new BlockOre(ore_name, material, ITEM!);

      If you want multiple ores that drop multiple items you need to create a custom class for each of them. I hope this helps, if it doesn’t just ask and I will how I can help!
      ~suppergerrie2

      • I don’t think that would work with the way i had my ores. I had my ores in my ModBlocks class as ORE_OVERWORLD, ORE_NETHER,ORE_END, which would be told what type of variant property the ore was by another class to make it easier to add more ores later on in my ModBlocks and to my CustomOreGen. Although i did find out how to make it work it had some issues, both of my ores drop the items i want them to but one of the ores drops the item with a broken texture and if i give myself the item from creative the texture is fine but not when mined. Here is the code maybe you can help somehow.

        @Override
        public Item getItemDropped(IBlockState state, Random rand, int fortune)
        {
        if(state.getValue(VARIANT) == EnumType.TIN)
        return ModItems.TIN_CLUSTER;
        else if(state.getValue(VARIANT) == EnumType.STEEL
        return ModItems.STEEL_CLUSTER;
        return null;
        }

        • Hello,

          Is the item also bugged in your inventory after you pick it up? The console probably has some model loading errors, those will probably say what is wrong. I can’t say anything from the code you gave sorry!

          ~Suppergerrie2

          • Thanks for trying to help i figured out the issue, it was the way i had set up the ores for the ore generation so i followed your way of ore generation instead and it worked flawlessly.

    • Hello,

      When creating an ItemStack you can pass in the meta you want the Item to have: new ItemStack(ITEM, AMOUNT, META)

      ~suppergerrie2

  6. Hello, for some reason the block does not drop the item specified and just drops itself?
    I did exactly everything you had written in this tutorial.

  7. Thank you very much for this tutorial, it was very helpful! I was wondering though, if you wanted to make the ore drop more than one kind of item, how would you go about doing that? I tried changing Item toDrop into Item[] toDrop, but that didn’t work. Thanks again!

    • Hello, thank you so much for the nice comment!
      To make a block drop more then one item you should override the ‘getDrops’ method instead of the ‘getItemDropped’ ‘quantityDropped’ and ‘quantityDroppedWithBonus’ methods. This method returns an ArrayList with type ItemStack (ArrayList). You can add drops to the ArrayList by adding ItemStacks, here you can also define how much it should drop.
      An example could be this: example method.
      This method makes the block drop 10 redstone items and a random amount of diamonds (Between 0 and 10).
      This would be the end result: complete class.
      I hope this helped you!

      • Thank you for this it really helped! Can you give me an example of how I’d register the block in the ModBlocks class though? I’m having difficulty with that.

        • Hello,

          You should register it in the same way as a normal block as shown in this tutorial, except instead of BlockBasic you should instantiate BlockOre. This should be the ModBlocks class. I hope this helped you! You can also look on github to see the complete files. This is the code from this tutorial.

          ~suppergerrie2

          • Thank you so much! This was very helpful and I’ll be looking forward to future tutorials!

          • Thank you! Good to hear it helped! If you have any ideas for future tutorials please let me know!

            ~suppergerrie2

Leave a Reply

Your email address will not be published. Required fields are marked *