aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/com/examplemod/ExampleMod.kt61
-rw-r--r--src/main/kotlin/com/examplemod/commands/ExampleCommand.kt23
-rw-r--r--src/main/kotlin/com/examplemod/config/Config.kt52
-rw-r--r--src/main/kotlin/com/examplemod/config/PersistentData.kt35
4 files changed, 171 insertions, 0 deletions
diff --git a/src/main/kotlin/com/examplemod/ExampleMod.kt b/src/main/kotlin/com/examplemod/ExampleMod.kt
new file mode 100644
index 0000000..50434a1
--- /dev/null
+++ b/src/main/kotlin/com/examplemod/ExampleMod.kt
@@ -0,0 +1,61 @@
+import com.examplemod.commands.ExampleCommand
+import com.examplemod.config.Config
+import com.examplemod.config.PersistentData
+import net.minecraft.client.Minecraft
+import net.minecraft.client.gui.GuiScreen
+import net.minecraftforge.client.ClientCommandHandler
+import net.minecraftforge.common.MinecraftForge
+import net.minecraftforge.fml.common.Mod
+import net.minecraftforge.fml.common.ModMetadata
+import net.minecraftforge.fml.common.event.FMLInitializationEvent
+import net.minecraftforge.fml.common.event.FMLPreInitializationEvent
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import net.minecraftforge.fml.common.gameevent.TickEvent
+import java.io.File
+
+@Mod(
+ modid = "examplemod",
+ name = "ExampleMod",
+ version = "1.0",
+ useMetadata = true,
+ clientSideOnly = true
+)
+class ExampleMod {
+
+ @Mod.EventHandler
+ fun preInit(event: FMLPreInitializationEvent) {
+ metadata = event.modMetadata
+ val directory = File(event.modConfigurationDirectory, event.modMetadata.modId)
+ directory.mkdirs()
+ configDirectory = directory
+ persistentData = PersistentData.load()
+ config = Config
+ }
+
+ @Mod.EventHandler
+ fun onInit(event: FMLInitializationEvent) {
+ ClientCommandHandler.instance.registerCommand(ExampleCommand())
+
+ listOf(
+ this
+ ).forEach(MinecraftForge.EVENT_BUS::register)
+ }
+
+ @SubscribeEvent
+ fun onTick(event: TickEvent.ClientTickEvent) {
+ if (event.phase != TickEvent.Phase.START || currentGui == null) return
+ mc.displayGuiScreen(currentGui)
+ currentGui = null
+ }
+
+ companion object {
+ val mc: Minecraft = Minecraft.getMinecraft()
+ var currentGui: GuiScreen? = null
+
+ lateinit var configDirectory: File
+ lateinit var config: Config
+ lateinit var persistentData: PersistentData
+
+ lateinit var metadata: ModMetadata
+ }
+} \ No newline at end of file
diff --git a/src/main/kotlin/com/examplemod/commands/ExampleCommand.kt b/src/main/kotlin/com/examplemod/commands/ExampleCommand.kt
new file mode 100644
index 0000000..12538ca
--- /dev/null
+++ b/src/main/kotlin/com/examplemod/commands/ExampleCommand.kt
@@ -0,0 +1,23 @@
+package com.examplemod.commands
+
+import ExampleMod
+import com.examplemod.config.Config
+import net.minecraft.command.CommandBase
+import net.minecraft.command.ICommandSender
+import net.minecraft.util.ChatComponentText
+import net.minecraft.util.IChatComponent
+
+class ExampleCommand : CommandBase() {
+ override fun getCommandName() = "examplemod"
+
+ override fun getCommandAliases() = listOf("example")
+
+ override fun getCommandUsage(sender: ICommandSender?) = "/$commandName"
+
+ override fun getRequiredPermissionLevel() = 0
+
+ override fun processCommand(sender: ICommandSender?, args: Array<out String>?) {
+ sender?.addChatMessage(ChatComponentText("Example command run!"))
+ ExampleMod.currentGui = Config.gui()
+ }
+} \ No newline at end of file
diff --git a/src/main/kotlin/com/examplemod/config/Config.kt b/src/main/kotlin/com/examplemod/config/Config.kt
new file mode 100644
index 0000000..3008de3
--- /dev/null
+++ b/src/main/kotlin/com/examplemod/config/Config.kt
@@ -0,0 +1,52 @@
+package com.examplemod.config
+
+import gg.essential.vigilance.Vigilant
+import java.awt.Color
+import java.io.File
+
+
+object Config : Vigilant(
+ File(ExampleMod.configDirectory, "config.toml"),
+ ExampleMod.metadata.name
+) {
+ var demoSwitch = false
+ var demoSelector = 0
+ var demoColor : Color = Color.WHITE
+ var demoText = ""
+
+ init {
+ category("One category") {
+ switch(
+ ::demoSwitch,
+ name = "Switch",
+ description = "This is a switch"
+ )
+
+ subcategory("An additional category") {
+ selector(
+ ::demoSelector,
+ name = "Selector",
+ description = "This is a selector",
+ options = listOf("Option 1", "Option 2", "Option 3")
+ )
+ color(
+ ::demoColor,
+ name = "Color",
+ description = "This sets a color"
+ )
+ }
+ }
+
+ category("Another category") {
+ text(
+ ::demoText,
+ name = "Text",
+ description = "This is text",
+ placeholder = "This is some placeholder text."
+ )
+
+ }
+ }
+
+
+} \ No newline at end of file
diff --git a/src/main/kotlin/com/examplemod/config/PersistentData.kt b/src/main/kotlin/com/examplemod/config/PersistentData.kt
new file mode 100644
index 0000000..58e0ac0
--- /dev/null
+++ b/src/main/kotlin/com/examplemod/config/PersistentData.kt
@@ -0,0 +1,35 @@
+package com.examplemod.config
+
+import kotlinx.serialization.Serializable
+import kotlinx.serialization.Transient
+import kotlinx.serialization.json.Json
+import java.io.File
+import kotlinx.serialization.encodeToString
+import kotlinx.serialization.decodeFromString
+
+@Serializable
+data class PersistentData(
+ var exampleData: Map<String, String> = mapOf("key1" to "entry1"),
+ var moreExampleData: Int = 5
+) {
+
+ fun save() {
+ configFile.writeText(Json.encodeToString(this))
+ }
+
+ companion object {
+ private val configFile: File = File(ExampleMod.configDirectory,"data.json")
+
+ fun load(): PersistentData {
+ val data = if (!configFile.exists()) {
+ configFile.createNewFile()
+ PersistentData()
+ } else configFile.runCatching {
+ Json.decodeFromString<PersistentData>(this.readText())
+ }.getOrNull() ?: PersistentData()
+ return data.apply {
+ this.save()
+ }
+ }
+ }
+} \ No newline at end of file