From 38e0ac2e71899be057fe12628a9dd30497abb523 Mon Sep 17 00:00:00 2001 From: Username404 Date: Tue, 1 Jun 2021 15:51:51 +0200 Subject: [PATCH] Add a configuration entry to toggle the alphabetical sorting of buttons and handle errors in the DiscordRPC feature --- .../fr/username404/snowygui/config/ConfigScreen.kt | 8 +++++++- .../username404/snowygui/config/Configuration.kt | 5 ++++- .../username404/snowygui/gui/elements/ClickBox.kt | 5 ++++- .../username404/snowygui/gui/feature/ButtonImpl.kt | 12 ++++++++---- .../username404/snowygui/gui/feature/DiscordRPC.kt | 14 ++++++++++---- .../main/resources/assets/snowygui/lang/en_us.json | 1 + .../main/resources/assets/snowygui/lang/fr_fr.json | 1 + gradle/wrapper/gradle-wrapper.properties | 2 +- 8 files changed, 36 insertions(+), 12 deletions(-) diff --git a/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt b/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt index 59e35f1..54e20a0 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt @@ -5,6 +5,7 @@ import fr.username404.snowygui.ClickGui import fr.username404.snowygui.gui.FontUtil import fr.username404.snowygui.gui.elements.ClickBox import fr.username404.snowygui.gui.elements.ClickBox.Companion.buttonsMax +import fr.username404.snowygui.gui.elements.ClickBox.Companion.sortAlphabetically import fr.username404.snowygui.gui.feature.Category import fr.username404.snowygui.gui.feature.Colors import fr.username404.snowygui.gui.feature.Macro @@ -35,7 +36,12 @@ val SnowyConfigScreen: Screen get() { .setShouldListSmoothScroll(true) .setTitle(translationComponent).apply { with(entryBuilder()) { - getOrCreateCategory(TranslatableComponent("$confPrefix.general")).addEntry(startSubCategory(TranslatableComponent("$confPrefix.behavior")).build()) + getOrCreateCategory(TranslatableComponent("$confPrefix.general")).addEntry(startSubCategory(TranslatableComponent("$confPrefix.behavior")).apply { + add(startBooleanToggle(TranslatableComponent("$confPrefix.behavior.sortalphabetically"), sortAlphabetically) + .setDefaultValue(true).requireRestart() + .setSaveConsumer { sortAlphabetically = it }.build() + ) + }.build()) .addEntry( startBooleanToggle(Component.nullToEmpty("Risky Cheats"), riskyCheatsEnabled) .setDefaultValue(false) diff --git a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt index cd87a1f..ab91545 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt @@ -5,6 +5,7 @@ import com.typesafe.config.ConfigFactory.* import fr.username404.snowygui.ClickGui import fr.username404.snowygui.Snowy import fr.username404.snowygui.gui.elements.ClickBox +import fr.username404.snowygui.gui.elements.ClickBox.Companion.sortAlphabetically import fr.username404.snowygui.gui.feature.Category import fr.username404.snowygui.gui.feature.Macro import fr.username404.snowygui.gui.feature.riskyCheatsEnabled @@ -25,7 +26,8 @@ object Configuration { "riskyCheats" to Supplier { ConfigValueFactory.fromAnyRef(riskyCheatsEnabled) }, "macros" to Supplier { ConfigValueFactory.fromIterable((ClickGui.components.find { it is ClickBox && it.isCategory(Category.MACROS) } as ClickBox).buttons.map { Json.encodeToString(it as Macro) - }) } + }) }, + "sortAlphabetically" to Supplier { ConfigValueFactory.fromAnyRef(sortAlphabetically) } ); inline fun setConfigValue(s: String, crossinline value: () -> Any) = ModifiableValues.put(s, Supplier { ConfigValueFactory.fromAnyRef(value.invoke()) }) private fun Config.withFullModifiableValues() = ModifiableValues.entries.fold(this) { previous, entry -> previous.withValue(entry.key, entry.value.get()) @@ -70,6 +72,7 @@ object Configuration { | displayInitMessage = false | macros = [] | riskyCheats = false + | sortAlphabetically = true |} """.trimMargin() ) diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt index b0bfdb4..c0d28c4 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt @@ -3,6 +3,7 @@ package fr.username404.snowygui.gui.elements import com.mojang.blaze3d.systems.RenderSystem import com.mojang.blaze3d.vertex.DefaultVertexFormat import com.mojang.blaze3d.vertex.PoseStack +import fr.username404.snowygui.config.Configuration import fr.username404.snowygui.gui.ColoredElement import fr.username404.snowygui.gui.Renderable.Rendering.buffer import fr.username404.snowygui.gui.Renderable.Rendering.defaultRectFunc @@ -10,6 +11,7 @@ import fr.username404.snowygui.gui.Renderable.Rendering.tessellator import fr.username404.snowygui.gui.feature.ButtonImpl import fr.username404.snowygui.gui.feature.Category import fr.username404.snowygui.gui.feature.Colors +import io.github.config4k.extract import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import net.minecraft.client.Minecraft @@ -24,7 +26,7 @@ class ClickBox( val name: TranslatableComponent? = null ): ColoredElement(x, y, 80, 10, color, 0.5F) { fun isCategory(c: Category): Boolean = (name?.key == c.translationKey) - val buttons = mutableSetOf() + val buttons = if (sortAlphabetically) sortedSetOf(compareBy(String.CASE_INSENSITIVE_ORDER) { it.title }) else mutableSetOf() override fun display(stack: PoseStack?) { hidden = buttons.isEmpty() || hidden super.display(stack) @@ -60,6 +62,7 @@ class ClickBox( } companion object { + var sortAlphabetically: Boolean = Configuration.obtained.extract("sortAlphabetically") const val buttonsMax: Short = 16 // TODO Remove the buttons limit const val clickboxHeightOffset: Int = 80 private const val inclination: Double = 2.5 diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt index 33c43cc..83423c5 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt @@ -33,10 +33,10 @@ sealed class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) { } }.entries.forEach { entry -> with(entry) { - value.sortedWith( - compareBy(String.CASE_INSENSITIVE_ORDER) { it.title } - ).forEach { - key?.buttons!!.add(it) + value.forEach { + if (!it.isDisabled) { + key?.buttons!!.add(it) + } } } } @@ -57,6 +57,10 @@ sealed class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) { ) } } + var isDisabled: Boolean = false; set(value) { + field = value + hidden = value + } private val info = this::class.findAnnotation() ?: throw Exception("Missing @ButtonInfo annotaton") override var color = info.parent.categoryColor open val title: String = this@ButtonImpl::class.simpleName.toString() diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/DiscordRPC.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/DiscordRPC.kt index 598ffd9..202ca5d 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/DiscordRPC.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/DiscordRPC.kt @@ -1,9 +1,10 @@ package fr.username404.snowygui.gui.feature -import net.arikia.dev.drpc.DiscordRichPresence -import net.minecraft.client.Minecraft +import fr.username404.snowygui.Snowy import fr.username404.snowygui.gui.feature.ButtonInfo.Companion.Type import net.arikia.dev.drpc.DiscordEventHandlers +import net.arikia.dev.drpc.DiscordRichPresence +import net.minecraft.client.Minecraft import net.arikia.dev.drpc.DiscordRPC as discord_rpc @ButtonInfo(Category.MISC, kind = Type.TOGGLE) @@ -18,7 +19,12 @@ object DiscordRPC: ButtonImpl() { else discord_rpc.discordClearPresence() } init { - Runtime.getRuntime().addShutdownHook(Thread { discord_rpc.discordShutdown() }) - discord_rpc.discordInitialize("839058922102849538", RPCHandlers, true) + try { + discord_rpc.discordInitialize("839058922102849538", RPCHandlers, true) + Runtime.getRuntime().addShutdownHook(Thread { discord_rpc.discordShutdown() }) + } catch (e: UnsatisfiedLinkError) { + Snowy.logs.error("Could not initialize DiscordRPC; the feature will therefore be disabled.") + isDisabled = true + } } } \ No newline at end of file diff --git a/common/src/main/resources/assets/snowygui/lang/en_us.json b/common/src/main/resources/assets/snowygui/lang/en_us.json index 571b839..ed77706 100644 --- a/common/src/main/resources/assets/snowygui/lang/en_us.json +++ b/common/src/main/resources/assets/snowygui/lang/en_us.json @@ -9,6 +9,7 @@ "screen.snowy.config.general.macros.toomuchcharacters": "Too much characters (16 is the maximum)", "screen.snowy.config.general.macros.toomuchbuttons": "Too much entries (16 is the maximum)", "screen.snowy.config.behavior": "Behavior", + "screen.snowy.config.behavior.sortalphabetically": "Sort buttons alphabetically", "category.snowy.keycategory": "SnowyGUI", "key.snowy.opengui": "Open the snowy gui", "key.snowy.configkey": "Open the snowy configuration screen", diff --git a/common/src/main/resources/assets/snowygui/lang/fr_fr.json b/common/src/main/resources/assets/snowygui/lang/fr_fr.json index 5309758..8cc9d92 100644 --- a/common/src/main/resources/assets/snowygui/lang/fr_fr.json +++ b/common/src/main/resources/assets/snowygui/lang/fr_fr.json @@ -9,6 +9,7 @@ "screen.snowy.config.general.macros.toomuchcharacters": "Trop de caractères (le maximum est de 16)", "screen.snowy.config.general.macros.toomuchbuttons": "Trop d'entrées (le maximum est de 16)", "screen.snowy.config.behavior": "Comportement", + "screen.snowy.config.behavior.sortalphabetically": "Trier les boutons alphabétiquement", "category.snowy.keycategory": "SnowyGUI", "key.snowy.opengui": "Ouvrir l'interface de snowy", "key.snowy.configkey": "Ouvrir l'écran de configuration de snowy", diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0f80bbf..7fb030b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-rc-1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists