Spawning Your Custom Ore

Now you will need to make sure that your RedDiamondOre will spawn in your world, so that it can be mined, to be able to create custom tools and weapons.  Fortunately, Minecraft allows for custom world generators to be added to the default set.  The first thing to do is to create a new class, OreManager, which will be used to generate your ore as chunks are created.  

package reddiamond.world;

import java.util.Random;

import reddiamond.RedDiamondMod;
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 OreManager implements IWorldGenerator {
    
@Override
public void generate(Random random, int chunkX, int chunkZ, World world,
IChunkProvider chunkGenerator, IChunkProvider chunkProvider) {
        switch(world.provider.dimensionId){
        case -1: generateNether(world, random, chunkX*16,

                     chunkZ*16);
        case 0: generateSurface(world, random, chunkX*16,

                     chunkZ*16);
        case 1: generateEnd(world, random, chunkX*16,

                     chunkZ*16);
        }
    }
    
    private void generateSurface(World world, Random random,

            int x, int z) {
        this.addOreSpawn(RedDiamondMod.redDiamondOre, world,

                 random, x, z, 16, 16, 16, 128, 15, 160);
    }

    private void generateEnd(World world, Random random, int x,

        int z) {
        // add a call here to spawn in the End
    }

    private void generateNether(World world, Random random, int x,

        int z) {
        // add a call here to spawn in the Nether
    }

    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);
        int diffBtwnMinMaxY = maxY – minY;
        for(int x = 0; x < 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);
        }
    }
}

 

The OreManager will need to be created in the package reddimaond.world, and it will need to implement the IWorldGenerator interface.  This interface requires only the generate() method be implemented.  The generate() method branches off to three other private methods that control which worlds the block will be generated in.  You have the choice of letting it spawn in any or all of the three worlds: Surface, Nether and End.  The generate() method will be called for each of the worlds, but you may choose whether to spawn or not, depending on which world it is.  In this example, we are only spawning our RedDiamondOre block in the surface world. 

Internally the OreManager will use the Minecraft class, WorldGenMinable() in the addOreSpawn() method to do the actual work of generating the blocks within the chunk.  The WorldGenMinable class gives a lot of flexibility in how to generate the blocks, but I will leave it to you to study this as a topic of further study. 

It is important to understand that this configuration of numbers was selected to make the ore easy to find.  You will probably want to adjust them once you are comfortable with the ore generation processing.  In particular, consider reducing the vein size parameter.  The current setting will create very large veins of ore.  Also, considering changing the chancesToSpawn parameter, which is set very high to ensure the ore is plentiful and easy to find. 

After the OreManager class has been created, you will need to create an instance of it in the base mod class and then register the OreManager with the world generators that are used by the game.  To register it, you would use the GameRegistry.registerWorldGenerator() method. 

private OreManager oreManager = new OreManager();

GameRegistry.registerWorldGenerator(oreManager);