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
Last revisionBoth sides next revision
zh_cn:tutorial:blocks [2023/11/18 08:35] – [下一步] solidblockzh_cn:tutorial:blocks [2024/04/15 01:45] – [为方块注册物品] solidblock
Line 3: Line 3:
 将方块添加到你的模组过程与[[zh_cn:tutorial:items|添加物品]]类似。你可以创建 ''Block'' 或自定义类的实例,然后将其注册到 ''Registries.BLOCK''(1.19.3 以上版本)或 ''Registry.BLOCK'' (1.19.2 以下)注册表。你还需要提供纹理和方块状态或模型文件以提供方块外观。如需了解方块模型格式的更多信息,请参考[[https://zh.minecraft.wiki/模型|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)); // fabric api 版本 <0.77.0 +     
-    public static final Block EXAMPLE_BLOCK  = new Block(FabricBlockSettings.create().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() {
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 54: Line 65:
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
  
-    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().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()));
     }     }
 } }
Line 162: Line 181:
 </code> </code>
  
-对于采集等级标签(''needs_stone_tool''、''needs_iron_tool'' 和 ''needs_diamond_tool'')生效,在方块定义中将 ''requiresTool()'' 到 ''FabricBlockSettings''+对于采集等级标签(''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.create().strength(4.0f).requiresTool());+    public static final Block EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.create().strength(4.0f).requiresTool());
 </code> </code>
  
Line 190: 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.literal("Hello, world!"), false);             player.sendMessage(Text.literal("Hello, world!"), false);
Line 206: Line 226:
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
  
-    public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(FabricBlockSettings.create().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 238: Line 258:
 {{:tutorial:voxelshape_fixed.png?200|}} {{:tutorial:voxelshape_fixed.png?200|}}
  
-==== 下一步 ====+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. 
 + 
 +===== 下一步 =====
 [[zh_cn:tutorial:blockstate|向方块添加简单状态,例如整数和布尔值]]。 [[zh_cn:tutorial:blockstate|向方块添加简单状态,例如整数和布尔值]]。
  
zh_cn/tutorial/blocks.txt · Last modified: 2024/04/15 01:52 by solidblock