我们本次针对矿物生成进行设计
1.在 blocks 包下新建 ZSOre 类:
在ZSOre.java 中 编写代码:
package com.Joy187.newmod.blocks;
import java.util.Random;
import com.Joy187.newmod.init.ModItems;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
public class ZSOre extends BlockBase{
//矿物名称 矿物材质 放在创造栏的位置
public ZSOre(String name, Material material,CreativeTabs tab) {
super(name, material, tab);
setSoundType(SoundType.STONE); //方块音效
setHardness(4.0f); //方块坚硬程度
setResistance(20.0f); //方块挖掘抗性
setHarvestLevel("pickaxe",2); //方块挖掘等级(铁镐为2级)
setLightLevel(12.0f); //方块发光度
}
//方块挖掘后掉落物品
@Override
public Item getItemDropped(IBlockState state, Random rand, int fortune){
return ModItems.ZS_INGOT;
}
//物品掉落个数
@Override
public int quantityDropped(Random rand){
int min = 1;
int max = 1;
return rand.nextInt(max) + min;
}
}
2.ModBlocks.java 中添加矿物信息:
public static final Block ZSORE_BLOCK = new ZSOre("zsore_block", Material.ROCK, Main.BLOCK_TAB);
3.en_us.lang中添加物品信息,blockstate等文件中添加物品信息(和第四章步骤相同):
4.创建 world 包
word 包中新建 ModWorldGen 类:
在 ModWorldGen.java 中编写矿石生成信息:
package com.Joy187.newmod.world;
import java.util.Random;
import com.Joy187.newmod.init.ModBlocks;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.IChunkGenerator;
import net.minecraft.world.gen.feature.WorldGenMinable;
import net.minecraftforge.fml.common.IWorldGenerator;
public class ModWorldGen implements IWorldGenerator{
@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator,IChunkProvider chunkProvider) {
//如果在0号世界(主世界),就生成矿物
if (world.provider.getDimension() == 0){
generateOverworld(random, chunkX, chunkZ, world, chunkGenerator, chunkProvider);
}
}
//在世界生成矿物的信息
private void generateOverworld(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator,IChunkProvider chunkProvider) {
generateOre(ModBlocks.ZSORE_BLOCK.getDefaultState(), world, random, chunkX*16, chunkZ*16, 5, 32, random.nextInt(2)+3, 10);
}
//生成的矿物 生成矿物的世界 生成数(随机的) 生成的x ,z坐标 Y最小,Y最大坐标在(minY,maxY)高度区间中生成矿石 矿脉大小 生成概率
private void generateOre(IBlockState ore, World world, Random random, int x, int z, int minY, int maxY, int size, int chances) {
int deltaY = maxY - minY;
for (int i = 0; i < chances; i++)
{
BlockPos pos = new BlockPos(x+random.nextInt(16), minY+random.nextInt(deltaY), z+random.nextInt(16));
WorldGenMinable generator = new WorldGenMinable(ore, size);
generator.generate (world, random, pos);
}
}
}
Note: Minecraft世界维度: 0:主世界 1:地狱 2:末地
Minecraft 矿石生成规则
5.在Main.java 中的PreInit()中添加矿物生成注册语句:
@EventHandler
public static void PreInit(FMLPreInitializationEvent event)
{
RegistryHandler.preInitRegistries( );
GameRegistry.registerWorldGenerator(new ModWorldGen(), 3);
}