====== 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. public class CustomCropBlock extends CropBlock { public CustomCropBlock(AbstractBlock.Settings settings) { super(settings); } } 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: public class CustomCropBlock extends CropBlock { private static final VoxelShape[] AGE_TO_SHAPE = new VoxelShape[]{Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 2.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 3.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 4.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 5.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 6.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 7.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 8.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 9.0D, 16.0D) }; public CustomCropBlock(AbstractBlock.Settings settings) { super(settings); } @Override protected ItemConvertible getSeedsItem() { return TutorialItems.CUSTOM_SEEDS; } @Override protected VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { return AGE_TO_SHAPE[getAge(state)]; } } ===== 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. What you need to note is, although seeds are the corresponding item of the crop, its name does not directly use the crop's name, but use a specific name. Therefore for versions before 1.21.2, please use ''AliasedBlockItem'' instead of ''BlockItem'', and in version 1.21.2 and later, please use ''useItemPrefixedTranslationKey()''. // For versions before 1.21.2: public static final CropBlock CUSTOM_CROP = register("custom_crop", new CustomCropBlock(AbstractBlock.Settings.create().nonOpaque().noCollision().ticksRandomly().breakInstantly().sounds(BlockSoundGroup.CROP))); // For version 1.21.2 and later: public static final Block CUSTOM_CROP = register("custom_crop", CustomCropBlock::new, AbstractBlock.Settings.create().nonOpaque().noCollision().ticksRandomly().breakInstantly().sounds(BlockSoundGroup.CROP)); // For version before 1.21.2: public static final Item CUSTOM_SEEDS = register("custom_seeds", new AliasedBlockItem(TutorialBlocks.CUSTOM_CROP, new Item.Settings())); // For version 1.21.2 and later: public static final Item CUSTOM_SEEDS = register("custom_seeds", settings -> new BlockItem(TutorialBlocks.CUSTOM_CROP, settings), new Item.Settings().useItemPrefixedTranslationKey()); > In the [[blocks]] tutorial, we wrote the ''register'' method which creates a corresponding item for each block, therefore here while we create ''custom_seeds'', we will also create an item named ''custom_crop''. If you do not need this item, you can create a method named ''registerBlockOnly'' which does not register the item, or add a parameter for ''register'' method which decides whether the item should be registered. > // ... public static final Block CUSTOM_CROP = registerBlockOnly("custom_crop", CustomCropBlock::new, AbstractBlock.Settings.create().nonOpaque().noCollision().ticksRandomly().breakInstantly().sounds(BlockSoundGroup.CROP)); // ... private static Block registerBlockOnly(String path, Function factory, AbstractBlock.Settings settings) { final Identifier identifier = Identifier.of("tutorial", path); final RegistryKey registryKey = RegistryKey.of(RegistryKeys.BLOCK, identifier); return Blocks.register(registryKey, factory, settings); } You also probably want the ''BlockRenderMapLayer'' to give your crop a transparent cutout (see [[blockappearance]]). Do that in your client initializer: @Environment(EnvType.CLIENT) public class ExampleModClient implements ClientModInitializer { // ... @Override public void onInitializeClient() { // ... BlockRenderLayerMap.INSTANCE.putBlocks(RenderLayer.getCutout(), TutorialBlocks.CUSTOM_CROP); } } ===== 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. { "parent": "minecraft:block/crop", "textures": { "crop": "tutorial:block/custom_crop_stage0" } } Lastly you will want to create a blockstates definition for your crop which dispatches a specific model for each age of your crop: { "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" } } } > The seeds item also need a corresponding item model and item models definition (for version 1.21.4 an later), which will not be explained in length here. ===== 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 [[datagen loot|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!