Hello everyone! In this part I will show you how to create a custom slab!
I’ll create 1 slab, it will be made with the purple tutorial blocks from the other tutorials. A slab in minecraft consists of 2 blocks, a half block and a double block. The half block can be either on the top or the bottom of a block, and if you place two of them on eachother they become the double block.
To start we need to create a new class in the blocks package. This class should extend BlockSlab and should be abstract, after doing this eclipse will complain because we need to add a constructor. The constructor takes a String for the name and a Material and calls the super constructor with the Material. We also need to add several methods:
- getUnlocalizedName, which returns a String and takes an int for the meta value.
- This method is used to convert the different meta values a slab can have to the correct UnlocalizedName, we will not use the meta value and just return super.getUnlocalizedName(). Because we only have 1 variant.
- getVariantProperty, which returns IProperty<?>.
- This method returns the property that is used to determine the value, this slab will only have one variant but we need to return a variant property or minecraft will crash.
- getTypeForItem, which returns Comparable<?> and takes an ItemStack.
- This method returns the variant that the ItemStack is, again we can just return a default variant here.
- getItemDropped, which returns Item and takes an IBlockState, Random and an int for the fortune level.
- Will return the item this slab drops, we need this to make sure it will never drop a double slab.
- getItem, which returns ItemStack and takes a World, a BlockPos and an IBlockState.
- This returns an ItemStack with the correct meta value.
- getMetaFromState, which returns an int and takes an IBlockState.
- This converts the state of the block in a 4 bit value, this is important because this is the value minecraft will save with your block.
- getStateFromMeta, which returns IBlockState and takes an int for the meta value.
- This does the exact opposite of getMetaFromState, this converts the 4 bit meta value to a blockstate.
- createBlockState, which returns BlockStateContainer.
- This tells minecraft which properties we will use in our blockstate, for a slab we need a variant property and a half property (to tell which half we are on).
I know, a lot of methods, but we are not done yet! We also need to add 2 classes, I’ll put them in the BlockTutorialSlab class because they will be really simple. We need:
- Double, this will be for when the slab is doule, and
- Half, which (surprise!) will be for when the slab is half.
The last thing before we can start implementing the “logic” is an enum for our variant. This enum also implements IStringSerializable. I’ll call it Variant, with this enum we create a variable of type PropertyEnum<Variant> I’ll call it VARIANT. With this line you can create the property:
public static final PropertyEnum&amp;amp;amp;lt;Variant&amp;amp;amp;gt; VARIANT = PropertyEnum.&amp;amp;amp;lt;Variant&amp;amp;amp;gt;create("variant", Variant.class);
Now eclipse will give a lot of errors because we don’t return anything and our classes and enum miss some methods. But this is the base template we have now:
First we’ll finish the Variant enum because we’ll need it correct so eclipse will stop screaming at us.
We need to override getName in the enum, this is a method that will return a String. Because we will only have 1 variant we can just return default.
We also need to add an enum type called DEFAULT.
Now we’ll fill in the constructor, just like basic blocks we need to set the registry names and the unlocalized names.
Now we need to set the default blockstate, this depends on if the slab is double or half, if we are not a double slab we need the HALF property to be set to a default (I’ll set it to the bottom). For both the double and the half version we need to set the VARIANT property to default. We also need to set useNeighborBrightness to make sure the lightning looks “good” when the half slab is placed. This will be true when it is not double.
Now that we created the Variant property, change the getVariantProperty method to return the VARIANT property.
The getTypeForItem is also really simple, this method just returns the DEFAULT variant.
The getItemDropped is a little bit more difficult but still quite simple, we need to return the result of the getItemFromBlock method of the Item class when we pass the (not yet created) half slab block from the ModBlocks class. If you follow this tutorial exactly that will be Modblocks.tutorialSlabHalf.
In this method we return a new ItemStack of the same ModBlocks.tutorialSlabHalf.
In the getStateFromMeta we create an IBlockState variable with a VARIANT property set to DEFAULT.
If this is the half version of the slab (So not double) we need to determine if this is a top or bottom half slab. We do this by checking if the 4th bit is set to 1 or 0.
In the getMetaFromState we do the exact opposite, we create an integer and set the 4th bit to 1 if the slab is on the top side of a block.
In the createBlockState method we need to tell minecraft which properties our block has. This is depends on whether the block is double or not.
If this is a double slab we only need the VARIANT property, if it isn’t a double slab we need the VARIANT and HALF property.
Now we just need to finish the Double and Half classes and this file is done, they are almost the same except for one boolean.
We need to add a constructor, this constructor takes a String for the name and a Material and will just call the super constructor.
We also need to override the isDouble method, in the Double class this returns true, and in the Half class this returns false.
Now in the ModBlocks class we need to add 2 public static variables of type , tutorialSlabHalf and tutorialSlabDouble.
In the init method, instead of initializing BlockTutorialSlab we initialize BlockTutorialSlab.Double for the double slab and BlockTutorialSlab.Half for the half slab.
The last difference from a normal block is that we need to create an ItemSlab instead of a normal Item. The ItemSlab takes the slabblock, the half slabblock and the double slabblock.
event.getRegistry().register(new ItemSlab(tutorialSlabHalf, tutorialSlabHalf, tutorialSlabDouble).setRegistryName(tutorialSlabHalf.getRegistryName()));
In the registerRenders method we only need to register the half version because there won’t be an Item for the double block.
Now we just need to add the model files and texture files. The model files and textures can be found here.
If everything is good it will look like this:
The full sourcecode will be available on github.
I know this was a lot, and this is probably not the best way to explain it, so any tips would help! But I hope it helped and you can have fun with your new slab! Till next time!