Modding: Ore generation

In this tutorial I will teach you how to generate a new ore in your world. This is the file I will be starting with.


package Tutorial.common;

import net.minecraft.src.Block;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraftforge.common.DungeonHooks;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = “YourName_ModName”, name = “ModName”, version = “Version number”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial 
{
public static Block oreblock;

public static Item youritem;

public static Item yourfood;

@SidedProxy(clientSide = “Tutorial.client.ClientProxyTutorial”, serverSide = “Tutorial.common.CommonProxyTutorial”)
    public static CommonProxyTutorial proxy;

@Init
public void load(FMLInitializationEvent event) 
{
oreblock = new BlockOres(230, 0).setStepSound(Block.soundStoneFootstep).setHardness(3F).setResistance(1.0F).setBlockName(“oreblock”);

GameRegistry.registerBlock(oreblock);

LanguageRegistry.addName(oreblock, “Your Ore”);

youritem = new ItemTutorial(550).setIconIndex(1).setItemName(“youritem”);

LanguageRegistry.addName(youritem, “Your Item”);

proxy.registerRenderThings();

DungeonHooks.addDungeonLoot(new ItemStack(youritem), 10, 2, 5);
DungeonHooks.setDungeonLootTries(50);

yourfood = new ItemTutorialFood(551, 8, true).setIconIndex(3).setItemName(“yourfood”);
LanguageRegistry.addName(yourfood, “Your Food”);

GameRegistry.registerFuelHandler(new TutorialFuel());
}
}


To add any generation to your world you will have to add this one line to your file.


GameRegistry.registerWorldGenerator(new WorldgeneratorTutorial());


The whole file should now look like this.


package Tutorial.common;

import net.minecraft.src.Block;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraftforge.common.DungeonHooks;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = “YourName_ModName”, name = “ModName”, version = “Version number”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial 
{
public static Block oreblock;

public static Item youritem;

public static Item yourfood;

@SidedProxy(clientSide = “Tutorial.client.ClientProxyTutorial”, serverSide = “Tutorial.common.CommonProxyTutorial”)
    public static CommonProxyTutorial proxy;

@Init
public void load(FMLInitializationEvent event) 
{
oreblock = new BlockOres(230, 0).setStepSound(Block.soundStoneFootstep).setHardness(3F).setResistance(1.0F).setBlockName(“oreblock”);

GameRegistry.registerBlock(oreblock);

LanguageRegistry.addName(oreblock, “Your Ore”);

youritem = new ItemTutorial(550).setIconIndex(1).setItemName(“youritem”);

LanguageRegistry.addName(youritem, “Your Item”);

proxy.registerRenderThings();

DungeonHooks.addDungeonLoot(new ItemStack(youritem), 10, 2, 5);
DungeonHooks.setDungeonLootTries(50);

yourfood = new ItemTutorialFood(551, 8, true).setIconIndex(3).setItemName(“yourfood”);
LanguageRegistry.addName(yourfood, “Your Food”);

GameRegistry.registerFuelHandler(new TutorialFuel());

GameRegistry.registerWorldGenerator(new WorldgeneratorTutorial());
}
}


The new line simply registers a class that you should use to start all your generation code.
This is what the file should look like when you just create it.


package Tutorial.common;

import java.util.Random;

import net.minecraft.src.IChunkProvider;
import net.minecraft.src.World;
import cpw.mods.fml.common.IWorldGenerator;

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

}

}


To add the methods that will generate you change it into this.


package Tutorial.common;

import java.util.Random;

import net.minecraft.src.IChunkProvider;
import net.minecraft.src.World;
import net.minecraft.src.WorldGenMinable;
import cpw.mods.fml.common.IWorldGenerator;

public class WorldgeneratorTutorial implements IWorldGenerator 
{
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);
}
}



private void generateSurface(World world, Random random, int blockX, int blockZ) 
{

}

private void generateNether(World world, Random random, int blockX, int blockZ) 
{

}

}


The new thing added is a switch method that will get the worldType. If the world type is -1 it will run the generateNether method. If the world type is 0 it will run the generateSurface method.
This will not do anything, because you haven’t added any WorldGen things in here. This is just the frame for the generation.
For this tutorial I will show you how to add a new ore to the Overworld. You will need this for it.


int Xcoord = blockX + random.nextInt(16);
int Ycoord = random.nextInt(60);
int Zcoord = blockZ + random.nextInt(16);

(new WorldGenMinable(Tutorial.oreblock.blockID, 10)).generate(world, random, Xcoord, Ycoord, Zcoord);


int Xcoord = blockX + random.nextInt(16) is used to get the location where the ore will start generating. blockX is the coordinate of the chunk and the random.nextInt(16) will choose a random number within that chunk to start from. This is exactly the same for the Z line.
int Ycoord = random.nextInt(60) will choose a number between 0 and 59. This is the height it will generate on.
WorldGenMinable is a class in the net.minecraft.src package and handles all the ore generation underground. It also generates the gravel and dirt pockets underground.
Tutorial.oreblock.blockID is the block used to be generated. 10 is the amount it will try to generate.
.generate() is a method inside of WorldGenMinable that places the blocks inside of the world.

The whole file should now look like this.


package Tutorial.common;

import java.util.Random;

import net.minecraft.src.IChunkProvider;
import net.minecraft.src.World;
import net.minecraft.src.WorldGenMinable;
import cpw.mods.fml.common.IWorldGenerator;

public class WorldgeneratorTutorial implements IWorldGenerator 
{
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);
}
}



private void generateSurface(World world, Random random, int blockX, int blockZ) 
{
int Xcoord = blockX + random.nextInt(16);
int Ycoord = random.nextInt(60);
int Zcoord = blockZ + random.nextInt(16);

(new WorldGenMinable(Tutorial.oreblock.blockID, 10)).generate(world, random, Xcoord, Ycoord, Zcoord);
}

private void generateNether(World world, Random random, int blockX, int blockZ) 
{

}
}


 

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">