User Tools

Site Tools


drafts: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
drafts:items [2023/09/13 18:46] – removed - external edit (Unknown date) 127.0.0.1drafts:items [2024/06/30 00:02] (current) – ↷ Links adapted because of a move operation banana
Line 1: Line 1:
 +====== Adding an Item (SIMPLIFIED ENGLISH DRAFT) ======
  
 +**NOTE: This isn't really a serious draft of any kind. It was just made to demonstrate how an article would look like if translated to a simplified, controlled form of English (specifically, ASD-STE100 issue 8).**
 +
 +==== Introduction ====
 +
 +Adding a basic item is one of the first things to do when learning how to make a mod. To add an item, you must make an ''<yarn class_1792>'' object, add it to the register, and give it a texture. To add more behavior to the item, it is necessary to make a custom Item class.
 +
 +In all of the tutorials, the "tutorial" namespace is used as a placeholder. Replace this with your modid in your code. Names starting with "example" are also placeholders -- replace these with sensible names in your code.   
 +
 +==== Add the item to the register ====
 +
 +First, make an instance of <yarn class_1792> and store it as a ''static final'' constant. You must also pass in a ''<yarn class_1792>.<yarn class_1793>'' (or a ''FabricItemSettings'') instance. This is used to set the item's properties -- for example, its durability or its stack count. 
 +
 +<yarncode java [enable_line_numbers="true"]>
 +public class ExampleMod implements ModInitializer {
 +
 +    // an instance of our new item
 +    public static final class_1792 EXAMPLE_ITEM = new class_1792(new FabricItemSettings());
 +    [...]
 +}
 +</yarncode>
 +
 +Then, you must add the item to the item register with the "register" method. This method is part of the vanilla registry system.
 +Use the ''<yarn class_2378>#<yarn method_10230>()'' method with these arguments in this sequence:
 +
 +  * Registry Type, which is the register to add the item to
 +  * <yarn class_2960>, which is the identifier of the item
 +  * Content, which is the item's properties.
 +
 +You must call the method during initialization. The location of the method in your code is not important.
 +
 +The identifier argument is made of two strings -- the modid and the item's name. Pass in the constant you made in the first step as the content argument.
 +
 +<yarncode java [enable_line_numbers="true"]>
 +public class ExampleMod implements ModInitializer {
 +
 +    // an instance of our new item
 +    public static final class_1792 EXAMPLE_ITEM = new class_1792(new FabricItemSettings());
 +
 +    @Override
 +    public void onInitialize() {
 +        class_2378.method_10230(class_7923.field_41178, new class_2960("tutorial", "example_item"), EXAMPLE_ITEM);
 +    }
 +}
 +</yarncode>
 +Your have added an item to Minecraft! Run the run config ''Minecraft Client'' or the ''runClient'' Gradle task to see it in action. You can use the command ''/give @s tutorial:example_item'' to get the item immediately.
 +
 +{{:tutorial:2019-02-17_16.50.44.png?400|}}
 +
 +You can also call the method when you make the constant in the first step. This can simplify the code.
 +<yarncode java [enable_line_numbers="true"]>
 +public class ExampleMod implements ModInitializer {
 +
 +    // an instance of our new item
 +    public static final class_1792 EXAMPLE_ITEM =
 +      class_2378.method_10230(class_7923.field_41178, new class_2960("tutorial", "example_item"),
 +        new class_1792(new FabricItemSettings()));
 +
 +    @Override
 +    public void onInitialize() {
 +    }
 +}
 +</yarncode>
 +
 +==== Add a texture for the item ====
 +
 +To add a texture for the item, you must have an item model json file and a texture image. You must add these to your resource directory. The direct path of each is:
 +
 +    Item model: .../resources/assets/tutorial/models/item/example_item.json
 +    Item texture: .../resources/assets/tutorial/textures/item/example_item.png
 +
 +Our example texture can be found [[https://i.imgur.com/CqLSMEQ.png|here]].
 +
 +If you added your item to the register correctly in the first step, your game will send a warning to the log similar to this:
 +
 +    [Server-Worker-1/WARN]: Unable to load model: 'tutorial:example_item#inventory' referenced from: tutorial:example_item#inventory: java.io.FileNotFoundException: tutorial:models/item/example_item.json
 +It usefully tells you where the game expects your asset(s) to be found -- when you are not sure that something is correct, look at the log.
 +
 +A basic item model template is:
 +<code JavaScript>
 +{
 +  "parent": "item/generated",
 +  "textures": {
 +    "layer0": "tutorial:item/example_item"
 +  }
 +}
 +</code>
 +The parent of your item changes how the item is rendered when held in the hand. Use "item/generated" for items that are held from the bottom center of the texture. Use "item/handheld" for tools that are held from the bottom left of the texture. The textures object is used to add texture files. Put the path of your texture file as the value for "layer0" in the format "[modid]:item/[item_name]".
 +
 +When you have [[tutorial:hotswapping|applied the changes]], your item will look like this:
 +
 +{{:tutorial:item_texture.png?400|}}
 +
 +==== Creating a Custom Item class ====
 +
 +To add more behavior to the item, it is necessary to create a custom Item class. You must pass in an Item.Settings object in the constructor. 
 +<yarncode java [enable_line_numbers="true"]>
 +public class ExampleItem extends class_1792 {
 +
 +    public ExampleItem(class_1793 settings) {
 +        super(settings);
 +    }
 +}
 +</yarncode>
 +
 +A custom item class can do many things. For example, this class tells the item make a sound when it is used.
 +<yarncode java [enable_line_numbers="true"]>
 +public class ExampleItem extends class_1792 {
 +
 +    public ExampleItem(class_1793 settings) {
 +        super(settings);
 +    }
 +
 +    @Override
 +    public class_1271<class_1799> method_7836(class_1937 world, class_1657 playerEntity, class_1268 hand) {
 +        playerEntity.method_5783(class_3417.field_14983, 1.0F, 1.0F);
 +        return class_1271.method_22427(playerEntity.method_5998(hand));
 +    }
 +}
 +</yarncode>
 +
 +To add the changes to the game, replace the old <yarn class_1792> object with an instance of your new custom item class
 +<yarncode java [enable_line_numbers="true"]>
 +public class ExampleMod implements ModInitializer {
 +
 +    // an instance of our new item
 +    public static final ExampleItem EXAMPLE_ITEM = new ExampleItem(new FabricItemSettings());
 +    [...]
 +}
 +</yarncode>
 +If you completed all the steps correctly, the item will make a sound when it is used.
 +
 +==== Changing the stack size of the item ====
 +
 +To change the maximum size of a stack of that item, use use ''<yarn method_7889>(int size)'' inside ''FabricItemSettings''. You cannot specify a maximum stack size if your item is damageable. If you do, the game will throw a RuntimeException.
 +<yarncode java [enable_line_numbers="true"]>
 +public class ExampleMod implements ModInitializer {
 +
 +    // An instance of our new item, where the maximum stack size is 16
 +    public static final ExampleItem EXAMPLE_ITEM = new ExampleItem(new FabricItemSettings().method_7889(16));
 +    [...]
 +}
 +</yarncode>
 +
 +==== Make your item usable as a fuel or be compostable ====
 +
 +If you want to let the item be used as fuel in a furnace, you can add it to the ''FuelRegistry''.
 +<code java>
 +public class ExampleMod implements ModInitializer {
 +    [...]
 +    
 +    @Override
 +    public void onInitialize() {
 +        [...]
 +        FuelRegistry.INSTANCE.add(EXAMPLE_ITEM, 300)
 +    }
 +}
 +</code>
 +
 +You can also add it to the ''CompostingChanceRegistry'' to make it compostable in a composter.
 +==== Next Steps ====
 +[[tutorial:itemgroup|Add your item to your own ItemGroup]].