tutorial:items
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:items [2022/04/29 09:34] – [What if I want to change the stack size of my item?] solidblock | tutorial: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 ''< | Adding a basic item is one of the first steps in modding. You're going to need to create an ''< | ||
+ | |||
==== Registering an Item ==== | ==== Registering an Item ==== | ||
- | First, create an instance of <yarn class_1792> | + | First, create an instance of '' |
< | < | ||
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 | + | |
+ | | ||
+ | // for versions since 1.20.5 | ||
+ | public static final class_1792 CUSTOM_ITEM = new class_1792(new class_1792.class_1793()); | ||
[...] | [...] | ||
} | } | ||
</ | </ | ||
- | You'll use the vanilla registry system for registering new content. The basic syntax is ''< | + | |
+ | You'll use the vanilla registry system for registering new content. The basic syntax is ''< | ||
< | < | ||
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 | + | public static final class_1792 |
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
- | class_2378.method_10230(class_2378.field_11142, new class_2960(" | + | class_2378.method_10230(class_7923.field_41178, new class_2960(" |
} | } | ||
} | } | ||
</ | </ | ||
- | Your new item has now been added to Minecraft. Run the '' | + | Your new item has now been added to Minecraft. Run the run config '' |
{{: | {{: | ||
+ | |||
+ | For simplicity, you can simplify your code as following: | ||
+ | < | ||
+ | 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_1792(new class_1792.class_1793())); | ||
+ | |||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | } | ||
+ | } | ||
+ | </ | ||
==== 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: .../ | + | Item model: .../ |
- | Item texture: .../ | + | Item texture: .../ |
Our example texture can be found [[https:// | Our example texture can be found [[https:// | ||
Line 43: | Line 65: | ||
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: | 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: | ||
- | [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. | It conveniently tells you exactly where it expects your asset[s] to be found-- when in doubt, check the log. | ||
Line 51: | Line 73: | ||
" | " | ||
" | " | ||
- | " | + | " |
} | } | ||
} | } | ||
Line 65: | Line 87: | ||
To add additional behavior to the item you will need to create an Item class. The default constructor requires an Item.Settings object. | To add additional behavior to the item you will need to create an Item class. The default constructor requires an Item.Settings object. | ||
< | < | ||
- | public class FabricItem | + | public class CustomItem |
- | public | + | public |
super(settings); | super(settings); | ||
} | } | ||
Line 75: | Line 97: | ||
A practical use-case for a custom item class would be making the item play a sound when you use it: | A practical use-case for a custom item class would be making the item play a sound when you use it: | ||
< | < | ||
- | public class FabricItem | + | public class CustomItem |
- | public | + | public |
super(settings); | super(settings); | ||
} | } | ||
@Override | @Override | ||
- | public class_1271< | + | public class_1271< |
- | | + | |
- | 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 FabricItem FABRIC_ITEM | + | public static final CustomItem CUSTOM_ITEM |
[...] | [...] | ||
} | } | ||
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 '' | + | 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 FabricItem FABRIC_ITEM | + | public static final CustomItem CUSTOM_ITEM |
[...] | [...] | ||
} | } | ||
Line 122: | Line 144: | ||
public void onInitialize() { | public void onInitialize() { | ||
[...] | [...] | ||
- | FuelRegistry.INSTANCE.add(FABRIC_ITEM, 300) | + | FuelRegistry.INSTANCE.add(CUSTOM_ITEM, 300) |
} | } | ||
} | } |
tutorial/items.1651224854.txt.gz · Last modified: 2022/04/29 09:34 by solidblock