User Tools

Site Tools


zh_cn:tutorial:items

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
Next revisionBoth sides next revision
zh_cn:tutorial:items [2020/11/02 05:24] – part wun leocth2zh_cn:tutorial:items [2022/12/16 00:05] – [添加物品纹理] solidblock
Line 1: Line 1:
-====== 添加一个物品 ======+====== 添加物品 ======
  
 ==== 介绍 ==== ==== 介绍 ====
  
-添加一个基本的物品是编写模组的第一步。 你将需要创建一个''Item''对象,注册,并赋予它一个材质。要向物品添加其他行为,你将需要一个自定义的''Item''类。 在本教程以及以后的所有教程中,''tutorial''(教程)空间均用作你模组的ID的占位符如果你有单独的模组ID,请**一定**使用它+添加一个基本的物品是编写模组的第一步。你将需要创建一个 ''Item'' 对象,注册,并提供一个纹理。要向物品添加其他行为,你将需要一个自定义的 ''Item'' 类。在本教程以及以后的所有教程中,均使用 ''tutorial'' 作为命名空间。如果你有单独的模组 ID,那就直接使用它
 ==== 注册物品 ==== ==== 注册物品 ====
-首先,创建一个''Item''对象。我们将其存放在模组主类顶部。''Item''的构造方法接受一个''Item.Settings''对象,该对象用于设置物品属性,例如创造模式物品栏中的分类(物品组),耐久和堆叠数量。 +首先,创建一个 ''Item''实例,存储为静态常量字段。''Item'' 的构造方法接受一个 ''Item.Settings''(或 ''FabricItemSettings''对象,该对象用于设置物品属性,例如耐久和堆叠数量。 
-<code java [enable_line_numbers="true"]> +<yarncode java [enable_line_numbers="true"]> 
-public class ExampleMod implements ModInitializer +public class ExampleMod implements ModInitializer { 
-+    // 新物品的实例 
-    // 创建新物品对象 +    public static final class_1792 CUSTOM_ITEM = new class_1792(new FabricItemSettings());
-    public static final Item FABRIC_ITEM = new Item(new Item.Settings().group(ItemGroup.MISC));+
     [...]     [...]
 } }
-</code+</yarncode
-这里使用原版注册方式来注册,基本语法是''Registry#register(Registry TypeIdentifierContent)''Registry Type参数指Registry类型中预定义的静态字段,Identifier指资源文件位置Content则是您定义的实例(FABRIC_ITEM)。 +这里使用原版注册方式来注册,基本语法是 ''Registry#register(注册表类型ID内容)''注册表类型是存储在 ''Registries'' 或 ''Registry'' 对象中的静态字段,标识符用来给内容“加标签”内容则是您添加的东西一个实例。这可以随时调用,只要发生在初始化阶段。 
-<code java [enable_line_numbers="true"]> +<yarncode java [enable_line_numbers="true"]> 
-public class ExampleMod implements ModInitializer +public class ExampleMod implements ModInitializer { 
-+    // 新物品的实例 
-    // 创建新物品的实例 +    public static final class_1792 CUSTOM_ITEM = new class_1792(new FabricItemSettings()); 
-    public static final Item FABRIC_ITEM = new Item(new Item.Settings().group(ItemGroup.MISC)); +
-      +
     @Override     @Override
-    public void onInitialize() +    public void onInitialize() { 
-    +        class_2378.method_10230(class_7923.field_41178, new class_2960("tutorial", "custom_item"), CUSTOM_ITEM); 
-        Registry.register(Registry.ITEM, new Identifier("tutorial", "fabric_item"), FABRIC_ITEM); +    }
-    } +
 } }
-</code+</yarncode
-现在新物品已添加完毕,运行`runClientGradle任务以查看它的运行情况。+现在新物品已添加到 Minecraft 中,运行“Minecraft Client”运行配置或者 ''runClient'' Gradle任务以查看它的运行情况。 
 {{:tutorial:2019-02-17_16.50.44.png?400|}} {{:tutorial:2019-02-17_16.50.44.png?400|}}
  
-==== 添加物品材质 ====+为了简便,也可以像这样简化代码: 
 +<yarncode java [enable_line_numbers="true"]> 
 +public class ExampleMod implements ModInitializer {
  
-为物品注册材质需要物品模型.json文件和材质图像文件。 您将需要将它们添加到资源目录中。每个的直接路径是:+    // an instance of our new item 
 +    public static final class_1792 CUSTOM_ITEM = 
 +      class_2378.method_10230(class_7923.field_41178, new class_2960("tutorial", "custom_item"), 
 +        new class_1792(new FabricItemSettings()));
  
-    物品模型: //<根目录>///resources/assets/tutorial/models/item/fabric_item.json +    @Override 
-    物品材质: //<根目录>///resources/assets/tutorial/textures/item/fabric_item.png +    public void onInitialize() { 
-我们将使用[[https://i.imgur.com/CqLSMEQ.png|这个示例材质]]。+    } 
 +
 +</yarncode>
  
-如果您在第一步中正确注册了物品,则游戏将以类似于以下方式的方式抱怨缺少材质文件: +==== 添加物品纹理 ==== 
-<code> + 
-    ''[Server-Worker-1/WARN]: Unable to load model: 'tutorial:fabric_item#inventory' referenced from: tutorial:fabric_item#inventory: java.io.FileNotFoundException: tutorial:models/item/fabric_item.json'' +为物品注册纹理需要物品模型.json文件和纹理图像文件。 您将需要将它们添加到资源目录中。每个的直接路径是: 
-</code> + 
-    +    物品模型: <根目录>/resources/assets/tutorial/models/item/custom_item.json 
 +    物品纹理: <根目录>/resources/assets/tutorial/textures/item/custom_item.png 
 +我们将使用[[https://i.imgur.com/CqLSMEQ.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
 游戏能很方便地告诉你它想要的资源路径。遇事不决,日志解决。 游戏能很方便地告诉你它想要的资源路径。遇事不决,日志解决。
  
-一个非常简单的模 +一个非常简单的物品型长这个样子:
-A basic item model template is:+
 <code JavaScript> <code JavaScript>
 { {
   "parent": "item/generated",   "parent": "item/generated",
   "textures": {   "textures": {
-    "layer0": "tutorial:item/fabric_item"+    "layer0": "tutorial:item/custom_item"
   }   }
 } }
 </code> </code>
-物品项会更改其在手中的呈现方式并且库存中的冻结物品有用。 “物品/手持”用于从纹理的左下角握住的工具。 textures / layer0是图像文件的位置。+物品模型会将所有属性继承自模型例如工具、方块等物品十分有用的自定义手持模型(''item/handheld''''textures/layer0'' 纹理图像文件的位置。
  
-最终纹理结果:+最终纹理结果:
  
 {{:tutorial:item_texture.png?400|}} {{:tutorial:item_texture.png?400|}}
  
-==== 创建item类 ==== +==== 创建物品类 ==== 
-要为物品添加功能/用处/特性,需要创建一个item其默认构造函数需要Item.Settings对象。+要为物品添加自定义行为需要创建一个物品其默认构造方法需要一个Item.Settings对象。
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-public class FabricItem extends Item +public class FabricItem extends Item { 
-+ 
-    public FabricItem(Settings settings) +    public FabricItem(Settings settings) {
-    {+
         super(settings);         super(settings);
     }     }
 } }
 </code> </code>
-自定义Item类的一个实际用例是使该物品在您单击时播放声音:+自定义物品类的一个实际用例是使该物品在击时播放声音:
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-public class FabricItem extends Item +public class FabricItem extends Item { 
-+ 
-    public FabricItem(Settings settings) +    public FabricItem(Settings settings) {
-    {+
         super(settings);         super(settings);
     }     }
              
     @Override     @Override
-    public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) +    public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
-    {+
         playerEntity.playSound(SoundEvents.BLOCK_WOOL_BREAK, 1.0F, 1.0F);         playerEntity.playSound(SoundEvents.BLOCK_WOOL_BREAK, 1.0F, 1.0F);
-        return new TypedActionResult<>(ActionResult.SUCCESS, playerEntity.getStackInHand(hand));+        return TypedActionResult.success(playerEntity.getStackInHand(hand));
     }     }
 } }
Line 93: Line 100:
 用新物品类的实例替换旧的Item对象: 用新物品类的实例替换旧的Item对象:
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-public class ExampleMod implements ModInitializer +public class ExampleMod implements ModInitializer {
-{+
     //创建新item的实例     //创建新item的实例
-    public static final FabricItem FABRIC_ITEM = new FabricItem(new Item.Settings().group(ItemGroup.MISC));+    public static final FabricItem FABRIC_ITEM = new FabricItem(new FabricItemSettings().group(ItemGroup.MISC));
     [...]     [...]
 } }
 </code> </code>
-如果正确执行了所有操作,则使用该物品现在应该会播放声音.+如果正确执行了所有操作,则使用该物品现在应该会播放声音
  
 ==== 如果我想更改物品的堆叠大小怎么办? ==== ==== 如果我想更改物品的堆叠大小怎么办? ====
  
-使用maxCount(int size)来设置最大堆叠数。请注意,如果的物品是有耐久的(及耐久归零后会被破坏),那么此物品无法设置最大堆叠数,否则游戏将抛出RuntimeException。+使用''FabricItemSettings''内的''maxCount(int size)''指定最大堆叠数。请注意,如果的物品是有耐久的(及耐久归零后会被破坏),那么此物品无法设置最大堆叠数,否则游戏将抛出运行时错误(''RuntimeException''
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-public class ExampleMod implements ModInitializer +public class ExampleMod implements ModInitializer { 
-{+
     //我们新物品的实例,最大堆叠大小为16     //我们新物品的实例,最大堆叠大小为16
-    public static final FabricItem FABRIC_ITEM = new FabricItem(new Item.Settings().group(ItemGroup.MISC).maxCount(16));+    public static final FabricItem FABRIC_ITEM = new FabricItem(new FabricItemSettings().group(ItemGroup.MISC).maxCount(16));
     [...]     [...]
 } }
 </code> </code>
-==== Next Steps ==== +==== 下一步 ==== 
-[[tutorial:itemgroup|Add your item to your own ItemGroup]].+试着[[zh_cn:tutorial:itemgroup|将你的物品添加到一个物品组中]]
zh_cn/tutorial/items.txt · Last modified: 2024/04/15 01:25 by solidblock