tutorial:items
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:items [2024/07/02 23:31] – [Make your item become fuel, or compostable] solidblock | tutorial:items [2024/10/27 14:45] (current) – [Item components] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~REDIRECT> | ||
+ | |||
====== Adding an Item ====== | ====== Adding an Item ====== | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Adding a basic item is one of the first steps in modding. You're going to need to create an ''< | + | Adding a basic item is one of the first steps in modding. You're going to need to create an ''< |
+ | |||
+ | ===== Create an Item instance ===== | ||
- | ===== Registering an Item ===== | + | :!: If you're in version 1.21.2 or above, please directly read [[#Creating Items in 1.21.2+]]. |
First, create an instance of ''< | First, create an instance of ''< | ||
Line 15: | Line 19: | ||
// for versions below 1.20.4 | // for versions below 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()); | ||
- | // for versions since 1.20.5 | + | // for versions since 1.20.5, below 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 21: | Line 25: | ||
</ | </ | ||
- | You'll use the vanilla registry system for registering new content. The basic syntax is ''< | + | ===== Register |
- | For versions since 1.21, an '' | + | We've create a basic item, but it still does not exist in Minecraft, because it has not been registered. In Minecraft, almost everything has an registry, and items are no exceptions. |
+ | |||
+ | You'll use the vanilla registry system for registering new content. The basic syntax is ''< | ||
+ | |||
+ | For versions since 1.21, an '' | ||
< | < | ||
Line 39: | Line 47: | ||
</ | </ | ||
Your new item has now been added to Minecraft. Run the run config '' | Your new item has now been added to Minecraft. Run the run config '' | ||
- | |||
- | {{: | ||
For more simplicity, you can simplify your code by directly registering them when assigning the fields, as '' | For more simplicity, you can simplify your code by directly registering them when assigning the fields, as '' | ||
Line 59: | Line 65: | ||
===== Best practice of registering items ===== | ===== Best practice of registering items ===== | ||
- | In the code above, you simply created //one// item. However, it is not convenient if you have //many// items, as you need to register and create an '' | + | In the code above, you simply created //one// item. However, it is not convenient if you have //many// items in your mod, as you need to register and create an '' |
+ | |||
+ | In this example, create a '' | ||
< | < | ||
Line 67: | Line 75: | ||
| | ||
// an instance of our new item | // an instance of our new item | ||
- | public static final class_1792 CUSTOM_ITEM = register(new class_1792(new class_1792.class_1793()), " | + | public static final class_1792 CUSTOM_ITEM = register(" |
- | public static Item register(Item instance, | + | public static |
// For versions below 1.21, please replace '' | // For versions below 1.21, please replace '' | ||
return class_2378.method_10230(class_7923.field_41178, | return class_2378.method_10230(class_7923.field_41178, | ||
Line 79: | Line 87: | ||
</ | </ | ||
- | Remember to refer to some method | + | Remember to refer to some methods or fields in the mods initializer |
< | < | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
Line 89: | Line 97: | ||
</ | </ | ||
- | > **Note:** Some experienced users may decide to use reflection to automatically all static fields of a class. This is also a preferable way, but should be used with caution. | + | > **Note:** Some experienced users may decide to use reflection to automatically |
- | ===== 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: | + | ===== Creating Items in 1.21.2+ ===== |
- | Item model: | + | Since 1.21.2, the item registration is tutally rewrited. You have to store a '' |
- | | + | |
- | Our example texture can be found [[https://i.imgur.com/CqLSMEQ.png|here]]. | + | < |
+ | java.lang.NullPointerException: | ||
+ | </code> | ||
- | If you registered your item properly in the first step, your game will complain about a missing texture file in a fashion similar to this: | + | To make it run correctly, you should write like below: |
+ | |||
+ | <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() { | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In the method '' | ||
+ | |||
+ | ===== Adding model and textures ===== | ||
+ | |||
+ | If you registered your item properly in the first step, you can successfully get your item by typing command ''/ | ||
[Server-Worker-1/ | [Server-Worker-1/ | ||
- | It conveniently tells you exactly where it expects your asset[s] to be found-- when in doubt, check the log. | + | |
+ | That's because we haven' | ||
+ | |||
+ | * Item model: '' | ||
+ | * Item texture: '' | ||
+ | |||
+ | Our example texture can be found [[https:// | ||
A basic item model template is: | A basic item model template is: | ||
Line 113: | Line 150: | ||
} | } | ||
</ | </ | ||
- | The parent of your item changes how it's rendered in the hand and comes in useful for things like block items in the inventory. | + | The '' |
- | + | ||
- | Final textured result: | + | |
- | + | ||
- | {{: | + | |
===== Creating an Item class ===== | ===== Creating an Item class ===== | ||
- | To add additional behavior to the item you will need to create an Item class. The default constructor requires an '' | + | You have created a simple |
< | < | ||
public class CustomItem extends class_1792 { | public class CustomItem extends class_1792 { | ||
- | |||
public CustomItem(class_1793 settings) { | public CustomItem(class_1793 settings) { | ||
super(settings); | super(settings); | ||
Line 134: | Line 166: | ||
< | < | ||
public class CustomItem extends class_1792 { | public class CustomItem extends class_1792 { | ||
- | |||
public CustomItem(class_1793 settings) { | public CustomItem(class_1793 settings) { | ||
super(settings); | super(settings); | ||
} | } | ||
+ | // write this if the version is below 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(user.method_5998(hand)); | return class_1271.method_22427(user.method_5998(hand)); | ||
+ | } | ||
+ | | ||
+ | // write this if the version is 1.21.2 or higher: | ||
+ | @Override | ||
+ | public ActionResult use(World world, PlayerEntity user, Hand hand) { | ||
+ | user.playSound(SoundEvents.BLOCK_WOOL_BREAK, | ||
+ | return ActionResult.SUCCESS; | ||
} | } | ||
} | } | ||
Line 150: | Line 189: | ||
< | < | ||
public final class TutorialItems { | public final class TutorialItems { | ||
- | |||
[...] | [...] | ||
// an instance of our new item | // an instance of our new item | ||
- | public static final CustomItem CUSTOM_ITEM = new CustomItem(new class_1792.class_1793()); | + | |
+ | // For versions below 1.21.2: | ||
+ | | ||
+ | |||
+ | // For versions since 1.21.2: | ||
+ | public static final CustomItem CUSTOM_ITEM = register(" | ||
[...] | [...] | ||
} | } | ||
</ | </ | ||
- | If you did everything correctly, using the item should now play a sound. | + | If you did everything correctly, using the item should now play a sound. You can also [[tooltip|add some tooltips]] for the item based on your item class. |
- | ===== What if I want to change | + | ===== Item components ===== |
+ | Sometimes you may need to add some default components for the item, such as max stack size or fire durability. This can be done by calling '' | ||
+ | |||
+ | In this example, the item will be unbreakable by default, while hiding tooltips about it. | ||
+ | < | ||
+ | // 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, | ||
+ | </ | ||
+ | |||
+ | Specifically, | ||
- | For this you would use '' | ||
< | < | ||
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 class_1792.class_1793().maxCount(16)); | + | |
+ | // For versions below 1.21.2: | ||
+ | | ||
+ | // For versions since 1.21.2: | ||
+ | public static final Item CUSTOM_ITEM = register(" | ||
[...] | [...] | ||
} | } | ||
Line 175: | Line 233: | ||
If you want to make it a fuel so that it can be used in a furnace, you can register in '' | If you want to make it a fuel so that it can be used in a furnace, you can register in '' | ||
+ | <code java> | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | [...] | ||
+ | | ||
+ | // For versions below 1.21.2 | ||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | [...] | ||
+ | FuelRegistry.INSTANCE.add(TutorialItems.CUSTOM_ITEM, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | However, in practice, when you have many items to register, as registering quantities of items may be effort-consuming and messy, you can consider placing the codes in a separate method, instead of writing like above. | ||
+ | |||
+ | In versions below 1.21.2, you need to use Fabric API's '' | ||
+ | <code java> | ||
+ | public final class TutorialItems { | ||
+ | [...] | ||
+ | | ||
+ | // For versions below 1.21.2 | ||
+ | public static void registerFuels() { | ||
+ | FuelRegistry.INSTANCE.add(CUSTOM_ITEM, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In versions since 1.21.2, use Fabric API's '' | ||
+ | <code java> | ||
+ | public final class TutorialItems { | ||
+ | [...] | ||
+ | | ||
+ | // For versions since 1.21.2 | ||
+ | public static void registerFuels() { | ||
+ | FuelRegistryEvents.BUILD.register((builder, | ||
+ | // You can add multiple items at once in this lambda. | ||
+ | builder.add(CUSTOM_ITEM, | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | And then refer to this method in your '' | ||
<code java> | <code java> | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
Line 182: | Line 283: | ||
public void onInitialize() { | public void onInitialize() { | ||
[...] | [...] | ||
- | | + | |
} | } | ||
} | } | ||
Line 188: | Line 289: | ||
Similarly, you can use a '' | Similarly, you can use a '' | ||
- | ==== Next Steps ==== | + | ===== Next Steps ===== |
[[tutorial: | [[tutorial: |
tutorial/items.1719963085.txt.gz · Last modified: 2024/07/02 23:31 by solidblock