Prompt: Can you show me an example of a Minecraft Mod written in Kotlin?
By: ChatGPT (chat.openai.com)
Sure, here’s a simple example of a Minecraft mod written in Kotlin:
import net.minecraft.item.Item
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.FMLInitializationEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.registry.GameRegistry
import net.minecraftforge.fml.common.registry.GameRegistry.ObjectHolder
import net.minecraftforge.fml.common.Mod.EventBusSubscriber
@Mod(modid = MyFirstMod.MOD_ID, name = MyFirstMod.MOD_NAME, version = MyFirstMod.MOD_VERSION)
@EventBusSubscriber(modid = MyFirstMod.MOD_ID)
object MyFirstMod {
const val MOD_ID = "myfirstmod"
const val MOD_NAME = "My First Mod"
const val MOD_VERSION = "1.0"
@ObjectHolder("myfirstmod:firstitem")
lateinit var firstItem: Item
@JvmStatic
@SubscribeEvent
fun init(event: FMLInitializationEvent) {
// Register new item
firstItem = Item().setRegistryName("firstitem").setUnlocalizedName("${MOD_ID}.firstitem")
GameRegistry.register(firstItem)
}
}
This mod creates a new item called “firstitem” and registers it in the game. The @Mod
annotation declares this class as a mod and provides metadata such as the mod ID, name, and version. The @EventBusSubscriber
annotation registers the class to listen for events in the game’s event bus. The init
function is called when the mod is initialized and registers the new item with the game registry.