User Tools

Site Tools


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
tutorial:items [2022/07/23 18:06] – Corrected parentheses in maxCount example. darthjaketutorial:items [2024/04/20 08:05] (current) – [Creating an Item class] ryhon
Line 4: Line 4:
  
 Adding a basic item is one of the first steps in modding. You're going to need to create an ''<yarn class_1792>'' object, register it, and give it a texture. To add additional behavior to the item you will need a custom <yarn class_1792> class. In this tutorial and all future ones, the “tutorial” namespace is used as a placeholder. If you have a separate modid, feel free to use it instead. Adding a basic item is one of the first steps in modding. You're going to need to create an ''<yarn class_1792>'' object, register it, and give it a texture. To add additional behavior to the item you will need a custom <yarn class_1792> class. In this tutorial and all future ones, the “tutorial” namespace is used as a placeholder. If you have a separate modid, feel free to use it instead.
 +
 ==== Registering an Item ==== ==== Registering an Item ====
  
-First, create an instance of <yarn class_1792>. We'll store it at the top of our initializer class. The constructor takes in an ''<yarn class_1792>.<yarn class_1793>'' (or a ''FabricItemSettings'') instance, which is used to set item properties such as the inventory category, durability, and stack count. +First, create an instance of ''<yarn class_1792>'' and store it as a static final field. The constructor takes in an ''<yarn class_1792>.<yarn class_1793>'' (or a ''FabricItemSettings'' unless versions since 1.20.5) instance, which is used to set item properties such as the durability, and stack count.  
 <yarncode java [enable_line_numbers="true"]> <yarncode java [enable_line_numbers="true"]>
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
  
     // an instance of our new item     // an instance of our new item
-    public static final class_1792 CUSTOM_ITEM = new class_1792(new FabricItemSettings().method_7892(class_1761.field_7932));+    // for versions below 1.20.4 
 +    public static final class_1792 CUSTOM_ITEM = new class_1792(new FabricItemSettings()); 
 +    // for versions since 1.20.5 
 +    public static final class_1792 CUSTOM_ITEM = new class_1792(new class_1792.class_1793());
     [...]     [...]
 } }
 </yarncode> </yarncode>
-You'll use the vanilla registry system for registering new content. The basic syntax is ''<yarn class_2378>#<yarn method_10230>(Registry Type, <yarn class_2960>, Content)''. Registry types are stored as static fields in the ''<yarn class_2378>'' class, and the identifier is what labels your content. Content is an instance of whatever you're adding. This can be called anywhere as long as it occurs during initialization.+ 
 +You'll use the vanilla registry system for registering new content. The basic syntax is ''<yarn class_2378>#<yarn method_10230>(Registry Type, <yarn class_2960>, Content)''. Registry types are stored as static fields in the ''<yarn class_7923>'' or ''<yarn class_2378>'' class, and the identifier is what labels your content. Content is an instance of whatever you're adding. This can be called anywhere as long as it occurs during initialization. 
 <yarncode java [enable_line_numbers="true"]> <yarncode java [enable_line_numbers="true"]>
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
  
     // an instance of our new item     // an instance of our new item
-    public static final class_1792 CUSTOM_ITEM = new class_1792(new FabricItemSettings().method_7892(class_1761.field_7932));+    public static final class_1792 CUSTOM_ITEM = new class_1792(new class_1792.class_1793());
  
     @Override     @Override
     public void onInitialize() {     public void onInitialize() {
-        class_2378.method_10230(class_2378.field_11142, new class_2960("tutorial", "custom_item"), CUSTOM_ITEM);+        class_2378.method_10230(class_7923.field_41178, new class_2960("tutorial", "custom_item"), CUSTOM_ITEM);
     }     }
 } }
 </yarncode> </yarncode>
-Your new item has now been added to Minecraft. Run the ''runClient'' Gradle task to see it in action.+Your new item has now been added to Minecraft. Run the run config ''Minecraft Client'' or ''runClient'' Gradle task to see it in action, execute the command ''/give @s tutorial:custom_item'' in game.
  
 {{:tutorial:2019-02-17_16.50.44.png?400|}} {{:tutorial:2019-02-17_16.50.44.png?400|}}
 +
 +For simplicity, you can simplify your code as following:
 +<yarncode java [enable_line_numbers="true"]>
 +public class ExampleMod implements ModInitializer {
 +
 +    // 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 class_1792.class_1793()));
 +
 +    @Override
 +    public void onInitialize() {
 +    }
 +}
 +</yarncode>
  
 ==== Adding Item textures ==== ==== Adding Item textures ====
  
-Registering a texture for an item requires an item model .json file and a texture image. You're going to need to add these to your resource directory. The direct path of each is:+Registering a texture for an item requires an item model json file and a texture image. You're going to need to add these to your resource directory. The direct path of each is:
  
     Item model: .../resources/assets/tutorial/models/item/custom_item.json     Item model: .../resources/assets/tutorial/models/item/custom_item.json
Line 82: Line 104:
  
     @Override     @Override
-    public class_1271<class_1799> method_7836(class_1937 world, class_1657 playerEntity, class_1268 hand) { +    public class_1271<class_1799> method_7836(class_1937 world, class_1657 user, class_1268 hand) { 
-        playerEntity.method_5783(class_3417.field_14983, 1.0F, 1.0F); +        user.method_5783(class_3417.field_14983, 1.0F, 1.0F); 
-        return class_1271.method_22427(playerEntity.method_5998(hand));+        return class_1271.method_22427(user.method_5998(hand));
     }     }
 } }
Line 94: Line 116:
  
     // an instance of our new item     // an instance of our new item
-    public static final CustomItem CUSTOM_ITEM = new CustomItem(new FabricItemSettings().method_7892(class_1761.field_7932));+    public static final CustomItem CUSTOM_ITEM = new CustomItem(new class_1792.class_1793());
     [...]     [...]
 } }
Line 102: Line 124:
 ==== What if I want to change the stack size of my item? ==== ==== What if I want to change the stack size of my item? ====
  
-For this you would use ''<yarn method_7889>(int size)'' inside ''FabricItemSettings'' to specify the max stack size. Note that if your item is damageable you cannot specify a maximum stack size or the game will throw a RuntimeException.+For this you would use ''maxCount(int size)'' inside ''Item.Settings'' to specify the max stack size. Note that if your item is damageable you cannot specify a maximum stack size or the game will throw a RuntimeException.
 <yarncode java [enable_line_numbers="true"]> <yarncode java [enable_line_numbers="true"]>
 public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
  
     // An instance of our new item, where the maximum stack size is 16     // An instance of our new item, where the maximum stack size is 16
-    public static final CustomItem CUSTOM_ITEM = new CustomItem(new FabricItemSettings().method_7892(class_1761.field_7932).method_7889(16));+    public static final CustomItem CUSTOM_ITEM = new CustomItem(new class_1792.class_1793().maxCount(16));
     [...]     [...]
 } }
tutorial/items.1658599583.txt.gz · Last modified: 2022/07/23 18:06 by darthjake