User Tools

Site Tools


zh_cn:tutorial:blocks

This is an old revision of the document!


添加方块

将方块添加到你的模组过程与添加物品类似。你可以创建 Block 或自定义类的实例,然后将其注册到 Registries.BLOCK(1.19.3 以上版本)或 Registry.BLOCK (1.19.2 以下)注册表。你还需要提供纹理和方块状态或模型文件以提供方块外观。如需了解方块模型格式的更多信息,请参考Minecraft Wiki模型页面

创建方块

首先创建 Block 的实例。该实例可以存储在任何地方,但是我们会在 ModInitializer 的顶部开始。Block 构造器函数需要一个 AbstractBlock.Settings 实例,也就是用于配置方块属性的构造器。Fabric 提供一个 FabricBlockSettings 构造器类以及更多可用的选项。

  1. public class ExampleMod implements ModInitializer {
  2.  
  3. /* 声明和初始化我们的自定义方块实例。
  4.   我们将方块材质(material)设置为 METAL(金属)。
  5.  
  6.   `strength` 会将方块的硬度和抗性设为同一个值。
  7.   硬度决定了方块需要多久挖掘,抗性决定了方块抵御爆破伤害(如爆炸)的能力。
  8.   石头的硬度为 1.5f,抗性为 6.0f,黑曜石的硬度为 50.0f,抗性为 1200.0f。
  9.  
  10.   可以在`Blocks`类中查找所有原版方块,你可以以此作为参考。
  11.   */
  12.  
  13. // 对于 1.20 以下版本:
  14. // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f));
  15. // 对于 1.20.5 以下版本:
  16. // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));
  17. // 对于自 1.20.5 之后的版本:
  18. public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));
  19.  
  20. @Override
  21. public void onInitialize() {
  22.  
  23. }
  24. }

注册方块

方块应该注册在 Registries.BLOCK 注册表中。调用 Registry.register 并传递适当的参数。你可以直接在 onInitialize 方法中注册方块,也可以在静态部分创建方块实例时将其注册,因为 register 方法会返回这个方块实例自身。

如果你使用的是 1.19.2 以下的版本,请将 Registries.BLOCK 替换为 Registry.BLOCK

  1. public class ExampleMod implements ModInitializer {
  2.  
  3. // 对于 1.20 以下版本:
  4. // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f));
  5. // 对于 1.20.5 以下版本:
  6. // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));
  7. // 对于自 1.20.5 之后的版本:
  8. public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));
  9.  
  10. @Override
  11. public void onInitialize() {
  12. Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
  13. }
  14. }

您的方块不能作为物品存入背包,但可以通过使用 /setblock <位置> tutorial:example_block 在游戏中看到。

为方块注册物品

在大多数情况下,您希望能够拿着物品放置方块。为此,您需要在物品注册表中注册一个相应的物品。您可以通过在 Registries.ITEM 下注册 BlockItem 的实例来执行此操作。该物品的注册表名称通常应与该方块的注册表名称相同。

  1. public class ExampleMod implements ModInitializer {
  2.  
  3. // 对于 1.20 以下版本:
  4. // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f));
  5. // 对于 1.20.5 以下版本:
  6. // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));
  7. // 对于自 1.20.5 之后的版本:
  8. public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));
  9.  
  10. @Override
  11. public void onInitialize() {
  12. Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
  13. // 对于 1.20.5 以下版本:
  14. Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new FabricItemSettings()));
  15. // 对于自 1.20.5 之后的版本:
  16. Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings()));
  17. }
  18. }

给予方块外观

你可能已经注意到,新的方块只是游戏中紫色和黑色棋盘格图案。这表明Minecraft加载方块资源或外观时出错。运行客户端时,完整的问题列表会输出在你的日志中。你需要以下文件来给予方块外观:

  • 方块状态文件
  • 方块模型文件
  • 纹理
  • 物品模型文件(如果方块有与之关联的物品)

这些文件位于:

  • 方块状态:src/main/resources/assets/tutorial/blockstates/example_block.json
  • 方块模型:src/main/resources/assets/tutorial/models/block/example_block.json
  • 物品模型:src/main/resources/assets/tutorial/models/item/example_block.json
  • 方块纹理:src/main/resources/assets/tutorial/textures/block/example_block.png

方块状态文件根据其方块装填确定该方块应使用的模型。由于我们的方块没有所谓状态,所以我们用空字符串表示所有:

src/main/resources/assets/tutorial/blockstates/example_block.json
{
  "variants": {
    "": { "model": "tutorial:block/example_block" }
  }
}

方块模型文件定义了方块的形状和纹理。我们将使用block/cube_all作为父模型,这将使我们能够轻松地在方块的所有面上设置相同的纹理。

src/main/resources/assets/tutorial/models/block/example_block.json
{
  "parent": "block/cube_all",
  "textures": {
    "all": "tutorial:block/example_block"
  }
}

在大多数情况下,您想让方块作为物品时外观还是这个方块。为此,您可以制作一个从方块模型文件继承的项目文件,这会使得该物品外观和方块相同:

src/main/resources/assets/tutorial/models/item/example_block.json
{
  "parent": "tutorial:block/example_block"
}

加载Minecraft,您的方块最终应该具有外观!

配置方块掉落物

该方块必须有战利品表,以便在该方块被破坏时掉落物品。以下文件会使方块被破坏时掉落其本身。

src/main/resources/data/tutorial/loot_tables/blocks/example_block.json
{
  "type": "minecraft:block",
  "pools": [
    {
      "rolls": 1,
      "entries": [
        {
          "type": "minecraft:item",
          "name": "tutorial:example_block"
        }
      ],
      "conditions": [
        {
          "condition": "minecraft:survives_explosion"
        }
      ]
    }
  ]
}

条件 minecraft:survives_explosion 的意思是,如果方块是在带有损耗的爆炸(例如苦力怕的爆炸,而非 TNT 的爆炸)中破坏的,有可能不会掉落。如果没有这个条件,那么方块在带有损耗的爆炸中总是会掉落。

在1.17,破坏方块有所改变,定义采集工具和采集等级需要使用标签,请参考标签教程。我们需要将方块添加到以下标签:

* 采集工具:src/main/resources/data/minecraft/tags/blocks/mineable/<tooltype>.json,其中 <tooltype> 可以是 axepickaxeshovelhoe * 采集等级:src/main/resources/data/minecraft/tags/blocks/needs_<tier>_tool.json,其中 <tier> 可以是:stoneirondiamond不包括 netherite

src/main/resources/data/minecraft/tags/blocks/mineable/pickaxe.json
{
  "replace": false,
  "values": [
    "example:example_block"
  ]
}
src/main/resources/data/minecraft/tags/blocks/needs_stone_tool.json
{
  "replace": false,
  "values": [
    "example:example_block"
  ]
}

对于采集等级标签(needs_stone_toolneeds_iron_toolneeds_diamond_tool)生效,在方块定义中将 requiresTool()Block.Settings(以 1.20.5 以上版本为例):

  1. public static final Block EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.create().strength(4.0f).requiresTool());

创建自定义方块类

当创建一个简单的方块时,上述方法效果很好,但是有时您想要一个具有独特机制的方块。我们将创建一个单独的继承 Block 的类来执行此操作。该类需要一个带有 AbstractBlock.Settings 参数的构造器:

  1. public class ExampleBlock extends Block {
  2.  
  3. public ExampleBlock(Settings settings) {
  4. super(settings);
  5. }
  6. }

你可以在方块类中覆盖方法以实现特殊功能。这里是 onUse 方法的实现,右键单击方块时会调用此方法。我们检查交互是否在服务器进行,并向玩家发送消息,“Hello, world!”

  1. public class ExampleBlock extends Block {
  2.  
  3. public ExampleBlock(Settings settings) {
  4. super(settings);
  5. }
  6.  
  7. // 对于 1.20.5 以下版本,方法参数应该是“BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit”
  8. @Override
  9. public ActionResult onUse(World world, PlayerEntity player, BlockHitResult hit) {
  10. if (!world.isClient) {
  11. player.sendMessage(Text.literal("Hello, world!"), false);
  12. }
  13.  
  14. return ActionResult.SUCCESS;
  15. }
  16. }

要使用自定义方块类,请在注册时将 new Block 替换为 new ExampleBlock

  1. public class ExampleMod implements ModInitializer {
  2.  
  3. public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.create().strength(4.0f));
  4.  
  5. @Override
  6. public void onInitialize() {
  7. Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
  8. Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings()));
  9. }
  10. }

自定义形状

当使用不能完整填充一个方块的方块模型(例如铁砧、台阶、楼梯)而其形状却仍是完整的时,邻近的方块的隐藏面就会暴露:

要解决这个问题,我们需要定义方块的 VoxelShape

public class ExambleBlock extends Block {
    [...]
    @Override
    public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) {
        return VoxelShapes.cuboid(0f, 0f, 0f, 1f, 1.0f, 0.5f);
    }
}

注意方块的碰撞形状默认为其描边形状,如果未被指定。

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.
  • 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 getCollisionShape method, you can also call noCollision in the FabricBlockSettings when creating the block.
  • 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.
  • 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.

下一步

向方块添加简单状态,例如整数和布尔值

给块一个方块实体,使它们可以具有物品栏之类的的高级状态。此外,还需要像 GUI 和自定义方块渲染。

要让方块可燃(也就是说,可以被火燃烧),可使用 FlammableBlockRegistry

zh_cn/tutorial/blocks.1713145453.txt.gz · Last modified: 2024/04/15 01:44 by solidblock