User Tools

Site Tools


tutorial:blocks

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
Next revisionBoth sides next revision
tutorial:blocks [2020/06/14 00:58] – copy comments to bottom code block draylartutorial:blocks [2020/06/14 08:31] – dryfix fudge
Line 11: Line 11:
  
     /* Declare and initialize our custom block instance.     /* Declare and initialize our custom block instance.
-       We set out block material to METAL, which requires a pickaxe to efficiently break.+       We set our block material to METAL, which requires a pickaxe to efficiently break.
        Hardness represents how long the break takes to break. Stone has a hardness of 1.5f, while Obsidian has a hardness of 50.0f.        Hardness represents how long the break takes to break. Stone has a hardness of 1.5f, while Obsidian has a hardness of 50.0f.
     */     */
Line 27: Line 27:
 Blocks should be registered under the ''Block.REGISTRY'' registry. Call //Registry.register// and pass in the appropriate arguments. Blocks should be registered under the ''Block.REGISTRY'' registry. Call //Registry.register// and pass in the appropriate arguments.
  
-<code java [enable_line_numbers="true",highlight_lines_extra="8"]>+<code java [enable_line_numbers="true",highlight_lines_extra="11"]>
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
  
-    /* Declare and initialize our custom block instance. 
-       We set out block material to METAL, which requires a pickaxe to efficiently break. 
-       Hardness represents how long the break takes to break. Stone has a hardness of 1.5f, while Obsidian has a hardness of 50.0f. 
-    */ 
     public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).hardness(4.0f));     public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).hardness(4.0f));
          
Line 49: Line 45:
 In most cases, you want to be able to place your block using an item. To do this, you need to register a corresponding BlockItem in the item registry. You can do this by registering an instance of BlockItem under Registry.ITEM. The registry name of the item should usually be the same as the registry name of the block. In most cases, you want to be able to place your block using an item. To do this, you need to register a corresponding BlockItem in the item registry. You can do this by registering an instance of BlockItem under Registry.ITEM. The registry name of the item should usually be the same as the registry name of the block.
  
-<code java [enable_line_numbers="true",highlight_lines_extra="9"]>+<code java [enable_line_numbers="true",highlight_lines_extra="12"]>
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
  
-    // an instance of our new block+    /* Declare and initialize our custom block instance. 
 +       We set out block material to METAL, which requires a pickaxe to efficiently break. 
 +       Hardness represents how long the break takes to break. Stone has a hardness of 1.5f, while Obsidian has a hardness of 50.0f. 
 +    */
     public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL));     public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL));
          
Line 88: Line 87:
 </code> </code>
  
-The block model file defines the shape and texture of your block. Our model will parent ''block/cube_all'', which applies the texture set as ''all'' to //all// sides of the block.+The block model file defines the shape and texture of your block. Our model will have ''block/cube_all''as a parent, which applies the texture ''all'' to //all// sides of the block.
  
 <code JavaScript src/main/resources/assets/tutorial/models/block/example_block.json> <code JavaScript src/main/resources/assets/tutorial/models/block/example_block.json>
Line 99: Line 98:
 </code> </code>
  
-In most cases, you will want the block to look the same in item form. You can make an item model that parents the block model file, which makes it appear exactly like the block:+In most cases, you will want the block to look the same in item form. You can make an item model that has the block model file as a parent, which makes it appear exactly like the block:
  
 <code JavaScript src/main/resources/assets/tutorial/models/item/example_block.json> <code JavaScript src/main/resources/assets/tutorial/models/item/example_block.json>
Line 150: Line 149:
 You can override methods in the block class for custom functionality. Here's an implementation of the ''onUse'' method, which is called when you right-click the block. We check if the interaction is occurring on the server, and then send the player a message saying, //"Hello, world!"// You can override methods in the block class for custom functionality. Here's an implementation of the ''onUse'' method, which is called when you right-click the block. We check if the interaction is occurring on the server, and then send the player a message saying, //"Hello, world!"//
  
-<code java [enable_line_numbers="true"]>+<code java [enable_line_numbers="true",highlight_lines_extra="8,9,10,11,12,13,14,15"]>
 @Override @Override
 public class ExampleBlock extends Block { public class ExampleBlock extends Block {
Line 160: Line 159:
     @Override     @Override
     public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {     public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
-        if(!world.isClient) {+        if (!world.isClient) {
             player.sendMessage(new LiteralText("Hello, world!"), false);             player.sendMessage(new LiteralText("Hello, world!"), false);
         }         }
Line 171: Line 170:
 To use your custom block class, replace //new Block// with //new ExampleBlock//: To use your custom block class, replace //new Block// with //new ExampleBlock//:
  
-<code java [enable_line_numbers="true"]>+<code java [enable_line_numbers="true",highlight_lines_extra="7"]>
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
  
-    /* Declare and initialize our custom block instance using our new ExampleBlock class. 
-       We set out block material to METAL, which requires a pickaxe to efficiently break. 
-       Hardness represents how long the break takes to break. Stone has a hardness of 1.5f, while Obsidian has a hardness of 50.0f. 
-    */ 
     public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE).hardness(4.0f));     public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE).hardness(4.0f));
          
Line 194: Line 189:
 {{:tutorial:voxelshape_wrong.png?200|}} {{:tutorial:voxelshape_wrong.png?200|}}
  
-To fix this, we have to define the //VoxelShape// of the new block:+To fix this, we have to define the ''VoxelShape'' of the new block:
  
 <code> <code>
tutorial/blocks.txt · Last modified: 2024/04/15 01:52 by solidblock