User Tools

Site Tools


tutorial:crops

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:crops [2024/08/26 13:09] – [Crop Block Finished!] solidblocktutorial:crops [2025/04/01 12:05] (current) – fix typo solidblock
Line 55: Line 55:
 ===== Registering your Crop and Seed Item ===== ===== 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.+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()''.
  
 <code java [enable_line_numbers="true"] TutorialBlocks.java> <code java [enable_line_numbers="true"] TutorialBlocks.java>
 +    // 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)));     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));
 </code> </code>
  
 <code java [enable_line_numbers="true"] TutorialItems.java> <code java [enable_line_numbers="true"] TutorialItems.java>
 +    // For version before 1.21.2:
     public static final Item CUSTOM_SEEDS = register("custom_seeds", new AliasedBlockItem(TutorialBlocks.CUSTOM_CROP, new Item.Settings()));     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());
 +</code>
 +
 +> 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.
 +> <code java TutorialBlocks.java>
 +  // ...
 +  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<AbstractBlock.Settings, Block> factory, AbstractBlock.Settings settings) {
 +    final Identifier identifier = Identifier.of("tutorial", path);
 +    final RegistryKey<Block> registryKey = RegistryKey.of(RegistryKeys.BLOCK, identifier);
 +
 +    return Blocks.register(registryKey, factory, settings);
 +  }
 </code> </code>
  
-You also probably want the ''BlockRenderMapLayer'' to give your crop a transparent cutout. Do that in your client initializer:+You also probably want the ''BlockRenderMapLayer'' to give your crop a transparent cutout (see [[blockappearance]]). Do that in your client initializer:
  
 <code java [enable_line_numbers="true"] TutorialModClient.class> <code java [enable_line_numbers="true"] TutorialModClient.class>
Line 92: Line 115:
 </code> </code>
  
-Lastly you will want to create a blockstate for your crop which registers your model for each age of your crop:+Lastly you will want to create a blockstates definition for your crop which dispatches a specific model for each age of your crop:
  
 <code JavaScript src/main/resources/assets/tutorial/blockstates/custom_crop.json> <code JavaScript src/main/resources/assets/tutorial/blockstates/custom_crop.json>
Line 124: Line 147:
 } }
 </code> </code>
 +
 +> 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 ===== ===== Loot Table =====
tutorial/crops.1724677756.txt.gz · Last modified: 2024/08/26 13:09 by solidblock