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 [2022/04/29 09:35] – [Next Steps] solidblock | tutorial:blocks [2023/06/28 10:53] – [Registering an Item for your Block] baka4n | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Adding a Block ====== | ====== Adding a Block ====== | ||
- | Adding blocks to your mod follows a similar process to [[tutorial: | + | Adding blocks to your mod follows a similar process to [[tutorial: |
===== Creating a Block ===== | ===== Creating a Block ===== | ||
Line 11: | Line 11: | ||
/* Declare and initialize our custom block instance. | /* Declare and initialize our custom block instance. | ||
- | We set our block material to `METAL`, which requires a pickaxe to efficiently break. | + | We set our block material to `METAL`. |
| | ||
Line 19: | Line 19: | ||
You can find the stats of all vanilla blocks in the class `Blocks`, where you can also reference other blocks. | 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).strength(4.0f)); | + | |
- | | + | |
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
Line 30: | Line 30: | ||
==== Registering your Block ==== | ==== Registering your Block ==== | ||
- | Blocks should be registered under the '' | + | Blocks should be registered under the '' |
+ | |||
+ | If you're using version 1.19.2 or below, please replace '' | ||
<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)); | + | |
+ | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create()).strength(4.0f); | ||
| | ||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
- | Registry.register(Registry.BLOCK, new Identifier(" | + | Registry.register(Registries.BLOCK, new Identifier(" |
} | } | ||
} | } | ||
Line 48: | Line 51: | ||
==== Registering an Item for your Block ==== | ==== Registering an Item for your 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. | + | 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 '' |
<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)); | + | |
+ | public static final Block EXAMPLE_BLOCK | ||
| | ||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
- | Registry.register(Registry.BLOCK, new Identifier(" | + | Registry.register(Registries.BLOCK, new Identifier(" |
- | Registry.register(Registry.ITEM, new Identifier(" | + | Registry.register(Registries.ITEM, new Identifier(" |
} | } | ||
} | } | ||
Line 137: | Line 141: | ||
In minecraft 1.17, there has been a change for breaking blocks. Now, to define harvest tools and harvest levels, we need to use tags. Read about tags at: [[tutorial: | In minecraft 1.17, there has been a change for breaking blocks. Now, to define harvest tools and harvest levels, we need to use tags. Read about tags at: [[tutorial: | ||
- | Harvest tool: src/ | + | Harvest tool: src/ |
Harvest level: src/ | Harvest level: src/ | ||
Line 190: | Line 194: | ||
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(Text.of(" |
} | } | ||
Line 198: | Line 202: | ||
</ | </ | ||
- | To use your custom block class, replace | + | To use your custom block class, replace |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 207: | Line 211: | ||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
- | Registry.register(Registry.BLOCK, new Identifier(" | + | Registry.register(Registries.BLOCK, new Identifier(" |
- | Registry.register(Registry.ITEM, new Identifier(" | + | Registry.register(Registries.ITEM, new Identifier(" |
} | } | ||
} | } | ||
</ | </ | ||
- | ==== Custom | + | ==== Custom |
- | When using block models that do not // | + | When using block models that do not // |
{{: | {{: | ||
Line 221: | Line 225: | ||
To fix this, we have to define the '' | To fix this, we have to define the '' | ||
- | < | + | < |
- | | + | public class ExambleBlock extends Block { |
- | | + | [...] |
- | | + | |
- | } | + | public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) { |
+ | return VoxelShapes.cuboid(0f, | ||
+ | } | ||
+ | } | ||
</ | </ | ||
tutorial/blocks.txt · Last modified: 2024/07/04 16:29 by mineblock11