User Tools

Site Tools


tutorial:crops

Adding a Custom Crop

This tutorial will cover adding a simple crop similar to carrots or wheat. When creating this custom crop you will need the following items:

  • Custom Seed item
  • Registry for the crop block and seed item
  • A class for your crop block
  • Models and a blockstate for your crop block

Creating the Crop Block Class

In order to create a crop we need a class for the block. You will need to make a class with your crop name and make it extend CropBlock. You'll want to add your AbstractBlock.Settings and create a shape for the crop. 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 AliasedBlockItem 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.create().nonOpaque().noCollision().ticksRandomly().breakInstantly().sounds(BlockSoundGroup.CROP));
  4.  
  5. public static final Item CUSTOM_SEEDS = new AliasedBlockItem(TutorialMod.CUSTOM_CROP_BLOCK, new Item.Settings());
  6.  
  7. @Override
  8. public void onInitialize() {
  9. Registry.register(Registries.BLOCK, new Identifier("tutorial","custom_crop_block"), CUSTOM_CROP_BLOCK);
  10. Registry.register(Registries.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. @Environment(EnvType.CLIENT)
  2. public class TutorialModClient implements ClientModInitializer {
  3. @Override
  4. public void onInitializeClient() {
  5. BlockRenderLayerMap.INSTANCE.putBlocks(RenderLayer.getCutout(), CUSTOM_CROP_BLOCK);
  6. }
  7. }

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.

tutorial/crops.txt · Last modified: 2023/11/18 08:44 by solidblock