From 1bfd0bdd9e02ebb44526fcc74a929a919e646e65 Mon Sep 17 00:00:00 2001 From: Iru21 <33987046+Iru21@users.noreply.github.com> Date: Sun, 11 Jun 2023 15:37:00 +0200 Subject: [PATCH] Add mod menu support for config and implement #3 as a toggleable option --- build.gradle.kts | 9 +++++---- gradle.properties | 3 ++- .../kotlin/me/iru/waxednotwaxed/ToggleConfig.kt | 9 --------- .../kotlin/me/iru/waxednotwaxed/WaxedNotWaxed.kt | 13 +++++-------- .../waxednotwaxed/config/ModMenuIntegration.kt | 16 ++++++++++++++++ .../waxednotwaxed/config/WaxedNotWaxedConfig.kt | 15 +++++++++++++++ .../waxednotwaxed/events/EndClientTickHandler.kt | 7 +++---- .../iru/waxednotwaxed/events/HudRenderHandler.kt | 6 +++++- .../assets/waxednotwaxed/lang/en_us.json | 3 +++ src/main/resources/fabric.mod.json | 10 ++++++++++ 10 files changed, 64 insertions(+), 27 deletions(-) delete mode 100644 src/main/kotlin/me/iru/waxednotwaxed/ToggleConfig.kt create mode 100644 src/main/kotlin/me/iru/waxednotwaxed/config/ModMenuIntegration.kt create mode 100644 src/main/kotlin/me/iru/waxednotwaxed/config/WaxedNotWaxedConfig.kt diff --git a/build.gradle.kts b/build.gradle.kts index b9d15f7..32b83f0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,6 +20,7 @@ group = mavenGroup repositories { mavenCentral() maven("https://maven.shedaniel.me/") + maven("https://maven.terraformersmc.com/releases/") } dependencies { val minecraftVersion: String by project @@ -33,11 +34,11 @@ dependencies { val fabricKotlinVersion: String by project modImplementation("net.fabricmc:fabric-language-kotlin:$fabricKotlinVersion") val clothConfigVersion: String by project - modApi("me.shedaniel.cloth:cloth-config-fabric:$clothConfigVersion") { - exclude("net.fabricmc.fabric-api") - } + modApi("me.shedaniel.cloth:cloth-config-fabric:$clothConfigVersion") + val modMenuVersion: String by project + modApi("com.terraformersmc:modmenu:$modMenuVersion") - modImplementation("blue.endless:jankson:1.2.1") + modImplementation("blue.endless:jankson:1.2.2") } tasks { val javaVersion = JavaVersion.VERSION_17 diff --git a/gradle.properties b/gradle.properties index b270dcd..bd96b7b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,4 +16,5 @@ archivesBaseName = waxednotwaxed systemProp.kotlinVersion = 1.6.10 fabricKotlinVersion = 1.8.7+kotlin.1.7.22 -clothConfigVersion = 11.0.99 \ No newline at end of file +clothConfigVersion = 11.0.99 +modMenuVersion = 7.0.1 \ No newline at end of file diff --git a/src/main/kotlin/me/iru/waxednotwaxed/ToggleConfig.kt b/src/main/kotlin/me/iru/waxednotwaxed/ToggleConfig.kt deleted file mode 100644 index 1ede0d9..0000000 --- a/src/main/kotlin/me/iru/waxednotwaxed/ToggleConfig.kt +++ /dev/null @@ -1,9 +0,0 @@ -package me.iru.waxednotwaxed - -import me.shedaniel.autoconfig.ConfigData -import me.shedaniel.autoconfig.annotation.Config - -@Config(name = "waxednotwaxed") -class ToggleConfig : ConfigData { - var toggled = false -} \ No newline at end of file diff --git a/src/main/kotlin/me/iru/waxednotwaxed/WaxedNotWaxed.kt b/src/main/kotlin/me/iru/waxednotwaxed/WaxedNotWaxed.kt index d9ac7f5..c0eb5b7 100644 --- a/src/main/kotlin/me/iru/waxednotwaxed/WaxedNotWaxed.kt +++ b/src/main/kotlin/me/iru/waxednotwaxed/WaxedNotWaxed.kt @@ -1,9 +1,9 @@ package me.iru.waxednotwaxed +import me.iru.waxednotwaxed.config.WaxedNotWaxedConfig import me.iru.waxednotwaxed.events.EndClientTickHandler import me.iru.waxednotwaxed.events.HudRenderHandler import me.shedaniel.autoconfig.AutoConfig -import me.shedaniel.autoconfig.ConfigHolder import me.shedaniel.autoconfig.annotation.Config import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer import net.fabricmc.api.ClientModInitializer @@ -14,24 +14,21 @@ import net.minecraft.client.option.KeyBinding import net.minecraft.client.util.InputUtil import org.lwjgl.glfw.GLFW - object WaxedNotWaxed : ClientModInitializer { var keyBinding: KeyBinding? = null - var config: ConfigHolder? = null - var toggledCached: Boolean = false + lateinit var config: WaxedNotWaxedConfig override fun onInitializeClient() { AutoConfig.register( - ToggleConfig::class.java - ) { definition: Config?, configClass: Class? -> + WaxedNotWaxedConfig::class.java + ) { definition: Config?, configClass: Class? -> JanksonConfigSerializer( definition, configClass ) } - this.config = AutoConfig.getConfigHolder(ToggleConfig::class.java) - this.toggledCached = this.config!!.get().toggled + this.config = AutoConfig.getConfigHolder(WaxedNotWaxedConfig::class.java).get() this.keyBinding = KeyBindingHelper.registerKeyBinding( KeyBinding( diff --git a/src/main/kotlin/me/iru/waxednotwaxed/config/ModMenuIntegration.kt b/src/main/kotlin/me/iru/waxednotwaxed/config/ModMenuIntegration.kt new file mode 100644 index 0000000..5cd652e --- /dev/null +++ b/src/main/kotlin/me/iru/waxednotwaxed/config/ModMenuIntegration.kt @@ -0,0 +1,16 @@ +package me.iru.waxednotwaxed.config + +import com.terraformersmc.modmenu.api.ConfigScreenFactory +import com.terraformersmc.modmenu.api.ModMenuApi +import me.shedaniel.autoconfig.AutoConfig +import net.minecraft.client.gui.screen.Screen + +class ModMenuIntegration: ModMenuApi { + override fun getModConfigScreenFactory(): ConfigScreenFactory<*> { + return ConfigScreenFactory { parent: Screen? -> + AutoConfig.getConfigScreen( + WaxedNotWaxedConfig::class.java, parent + ).get() + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/me/iru/waxednotwaxed/config/WaxedNotWaxedConfig.kt b/src/main/kotlin/me/iru/waxednotwaxed/config/WaxedNotWaxedConfig.kt new file mode 100644 index 0000000..21b4304 --- /dev/null +++ b/src/main/kotlin/me/iru/waxednotwaxed/config/WaxedNotWaxedConfig.kt @@ -0,0 +1,15 @@ +package me.iru.waxednotwaxed.config + +import me.shedaniel.autoconfig.AutoConfig +import me.shedaniel.autoconfig.ConfigData +import me.shedaniel.autoconfig.annotation.Config + +@Config(name = "waxednotwaxed") +class WaxedNotWaxedConfig : ConfigData { + var enabled: Boolean = false + var onlyShowWhenHoldingAxe: Boolean = false + + fun save() { + AutoConfig.getConfigHolder(this::class.java).save() + } +} \ No newline at end of file diff --git a/src/main/kotlin/me/iru/waxednotwaxed/events/EndClientTickHandler.kt b/src/main/kotlin/me/iru/waxednotwaxed/events/EndClientTickHandler.kt index 45abbdb..2c9fba3 100644 --- a/src/main/kotlin/me/iru/waxednotwaxed/events/EndClientTickHandler.kt +++ b/src/main/kotlin/me/iru/waxednotwaxed/events/EndClientTickHandler.kt @@ -9,10 +9,9 @@ import net.minecraft.sound.SoundEvents fun EndClientTickHandler(): ClientTickEvents.EndTick { return ClientTickEvents.EndTick { while(WaxedNotWaxed.keyBinding!!.wasPressed()) { - val newValue = !WaxedNotWaxed.toggledCached - WaxedNotWaxed.toggledCached = newValue - WaxedNotWaxed.config!!.config.toggled = newValue - WaxedNotWaxed.config!!.save() + val newValue = !WaxedNotWaxed.config.enabled + WaxedNotWaxed.config.enabled = newValue + WaxedNotWaxed.config.save() val mc = MinecraftClient.getInstance() val p = mc.player!! mc.world!!.playSound(p, p.blockPos, SoundEvents.UI_BUTTON_CLICK.value(), SoundCategory.MASTER, 0.1f, 1f) diff --git a/src/main/kotlin/me/iru/waxednotwaxed/events/HudRenderHandler.kt b/src/main/kotlin/me/iru/waxednotwaxed/events/HudRenderHandler.kt index c429de3..23a39e4 100644 --- a/src/main/kotlin/me/iru/waxednotwaxed/events/HudRenderHandler.kt +++ b/src/main/kotlin/me/iru/waxednotwaxed/events/HudRenderHandler.kt @@ -8,12 +8,16 @@ import net.minecraft.block.OxidizableSlabBlock import net.minecraft.block.OxidizableStairsBlock import net.minecraft.client.MinecraftClient import net.minecraft.client.gui.DrawContext +import net.minecraft.item.AxeItem import net.minecraft.util.Identifier class HudRenderHandler : HudRenderCallback { override fun onHudRender(drawContext: DrawContext?, tickDelta: Float) { - if(drawContext == null || !WaxedNotWaxed.toggledCached) return + if(drawContext == null || + !WaxedNotWaxed.config.enabled || + (WaxedNotWaxed.config.onlyShowWhenHoldingAxe && MinecraftClient.getInstance().player!!.mainHandStack.item !is AxeItem) + ) return val lookingAt = Utils.getBlockAtCrosshair() ?: return if (lookingAt.name.toString().lowercase().contains("waxed")) render(drawContext, true) else if(lookingAt is OxidizableBlock || lookingAt is OxidizableSlabBlock || lookingAt is OxidizableStairsBlock) { diff --git a/src/main/resources/assets/waxednotwaxed/lang/en_us.json b/src/main/resources/assets/waxednotwaxed/lang/en_us.json index aef1d54..5f3711d 100644 --- a/src/main/resources/assets/waxednotwaxed/lang/en_us.json +++ b/src/main/resources/assets/waxednotwaxed/lang/en_us.json @@ -1,4 +1,7 @@ { + "text.autoconfig.waxednotwaxed.title": "Waxed Not Waxed Config", + "text.autoconfig.waxednotwaxed.option.enabled": "Enabled", + "text.autoconfig.waxednotwaxed.option.onlyShowWhenHoldingAxe": "Only show when holding an axe", "key.waxednotwaxed.toggle": "Toggle Indicator", "category.waxednotwaxed.keybindcategory": "Waxed Not Waxed" } \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 81323e9..5e33d02 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -21,6 +21,9 @@ "adapter": "kotlin", "value": "me.iru.waxednotwaxed.WaxedNotWaxed" } + ], + "modmenu": [ + "me.iru.waxednotwaxed.config.ModMenuIntegration" ] }, "depends": { @@ -29,5 +32,12 @@ "fabric-language-kotlin": ">=1.8.7+kotlin.1.7.22", "minecraft": ">=1.20", "java": ">=17" + }, + "custom": { + "modmenu": { + "links": { + "modmenu.discord": "https://discord.gg/jrebrmDD5X" + } + } } } \ No newline at end of file