zh_cn: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 | ||
zh_cn:tutorial:blocks [2022/12/16 01:08] – [添加一个方块] solidblock | zh_cn:tutorial:blocks [2022/12/16 01:20] – [自定义VoxelShape] solidblock | ||
---|---|---|---|
Line 5: | Line 5: | ||
===== 创建一个方块 ===== | ===== 创建一个方块 ===== | ||
- | 首先创建'' | + | 首先创建 '' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 11: | Line 11: | ||
/* 声明和初始化我们的自定义方块实例。 | /* 声明和初始化我们的自定义方块实例。 | ||
- | | + | |
- | | + | |
| | ||
- | | + | |
| | ||
Line 29: | Line 29: | ||
==== 注册方块 ==== | ==== 注册方块 ==== | ||
- | 方块应该注册在'' | + | 方块应该注册在 '' |
- | <code java [enable_line_numbers=" | + | 如果你使用的是 1.19.2 以下的版本,请将 '' |
+ | |||
+ | <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).hardness(4.0f)); | + | 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(Registries.BLOCK, new Identifier(" |
- | Registry.register(Registry.BLOCK, new Identifier(" | + | |
} | } | ||
} | } | ||
</ | </ | ||
- | 您的方块不能作为物品存入背包,但可以通过使用''/ | + | 您的方块不能作为物品存入背包,但可以通过使用 ''/ |
==== 为方块注册物品 ==== | ==== 为方块注册物品 ==== | ||
- | 在大多数情况下,您希望能够拿着物品放置方块。为此,您需要在物品注册表中注册一个相应的BlockItem。您可以通过在Registry.ITEM下注册BlockItem的实例来执行此操作。该物品的注册表名称通常应与该方块的注册表名称相同。 | + | 在大多数情况下,您希望能够拿着物品放置方块。为此,您需要在物品注册表中注册一个相应的物品。您可以通过在 |
<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 | ||
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 171: | Line 168: | ||
===== 创建自定义方块类 ===== | ===== 创建自定义方块类 ===== | ||
- | 当创建一个简单的方块时,上述方法效果很好,但是有时您想要一个具有// | + | 当创建一个简单的方块时,上述方法效果很好,但是有时您想要一个具有// |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 182: | Line 179: | ||
</ | </ | ||
- | 你可以在方块类中覆盖方法以实现特殊功能。这里是'' | + | 你可以在方块类中覆盖方法以实现特殊功能。这里是 '' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 202: | Line 199: | ||
</ | </ | ||
- | 要使用自定义方块类,请在注册时将//new Block//替换为//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(Block.Settings.of(Material.STONE).hardness(4.0f)); | + | public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(FabricBlockSettings.of(Material.STONE).hardness(4.0f)); |
| | ||
@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(" |
} | } | ||
} | } | ||
</ | </ | ||
- | ==== 自定义VoxelShape | + | ==== 自定义形状e |
- | 当使用不能'' | + | 当使用不能完整填充一个方块的方块模型(例如铁砧、台阶、楼梯)而其形状却仍是完整的时,邻近的方块的隐藏面就会暴露: |
{{: | {{: | ||
- | 要解决这个问题,我们需要定义方块的'' | + | 要解决这个问题,我们需要定义方块的 '' |
- | < | + | < |
- | | + | public class ExambleBlock extends Block { |
- | | + | [...] |
- | | + | |
- | } | + | public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) { |
+ | return VoxelShapes.cuboid(0f, | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | 注意方块的// | + | 注意方块的// |
{{: | {{: |
zh_cn/tutorial/blocks.txt · Last modified: 2024/04/15 01:52 by solidblock