zh_cn:tutorial:blocks
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
zh_cn:tutorial:blocks [2024/04/15 01:44] – 更新 solidblock | zh_cn:tutorial:blocks [2024/12/08 14:23] (current) – [下一步] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== 添加方块 ====== | ====== 添加方块 ====== | ||
- | 将方块添加到你的模组过程与[[zh_cn: | + | 将方块添加到你的模组过程与[[zh_cn: |
===== 创建方块 ===== | ===== 创建方块 ===== | ||
+ | :!: 使用你使用的是 1.21.2 或者之后的版本,请直接看 [[#在 1.21.2+ 中注册方块]]。 | ||
首先创建 '' | 首先创建 '' | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
Line 24: | Line 25: | ||
// 对于 1.20.5 以下版本: | // 对于 1.20.5 以下版本: | ||
// public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); | ||
- | // 对于自 1.20.5 之后的版本: | + | // 对于自 1.20.5 之后、1.21.2 以前的版本: |
public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f)); | public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f)); | ||
| | ||
Line 33: | Line 34: | ||
} | } | ||
</ | </ | ||
- | ==== 注册方块 ==== | + | ===== 注册方块 |
- | 方块应该注册在 '' | + | 方块应该注册在 '' |
如果你使用的是 1.19.2 以下的版本,请将 '' | 如果你使用的是 1.19.2 以下的版本,请将 '' | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
Line 46: | Line 47: | ||
// 对于 1.20.5 以下版本: | // 对于 1.20.5 以下版本: | ||
// public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); | ||
- | // 对于自 1.20.5 之后的版本: | + | // 对于自 1.20.5 之后、1.21.2 以前的版本: |
public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f)); | public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f)); | ||
| | ||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
- | Registry.register(Registries.BLOCK, | + | |
+ | // Registry.register(Registries.BLOCK, | ||
+ | // 对于 1.21 之后的版本: | ||
+ | Registry.register(Registries.BLOCK, | ||
} | } | ||
} | } | ||
</ | </ | ||
- | 您的方块不能作为物品存入背包,但可以通过使用 ''/ | + | 你的自定义方块不能作为物品存入背包,但可以通过使用 ''/ |
==== 为方块注册物品 ==== | ==== 为方块注册物品 ==== | ||
Line 62: | Line 66: | ||
在大多数情况下,您希望能够拿着物品放置方块。为此,您需要在物品注册表中注册一个相应的物品。您可以通过在 '' | 在大多数情况下,您希望能够拿着物品放置方块。为此,您需要在物品注册表中注册一个相应的物品。您可以通过在 '' | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
Line 69: | Line 73: | ||
// 对于 1.20.5 以下版本: | // 对于 1.20.5 以下版本: | ||
// public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); | ||
- | // 对于自 1.20.5 之后的版本: | + | // 对于自 1.20.5 之后、1.21.2 之前的版本: |
public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f)); | public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f)); | ||
| | ||
Line 76: | Line 80: | ||
Registry.register(Registries.BLOCK, | Registry.register(Registries.BLOCK, | ||
// 对于 1.20.5 以下版本: | // 对于 1.20.5 以下版本: | ||
- | Registry.register(Registries.ITEM, | + | |
- | // 对于自 1.20.5 之后的版本: | + | // 对于 1.21 以下版本: |
- | Registry.register(Registries.ITEM, | + | // Registry.register(Registries.ITEM, |
+ | // 对于 1.21 之后的版本: | ||
+ | Registry.register(Registries.ITEM, | ||
} | } | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ===== 注册方块的最佳实践 ===== | ||
+ | :!: 本段不适用于 1.21.2 之后的版本。 | ||
+ | |||
+ | 有时你的模组有许多方块。如果这样注册,你会为每个方块都写这样复杂的代码,代码就会显乱。所以,类似于注册物品,我们为方块创建单独的类,以及一个实用方法以注册方块和物品。 | ||
+ | |||
+ | <code java TutorialBlocks.java> | ||
+ | public final class TutorialBlocks { | ||
+ | public static final Block EXAMPLE_BLOCK = register(" | ||
+ | | ||
+ | private static <T extends Block> T register(String path, T block) { | ||
+ | Registry.register(Registries.BLOCK, | ||
+ | Registry.register(Registries.ITEM, | ||
+ | return block; | ||
+ | } | ||
+ | | ||
+ | public static void initialize() { | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 记得要在 '' | ||
+ | <code java ExampleMod.java> | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | TutorialBlocks.initialize(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== 在 1.21.2+ 中注册方块 ===== | ||
+ | 在 1.21.2+ 中,要把 '' | ||
+ | |||
+ | <code java> | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | public static final Block EXAMPLE_BLOCK = register(" | ||
+ | |||
+ | private static Block register(String path, Function< | ||
+ | final Identifier identifier = Identifier.of(" | ||
+ | final RegistryKey< | ||
+ | |||
+ | final Block block = Blocks.register(registryKey, | ||
+ | Items.register(block); | ||
+ | return block; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 上面的代码中,'' | ||
===== 给予方块外观 ===== | ===== 给予方块外观 ===== | ||
- | 你可能已经注意到,新的方块只是游戏中紫色和黑色棋盘格图案。这表明Minecraft加载方块资源或外观时出错。运行客户端时,完整的问题列表会输出在你的日志中。你需要以下文件来给予方块外观: | + | 你可能已经注意到,新的方块只是游戏中紫色和黑色棋盘格图案。这表明 Minecraft 加载方块资源或外观时出错。运行客户端时,完整的问题列表会输出在你的日志中。你需要以下文件来给予方块外观: |
* 方块状态文件 | * 方块状态文件 | ||
Line 134: | Line 190: | ||
该方块必须有// | 该方块必须有// | ||
- | <code JavaScript src/ | + | 对于 1.21 之后的版本,路径为 '' |
+ | |||
+ | <code JavaScript src/ | ||
{ | { | ||
" | " | ||
Line 158: | Line 216: | ||
条件 '' | 条件 '' | ||
- | 在1.17,破坏方块有所改变,定义采集工具和采集等级需要使用标签,请参考[[zh_cn: | + | 在 1.17,破坏方块有所改变,定义采集工具和采集等级需要使用标签,请参考[[zh_cn: |
- | * 采集工具:'' | + | |
- | * 采集等级:'' | + | * 采集等级:'' |
<code JavaScript src/ | <code JavaScript src/ | ||
Line 167: | Line 225: | ||
" | " | ||
" | " | ||
- | "example: | + | "tutorial: |
] | ] | ||
} | } | ||
Line 176: | Line 234: | ||
" | " | ||
" | " | ||
- | "example: | + | "tutorial: |
] | ] | ||
} | } | ||
</ | </ | ||
- | 对于采集等级标签('' | + | 要采集等级标签('' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 191: | Line 249: | ||
当创建一个简单的方块时,上述方法效果很好,但是有时您想要一个具有// | 当创建一个简单的方块时,上述方法效果很好,但是有时您想要一个具有// | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class ExampleBlock extends Block { | public class ExampleBlock extends Block { | ||
- | |||
public ExampleBlock(Settings settings) { | public ExampleBlock(Settings settings) { | ||
super(settings); | super(settings); | ||
Line 202: | Line 259: | ||
你可以在方块类中覆盖方法以实现特殊功能。这里是 '' | 你可以在方块类中覆盖方法以实现特殊功能。这里是 '' | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public class ExampleBlock extends Block { | public class ExampleBlock extends Block { | ||
Line 223: | Line 280: | ||
要使用自定义方块类,请在注册时将 '' | 要使用自定义方块类,请在注册时将 '' | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
- | public class ExampleMod implements ModInitializer | + | public |
- | public static final ExampleBlock | + | public static final Block EXAMPLE_BLOCK = register(" |
| | ||
- | | + | |
- | public void onInitialize() { | + | |
- | Registry.register(Registries.BLOCK, new Identifier(" | + | |
- | Registry.register(Registries.ITEM, | + | |
- | } | + | |
} | } | ||
</ | </ | ||
Line 244: | Line 297: | ||
要解决这个问题,我们需要定义方块的 '' | 要解决这个问题,我们需要定义方块的 '' | ||
- | <code java> | + | < |
public class ExambleBlock extends Block { | public class ExambleBlock extends Block { | ||
[...] | [...] | ||
Line 258: | Line 311: | ||
{{: | {{: | ||
- | You can also define other types of shapes for the block. The type of shapes of blocks include: | + | 你也可以定义其他几类方块形状,方块形状的类型包括: |
- | * **outline shape**: the shape used as default value for most type of shapes. In the worlds, when you points to the shape, the translucent black outline is displayed according to this shape. Most times it should not be empty. | + | * **外观形状(outline shape)**:方块大多数类型的形状都使用这个值作为默认。在世界中,当你指向这个形状时,会根据此形状绘制道明的黑色边框。大多数时候,这个形状不应该是空的。 |
- | * **collision shape**: the shape used to calculate collisions. When entities (including players) are moving, their collision box usually cannot intersect the collision shape of blocks. Some blocks, such as fences and walls, may have a collision shape higher than one block. Some blocks, such as flowers, have an empty collision shape. Apart from modifying | + | * **碰撞形状(collision shape)**:用于计算碰撞的形状。实体(包括玩家)移动时,其碰撞箱通常不能与方块的碰撞形状重合。一些方块,例如栅栏和墙,碰撞形状高于一格。一些方块,例如花,碰撞形状是空的。除了修改 |
- | * **raycasting shape**: the shape used to calculate raycasting (the process judging which block you are pointing to). You usually do not need to specify it. | + | * **raycasting shape**:用于计算视线投射(判断你正在指向哪个方块的过程)的形状。通过不需要指定。 |
- | * **camera collision shape**: the shape used to calculate the position of camera in third-party view. Glass and powder snow have an empty camera collision shape. | + | * **相机碰撞形状(camera collision shape)**:用于计算第三人称视角的相机的位置的形状。玻璃和细雪的相机碰撞形状为空。 |
+ | | ||
===== 下一步 ===== | ===== 下一步 ===== | ||
[[zh_cn: | [[zh_cn: | ||
- | [[zh_cn: | + | [[zh_cn: |
要让方块可燃(也就是说,可以被火燃烧),可使用 '' | 要让方块可燃(也就是说,可以被火燃烧),可使用 '' |
zh_cn/tutorial/blocks.1713145453.txt.gz · Last modified: 2024/04/15 01:44 by solidblock