Add a configuration entry to toggle the alphabetical sorting of buttons and handle errors in the DiscordRPC feature

This commit is contained in:
Username404 2021-06-01 15:51:51 +02:00
parent d8bbab0ff7
commit 38e0ac2e71
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
8 changed files with 36 additions and 12 deletions

View File

@ -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)

View File

@ -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()
)

View File

@ -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<ButtonImpl>()
val buttons = if (sortAlphabetically) sortedSetOf<ButtonImpl>(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

View File

@ -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<ButtonInfo>() ?: throw Exception("Missing @ButtonInfo annotaton")
override var color = info.parent.categoryColor
open val title: String = this@ButtonImpl::class.simpleName.toString()

View File

@ -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
}
}
}

View File

@ -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",

View File

@ -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",

View File

@ -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