Step 7: Generating Ores!

So now we have a textured block that people in survival can craft. But what if we want to make an ore? If you plan on doing this, I would remove the ability for people to craft the block, because obviously ores aren't craftable. You can also re-think how you want your ore block to look and redo the textures either now or after this tutorial, as a big blue block with a gray one in the middle doesn't really fit in with other ores. Depending on how good you are with photoshop or gimp, you can even copy in the default gold texture and just adjust the hue/saturation of the colored pixels on the texture to your liking. Remember in the first tutorial when we added ".setHardness(2.5F)" ? Well you might also want to take this into closer consideration when making an ore, because making it 0.1F will make a block that breaks (almost) instantly, wheras setting it to 1000.0F will make people give up and stop mining it. Also note that this has nothing to do with whether you can actually mine the block or not, that will be covered later. We'll want to override the ModLoader method called generateSurface(..) which will be called when the world is generated, adding whatever you put in the method to the world gen. Add the following code to your mod_ file: Overriding The ModLoader Method @Override public void generateSurface(World world, Random random, int chunkX, int chunkZ) {      }






Again, don't change any of this or you won't be overriding the default method and nothing will happen later. Inside there we'll want to add a for loop: Generation For Loop for(int i = 0; i < 20; i++) {     int randPosX = chunkX + random.nextInt(16);     int randPosY = random.nextInt(20);     int randPosZ = chunkZ + random.nextInt(16);     new WorldGenMinable(this.yourFirstOre.blockID, 8).generate(world, random, randPosX, randPosY, randPosZ); }





for(..) - The for loop. Read java tutorials on this for more info. int i = 0; i < 20; i++ - Default for loop syntax. The 20 is the only thing that matters here. If you set this to 1 it will be as rare as diamond and if you set it to 20 it will generate as much as coal. Play around with this number until you get it right, to test if your generation is working, you can set this to about 50. int randPosX = chunkX + random.nextInt(16); - The exact block location on the X axis is stored in a variable called randPosX. It will take the chunk position and add a random number between 1 and 16 (the chunk size) to determine the exact coords of this ore vein. DO NOT CHANGE THE 16. int randPosY = random.nextInt(20); - This will generate a random number between 1 and 20 for the Y position of this vein. You can change 20 to whatever you like (above 0), such as diamond is set to 16 and coal is set to 128. int randPosZ = chunkZ + random.nextInt(16); - The exact block location on the Z axis is stored in a variable called randPosZ. It will take the chunk position and add a random number between 1 and 16 (the chunk size) to determine the exact coords of this ore vein. DO NOT CHANGE THE 16. new WorldGenMinable - Creating a new instance of the WorldGenMinable class, which takes the paramaters and does the generating. (this.yourFirstOre.blockID, 8) - Passing the block to generate and the number of blocks (max) to generate in one vein, minimum is 3 which will generate one block max. .generate - Also calling the generate method from WorldGenMinable to do the generation. (world, random, randPosX, randPosY, randPosZ) - "world" is the world to spawn it in, which is passed from the method declaration. "random" is the java Random field, also passed from the method declaration. "randPos" X, Y and Z are the integers we declared above, holding the vien location. Last, we'll set the harvest level of the block, to determine what tier of pickaxe can mine it. Obsidian ore is 3, Diamond ore is 2, Iron ore is 1, to give examples of the default setup. Add the following code into your load() method. Setting Harvest Level     MinecraftForge.setBlockHarvestLevel(yourFirstOre, "pickaxe", 3);
That should be it! Here's an example of mine generating using the above code (With a high generation rate and my horrible texture!): Oh, make sure you create a new world before telling me this one doesn't work, as you are changing the world generation and you have to check if it's working on a chunk generated AFTER you edited the code.

Previous                 Back to Tutorials Index                 Next
Minecraft and Forge are always changing, found something wrong with this tutorial?

external links

social