Minecraft Modding: Fabric + Kotlin – Strings and Refactoring

This entry is part 3 of 3 in the series Minecraft Modding: Fabric + Kotlin

Translation/Strings

You may have noticed that, in the inventory, the items and creative tabs don’t show a display name – rather, they show a name like *.modid.**. This is because we haven’t added strings to our mod, so Minecraft is just using the ID.

In assets/modid, create a new folder called lang. In it, you can create JSON files whose names are a language code, followed by an underscore, followed by a country code (everything is lowercased). You can find a complete list here. I’ll use en_us for my example. If you plan on releasing a mod, it’s a good idea to translate strings into many languages to allow more people to use it.

Example for lang/en_us.json:

{
  "item.modid.ice_cube": "Ice Cube",
  "itemGroup.modid.modid_group": "Mod Name"
}

Run the client. If everything worked, when hovering over ice cubes in your inventory, it should show “Ice Cube” instead of “item.modid.ice_cube”. Similarly, the creative tab should be titled with your mod name instead of the ID.

Refactoring

So far, in our Kotlin code, we’ve been hardcoding our mod ID into strings. It’s easy to see why it might get unwieldy with more items. To combat this, we can add a MODID property to the entrypoint class and use that everywhere.

Add the MODID property to your entrypoint class. After you do that, either replace or interpolate hardcoded strings in the following places; of course, you’ll have to change it in other/different places too if you’ve added more items or done things differently than I have:

  • The ICE_CUBE property on the mod entrypoint class
  • The MODID_GROUP (remember to change the identifier path too, not just the namespace)
  • Registering the group in onInitialize

After you’ve done that, you’re done with refactoring! Run the client and test everything to make sure you’ve done everything correctly.

In the next part, we’ll see how we can make our items do interesting things when players interact with them.

Series Navigation<< Minecraft Modding: Fabric + Kotlin – Items

Leave a Reply

Your email address will not be published. Required fields are marked *