tutorial:blocks
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:blocks [2020/06/14 00:58] – copy comments to bottom code block draylar | tutorial:blocks [2021/05/24 18:52] – [Creating a Custom Block Class] Replace Block.Settings by FabricBlockSettings technici4n | ||
---|---|---|---|
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. |
- | | + | |
+ | | ||
+ | | ||
+ | Stone has a hardness of 1.5f and a resistance of 6.0f, while Obsidian has a hardness of 50.0f and a resistance of 1200.0f. | ||
+ | |||
+ | You can find the stats of all vanilla blocks in the class `Blocks`, where you can also reference other blocks. | ||
*/ | */ | ||
- | 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).strength(4.0f)); |
| | ||
@Override | @Override | ||
Line 25: | Line 30: | ||
==== Registering your Block ==== | ==== Registering your Block ==== | ||
- | Blocks should be registered under the '' | + | Blocks should be registered under the '' |
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
- | | + | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); |
- | We set out block material to METAL, which requires a pickaxe to efficiently break. | + | |
- | | + | |
- | */ | + | |
- | | + | |
| | ||
@Override | @Override | ||
Line 49: | Line 50: | ||
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=" | + | <code java [enable_line_numbers=" |
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
- | | + | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); |
- | | + | |
| | ||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
Registry.register(Registry.BLOCK, | Registry.register(Registry.BLOCK, | ||
- | Registry.register(Registry.ITEM, | + | Registry.register(Registry.ITEM, |
} | } | ||
} | } | ||
Line 88: | Line 88: | ||
</ | </ | ||
- | The block model file defines the shape and texture of your block. Our model will parent | + | The block model file defines the shape and texture of your block. Our model will have '' |
<code JavaScript src/ | <code JavaScript src/ | ||
Line 99: | Line 99: | ||
</ | </ | ||
- | In most cases, you will want the block to look the same in item form. You can make an item model that parents | + | 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/ | <code JavaScript src/ | ||
Line 150: | Line 150: | ||
You can override methods in the block class for custom functionality. Here's an implementation of the '' | You can override methods in the block class for custom functionality. Here's an implementation of the '' | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
- | @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(" | player.sendMessage(new LiteralText(" | ||
} | } | ||
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=" | + | <code java [enable_line_numbers=" |
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
- | | + | public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(FabricBlockSettings.of(Material.STONE).hardness(4.0f)); |
- | We set out block material to METAL, which requires a pickaxe to efficiently break. | + | |
- | | + | |
- | */ | + | |
- | | + | |
| | ||
@Override | @Override | ||
Line 194: | Line 189: | ||
{{: | {{: | ||
- | To fix this, we have to define the //VoxelShape// of the new block: | + | To fix this, we have to define the '' |
< | < | ||
- | @Override | + | @Override |
- | | + | |
| | ||
} | } |
tutorial/blocks.txt · Last modified: 2024/07/04 16:29 by mineblock11