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.

  1. public class CustomCropBlock extends CropBlock {
  2. public CustomCropBlock(AbstractBlock.Settings settings) {
  3. super(settings);
  4. }
  5.  
  6. }

Once you've configured that, you can define your seed item and add an outline shape. Each Block.createCubiodShape defines the hitbox size for each growth stage of the crop. You can configure the values to your liking, or do nothing while directly using the vanilla one inherited from CropBlock, which will be identical to the shape for wheat.

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. @Override
  17. protected ItemConvertible getSeedsItem() {
  18. return TutorialItems.CUSTOM_SEEDS;
  19. }
  20.  
  21. @Override
  22. protected VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
  23. return AGE_TO_SHAPE[getAge(state)];
  24. }
  25. }

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 (including the static register method we use for convenience) will not be covered in this tutorial but you can refer to the items and blocks page. It is important to use AliasedBlockItem to make sure your seed item is bound to your crop block.

TutorialBlocks.java
  1. public static final CropBlock CUSTOM_CROP = register("custom_crop", new CustomCropBlock(AbstractBlock.Settings.create().nonOpaque().noCollision().ticksRandomly().breakInstantly().sounds(BlockSoundGroup.CROP)));
TutorialItems.java
  1. public static final Item CUSTOM_SEEDS = register("custom_seeds", new AliasedBlockItem(TutorialBlocks.CUSTOM_CROP, new Item.Settings()));

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

TutorialModClient.class
  1. @Environment(EnvType.CLIENT)
  2. public class ExampleModClient implements ClientModInitializer {
  3. // ...
  4.  
  5. @Override
  6. public void onInitializeClient() {
  7. // ...
  8. BlockRenderLayerMap.INSTANCE.putBlocks(RenderLayer.getCutout(), TutorialBlocks.CUSTOM_CROP);
  9. }
  10. }

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": "tutorial:block/custom_crop_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.json
{
  "variants": {
    "age=0": {
      "model": "tutorial:block/custom_crop_stage0"
    },
    "age=1": {
      "model": "tutorial:block/custom_crop_stage0"
    },
    "age=2": {
      "model": "tutorial:block/custom_crop_stage1"
    },
    "age=3": {
      "model": "tutorial:block/custom_crop_stage1"
    },
    "age=4": {
      "model": "tutorial:block/custom_crop_stage2"
    },
    "age=5": {
      "model": "tutorial:block/custom_crop_stage2"
    },
    "age=6": {
      "model": "tutorial:block/custom_crop_stage3"
    },
    "age=7": {
      "model": "tutorial:block/custom_crop_stage3"
    }
  }
}

Loot Table

The block also needs a loot table, or it will drop nothing when mined. However, you can directly imitate vanillas. For example, copy vanilla's data/minecraft/loot_table/blocks/wheat.json into your mod's src/main/resources/data/tutorial/loot_table/blocks/custom_crop.json, and replace related IDs. You can also use data generators to generate loot tables.

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. You can also add other interesting funcionalities for the crops!

tutorial/crops.txt · Last modified: 2024/08/27 04:56 by solidblock