After a bit long time of creating Fabric mods for Minecraft. I collected some tips and tricks that I found useful for mod development and want to share it. Because I’m not good at Java so these codes below are written in Kotlin.
1. Utilities functions
These life savers functions that I always use when developing mods.
1.1 toEnglishName(path)
import java.util.Locale
fun toEnglishName(name: String): String {
return name.lowercase(Locale.ROOT).split("_").joinToString(" ") {
it.replaceFirstChar { char -> char.uppercaseChar() }
}
}
This function will convert a name
with any formats (camel_case, snake_case, etc.) to readable English name.
For instance:
wrench
-> Wrench
lava_generator
-> Lava Generator
2. Utilities classes
2.1 Identifier
Instead of spamming long ResourceLocation
, we should make a class extends it and only receive path. This will decrease the amount of code we need to write.
class Identifier(path: String) : ResourceLocation(MOD_NAME, path) {}
By using that, we can replace this:
val wrench = ResourceLocation(MOD_ID, "wrench")
val lavaGenerator = ResourceLocation(MOD_ID, "lava_generator")
to this:
val wrench = Identifier("wrench")
val lavaGenerator = Identifier("lava_generator")