User Tools

Site Tools


zh_cn:tutorial:items

This is an old revision of the document!


添加物品

介绍

添加一个基本的物品是编写模组的第一步。你将需要创建一个 Item 对象,注册,并提供一个纹理。要向物品添加其他行为,你将需要一个自定义的 Item 类。在本教程以及以后的所有教程中,均使用 tutorial 作为命名空间。如果你有单独的模组 ID,那就直接使用它。

注册物品

首先,创建一个 Item 的实例,存储为静态常量字段。Item 的构造方法接受一个 Item.Settings(或 FabricItemSettings)对象,该对象用于设置物品属性,例如耐久和堆叠数量。

  1. public class ExampleMod implements ModInitializer {
  2. // 新物品的实例
  3. public static final Item CUSTOM_ITEM = new Item(new FabricItemSettings());
  4. [...]
  5. }
这里使用原版注册方式来注册,基本语法是 Registry#register(注册表类型, ID, 内容),注册表类型是存储在 RegistriesRegistry 对象中的静态字段,标识符用来给内容“加标签”。内容则是您添加的东西的一个实例。这可以随时调用,只要发生在初始化阶段。
  1. public class ExampleMod implements ModInitializer {
  2. // 新物品的实例
  3. public static final Item CUSTOM_ITEM = new Item(new FabricItemSettings());
  4.  
  5. @Override
  6. public void onInitialize() {
  7. Registry.register(Registries.ITEM, new Identifier("tutorial", "custom_item"), CUSTOM_ITEM);
  8. }
  9. }
现在新物品已添加到 Minecraft 中,运行“Minecraft Client”运行配置或者 runClient Gradle任务以查看它的运行情况,在游戏内执行命令 /​give @s tutorial:​custom_item​。

为了简便,也可以像这样简化代码:

  1. public class ExampleMod implements ModInitializer {
  2.  
  3. // an instance of our new item
  4. public static final Item CUSTOM_ITEM =
  5. Registry.register(Registries.ITEM, new Identifier("tutorial", "custom_item"),
  6. new Item(new FabricItemSettings()));
  7.  
  8. @Override
  9. public void onInitialize() {
  10. }
  11. }

添加物品纹理

为物品注册纹理需要物品模型.json文件和纹理图像文件。 您将需要将它们添加到资源目录中。每个的直接路径是:

  物品模型: <根目录>/resources/assets/tutorial/models/item/custom_item.json
  物品纹理: <根目录>/resources/assets/tutorial/textures/item/custom_item.png

我们将使用这个示例纹理

如果您在第一步中正确注册了物品,则游戏将以类似于以下方式的方式抱怨缺少纹理文件:

  [Server-Worker-1/WARN]: Unable to load model: 'tutorial:custom#inventory' referenced from: tutorial:custom_item#inventory: java.io.FileNotFoundException: tutorial:models/item/custom_item.json

游戏能很方便地告诉你它想要的资源路径。遇事不决,日志解决。

一个非常简单的物品模型长这个样子:

{
  "parent": "item/generated",
  "textures": {
    "layer0": "tutorial:item/custom_item"
  }
}

物品模型会将所有属性继承自父模型,例如对工具、方块等物品十分有用的自定义手持模型(item/handheld)。textures/layer0 是纹理图像文件的位置。

最终纹理的结果:

创建物品类

要为物品添加自定义行为,则需要创建一个物品类。其默认的构造方法需要一个 Item.Settings 对象。

  1. public class CustomItem extends Item {
  2.  
  3. public CustomItem(Settings settings) {
  4. super(settings);
  5. }
  6. }
自定义物品类的一个实际用例是使该物品在右击时播放声音:
  1. public class CustomItem extends Item {
  2.  
  3. public CustomItem(Settings settings) {
  4. super(settings);
  5. }
  6.  
  7. @Override
  8. public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
  9. playerEntity.playSound(SoundEvents.BLOCK_WOOL_BREAK, 1.0F, 1.0F);
  10. return TypedActionResult.success(playerEntity.getStackInHand(hand));
  11. }
  12. }

用新物品类的实例替换旧的 Item 对象:

  1. public class ExampleMod implements ModInitializer {
  2.  
  3. // 新物品的实例
  4. public static final CustomItem CUSTOM_ITEM = new CustomItem(new FabricItemSettings());
  5. [...]
  6. }
如果你正确执行了所有操作,则使用该物品现在应该会播放声音。

如果我想更改物品的堆叠大小怎么办?

使用 FabricItemSettings 内的 maxCount(int size) 来指定最大堆叠数。请注意,如果你的物品是有耐久的(及耐久归零后会被破坏),那么此物品无法设置最大堆叠数,否则游戏将抛出 RuntimeException。

  1. public class ExampleMod implements ModInitializer {
  2.  
  3. // 我们新物品的实例,最大堆叠大小为16
  4. public static final CustomItem CUSTOM_ITEM = new CustomItem(new FabricItemSettings().method_7889(16));
  5. [...]
  6. }

让物品能作为燃料或者可堆肥

如果需要让物品能作为燃料在熔炉中燃烧,可以使用 FuelRegistry,例如:

public class ExampleMod implements ModInitializer {
    [...]
 
    @Override
    public void onInitialize() {
        [...]
        FuelRegistry.INSTANCE.add(CUSTOM_ITEM, 300)
    }
}

类似地,你也可以使用 CompostingChanceRegistry 来让它可以在堆肥桶中堆肥。

下一步

zh_cn/tutorial/items.1671438661.txt.gz · Last modified: 2022/12/19 08:31 by solidblock