5.22日,袁老永远的离开了我们。一时所思万千……
于是想制作一期作物教程,让我们可以在Minecraft中进行作物种植,以寄哀思.
1. 在 items.food 包中新建 ItemHerb 类
在 ItemHerb.java 中编写:
package com.Joy187.newmod.items.food;
import com.Joy187.newmod.Main;
import com.Joy187.newmod.init.ModBlocks;
import com.Joy187.newmod.init.ModItems;
import com.Joy187.newmod.util.IHasModel;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import net.minecraftforge.common.IPlantable;
public class ItemHerb extends ItemFood implements IHasModel, IPlantable{
public ItemHerb(String name, int amount, float saturation, boolean iswolfFood,CreativeTabs tab) {
super( amount, saturation, iswolfFood );
setUnlocalizedName(name);
setRegistryName(name);
setCreativeTab(tab);
ModItems.ITEMS.add(this);
}
@Override
public void registerModels() {
Main.proxy.registerItemRenderer(this, 0, "inventory");
}
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX , float hitY , float hitZ)
{
ItemStack stack = player.getHeldItem(hand);
IBlockState state = worldIn.getBlockState(pos);
if(facing == EnumFacing.UP && player.canPlayerEdit(pos.offset(facing), facing, stack) && state.getBlock( ).canSustainPlant(state, worldIn, pos, EnumFacing.UP, this) && worldIn.isAirBlock(pos.up()))
{
worldIn.setBlockState(pos.up(), ModBlocks.HERB1_BLOCK.getDefaultState());
stack.shrink(1);
return EnumActionResult.SUCCESS;
}
else return EnumActionResult.FAIL;
}
@Override
public EnumPlantType getPlantType(IBlockAccess world, BlockPos pos) {
return EnumPlantType.Crop;
}
@Override
public IBlockState getPlant(IBlockAccess world, BlockPos pos) {
return ModBlocks.HERB1_BLOCK.getDefaultState();
}
}
2.在 ModItems中添加作物的注册信息:
3.在 Blocks 包中新建一个 作物类(作物种下去时相当于在湿润的耕地上放置了作物方块(crop block))
在作物类中编写代码:
package com.Joy187.newmod.blocks;
import com.Joy187.newmod.init.ModBlocks;
import com.Joy187.newmod.init.ModItems;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
public class Herb1Block extends BlockCrops{
//这里的参数对应着作物生长的不同阶段
private static final AxisAlignedBB[] herb1 = new AxisAlignedBB[] {new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.125D,1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.25D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D,1.0D, 0.375D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.5625D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.8125D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D)};
public Herb1Block(String name)
{
setUnlocalizedName(name);
setRegistryName(name);
ModBlocks.BLOCKS.add(this);
ModItems.ITEMS.add(new ItemBlock(this).setRegistryName(this.getRegistryName()));
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
if(!worldIn.isRemote)
{
if(this.isMaxAge(state))
{
worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(),pos.getY(), pos.getZ(), new ItemStack(ModItems.HERB1, 2)));
worldIn.setBlockState(pos, this.withAge(0));
return true;
}
}
return false;
}
@Override
protected Item getSeed()
{
return ModItems.HERB1;
}
@Override
protected Item getCrop()
{
return ModItems.HERB1;
}
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
{
return herb1[((Integer)state.getValue(this.getAgeProperty())).intValue()];
}
}
4.在 ModBlocks.java 中添加作物方块信息:
5.在 resource包中的 en_us.lang 中添加物品名称信息(一个方块,一个作物)
item.herb1.name=Herb1 //作物
tile.herb1_block.name=Herb1 Plant //方块
6 在 blockstates 包中添加作物成长信息:(一共有八个生长阶段)
{
"variants":
{
"age=0":{"model": "joymod:herb1_block0"},
"age=1":{"model": "joymod:herb1_block1"},
"age=2":{"model": "joymod:herb1_block2"},
"age=3":{"model": "joymod:herb1_block3"},
"age=4":{"model": "joymod:herb1_block4"},
"age=5":{"model": "joymod:herb1_block5"},
"age=6":{"model": "joymod:herb1_block6"},
"age=7":{"model": "joymod:herb1_block7"}
}
}
7.在 models.item 中添加方块和作物的信息
作物
"parent": "item/generated",
"textures": {
"layer0": "joymod:items/herb1"
}
}
作物方块(通过种植作物方块来得到作物,所以是其父类)
{
"parent":"joymod:item/herb1"
}
8.将作物皮肤放入 texture.items 中
9.将8张作物生长形态的贴图放入 textures.blocks 中:
10.保存操作 -> 进入游戏
用骨粉催熟我们的作物,同时观察作物不同阶段的贴图变化。