@Serializable data class EnergyComponent(var amount: Int, val max: Int) // Instantiate val battery = EnergyComponent(100, 500) val copy = battery.copy(amount = 200) // Immutable copy with change Need to run code after 5 seconds without blocking the game thread?
Happy modding! 🧙♂️ Have you tried Kotlin for Fabric? Share your experience in the comments below! kotlin for fabric
fun BlockView.isSkyExposed(pos: BlockPos): Boolean return getLightLevel(pos) >= 15 && !isRainOccluded(pos) Share your experience in the comments below
// Usage if (world.isSkyExposed(pos)) ... Define a custom component for your mod’s energy system: If you’re starting a new mod or refactoring
The Fabric ecosystem has embraced Kotlin through fabric-language-kotlin , and the setup is painless. If you’re starting a new mod or refactoring an old one, give Kotlin a try. Your future self (and your users) will thank you for the fewer crashes and cleaner code.
player?.world?.setBlockState(pos, state) // Safe call chain If player or world is null, nothing happens. No crash. Add a helper to check if a block is exposed to sky: