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:
- 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.
- An int, I will call it minDropAmount. This will be the minimum amount it is gonna drop.
- 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):
- getItemDropped this will return the Item this block drops. It takes in an IBlockState, an random object and an integer.
- quantityDropped this will return the amount of items it drops without fortune. This takes in an random object.
- 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:
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.
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!