zh_cn:tutorial:blocks
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
zh_cn:tutorial:blocks [2022/12/16 01:12] – [注册方块] solidblock | zh_cn:tutorial:blocks [2024/04/15 01:45] – [为方块注册物品] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== 添加一个方块 ====== | + | ====== 添加方块 ====== |
- | 将方块添加到你的模组过程与[[zh_cn: | + | 将方块添加到你的模组过程与[[zh_cn: |
- | ===== 创建一个方块 ===== | + | ===== 创建方块 ===== |
- | 首先创建'' | + | 首先创建 '' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 17: | Line 17: | ||
| | ||
- | | + | |
*/ | */ | ||
- | 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)); | + | |
+ | // 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: | ||
</ | </ | ||
- | 您的方块不能作为物品存入背包,但可以通过使用 ''/ | + | 您的方块不能作为物品存入背包,但可以通过使用 ''/ |
==== 为方块注册物品 ==== | ==== 为方块注册物品 ==== | ||
- | 在大多数情况下,您希望能够拿着物品放置方块。为此,您需要在物品注册表中注册一个相应的BlockItem。您可以通过在Registry.ITEM下注册BlockItem的实例来执行此操作。该物品的注册表名称通常应与该方块的注册表名称相同。 | + | 在大多数情况下,您希望能够拿着物品放置方块。为此,您需要在物品注册表中注册一个相应的物品。您可以通过在 |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
- | /* Declare and initialize our custom block instance. | + | // 对于 1.20 以下版本: |
- | We set out block material to METAL, which requires a pickaxe to efficiently break. | + | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); |
- | | + | // 对于 |
- | | + | // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f)); |
- | public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL)); | + | // 对于自 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(Registry.BLOCK, new Identifier(" | + | Registry.register(Registries.BLOCK, new Identifier(" |
- | Registry.register(Registry.ITEM, new Identifier(" | + | |
+ | // Registry.register(Registries.ITEM, new Identifier(" | ||
+ | // 对于自 1.20.5 之后的版本: | ||
+ | Registry.register(Registries.ITEM, new Identifier(" | ||
} | } | ||
} | } | ||
</ | </ | ||
- | ===== 给您的方块一个外观 ===== | + | ===== 给予方块外观 ===== |
- | 您可能已经注意到,新的方块只是游戏中紫色和黑色棋盘格图案。这表明Minecraft加载方块资源或外观时出错。运行客户端时,完整的问题列表会输出在你的日志中。你需要以下文件来给予方块外观: | + | 你可能已经注意到,新的方块只是游戏中紫色和黑色棋盘格图案。这表明Minecraft加载方块资源或外观时出错。运行客户端时,完整的问题列表会输出在你的日志中。你需要以下文件来给予方块外观: |
* 方块状态文件 | * 方块状态文件 | ||
Line 79: | Line 94: | ||
这些文件位于: | 这些文件位于: | ||
- | 方块状态:src/ | + | |
- | 方块模型:src/ | + | |
- | 物品模型:src/ | + | |
- | 方块纹理:src/ | + | |
方块状态文件根据其方块装填确定该方块应使用的模型。由于我们的方块没有所谓状态,所以我们用空字符串表示所有: | 方块状态文件根据其方块装填确定该方块应使用的模型。由于我们的方块没有所谓状态,所以我们用空字符串表示所有: | ||
Line 117: | Line 132: | ||
===== 配置方块掉落物 ===== | ===== 配置方块掉落物 ===== | ||
- | 该方块必须有一个// | + | 该方块必须有// |
<code JavaScript src/ | <code JavaScript src/ | ||
Line 140: | Line 155: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | 条件 '' | ||
在1.17,破坏方块有所改变,定义采集工具和采集等级需要使用标签,请参考[[zh_cn: | 在1.17,破坏方块有所改变,定义采集工具和采集等级需要使用标签,请参考[[zh_cn: | ||
- | | + | |
- | 采集等级:src/ | + | * 采集等级:'' |
<code JavaScript src/ | <code JavaScript src/ | ||
Line 164: | Line 181: | ||
</ | </ | ||
- | 对于采集等级标签(needs_stone_tool、needs_iron_tool和needs_diamond_tool)生效,在方块定义中将requiresTool()到FabricToolSettings: | + | 对于采集等级标签('' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | 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 java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 183: | Line 200: | ||
</ | </ | ||
- | 你可以在方块类中覆盖方法以实现特殊功能。这里是'' | + | 你可以在方块类中覆盖方法以实现特殊功能。这里是 '' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 192: | Line 209: | ||
} | } | ||
+ | // 对于 1.20.5 以下版本,方法参数应该是“BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit” | ||
@Override | @Override | ||
- | public ActionResult onUse(BlockState state, | + | public ActionResult onUse(World world, PlayerEntity player, BlockHitResult hit) { |
if (!world.isClient) { | if (!world.isClient) { | ||
- | player.sendMessage(Text.of(" | + | player.sendMessage(Text.literal(" |
} | } | ||
Line 203: | Line 221: | ||
</ | </ | ||
- | 要使用自定义方块类,请在注册时将//new Block//替换为//new ExampleBlock//: | + | 要使用自定义方块类,请在注册时将 |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
- | public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.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(Registry.BLOCK, new Identifier(" | + | Registry.register(Registries.BLOCK, new Identifier(" |
- | Registry.register(Registry.ITEM, new Identifier(" | + | Registry.register(Registries.ITEM, new Identifier(" |
} | } | ||
} | } | ||
</ | </ | ||
- | ==== 自定义VoxelShape | + | ==== 自定义形状 |
- | 当使用不能'' | + | 当使用不能完整填充一个方块的方块模型(例如铁砧、台阶、楼梯)而其形状却仍是完整的时,邻近的方块的隐藏面就会暴露: |
{{: | {{: | ||
- | 要解决这个问题,我们需要定义方块的'' | + | 要解决这个问题,我们需要定义方块的 '' |
- | < | + | < |
- | | + | public class ExambleBlock extends Block { |
- | | + | [...] |
- | | + | |
- | } | + | public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) { |
+ | return VoxelShapes.cuboid(0f, | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | 注意方块的// | + | 注意方块的// |
{{: | {{: | ||
- | ==== 下一步 ==== | + | 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 '' | ||
+ | * **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: | [[zh_cn: | ||
- | [[zh_cn: | + | [[zh_cn: |
+ | |||
+ | 要让方块可燃(也就是说,可以被火燃烧),可使用 '' |
zh_cn/tutorial/blocks.txt · Last modified: 2024/04/15 01:52 by solidblock