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/08/26 01:04] – solidblock | tutorial:items [2024/10/27 14:45] (current) – [Item components] solidblock | ||
---|---|---|---|
Line 8: | Line 8: | ||
===== Create an Item instance ===== | ===== Create an Item instance ===== | ||
+ | |||
+ | :!: 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 17: | 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 96: | Line 98: | ||
> **Note:** Some experienced users may decide to use reflection to automatically register 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 register all static fields of a class. This is also a preferable way, but should be used with caution. | ||
+ | |||
+ | ===== Creating Items in 1.21.2+ ===== | ||
+ | |||
+ | Since 1.21.2, the item registration is tutally rewrited. You have to store a '' | ||
+ | |||
+ | < | ||
+ | java.lang.NullPointerException: | ||
+ | </ | ||
+ | |||
+ | 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 ===== | ===== Adding model and textures ===== | ||
Line 138: | Line 170: | ||
} | } | ||
+ | // 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 152: | Line 192: | ||
// an instance of our new item | // an instance of our new item | ||
+ | | ||
+ | // For versions below 1.21.2: | ||
public static final CustomItem CUSTOM_ITEM = register(" | public static final CustomItem CUSTOM_ITEM = register(" | ||
+ | | ||
+ | // For versions since 1.21.2: | ||
+ | public static final CustomItem CUSTOM_ITEM = register(" | ||
[...] | [...] | ||
} | } | ||
Line 159: | Line 204: | ||
===== Item components ===== | ===== 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 '' | + | 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. | In this example, the item will be unbreakable by default, while hiding tooltips about it. | ||
< | < | ||
- | public static final CustomItem CUSTOM_ITEM = new CustomItem(new class_1792.class_1793().component(DataComponentTypes.UNBREAKABLE, | + | |
+ | | ||
+ | .component(DataComponentTypes.UNBREAKABLE, | ||
+ | // For versions since 1.21.2: | ||
+ | public static final Item CUSTOM_ITEM = register(" | ||
+ | | ||
</ | </ | ||
- | Specifically, | + | Specifically, |
< | < | ||
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 183: | Line 237: | ||
[...] | [...] | ||
| | ||
+ | // For versions below 1.21.2 | ||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
[...] | [...] | ||
- | FuelRegistry.INSTANCE.add(TutorialItems.CUSTOM_ITEM, | + | FuelRegistry.INSTANCE.add(TutorialItems.CUSTOM_ITEM, |
} | } | ||
} | } | ||
</ | </ | ||
- | In practice, when you have many items to register, as registering quantities of items may be effort-consuming and messy, you can consider placing | + | 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 |
+ | |||
+ | In versions below 1.21.2, you need to use Fabric API's '' | ||
<code java> | <code java> | ||
public final class TutorialItems { | public final class TutorialItems { | ||
[...] | [...] | ||
| | ||
+ | // For versions below 1.21.2 | ||
public static void registerFuels() { | public static void registerFuels() { | ||
- | FuelRegistry.INSTANCE.add(CUSTOM_ITEM, | + | 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 '' | And then refer to this method in your '' | ||
<code java> | <code java> |
tutorial/items.1724634264.txt.gz · Last modified: 2024/08/26 01:04 by solidblock