鉱石の追加(1.7.10)

1.7.10の開発講座を修正中です。このページには誤りや古い情報が含まれる可能性があります。

ドロップアイテムがそのブロック自身ではなく、さらに経験値をドロップするブロックを追加し、地下に生成させる。

ソースコード


package tutorial.aluminiummod;

import net.minecraft.block.Block; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.registry.GameRegistry;

@Mod(modid = AluminiumMod.MODID, name = AluminiumMod.MODNAME, version = AluminiumMod.VERSION) public class AluminiumMod {

public static final String MODID = "AluminiumMod"; public static final String MODNAME = "Aluminium Mod"; public static final String VERSION = "1.0.0";

public static Item aluminium;

public static Block oreAluminium;

@EventHandler public void perInit(FMLPreInitializationEvent event) { aluminium = new Item() .setCreativeTab(CreativeTabs.tabMaterials) .setUnlocalizedName("aluminium") .setTextureName("aluminiummod:aluminium"); GameRegistry.registerItem(aluminium, "aluminium");

oreAluminium = new AluminiumOre() .setBlockName("oreAluminium") .setBlockTextureName("aluminiummod:aluminium_ore"); GameRegistry.registerBlock(oreAluminium, "oreAluminium"); }

@EventHandler public void init(FMLInitializationEvent event) { GameRegistry.registerWorldGenerator(new AluminiumOreGenerator(), 0); }

}


package tutorial.aluminiummod;

import java.util.Random;

import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; import net.minecraft.util.MathHelper; import net.minecraft.world.IBlockAccess;

public class AluminiumOre extends Block {

private Random random = new Random();

public AluminiumOre() { super(Material.rock); this.setCreativeTab(CreativeTabs.tabBlock); this.setHardness(3.0F); this.setResistance(5.0F); this.setStepSound(Block.soundTypeStone); this.setHarvestLevel("pickaxe", 2); }

@Override public Item getItemDropped(int meta, Random random, int fortune) { return AluminiumMod.aluminium; }

@Override public int quantityDroppedWithBonus(int fortune, Random random) { if (fortune > 0 && Item.getItemFromBlock(this) != this.getItemDropped(0, random, fortune)) { int i = random.nextInt(fortune + 2) - 1; if (i < 0) { i = 0; } return this.quantityDropped(random) * (i + 1); } else { return this.quantityDropped(random); } }

@Override public int getExpDrop(IBlockAccess iBlockAccess, int meta, int fortune) { return MathHelper.getRandomIntegerInRange(random, 3, 7); }

}


package tutorial.aluminiummod;

import java.util.Random;

import net.minecraft.init.Blocks; import net.minecraft.world.World; import net.minecraft.world.WorldProviderSurface; import net.minecraft.world.chunk.IChunkProvider; import net.minecraft.world.gen.feature.WorldGenMinable; import cpw.mods.fml.common.IWorldGenerator;

public class AluminiumOreGenerator implements IWorldGenerator {

@Override public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) { if (world.provider instanceof WorldProviderSurface) { generateOre(world, random, chunkX << 4, chunkZ << 4); } }

private void generateOre(World world, Random random, int x, int z) { for(int i = 0; i < 10; i++) { int genX = x + random.nextInt(16); int genY = 1 + random.nextInt(15); int genZ = z + random.nextInt(16); new WorldGenMinable(AluminiumMod.oreAluminium, 0, 20, Blocks.stone).generate(world, random, genX, genY, genZ); } }

}

解説

Block

Item getItemDropped(int meta, Random random, int fortune)

そのブロックを破壊したときにドロップするアイテムを返す。

int quantityDroppedWithBonus(int fortune, Random random)

破壊するのに使用したツールの幸運レベルをもとに、ドロップ数を変更する。

int getExpDrop(IBlockAccess iBlockAccess, int meta, int fortune)

経験値のドロップ量を返す。

GameRegistry

void registerWorldGenerator(IWorldGenerator generator, int modGenerationWeight)

GameRegistryにチャンク生成時にブロックを自然生成させるためのクラスを登録するメソッド。
第二引数は生成の優先度(?)。

IWorldGenerator

チャンク生成時にブロックを自然生成させるためのインターフェース。

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

world.providerはそのワールド(ディメンション)のプロバイダ。
これを使って判定することで鉱石を生成するディメンションを制御できる。

WorldGenMinable

ブロックを生成させるクラスの一つ。
コンストラクタで指定したパラメータをもとにgenerateで鉱脈を生成する。

コンストラクタ(Block block, int meta, int number, Block target)

引数は、生成するブロック、生成するブロックのメタデータ、生成する鉱脈の大きさ、置き換えるブロック。
第二引数はなくてもよい。

boolean generate(World world, Random random, int x, int y, int z)

鉱脈を生成する処理。
戻り値は生成が成功したかどうか。

使用例

オファレン鉱石を追加している部分。


package nahama.ofalenmod;

/*略*/

/**@author Akasata Nahama*/ @Mod(modid = OfalenModCore.MODID, name = OfalenModCore.MODNAME, version = OfalenModCore.VERSION) public class OfalenModCore {

public static final String MODID = "OfalenMod"; public static final String MODNAME = "Ofalen Mod"; public static final String VERSION = "[1.7.10]1.0.0";

/*略*/

/**最初に行われる処理。アイテム・ブロックの追加などを行う*/ @EventHandler public void preInit(FMLPreInitializationEvent event) { /*略*/ //アイテムを設定するメソッドを実行 OfalenModItemCore.registerItem();

//ブロックを設定するメソッドを実行 OfalenModBlockCore.registerBlock(); /*略*/ }

/**2番目に行われる処理。レシピの追加などを行う*/ @EventHandler public void init (FMLInitializationEvent event) { //鉱石を生成させる GameRegistry.registerWorldGenerator(new OfalenOreGenerator(), 1); /*略*/ }

/*略*/

}


package nahama.ofalenmod.core;

/*略*/

public class OfalenModBlockCore { //ブロックの定義 public static Block oreOfalen; /*略*/

/**ブロックを設定する*/ public static void registerBlock () { oreOfalen = new OfalenOre() .setBlockName("oreOfalen") .setBlockTextureName("ofalenmod:ofalen_ore-"); GameRegistry.registerBlock(oreOfalen, ItemOfalenBlock.class, "oreOfalen"); /*略*/ }

}


package nahama.ofalenmod.block;

import java.util.List; import java.util.Random;

import nahama.ofalenmod.OfalenModCore; import nahama.ofalenmod.core.OfalenModConfigCore; import nahama.ofalenmod.core.OfalenModItemCore; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.IIcon; import net.minecraft.util.MathHelper; import net.minecraft.world.IBlockAccess; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly;

public class OfalenOre extends Block {

private IIcon[] iicon = new IIcon[4]; private Random random = new Random();

public OfalenOre() { super(Material.rock); this.setCreativeTab(OfalenModCore.tabOfalen); this.setHardness(5.0F); this.setResistance(7.5F); this.setStepSound(Block.soundTypePiston); this.setLightLevel(0.6F); this.setHarvestLevel("pickaxe", 3); }

/**ドロップアイテムの設定*/ @Override public Item getItemDropped(int meta, Random random, int fortune) { return OfalenModItemCore.fragmentOfalen; }

/**ドロップ数の設定*/ @Override public int quantityDropped(Random random) { return OfalenModConfigCore.amountDrop; }

/**fortuneによるドロップ増加の設定。BlockOre参照*/ @Override public int quantityDroppedWithBonus(int level, Random random) { //幸運のレベルが1以上で、ドロップアイテムがこのブロック自身でない場合 if (level > 0 && Item.getItemFromBlock(this) != this.getItemDropped(0, random, level)) { int i = random.nextInt(level + 2) - 1; if (i < 0) { i = 0; } return this.quantityDropped(random) * (i + 1); } else { return this.quantityDropped(random); } }

/**経験値ドロップの設定*/ @Override public int getExpDrop(IBlockAccess iBlockAccess, int meta, int fortune) { return MathHelper.getRandomIntegerInRange(random, 3, 7); }

/**メタデータ違いのテクスチャを登録する*/ @Override @SideOnly(Side.CLIENT) public void registerBlockIcons(IIconRegister iicon) { for (int i = 0; i < 4; i ++) { this.iicon[i] = iicon.registerIcon(this.getTextureName() + i); } }

/**メタデータにより返すIIconを変える*/ @Override @SideOnly(Side.CLIENT) public IIcon getIcon(int side, int meta) { return iicon[meta & 3]; }

/**メタデータ違いのブロックを登録する*/ @Override @SideOnly(Side.CLIENT) public void getSubBlocks(Item item, CreativeTabs creativeTab, List list) { for (int i = 0; i < 4; i ++) { list.add(new ItemStack(item, 1, i)); } }

/**メタデータによりドロップ品を変える*/ @Override public int damageDropped(int meta) { return meta & 3; }

}


package nahama.ofalenmod.generator;

import java.util.Random;

import nahama.ofalenmod.core.OfalenModBlockCore; import nahama.ofalenmod.core.OfalenModConfigCore; import net.minecraft.init.Blocks; import net.minecraft.world.World; import net.minecraft.world.WorldProviderSurface; import net.minecraft.world.chunk.IChunkProvider; import net.minecraft.world.gen.feature.WorldGenMinable; import cpw.mods.fml.common.IWorldGenerator;

public class OfalenOreGenerator implements IWorldGenerator {

public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) { if (OfalenModConfigCore.enabledGenerator) { if (world.provider instanceof WorldProviderSurface) { generateOreRed(world, random, chunkX << 4, chunkZ << 4); generateOreGreen(world, random, chunkX << 4, chunkZ << 4); generateOreBlue(world, random, chunkX << 4, chunkZ << 4); generateOreBig(world, random, chunkX << 4, chunkZ << 4); } } }

private void generateOreRed(World world, Random random, int x, int z) { for(int i = 0; i < OfalenModConfigCore.probabilityGeneration; i++) { int genX = x + random.nextInt(16); int genY = 1 + random.nextInt(15); int genZ = z + random.nextInt(16); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 0, OfalenModConfigCore.limitGeneration, Blocks.stone).generate(world, random, genX, genY, genZ); } }

private void generateOreGreen(World world, Random random, int x, int z) { for(int i = 0; i < OfalenModConfigCore.probabilityGeneration; i++) { int genX = x + random.nextInt(16); int genY = 1 + random.nextInt(15); int genZ = z + random.nextInt(16); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 1, OfalenModConfigCore.limitGeneration, Blocks.stone).generate(world, random, genX, genY, genZ); } }

private void generateOreBlue(World world, Random random, int x, int z) { for(int i = 0; i < OfalenModConfigCore.probabilityGeneration; i++) { int genX = x + random.nextInt(16); int genY = 1 + random.nextInt(15); int genZ = z + random.nextInt(16); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 2, OfalenModConfigCore.limitGeneration, Blocks.stone).generate(world, random, genX, genY, genZ); } }

private void generateOreBig(World world, Random random, int x, int z) { int i = random.nextInt(10000); if (i < OfalenModConfigCore.probabilityGenerationLode) { int genX = x + random.nextInt(16); int genY = 1 + random.nextInt(15); int genZ = z + random.nextInt(16);

int type = random.nextInt(19)/3; switch (type) { case 0: new WorldGenMinable(OfalenModBlockCore.oreOfalen, 0, 40, Blocks.stone).generate(world, random, genX, genY, genZ); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 1, 40, Blocks.stone).generate(world, random, genX, genY, genZ); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 2, 40, Blocks.stone).generate(world, random, genX, genY, genZ); break;

case 1: new WorldGenMinable(OfalenModBlockCore.oreOfalen, 0, 40, Blocks.stone).generate(world, random, genX, genY, genZ); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 2, 40, Blocks.stone).generate(world, random, genX, genY, genZ); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 1, 40, Blocks.stone).generate(world, random, genX, genY, genZ); break;

case 2: new WorldGenMinable(OfalenModBlockCore.oreOfalen, 1, 40, Blocks.stone).generate(world, random, genX, genY, genZ); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 0, 40, Blocks.stone).generate(world, random, genX, genY, genZ); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 2, 40, Blocks.stone).generate(world, random, genX, genY, genZ); break;

case 3: new WorldGenMinable(OfalenModBlockCore.oreOfalen, 1, 40, Blocks.stone).generate(world, random, genX, genY, genZ); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 2, 40, Blocks.stone).generate(world, random, genX, genY, genZ); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 0, 40, Blocks.stone).generate(world, random, genX, genY, genZ); break;

case 4: new WorldGenMinable(OfalenModBlockCore.oreOfalen, 2, 40, Blocks.stone).generate(world, random, genX, genY, genZ); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 0, 40, Blocks.stone).generate(world, random, genX, genY, genZ); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 1, 40, Blocks.stone).generate(world, random, genX, genY, genZ); break;

case 5: new WorldGenMinable(OfalenModBlockCore.oreOfalen, 2, 40, Blocks.stone).generate(world, random, genX, genY, genZ); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 1, 40, Blocks.stone).generate(world, random, genX, genY, genZ); new WorldGenMinable(OfalenModBlockCore.oreOfalen, 0, 40, Blocks.stone).generate(world, random, genX, genY, genZ); break;

case 6: new WorldGenMinable(OfalenModBlockCore.oreOfalen, 3, 40, Blocks.stone).generate(world, random, genX, genY, genZ); } new WorldGenMinable(OfalenModBlockCore.oreOfalen, 3, 40, Blocks.stone).generate(world, random, genX, genY, genZ); } }

}

過去の質問

  • バニラでWorldGenMinableのインスタンスを生成しているコードはどこにありますか?
    • net.minecraft.world.biome.BiomeDecoratorのコンストラクタにおいて、通常世界の鉱石類のためにWorldGenMinableインスタンスの生成が行われています。

コメントはこちらです。(スパム対策の為コメントは手動承認になっています。未承認のコメントは表示されないので連投はお控え下さい。)

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください