Tutorial 4: Basic Blocks

In this tutorial we will be creating a basic block for minecraft!  Let's begin!

Right-click on com.yourname.yourmodname and go to New > Package.  Name the package core.init and right-click on it, go to New > Class and call the class BlockInit.  Open the class by double-clicking it and you are ready to code an item! 
We need to write some code.  Let's start off by creating a Deferred Registry were we can store all of our blocks.  We can do this by writing: 
public class BlockInit {

  public static final DeferredRegister BLOCKS = new DeferredRegister<>(ForgeRegistries.BLOCKS, HumlandsMod.MOD_ID);
}
This code will create a DeferredRegister of with the default forge BLOCKS DeferredRegistry and your Mod ID.  Remember to press Cntl + Shift + O to import everything!  In order to add any basic block, we can write a single line of code!  After the DeferredRegister, add this code: 
public static final RegistryObject EXAMPLE_BLOCK = BLOCKS.register("example_block",
      () -> new Block(Block.Properties.create(Material.IRON).hardnessAndResistance(3.0F, 3.0F)
          .sound(SoundType.METAL)));
This will define our block in the game.  The part in all capitals will be accessible from the code, where as the part in lower case will be accessible from the resources.  The .hardnessAndResistance(3.0F, 3.0F) is how long it takes for the block to be broken (See hardness and resistance values).  The .sound(SoundType.METAL) is what sound the block has (See sound types).  You can change these values if you wish too!  That's it for the block! 
Now, we need to register the class we main in our main class.  Open the main class, and, in the constructor, after this line: modEventBus.addListener(this::doClientStuff); , add the line: BlockInit.BLOCKS.register(bus); .  This will register all the blocks in your DeferredRegistry into the game!  Remember to press Cntl + Shift + O to import everything!. 
Each block in the game can't be used unless it is also an item.  Instead of making all of these manually, we can write some code in our main class.  In your main class, add the following class: 
@Mod.EventBusSubscriber(modid = MCMCMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
  public static class RegistryEvents {

    @SubscribeEvent
    public static void onRegisterItems(final RegistryEvent.Register <Item  > event) {
      
    }

    @Nonnull
    private static  <T extends IForgeRegistryEntry <T >  > T setup(@Nonnull final T entry, @Nonnull final String name) {
      Preconditions.checkNotNull(name, "Name to assign to entry cannot be null!");
      return setup(entry, new ResourceLocation(MCMCMod.MOD_ID, name));
    }

    @Nonnull
    private static  <T extends IForgeRegistryEntry <T >> T setup(@Nonnull final T entry,
        @Nonnull final ResourceLocation registryName) {
      Preconditions.checkNotNull(entry, "Entry cannot be null!");
      Preconditions.checkNotNull(registryName, "Registry name to assign to entry cannot be null!");
      entry.setRegistryName(registryName);
      return entry;
    }
  }
This will be called automatically when the game loads.  Now, we need to write some code in the onRegisterItems method: 
@SubscribeEvent
    public static void onRegisterItems(final RegistryEvent.Register<Item> event) {
      final IForgeRegistry<Item> registry = event.getRegistry();
      BlockInit.BLOCKS.getEntries().stream().map(RegistryObject::get)
          .forEach(block -> {
            // Make the properties, and make it so that the item will be on our ItemGroup
            // (CreativeTab)
            final Item.Properties properties = new Item.Properties().group(ItemGroup.MISC);
            // Create the new BlockItem with the block and it's properties
            final BlockItem blockItem = new BlockItem(block, properties);
            // Set the new BlockItem's registry name to the block's registry name
            blockItem.setRegistryName(block.getRegistryName());
            // Register the BlockItem
            registry.register(blockItem);
          });
    }
This will create a block item for each block when the game loads.  Great!  Now we are done with the code, we can do the resources! 
In your lang file, you will need to add the following: 
"block.yourmodid.example_block": "Example Block"
This will make the item show up in game as Example Block and not block.yourmodid.example_block.  Remember to have a comma at the end of each line (except for the first and last two lines).
 
Next, we need to create the model for our block.  In the models > block folder, create a new Untitled Text File by right clicking on the package and going to New > Untitled Text File.  In there, type: 
{
    "parent": "block/cube_all",
    "textures": 
    {
        "all": "yourmodid:blocks/example_block"
    }
}
That will allow for you item to have a basic model inside the game.  Now we need to save this file.  Press Cntrl + S and then navigate to src > main > resources > assets > yourmodid > models > block.  Name the file the name of your block followed by .json, for example example_block.json, and click OK.  You will also need an item model for the block.  In the models > item folder, create a new Untitled Text File by right clicking on the package and going to New > Untitled Text File.  In there, type: 
{
    "parent": "yourmodid:block/example_block"
}
Now we need to save this file.  Press Cntrl + S and then navigate to src > main > resources > assets > yourmodid > models > item.  Name the file the name of your block followed by .json, for example example_block.json, and click OK. 
We also need a blockstate which loads our model.  In the blockstates folder, create a new Untitled Text File by right clicking on the package and going to New > Untitled Text File.  In there, type: 
{
    "variants": {
        "": { "model": "yourmodid:block/example_block" }
    }
}
Now we need to save this file.  Press Cntrl + S and then navigate to src > main > resources > assets > yourmodid > blockstates.  Name the file the name of your block followed by .json, for example example_block.json, and click OK. 
Finally, add the texure. The texture must be a .png file, which is 16x16 pixels in size. Name this file the name of your block followed by .png, for example example_block.png, and place it in src > main > resources > assets > yourmodid > textures > blocks.  And that is it! You can load up the game and your new block is fully functional! 

That's blocks done...  Mappings in the next tutorial!