zh_cn:tutorial:items
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
zh_cn:tutorial:items [2024/08/26 01:24] – solidblock | zh_cn:tutorial:items [2024/12/08 14:24] (current) – [创建物品实例] solidblock | ||
---|---|---|---|
Line 3: | Line 3: | ||
===== 介绍 ===== | ===== 介绍 ===== | ||
- | 添加一个基本的物品是编写模组的第一步。你将需要创建一个 | + | 添加基本的物品是编写模组的第一步。你将需要创建 '' |
- | ===== 注册物品 ===== | + | ===== 创建物品实例 |
- | 首先,创建一个 | + | :!: 如果是使用的 1.21.2 之后的版本,请直接跳到[[# |
+ | |||
+ | 首先,创建 '' | ||
< | < | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
Line 12: | Line 15: | ||
// 对于 1.20.4 以下版本 | // 对于 1.20.4 以下版本 | ||
public static final class_1792 CUSTOM_ITEM = new class_1792(new FabricItemSettings()); | public static final class_1792 CUSTOM_ITEM = new class_1792(new FabricItemSettings()); | ||
- | // 对于 1.20.5 之后的版本 | + | // 对于 1.20.5 之后,1.21.2 之前的版本 |
public static final class_1792 CUSTOM_ITEM = new class_1792(new class_1792.class_1793()); | public static final class_1792 CUSTOM_ITEM = new class_1792(new class_1792.class_1793()); | ||
[...] | [...] | ||
Line 22: | Line 25: | ||
我们创建了基本的物品,但是在 Minecraft 中还不存在,因为还没有注册。在 Minecraft 中,几乎所有东西都有注册表,物品也不例外。 | 我们创建了基本的物品,但是在 Minecraft 中还不存在,因为还没有注册。在 Minecraft 中,几乎所有东西都有注册表,物品也不例外。 | ||
- | 注册新的内容使用原版的注册表,基本语法为 ''< | + | 注册新的内容使用原版的注册表,基本语法为 ''< |
对于 1.21 之后的版本,'' | 对于 1.21 之后的版本,'' | ||
Line 44: | Line 47: | ||
在上面的代码中,简直创建了// | 在上面的代码中,简直创建了// | ||
- | 在这个例子中,创建一个 '' | + | 在这个例子中,创建一个 '' |
< | < | ||
Line 75: | Line 78: | ||
> **注意:**一些有经验的用户也可能会决定使用反射来自动注册一个类的所有静态字段。这也可以,但是请小心使用。 | > **注意:**一些有经验的用户也可能会决定使用反射来自动注册一个类的所有静态字段。这也可以,但是请小心使用。 | ||
+ | |||
+ | ===== 在 1.21.2+ 中创建物品 ===== | ||
+ | |||
+ | 从 1.21.2 开始,物品注册重写了。你需要把 '' | ||
+ | |||
+ | < | ||
+ | java.lang.NullPointerException: | ||
+ | </ | ||
+ | |||
+ | 要让它正常运行,就要像下面这样写: | ||
+ | |||
+ | <code java> | ||
+ | public final class TutorialItems { | ||
+ | private TutorialItems() { | ||
+ | } | ||
+ | |||
+ | public static final Item CUSTOM_ITEM = register(" | ||
+ | |||
+ | public static Item register(String path, Function< | ||
+ | final RegistryKey< | ||
+ | return Items.register(registryKey, | ||
+ | } | ||
+ | |||
+ | public static void initialize() { | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 在方法 '' | ||
===== 添加物品纹理 ===== | ===== 添加物品纹理 ===== | ||
Line 123: | Line 155: | ||
} | } | ||
+ | // 1.21.2 之前的版本,请这么写: | ||
@Override | @Override | ||
public class_1271< | public class_1271< | ||
user.method_5783(class_3417.field_14983, | user.method_5783(class_3417.field_14983, | ||
return class_1271.method_22427(playerEntity.method_5998(hand)); | return class_1271.method_22427(playerEntity.method_5998(hand)); | ||
+ | } | ||
+ | | ||
+ | // 1.21.2 以及之后的版本,请这么写: | ||
+ | @Override | ||
+ | public ActionResult use(World world, PlayerEntity user, Hand hand) { | ||
+ | user.playSound(SoundEvents.BLOCK_WOOL_BREAK, | ||
+ | return ActionResult.SUCCESS; | ||
} | } | ||
} | } | ||
Line 136: | Line 176: | ||
// 新物品的实例 | // 新物品的实例 | ||
- | public static final CustomItem CUSTOM_ITEM = new CustomItem(new | + | |
+ | // 对于 1.21.2 之前的版本: | ||
+ | | ||
+ | // 对于 1.21.2 之后的版本: | ||
+ | public static final CustomItem CUSTOM_ITEM = register(" | ||
[...] | [...] | ||
} | } | ||
Line 142: | Line 186: | ||
如果你正确执行了所有操作,则使用该物品现在应该会播放声音。 | 如果你正确执行了所有操作,则使用该物品现在应该会播放声音。 | ||
- | ===== 如果我想更改物品的堆叠大小怎么办? | + | ===== 物品组件 ===== |
+ | 有时你会想给物品添加一些默认的物品组件,例如最大堆叠数量或防火。可以通过调用 '' | ||
+ | |||
+ | 这个例子中,物品默认不可破坏,并隐藏关于这一点的物品提示: | ||
+ | < | ||
+ | // For versions below 1.21.2: | ||
+ | public static final CustomItem CUSTOM_ITEM | ||
+ | .component(DataComponentTypes.UNBREAKABLE, | ||
+ | // For versions since 1.21.2: | ||
+ | public static final Item CUSTOM_ITEM | ||
+ | .component(DataComponentTypes.UNBREAKABLE, | ||
+ | </ | ||
+ | |||
+ | 特别地,最大堆叠数可使用 '' | ||
- | 使用 '' | ||
< | < | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
- | + | | |
- | | + | |
- | public static final CustomItem CUSTOM_ITEM = new CustomItem(new class_1792.class_1793().maxCount(16)); | + | // For versions below 1.21.2: |
+ | public static final CustomItem CUSTOM_ITEM = register(" | ||
+ | // For versions since 1.21.2: | ||
+ | public static final Item CUSTOM_ITEM = register(" | ||
[...] | [...] | ||
} | } | ||
</ | </ | ||
+ | |||
===== 让物品能作为燃料或者可堆肥 ===== | ===== 让物品能作为燃料或者可堆肥 ===== | ||
如果需要让物品能作为燃料在熔炉中燃烧,可以使用 '' | 如果需要让物品能作为燃料在熔炉中燃烧,可以使用 '' | ||
+ | <code java> | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | [...] | ||
+ | | ||
+ | // 对于 1.21.2 之前的版本 | ||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | [...] | ||
+ | FuelRegistry.INSTANCE.add(CUSTOM_ITEM, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 然而,在实践中,你可能有许多要注册的物品,注册大量物品可能会耗费精力而且乱,所以可以考虑把代码放到单独的方法中,而不是上面的写法。 | ||
+ | |||
+ | 在 1.21.2 之前的版本,需要使用 Fabric API 的 '' | ||
+ | <code java> | ||
+ | public final class TutorialItems { | ||
+ | [...] | ||
+ | | ||
+ | // 对于 1.21.2 之前的版本 | ||
+ | public static void registerFuels() { | ||
+ | FuelRegistry.INSTANCE.add(CUSTOM_ITEM, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 而自从 1.21.2 之后,使用的则是 Fabric API 的 '' | ||
+ | <code java> | ||
+ | public final class TutorialItems { | ||
+ | [...] | ||
+ | | ||
+ | // 对于 1.21.2 之后的版本 | ||
+ | public static void registerFuels() { | ||
+ | FuelRegistryEvents.BUILD.register((builder, | ||
+ | // 可以一次在这个 lambda 中添加多个物品。 | ||
+ | builder.add(CUSTOM_ITEM, | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 然后在你的 '' | ||
<code java> | <code java> | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
Line 164: | Line 267: | ||
public void onInitialize() { | public void onInitialize() { | ||
[...] | [...] | ||
- | | + | |
} | } | ||
} | } | ||
</ | </ | ||
- | 类似地,你也可以使用 '' | + | 类似地,你也可以使用 '' |
===== 下一步 ===== | ===== 下一步 ===== | ||
试着[[zh_cn: | 试着[[zh_cn: |
zh_cn/tutorial/items.1724635492.txt.gz · Last modified: 2024/08/26 01:24 by solidblock