Lesson: Minecraft Ore Generation

Let’s recap what we’ve done so far. We’ve made custom recipes, custom blocks, and we made those blocks manipulate the Minecraft world around us. Now it’s time to make those blocks spawn naturally in the world.

In Minecraft all blocks have rules which control how and where they spawn. The block spawning process is determined the first time a chunk is loaded. In Minecraft a chunk is 16x 16 x 256 blocks. When you generate a new world some chunks around the spawn area are created and as you explore you will create and load more chunks.

You’ll want to download the latest zip of our mod here. Once downloaded open the zip file. Copy the “com” and “assests” folders to your folder “forge/mcp/src/minecraft/” (overwriting any previous files in there). Now fire up Eclipse and let’s take a look at the new code.

If you open up our com.fredxcoders.dojo.mod.BaseMod.java file you may miss the change we did here but it’s an important one.

EventManager eventManager = new EventManager();
GameRegistry.registerWorldGenerator(eventManager);

What we’ve done is create an EventManager object and register it as a WorldGenerator. What is an EventManager you ask? Well let’s take a look at that then. Open up com.fredxcoders.dojo.mod.managers.EventManager.java. You should see something like this:

package com.fredxcoders.dojo.mod.managers;</code>

import java.util.Random;

import com.fredxcoders.dojo.mod.BaseMod;

import net.minecraft.block.Block;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.feature.WorldGenMinable;
import cpw.mods.fml.common.IWorldGenerator;

public class EventManager implements IWorldGenerator{

@Override
public void generate(Random random, int chunkX, int chunkZ, World world,
IChunkProvider chunkGenerator, IChunkProvider chunkProvider) {

//world.provider.dimensionId -1 is nether, 0 is overworld, 1 is end
if(world.provider.dimensionId == 0){
generateOverworld(world, random,chunkX*16, chunkZ*16);
}
}

private void generateOverworld(World world, Random random, int x, int z) {
int maxX = 16; //minX is one, so setting this to 16 says all of the x ranges can be used
int maxZ = 16; //minY is one, so setting this to 16 says all of the y ranges can be used
int maxVeinSize = 4 + random.nextInt(5); //this means each patch of our blocks will be between 4 and 8 large
int chancesToSpawn = 50; //How many times our ore can spawn in one chunk
int minY = 15; //how low the ore can spawn
int maxY = 80; //how high the ore can spawn
this.addOreSpawn(BaseMod.MY_BLOCK, world, random, x, z, maxX, maxZ, maxVeinSize, chancesToSpawn, minY, maxY);
}

/**
* Adds an Ore Spawn to Minecraft. Simply register all Ores to spawn with this method in your Generation method in your IWorldGeneration extending Class
*
* @param The Block to spawn
* @param The World to spawn in
* @param A Random object for retrieving random positions within the world to spawn the Block
* @param An int for passing the X-Coordinate for the Generation method
* @param An int for passing the Z-Coordinate for the Generation method
* @param An int for setting the maximum X-Coordinate values for spawning on the X-Axis on a Per-Chunk basis
* @param An int for setting the maximum Z-Coordinate values for spawning on the Z-Axis on a Per-Chunk basis
* @param An int for setting the maximum size of a vein
* @param An int for the Number of chances available for the Block to spawn per-chunk
* @param An int for the minimum Y-Coordinate height at which this block may spawn
* @param An int for the maximum Y-Coordinate height at which this block may spawn
**/
public void addOreSpawn(Block block, World world, Random random, int blockXPos, int blockZPos, int maxX, int maxZ, int maxVeinSize, int chancesToSpawn, int minY, int maxY)
{
int maxPossY = minY + (maxY - 1);
assert maxY &gt; minY: "The maximum Y must be greater than the Minimum Y";
assert maxX &gt; 0 &amp;&amp; maxX &lt;= 16: "addOreSpawn: The Maximum X must be greater than 0 and less than 16"; assert minY &gt; 0: "addOreSpawn: The Minimum Y must be greater than 0";
assert maxY &lt; 256 &amp;&amp; maxY &gt; 0: "addOreSpawn: The Maximum Y must be less than 256 but greater than 0";
assert maxZ &gt; 0 &amp;&amp; maxZ &lt;= 16: "addOreSpawn: The Maximum Z must be greater than 0 and less than 16";

int diffBtwnMinMaxY = maxY - minY;
for(int x = 0; x &lt; chancesToSpawn; x++)
{
int posX = blockXPos + random.nextInt(maxX);
int posY = minY + random.nextInt(diffBtwnMinMaxY);
int posZ = blockZPos + random.nextInt(maxZ);
(new WorldGenMinable(block.blockID, maxVeinSize)).generate(world, random, posX, posY, posZ);
// System.out.println("We just spawned a MY_BLOCK! [" + x + "]");
}
}
}

Let’s go over what this code is doing. First you can see that our EventManager object Implements IWorldGenerator. The IWorldGenerator class was created by Minecraft to help control how blocks are generated. When we implement that interface we have to include one method:

public void generate(Random random, int chunkX, int chunkZ, World world,
IChunkProvider chunkGenerator, IChunkProvider chunkProvider) {

This is the method Minecraft is going to execute when it goes to create a new chunk. What does this method do? Well, it checks  if the current world.provider.dimensionId is 0 (according to the comment that means that the player generating the new chunk is in the overworld) and then calls the generateOverworld method.

private void generateOverworld(World world, Random random, int x, int z) {
int maxX = 16; //minX is one, so setting this to 16 says all of the x ranges can be used
int maxZ = 16; //minY is one, so setting this to 16 says all of the y ranges can be used
int maxVeinSize = 4 + random.nextInt(5); //this means each patch of our blocks will be between 4 and 8 large
int chancesToSpawn = 50; //How many times our ore can spawn in one chunk
int minY = 15; //how low the ore can spawn
int maxY = 80; //how high the ore can spawn
this.addOreSpawn(BaseMod.MY_BLOCK, world, random, x, z, maxX, maxZ, maxVeinSize, chancesToSpawn, minY, maxY);
}

This method sets a bunch of parameters for how our ore will be spawned (check the comments for what each one does). Finally after the parameters are set it calls addOreSpawn which is a nice little method I borrowed from wuppy29 who has some great tutorials here. We pass BaseMod.MY_BLOCK (our custom block) to addOreSpawn so that it can be added to the chunk.

Now that we’ve looked at the code let’s fire up Minecraft and see if we can spot our ores in the wild! You’ll probably want to create a new world and hit  F3 to show your coordinated. Make sure your “y” is in the minY and maxY range specified in generateOverworld when looking for the ore. It also helps to be on creative mode.

Once you’ve spotted some ore and verify these changes worked, start playing around with the parameters in generateOverworld. Here are some tips:

    1. Make the ore spawn only in high places or only low.
    2. Make it rare but concentrated in large clusters or bountiful but in small amounts.
    3. Manage to crash Minecraft with your changes. Awesome! Write down your steps. Knowing how to crash a program will help you figure out how not to.
    4. What happens if you don’t start with a fresh world after each change? When do your changes start happening?
    5. Take a screenshot of something that looks cool.
    6. Uncomment this line. When do you notice these statements print to your console in Eclipse?:
// System.out.println("We just spawned a MY_BLOCK! [" + x + "]");

Extra credit:

  1. Our code currently spawns our custom block. What if we want to spawn some existing blocks in new places or next to our custom block. How could we do that?
  2. In our generate method there is a comment which references the nether. See if you can get our custom block to spawn there as well.