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/08/26 01:18] – solidblock | zh_cn:tutorial:blocks [2025/04/02 01:11] (current) – [给予方块外观] solidblock | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== 添加方块 ====== | ====== 添加方块 ====== | ||
| - | 将方块添加到你的模组过程与[[zh_cn: | + | 将方块添加到你的模组过程与[[zh_cn: |
| - | ===== 创建方块 ===== | + | ===== 创建方块(1.21.2 之前) |
| + | > :!: 如果使用的是 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 22: | Line 23: | ||
| // 对于 1.20 以下版本: | // 对于 1.20 以下版本: | ||
| // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); | ||
| + | | ||
| // 对于 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 之后的版本: | + | |
| + | | ||
| 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 36: | ||
| } | } | ||
| </ | </ | ||
| - | ===== 注册方块 ===== | + | ===== 注册方块(1.21.2 之前) |
| 方块应该注册在 '' | 方块应该注册在 '' | ||
| Line 39: | Line 42: | ||
| 如果你使用的是 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 49: | ||
| // 对于 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 61: | Line 64: | ||
| 你的自定义方块不能作为物品存入背包,但可以通过使用 ''/ | 你的自定义方块不能作为物品存入背包,但可以通过使用 ''/ | ||
| - | ==== 为方块注册物品 ==== | + | ==== 为方块注册物品(1.21.2 之前) |
| 在大多数情况下,您希望能够拿着物品放置方块。为此,您需要在物品注册表中注册一个相应的物品。您可以通过在 '' | 在大多数情况下,您希望能够拿着物品放置方块。为此,您需要在物品注册表中注册一个相应的物品。您可以通过在 '' | ||
| - | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
| public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
| Line 72: | Line 75: | ||
| // 对于 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 88: | Line 91: | ||
| </ | </ | ||
| - | ===== 注册方块的最佳实践 ===== | + | ===== 注册方块的最佳实践(1.21.2 之前) |
| + | :!: 本段不适用于 1.21.2 之后的版本。 | ||
| 有时你的模组有许多方块。如果这样注册,你会为每个方块都写这样复杂的代码,代码就会显乱。所以,类似于注册物品,我们为方块创建单独的类,以及一个实用方法以注册方块和物品。 | 有时你的模组有许多方块。如果这样注册,你会为每个方块都写这样复杂的代码,代码就会显乱。所以,类似于注册物品,我们为方块创建单独的类,以及一个实用方法以注册方块和物品。 | ||
| - | <code java TutorialBlocks> | + | <code java TutorialBlocks.java> |
| public final class TutorialBlocks { | public final class TutorialBlocks { | ||
| public static final Block EXAMPLE_BLOCK = register(" | public static final Block EXAMPLE_BLOCK = register(" | ||
| Line 107: | Line 112: | ||
| 记得要在 '' | 记得要在 '' | ||
| - | <code java ExampleMod> | + | <code java ExampleMod.java> |
| public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
| @Override | @Override | ||
| Line 113: | Line 118: | ||
| TutorialBlocks.initialize(); | TutorialBlocks.initialize(); | ||
| } | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== 在 1.21.2+ 中注册方块 ===== | ||
| + | 在 1.21.2+ 中,要把 '' | ||
| + | |||
| + | <code java TutorialBlocks.java> | ||
| + | public class TutorialBlocks { | ||
| + | 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; | ||
| + | } | ||
| + | |||
| + | public static void init() { | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 上面的代码中,'' | ||
| + | |||
| + | 别忘了要在模组初始化器中静态加载这个类: | ||
| + | <code java ExampleMod.java> | ||
| + | public class ExampleMod implements ModInitializer { | ||
| + | |||
| + | @Override | ||
| + | public void onInitialize() { | ||
| + | // ... | ||
| + | | ||
| + | TutorialBlocks.init(); | ||
| + | } | ||
| } | } | ||
| </ | </ | ||
| Line 120: | Line 161: | ||
| 你可能已经注意到,新的方块只是游戏中紫色和黑色棋盘格图案。这表明 Minecraft 加载方块资源或外观时出错。运行客户端时,完整的问题列表会输出在你的日志中。你需要以下文件来给予方块外观: | 你可能已经注意到,新的方块只是游戏中紫色和黑色棋盘格图案。这表明 Minecraft 加载方块资源或外观时出错。运行客户端时,完整的问题列表会输出在你的日志中。你需要以下文件来给予方块外观: | ||
| - | * 方块状态文件 | + | * 方块状态映射 |
| - | * 方块模型文件 | + | * 方块烘焙模型 |
| - | * 纹理 | + | * 方块纹理 |
| - | * 物品模型文件(如果方块有与之关联的物品) | + | * //1.21.3 及之前的版本:// |
| + | * //1.21.4 及之后的版本:// | ||
| 这些文件位于: | 这些文件位于: | ||
| - | * 方块状态:'' | + | * 方块状态映射:'' |
| - | * 方块模型:'' | + | * 方块烘焙模型:'' |
| - | * 物品模型:'' | + | |
| * 方块纹理:'' | * 方块纹理:'' | ||
| + | * //1.21.3 及之前的版本:// | ||
| + | * //1.21.4 及之后的版本:// | ||
| - | 方块状态文件根据其方块装填确定该方块应使用的模型。由于我们的方块没有所谓状态,所以我们用空字符串表示所有: | + | 方块状态映射会根据其方块状态确定该方块应使用的模型。由于方块没有属性,只有一个状态,所以用空字符串表示所有: |
| <code JavaScript src/ | <code JavaScript src/ | ||
| Line 142: | Line 185: | ||
| </ | </ | ||
| - | 方块模型文件定义了方块的形状和纹理。我们将使用block/ | + | 方块烘焙模型定义了方块的形状和纹理。我们将使用 |
| <code JavaScript src/ | <code JavaScript src/ | ||
| Line 153: | Line 196: | ||
| </ | </ | ||
| - | 在大多数情况下,您想让方块作为物品时外观还是这个方块。为此,您可以制作一个从方块模型文件继承的项目文件,这会使得该物品外观和方块相同: | + | 在大多数情况下,您想让方块作为物品时外观还是这个方块。 |
| + | |||
| + | 对于 1.21.3 以及之前的版本,可以为物品创建对应的物品模型,该物品模型直接继承方块模型,这会使得该物品外观和方块相同: | ||
| <code JavaScript src/ | <code JavaScript src/ | ||
| { | { | ||
| " | " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 对于 1.21.4 以及之后的版本,可以为物品创建对应的物品模型映射,让物品直接使用方块的模型: | ||
| + | <code JavaScript src/ | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| } | } | ||
| </ | </ | ||
| Line 195: | Line 250: | ||
| 在 1.17,破坏方块有所改变,定义采集工具和采集等级需要使用标签,请参考[[zh_cn: | 在 1.17,破坏方块有所改变,定义采集工具和采集等级需要使用标签,请参考[[zh_cn: | ||
| - | * 采集工具:'' | + | |
| - | * 采集等级:'' | + | * 采集等级:'' |
| <code JavaScript src/ | <code JavaScript src/ | ||
| Line 226: | Line 281: | ||
| 当创建一个简单的方块时,上述方法效果很好,但是有时您想要一个具有// | 当创建一个简单的方块时,上述方法效果很好,但是有时您想要一个具有// | ||
| - | <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 237: | Line 291: | ||
| 你可以在方块类中覆盖方法以实现特殊功能。这里是 '' | 你可以在方块类中覆盖方法以实现特殊功能。这里是 '' | ||
| - | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
| public class ExampleBlock extends Block { | public class ExampleBlock extends Block { | ||
| Line 258: | Line 312: | ||
| 要使用自定义方块类,请在注册时将 '' | 要使用自定义方块类,请在注册时将 '' | ||
| - | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
| public final class TutorialBlocks { | public final class TutorialBlocks { | ||
| Line 275: | Line 329: | ||
| 要解决这个问题,我们需要定义方块的 '' | 要解决这个问题,我们需要定义方块的 '' | ||
| - | <code java> | + | < |
| public class ExambleBlock extends Block { | public class ExambleBlock extends Block { | ||
| [...] | [...] | ||
| Line 296: | Line 350: | ||
| * | * | ||
| ===== 下一步 ===== | ===== 下一步 ===== | ||
| - | [[zh_cn: | + | * [[blockstate|向方块添加简单状态,例如整数和布尔值]]。 |
| - | + | | |
| - | [[zh_cn: | + | * [[datagen_model|使用数据生成器为方块和物品生成方块模型、方块状态映射、物品模型映射]]。 |
| + | * [[datagen_tags|使用数据生成器为方块生成标签]]。 | ||
| + | * 别忘了,你的方块不能够没有[[lang|可翻译的名字]]。 | ||
| 要让方块可燃(也就是说,可以被火燃烧),可使用 '' | 要让方块可燃(也就是说,可以被火燃烧),可使用 '' | ||
zh_cn/tutorial/blocks.1724635084.txt.gz · Last modified: 2024/08/26 01:18 by solidblock