User Tools

Site Tools


zh_cn:tutorial:crops

This is an old revision of the document!


添加你的自定义作物

这篇教程会教会你如何添加一个像是小麦和胡萝卜那样的作物。如果你想创建属于你的作物的话,你需要以下物品:

  • 自定义的种子
  • 注册你的种子和作物方块
  • 作物方块的类
  • 为你的作物设计的方块状态和模型

创建作物类

为了创建自定义作物我们需要先创建一个方块类。 你需要用你的作物名命名你的类并且让他继承 CropBlock。 你需要添加 AbstractBlock.Settings 和创建你的作物的形状。 Each Block.createCubiodShape defines the hitbox size for each growth stage of the crop. You can configure the values to your liking.

  1. public class CustomCropBlock extends CropBlock {
  2. private static final VoxelShape[] AGE_TO_SHAPE = new VoxelShape[]{Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 2.0D, 16.0D),
  3. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 3.0D, 16.0D),
  4. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 4.0D, 16.0D),
  5. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 5.0D, 16.0D),
  6. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 6.0D, 16.0D),
  7. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 7.0D, 16.0D),
  8. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 8.0D, 16.0D),
  9. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 9.0D, 16.0D)
  10. };
  11.  
  12. public CustomCropBlock(AbstractBlock.Settings settings) {
  13. super(settings);
  14. }
  15.  
  16. }

Once you've configured that, you need to define your seed item and add an outline shape. We haven't added our seed item yet so you can use something else temporarily. Here is what the code should look like with your cuboid shape, seed item, and outline shape:

  1. public class CustomCropBlock extends CropBlock {
  2. private static final VoxelShape[] AGE_TO_SHAPE = new VoxelShape[]{Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 2.0D, 16.0D),
  3. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 3.0D, 16.0D),
  4. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 4.0D, 16.0D),
  5. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 5.0D, 16.0D),
  6. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 6.0D, 16.0D),
  7. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 7.0D, 16.0D),
  8. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 8.0D, 16.0D),
  9. Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 9.0D, 16.0D)
  10. };
  11.  
  12. public CustomCropBlock(AbstractBlock.Settings settings) {
  13. super(settings);
  14. }
  15.  
  16. public ItemConvertible getSeedsItem() {
  17. return TutorialMod.CUSTOM_SEEDS;
  18. }
  19.  
  20. public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
  21. return AGE_TO_SHAPE[(Integer)state.get(this.getAgeProperty())];
  22. }
  23. }

Registering your Crop and Seed Item

Now we need to register our crop and the item to use for our seed. The seed model and class will not be covered in this tutorial but you can refer to the Item page. It is important you add AlisasedBockItem to make sure your seed item is bound to your crop block.

  1. public class TutorialMod implements ModInitializer {
  2.  
  3. public static final CropBlock CUSTOM_CROP_BLOCK = new CustomCropBlock(AbstractBlock.Settings.of(Material.PLANT).nonOpaque().noCollision().ticksRandomly().breakInstantly().sounds(BlockSoundGroup.CROP));
  4.  
  5. public static final Item CUSTOM_SEEDS = new AliasedBlockItem(TutorialMod.CUSTOM_CROP_BLOCK, new Item.Settings().group(ItemGroup.MISC));
  6.  
  7. @Override
  8. public void onInitialize() {
  9. Registry.register(Registry.BLOCK, new Identifier("tutorial","custom_crop_block"), CUSTOM_CROP_BLOCK);
  10. Registry.register(Registry.ITEM, new Identifier("tutorial","custom_seeds"), CUSTOM_SEEDS);
  11.  
  12. }
  13. }

You also probably want the BlockRenderMapLayer to give your crop a transparent cutout. Do that in your client initializer:

  1. public class TutorialModClient implements ClientModInitializer {
  2. @Override
  3. public void onInitializeClient() {
  4. BlockRenderLayerMap.INSTANCE.putBlocks(RenderLayer.getCutout(), CUSTOM_CROP_BLOCK);
  5. }
  6. }

Creating our Blockstate and Models

Now that we have finished the registry and code, we can add our models. The example below shows a simple growth stage model that uses the minecraft:block/crop format. You may also use minecraft:block/cross for a cross model shape. You must have a separate model for each growth stage you will have. This example shows a singular stage but you can copy it and replace the “0” with your growth stage number.

src/main/resources/assets/tutorial/models/block/custom_crop_stage0.json
{
  "parent": "minecraft:block/crop",
  "textures": {
    "crop": "minecraft:block/custom_crop_block_stage0"
  }
}

Lastly you will want to create a blockstate for your crop which registers your model for each age of your crop:

src/main/resources/assets/tutorial/blockstates/custom_crop_block.json
{
  "variants": {
    "age=0": {
      "model": "tutorial:block/custom_crop_block_stage0"
    },
    "age=1": {
      "model": "tutorial:block/custom_crop_block_stage0"
    },
    "age=2": {
      "model": "tutorial:block/custom_crop_block_stage1"
    },
    "age=3": {
      "model": "tutorial:block/custom_crop_block_stage1"
    },
    "age=4": {
      "model": "tutorial:block/custom_crop_block_stage2"
    },
    "age=5": {
      "model": "tutorial:block/custom_crop_block_stage2"
    },
    "age=6": {
      "model": "tutorial:block/custom_crop_block_stage3"
    },
    "age=7": {
      "model": "tutorial:block/custom_crop_block_stage3"
    }
  }
}

Crop Block Finished!

If you completed all parts of this tutorial correctly, you should now have a working crop! Your crop will be usable with bone meal and can only be placed on farmland with your seed item.

zh_cn/tutorial/crops.1636456591.txt.gz · Last modified: 2021/11/09 11:16 by breakice