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 [2019/03/18 23:52] – fixed BlockItem arguments draylartutorial:blocks [2019/11/14 22:00] fudge
Line 3: Line 3:
 ==== Introduction ==== ==== Introduction ====
  
-Like itemsnew blocks are added by most mods. You'll need to create a Block, register it and create model for it. To add additional behavior to the block you will need Block class. +To add a block to your modyou will need to register a new instance of the Block class. For more control over your blockyou can create custom block class. We'll also look at adding a block model. 
-  +
 ==== Creating a Block ==== ==== Creating a Block ====
-The first step is to create a Block. This can be done by using the Block.Settings's method //of// which takes in a material. The Block's Block.Settings define many of the Block's characteristics like hardness and blast resistance. This will create a basic block which won't always be enough, but that will be covered further down. 
  
-  public class ExampleMod implements ModInitializer +To start, create an instance of Block in your main mod class. Block's constructor uses the FabricBlockSettings builder to set up basic properties of the block, such as hardness and resistance: 
-  +<code java [enable_line_numbers="true"]> 
-      // an instance of our new block +public class ExampleMod implements ModInitializer 
-      public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.of(Material.STONE)); +
-      [...] +    // an instance of our new block 
-  +    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).build()); 
-  +    [...] 
 +
 +</code> 
 ==== Registering a Block ==== ==== Registering a Block ====
-As blocks exist in vanilla, they are registered with the vanilla registry that can be gotten by using //Registry.register//. The first parameter defines what should be registered, so for blocks its Registry.BLOCK. The second parameter is the registry name of the block in the form of an identifier. The identifier will consist of the id of the mod and the name of the block. Finally, the third parameter takes in the block that you want to register. We will use the initializer's //onInitialize// method to register the block. 
  
-  public class ExampleMod implements ModInitializer +Registering blocks is the same as registering items. Call //Registry.register// and pass in the appropriate arguments.
-  { +
-      // block creation +
-      […] +
-       +
-      @Override +
-      public void onInitialize() +
-      { +
-          Registry.register(Registry.BLOCK, new Identifier("my-mod", "example_block"), EXAMPLE_BLOCK); +
-      } +
-  }+
  
-Your block will currently not be accessible as an item, but it can now be seen ingame by using /setblock ~ ~ ~ modid:name.+<code java [enable_line_numbers="true"]> 
 +public class ExampleMod implements ModInitializer 
 +
 +    // block creation 
 +    […] 
 +     
 +    @Override 
 +    public void onInitialize() 
 +    { 
 +        Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK); 
 +    } 
 +
 +</code> 
 + 
 +Your block will //not// be accessible as an item, but it can be seen in-game by using ''/setblock tutorial:example_block.''
  
 ==== Registering a BlockItem ==== ==== Registering a BlockItem ====
-In most cases you want the block to accessible in a creative tab and as an item. To make the block available as an item, you need to register a corresponding BlockItem in the item registry. The registry name of the item should usually be the same as the registry name of the block. A BlockItem can be created with //new BlockItem(block)//. Like registering the block, we will register the BlockItem in the initializer's //onInitialize// method. 
  
-  public class ExampleMod implements ModInitializer +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. 
-  + 
-      // block creation +<code java [enable_line_numbers="true"]> 
-      […] +public class ExampleMod implements ModInitializer 
-       +
-      @Override +    // block creation 
-      public void onInitialize() +    […] 
-      +     
-          // block registration +    @Override 
-          [...] +    public void onInitialize() 
-           +    
-          Registry.register(Registry.ITEM, new Identifier("my-mod", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings().itemGroup(ItemGroup.MISC))); +        // block registration 
-      +        [...] 
-  }+         
 +        Registry.register(Registry.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings().group(ItemGroup.MISC))); 
 +    
 +} 
 +</code>
  
 ==== Giving your block a model ==== ==== Giving your block a model ====
-As you probably have noticed, the block both in world and in item form is a purple and black checkerboard pattern. This is Minecraft's way to show that the block has no model. Modeling a block can be more advanced than modeling an item. You will need three files: A blockstate file, a block model file and a item model file (if the block has a BlockItem). Textures are also required if you don't use vanilla ones. The files should be located here: 
  
-  Blockstate: src/main/resources/assets/my-mod/blockstates/example_block.json +As you probably have noticed, the block is simply a purple and black checkerboard pattern in-game. This is Minecraft's way of showing you that the block has no model. Modeling a block is a little bit more difficult than modeling an item. You will need three files: A blockstate file, a block model file, and an item model file if the block has a BlockItem. Textures are also required if you don't use vanilla ones. The files should be located here: 
-  Block model: src/main/resources/assets/my-mod/models/block/example_block.json + 
-  Item model: src/main/resources/assets/my-mod/models/item/example_block.json +  Blockstate: src/main/resources/assets/tutorial/blockstates/example_block.json 
-  Block texture: src/main/resources/assets/my-mod/textures/block/example_block.png+  Block Model: src/main/resources/assets/tutorial/models/block/example_block.json 
 +  Item Model: src/main/resources/assets/tutorial/models/item/example_block.json 
 +  Block Texture: src/main/resources/assets/tutorial/textures/block/example_block.png
  
 The blockstate file determines which model that the block should use depending on it's blockstate. As our block has only one state, the file is a simple as this: The blockstate file determines which model that the block should use depending on it's blockstate. As our block has only one state, the file is a simple as this:
  
-  +<code JavaScript src/main/resources/assets/tutorial/blockstates/example_block.json> 
-    "variants":+
-      "": { "model": "my-mod:block/example_block"+  "variants":
-    +    "": { "model": "tutorial:block/example_block"
-   +  
 +} 
 +</code>
  
 The block model file defines the shape and texture of your block. We will use block/cube_all, which will allow us to easily set the same texture on all sides of the block. The block model file defines the shape and texture of your block. We will use block/cube_all, which will allow us to easily set the same texture on all sides of the block.
  
-  +<code JavaScript src/main/resources/assets/tutorial/models/block/example_block.json> 
-    "parent": "block/cube_all", +
-    "textures":+  "parent": "block/cube_all", 
-      "all": "my-mod:block/example_block" +  "textures":
-    }+    "all": "tutorial:block/example_block"
   }   }
 +}
 +</code>
  
-In most cases you want the block to look the same in hand as in the world and we can therefore create a item model file that simply inherits from the block model file.+In most cases you want the block to look the same in hand. To do this, you can make an item file that inherits from the block model file:
  
-  +<code JavaScript src/main/resources/assets/tutorial/models/item/example_block.json> 
-    "parent": "my-mod:block/example_block" +
-  }+  "parent": "tutorial:block/example_block" 
 +} 
 +</code>
  
 Load up Minecraft and your block should finally have a texture! Load up Minecraft and your block should finally have a texture!
 +
 +==== Adding a block loot table ====
 +
 +The block must have a loot table for any items to drop when the block is broken. Assuming you have created an item for your block and registered it using the same name as the block, the following file will produce regular block drops ''src/main/resources/data/wikitut/loot_tables/blocks/example_block.json''.
 +
 +<code JavaScript src/main/resources/data/tutorial/loot_tables/blocks/example_block.json>
 +{
 +  "type": "minecraft:block",
 +  "pools": [
 +    {
 +      "rolls": 1,
 +      "entries": [
 +        {
 +          "type": "minecraft:item",
 +          "name": "tutorial:example_block"
 +        }
 +      ],
 +      "conditions": [
 +        {
 +          "condition": "minecraft:survives_explosion"
 +        }
 +      ]
 +    }
 +  ]
 +}
 +</code>
 +
 +When broken in survival mode, the block will now drop an item.
  
 ==== Creating a Block class ==== ==== Creating a Block class ====
-When creating a simple decorational block the above approch works well, but sometimes you want to create something more advanced and will therefore need custom Block class. We will call it ExampleBlock. The class needs a constructor that takes in a BlockSettings argument.+When creating a simple block the above approach works well, but sometimes you want a //special// block with unique mechanics. We'll create a separate class that extends Block to do this. The class needs a constructor that takes in a BlockSettings argument.
  
-  public class ExampleBlock extends Block +<code java [enable_line_numbers="true"]> 
-  +public class ExampleBlock extends Block 
-      public ExampleBlock(Settings settings) +
-      +    public ExampleBlock(Settings settings) 
-          super(settings); +    
-      +        super(settings); 
-  }+    
 +} 
 +</code>
  
-Here in the your custom block class you can customize your blocks in many ways, for example make you block function like a door or be transparent. That is what we will do now.+Just like we did in the item tutorial, you can override methods in the block class for custom functionality. Say you want your block to be transparent:
  
-   @Environment(EnvType.CLIENT) +<code java> 
-   public BlockRenderLayer getRenderLayer() { +    @Environment(EnvType.CLIENT) 
-       return BlockRenderLayer.TRANSLUCENT; +    public BlockRenderLayer getRenderLayer() { 
-   }+        return BlockRenderLayer.TRANSLUCENT; 
 +    } 
 +</code>
  
-To add this block into the game you have to replace //new Block// with //new ExampleBlock// when you create the block.+To add this block into the gamereplace //new Block// with //new ExampleBlock// when you register it.
  
-  public class ExampleMod implements ModInitializer +<code java [enable_line_numbers="true"]> 
-  +public class ExampleMod implements ModInitializer 
-      // an instance of our new item +
-      public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE)); +    // an instance of our new item 
-      [...] +    public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE)); 
-  +    [...] 
-   +
-Your custom block should now be transparent if you have followed the tutorial correctly and use transparent texture.+</code> 
 + 
 +Your custom block should now be transparent
 +==== Next Steps ==== 
 +[[tutorial:blockstate|Adding simple state to a block, like ints and booleans]].  
 + 
 +[[tutorial:blockentity|Giving blocks block entity so they can have advanced state like inventories and classes]]. Also needed for many things like GUI and custom block rendering.
tutorial/blocks.txt · Last modified: 2024/07/04 16:29 by mineblock11