diff --git a/wiki/items/first-item/en.md b/wiki/items/first-item/en.md index b3b8e2f6..c008b5cf 100644 --- a/wiki/items/first-item/en.md +++ b/wiki/items/first-item/en.md @@ -10,12 +10,18 @@ The first thing we need to do is register the item so that the game knows to add --- -First, we need to declare an instance of `net.minecraft.item.Item` with the parameters for our item. +First, we need to set up a place for our mod's items. Make a new class called `ExampleModItems`, replacing `ExampleMod` with your mod's name, in the same package as your main class. -In theory, we could do this directly in the registration line but having a separate variable allows us to reference it elsewhere for other purposes. +Next, we'll declare an instance of `net.minecraft.item.Item` with the parameters for our item. + +In theory, we could skip this step and declare the item as we register it, but having a separate variable allows us to reference it elsewhere for other purposes. + +`src/main/com/example/example_mod/ExampleModItems`: ```java -public static final Item EXAMPLE_ITEM = new Item(new QuiltItemSettings()); +public class ExampleModItems { + public static final Item EXAMPLE_ITEM = new Item(new QuiltItemSettings()); +} ``` Here, the `public static final` ensures that we can access the item elsewhere but can't reassign the variable itself, making sure that we don't accidentally alter it somewhere else. @@ -24,10 +30,17 @@ Our new instance of `Item` takes in an instance of `QuiltItemSettings` as an arg --- -Now that we've declared the item, we need to tell the game registry to put it into the game. We do so by putting this into the mod's `ModInitializer` ([Read more about mod initializers here](../concepts/sideness#on-mod-initializers)) in the `onInitialize` method: +Now that we've declared the item, we need to register it in order to put it into the game. We're going to set up a new method in your items class to hold the registrations for all your items. + +`src/main/com/example/example_mod/ExampleModItems`: ```java -Registry.register(Registries.ITEM, new Identifier(mod.metadata().id(), "example_item"), EXAMPLE_ITEM); +public class ExampleModItems { + // ... + public static void register() { + Registry.register(Registries.ITEM, new Identifier(mod.metadata().id(), "example_item"), EXAMPLE_ITEM); + } +} ``` `Registry.register()` takes three parameters: @@ -36,6 +49,22 @@ Registry.register(Registries.ITEM, new Identifier(mod.metadata().id(), "example_ - The `Identifier` used for the item. This must be unique. The first part is the namespace (which should be the mod id) and the item name itself. Only lowercase letters, numbers, underscores, dashes, periods, and slashes are allowed. To convert the item's name into this format, write everything in lowercase and separate words using underscores. - The `Item` to register. Here, we pass in the item we declared earlier. +Finally, we need to make sure that the `register()` method is called as the game boots up. +We do so by putting this into the mod's [`ModInitializer`](../concepts/sideness#on-mod-initializers) in the `onInitialize` method: + +`src/main/com/example/example_mod/ExampleMod`: + +```java +public class ExampleMod implements ModInitializer { + // ... + @Override + public void onInitialize(ModContainer mod) { + // ... + ExampleModItems.register(); + } +} +``` + Having done all of this, if we run the game we can see that we can give the item using the give command: `/give @s simple_item_mod:example_item`! But it doesn't appear in the creative menu, nor does it have a texture, and its name isn't translated properly. How do we fix this? ## Adding the Item to a Group @@ -43,10 +72,18 @@ Having done all of this, if we run the game we can see that we can give the item `ItemGroup`s represent the tabs in the creative menu. Because of a change in 1.19.3, you can't add items to item groups using only [Quilt Standard Libraries](../concepts/qsl-qfapi#quilt-standard-libraries) (From now on QSL). However, [Fabric API](../concepts/qsl-qfapi#fabric-api) has an API for it. Thanks to [Quilted Fabric API](../concepts/qsl-qfapi#quilted-fabric-api), which the template mod includes and users download with QSL, we can use it on Quilt, too: +`src/main/com/example/example_mod/ExampleModItems`: + ```java -ItemGroupEvents.modifyEntriesEvent(ItemGroups.INGREDIENTS).register(entries -> { - entries.addItem(EXAMPLE_ITEM); -}); +public class ExampleModItems { + // ... + public static void register() { + //... + ItemGroupEvents.modifyEntriesEvent(ItemGroups.INGREDIENTS).register(entries -> { + entries.addItem(EXAMPLE_ITEM); + }); + } +} ``` Here we are using the `ItemGroupEvents` API. We get the [event](../concepts/events) for modifying the `INGREDIENTS` item group and register a new listener. [Events](../concepts/events) have two main use cases. On the one side you can use them to do things when something happens, for example when block gets broken or even simply at each tick. On the other side they can be used for modifying things like item groups, where ordering is important. In this case, however, we are doing nothing complicated and simply adding the item to the end of the ingredients item group. This will also add them to the creative menu search. @@ -73,6 +110,8 @@ The texture file, as shown in the model, should match the path specified in the Finally, we need to add a translation. Put this in `assets/simple_item_mod/lang/en_us.json`, replacing the mod id and item name as before: +`assets/simple_item_mod/lang/en_us.json`: + ```json { "item.simple_item_mod.example_item": "Example Item"