User Tools

Site Tools


zh_cn:tutorial:blocks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
zh_cn:tutorial:blocks [2023/08/23 00:10] – [创建一个方块] wjz_pzh_cn:tutorial:blocks [2024/04/15 01:52] (current) – [自定义形状] solidblock
Line 1: Line 1:
-====== 添加一个方块 ======+====== 添加方块 ======
  
-将方块添加到你的模组过程与[[zh_cn:tutorial:items|添加物品]]类似。你可以创建 ''Block'' 或自定义类的实例,然后将其注册到 ''Registries.BLOCK''(1.19.3 以上版本)或 ''Registry.BLOCK'' (1.19.2 以下)注册表。你还需要提供纹理和方块状态或模型文件以提供方块外观。如需了解方块模型格式的更多信息,请参考[[https://minecraft-zh.gamepedia.com/模型|Minecraft Wiki模型页面]]。+将方块添加到你的模组过程与[[zh_cn:tutorial:items|添加物品]]类似。你可以创建 ''Block'' 或自定义类的实例,然后将其注册到 ''Registries.BLOCK''(1.19.3 以上版本)或 ''Registry.BLOCK'' (1.19.2 以下)注册表。你还需要提供纹理和方块状态或模型文件以提供方块外观。如需了解方块模型格式的更多信息,请参考[[https://zh.minecraft.wiki/模型|Minecraft Wiki模型页面]]。
  
-===== 创建一个方块 =====+===== 创建方块 =====
  
 首先创建 ''Block'' 的实例。该实例可以存储在任何地方,但是我们会在 ''ModInitializer'' 的顶部开始。''Block'' 构造器函数需要一个 ''AbstractBlock.Settings'' 实例,也就是用于配置方块属性的构造器。Fabric 提供一个 ''FabricBlockSettings'' 构造器类以及更多可用的选项。 首先创建 ''Block'' 的实例。该实例可以存储在任何地方,但是我们会在 ''ModInitializer'' 的顶部开始。''Block'' 构造器函数需要一个 ''AbstractBlock.Settings'' 实例,也就是用于配置方块属性的构造器。Fabric 提供一个 ''FabricBlockSettings'' 构造器类以及更多可用的选项。
Line 19: Line 19:
        可以在`Blocks`类中查找所有原版方块,你可以以此作为参考。        可以在`Blocks`类中查找所有原版方块,你可以以此作为参考。
     */     */
-    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).hardness(4.0f));+     
 +    // 对于 1.20 以下版本: 
 +    // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); 
 +    // 对于 1.20.5 以下版本: 
 +    // 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));
          
     @Override     @Override
Line 36: Line 42:
 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));+    // 对于 1.20 以下版本: 
 +    // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); 
 +    // 对于 1.20.5 以下版本: 
 +    // 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));
          
     @Override     @Override
Line 45: Line 56:
 </code> </code>
  
-您的方块不能作为物品存入背包,但可以通过使用 ''/setblock tutorial:example_block'' 在游戏中看到。+您的方块不能作为物品存入背包,但可以通过使用 ''/setblock <位置> tutorial:example_block'' 在游戏中看到。
  
 ==== 为方块注册物品 ==== ==== 为方块注册物品 ====
Line 54: Line 65:
 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));+    // 对于 1.20 以下版本: 
 +    // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); 
 +    // 对于 1.20.5 以下版本: 
 +    // 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));
          
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
         Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);         Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
-        Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new FabricItemSettings()));+        // 对于 1.20.5 以下版本: 
 +        // Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new FabricItemSettings())); 
 +        // 对于自 1.20.5 之后的版本: 
 +        Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings()));
     }     }
 } }
 </code> </code>
  
-===== 给您的方块一个外观 =====+===== 给方块外观 =====
  
-可能已经注意到,新的方块只是游戏中紫色和黑色棋盘格图案。这表明Minecraft加载方块资源或外观时出错。运行客户端时,完整的问题列表会输出在你的日志中。你需要以下文件来给予方块外观:+可能已经注意到,新的方块只是游戏中紫色和黑色棋盘格图案。这表明Minecraft加载方块资源或外观时出错。运行客户端时,完整的问题列表会输出在你的日志中。你需要以下文件来给予方块外观:
  
   * 方块状态文件   * 方块状态文件
Line 75: Line 94:
 这些文件位于: 这些文件位于:
  
-  方块状态:src/main/resources/assets/tutorial/blockstates/example_block.json +  方块状态:''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/block/example_block.json'' 
-  物品模型:src/main/resources/assets/tutorial/models/item/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/textures/block/example_block.png''
  
 方块状态文件根据其方块装填确定该方块应使用的模型。由于我们的方块没有所谓状态,所以我们用空字符串表示所有: 方块状态文件根据其方块装填确定该方块应使用的模型。由于我们的方块没有所谓状态,所以我们用空字符串表示所有:
Line 113: Line 132:
 ===== 配置方块掉落物 ===== ===== 配置方块掉落物 =====
  
-该方块必须有一个//战利品表//,以便在该方块被破坏时掉落物品。以下文件会使方块被破坏时掉落其本身。+该方块必须有//战利品表//,以便在该方块被破坏时掉落物品。以下文件会使方块被破坏时掉落其本身。
  
 <code JavaScript src/main/resources/data/tutorial/loot_tables/blocks/example_block.json> <code JavaScript src/main/resources/data/tutorial/loot_tables/blocks/example_block.json>
Line 136: Line 155:
 } }
 </code> </code>
 +
 +条件 ''minecraft:survives_explosion'' 的意思是,如果方块是在带有损耗的爆炸(例如苦力怕的爆炸,而非 TNT 的爆炸)中破坏的,//有可能//不会掉落。如果没有这个条件,那么方块在带有损耗的爆炸中总是会掉落。
  
 在1.17,破坏方块有所改变,定义采集工具和采集等级需要使用标签,请参考[[zh_cn:tutorial:tags|标签教程]]。我们需要将方块添加到以下标签: 在1.17,破坏方块有所改变,定义采集工具和采集等级需要使用标签,请参考[[zh_cn:tutorial:tags|标签教程]]。我们需要将方块添加到以下标签:
  
-  采集工具:src/main/resources/data/minecraft/tags/blocks/mineable/<tooltype>.json,其中'tooltype'可以是'axe'、'pickaxe'、'shovel'、'hoe' + 采集工具:''src/main/resources/data/minecraft/tags/blocks/mineable/<tooltype>.json'',其中 ''<tooltype>'' 可以是 ''axe''、''pickaxe''、''shovel''、''hoe'
-  采集等级:src/main/resources/data/minecraft/tags/blocks/needs_<tier>_tool.json,其中'tier'可以是:'stone'、'iron'、'diamond'+ 采集等级:''src/main/resources/data/minecraft/tags/blocks/needs_<tier>_tool.json'',其中 ''<tier>'' 可以是:''stone''、''iron''、''diamond''(//不包括// ''netherite''
  
 <code JavaScript src/main/resources/data/minecraft/tags/blocks/mineable/pickaxe.json> <code JavaScript src/main/resources/data/minecraft/tags/blocks/mineable/pickaxe.json>
Line 160: Line 181:
 </code> </code>
  
-对于采集等级标签(needs_stone_tool、needs_iron_tool和needs_diamond_tool)生效,在方块定义中将requiresTool()到FabricToolSettings+对于采集等级标签(''needs_stone_tool''''needs_iron_tool'' 和 ''needs_diamond_tool'')生效,在方块定义中将 ''requiresTool()'' 到 ''Block.Settings''(以 1.20.5 以上版本为例)
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-    public static final Block EXAMPLE_BLOCK = new ExampleBlock(FabricBlockSettings.of(Material.METAL).strength(4.0f).requiresTool());+    public static final Block EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.create().strength(4.0f).requiresTool());
 </code> </code>
  
 ===== 创建自定义方块类 ===== ===== 创建自定义方块类 =====
  
-当创建一个简单的方块时,上述方法效果很好,但是有时您想要一个具有//一无二//机制的方块。我们将创建一个//单独//扩展 ''Block'' 的类来执行此操作。该类需要一个带有 ''AbstractBlock.Settings'' 参数的构造器:+当创建一个简单的方块时,上述方法效果很好,但是有时您想要一个具有////机制的方块。我们将创建一个//单独//继承 ''Block'' 的类来执行此操作。该类需要一个带有 ''AbstractBlock.Settings'' 参数的构造器:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
Line 188: Line 209:
     }     }
  
 +    // 对于 1.20.5 以下版本,方法参数应该是“BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit”
     @Override     @Override
-    public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {+    public ActionResult onUse(World world, PlayerEntity player, BlockHitResult hit) {
         if (!world.isClient) {         if (!world.isClient) {
-            player.sendMessage(Text.of("Hello, world!"), false);+            player.sendMessage(Text.literal("Hello, world!"), false);
         }         }
  
Line 204: Line 226:
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
  
-    public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(FabricBlockSettings.of(Material.STONE).hardness(4.0f));+    public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.create().strength(4.0f));
          
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
         Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);         Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
-        Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new FabricItemSettings()));+        Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings()));
     }     }
 } }
Line 236: Line 258:
 {{:tutorial:voxelshape_fixed.png?200|}} {{:tutorial:voxelshape_fixed.png?200|}}
  
-==== 下一步 ====+你也可以定义其他几类方块形状,方块形状的类型包括: 
 +  * **外观形状(outline shape)**:方块大多数类型的形状都使用这个值作为默认。在世界中,当你指向这个形状时,会根据此形状绘制道明的黑色边框。大多数时候,这个形状不应该是空的。 
 +  * **碰撞形状(collision shape)**:用于计算碰撞的形状。实体(包括玩家)移动时,其碰撞箱通常不能与方块的碰撞形状重合。一些方块,例如栅栏和墙,碰撞形状高于一格。一些方块,例如花,碰撞形状是空的。除了修改 ''getCollisionShape'' 方法外,你也可以在创建方块时调用 ''Block.Settings'' 中的 ''noCollision''。 
 +  * **raycasting shape**:用于计算视线投射(判断你正在指向哪个方块的过程)的形状。通过不需要指定。 
 +  * **相机碰撞形状(camera collision shape)**:用于计算第三人称视角的相机的位置的形状。玻璃和细雪的相机碰撞形状为空。 
 +===== 下一步 =====
 [[zh_cn:tutorial:blockstate|向方块添加简单状态,例如整数和布尔值]]。 [[zh_cn:tutorial:blockstate|向方块添加简单状态,例如整数和布尔值]]。
  
-[[zh_cn:tutorial:blockentity|给块一个方块实体,使它们可以具有物品栏之类的的高级状态]]。此外,还需要像GUI和自定义块渲染很多东西+[[zh_cn:tutorial:blockentity|给块一个方块实体,使它们可以具有物品栏之类的的高级状态]]。此外,还需要像 GUI 和自定义块渲染。 
 + 
 +要让方块可燃(也就是说,可以被火燃烧),可使用 ''FlammableBlockRegistry''
zh_cn/tutorial/blocks.1692749411.txt.gz · Last modified: 2023/08/23 00:10 by wjz_p