diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-08-29 20:27:32 +0200 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-08-29 20:27:32 +0200 |
commit | a5c6116b3cf024ded69e58060a078a04776de6c4 (patch) | |
tree | a81e8a0f841ffd92000c4aece778c13f81fced3e /src/main | |
parent | 71a6b08b376be7d742107df982905cac0d7ecf70 (diff) | |
download | skyhanni-a5c6116b3cf024ded69e58060a078a04776de6c4.tar.gz skyhanni-a5c6116b3cf024ded69e58060a078a04776de6c4.tar.bz2 skyhanni-a5c6116b3cf024ded69e58060a078a04776de6c4.zip |
code cleanup
Diffstat (limited to 'src/main')
8 files changed, 118 insertions, 115 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt index 6d60ebca7..1efe4dab9 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt @@ -7,7 +7,6 @@ import at.hannibal2.skyhanni.config.commands.Commands.init import at.hannibal2.skyhanni.data.* import at.hannibal2.skyhanni.data.repo.RepoManager import at.hannibal2.skyhanni.events.LorenzTickEvent -import at.hannibal2.skyhanni.features.misc.massconfiguration.DefaultConfigFeatures import at.hannibal2.skyhanni.features.anvil.AnvilCombineHelper import at.hannibal2.skyhanni.features.bazaar.BazaarApi import at.hannibal2.skyhanni.features.bazaar.BazaarBestSellMethod @@ -57,6 +56,7 @@ import at.hannibal2.skyhanni.features.misc.discordrpc.DiscordRPCManager import at.hannibal2.skyhanni.features.misc.ghostcounter.GhostCounter import at.hannibal2.skyhanni.features.misc.items.EstimatedItemValue import at.hannibal2.skyhanni.features.misc.items.EstimatedWardrobePrice +import at.hannibal2.skyhanni.features.misc.massconfiguration.DefaultConfigFeatures import at.hannibal2.skyhanni.features.misc.tabcomplete.PlayerTabComplete import at.hannibal2.skyhanni.features.misc.tabcomplete.WarpTabComplete import at.hannibal2.skyhanni.features.misc.teleportpad.TeleportPadCompactName diff --git a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt index 73b6cb46d..acd280967 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt @@ -329,8 +329,8 @@ object Commands { ClientCommandHandler.instance.registerCommand( SimpleCommand( name, - createCommand(function), - { a, b, c -> autoComplete(b) }) + createCommand(function) + ) { _, b, _ -> autoComplete(b) } ) commands.add(CommandInfo(name, description, currentCategory)) } diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/Category.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/Category.kt new file mode 100644 index 000000000..3172df6e0 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/Category.kt @@ -0,0 +1,3 @@ +package at.hannibal2.skyhanni.features.misc.massconfiguration + +data class Category(val name: String, val description: String)
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/DefaultConfigFeatures.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/DefaultConfigFeatures.kt index 72f205b5e..a1f40c6e9 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/DefaultConfigFeatures.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/DefaultConfigFeatures.kt @@ -1,24 +1,22 @@ package at.hannibal2.skyhanni.features.misc.massconfiguration import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.utils.LorenzUtils import io.github.moulberry.moulconfig.processor.ConfigProcessorDriver import net.minecraft.client.Minecraft import net.minecraft.command.CommandBase -import net.minecraft.event.ClickEvent -import net.minecraft.util.ChatComponentText -import net.minecraft.util.ChatStyle import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import net.minecraftforge.fml.common.gameevent.TickEvent object DefaultConfigFeatures { var didNotifyOnce = false @SubscribeEvent - fun onTick(event: TickEvent) { + fun onTick(event: LorenzTickEvent) { if (didNotifyOnce) return - val player = Minecraft.getMinecraft().thePlayer ?: return + Minecraft.getMinecraft().thePlayer ?: return didNotifyOnce = true + val knownToggles = SkyHanniMod.feature.storage.knownFeatureToggles val updated = SkyHanniMod.version !in knownToggles val processor = FeatureToggleProcessor() @@ -27,35 +25,19 @@ object DefaultConfigFeatures { SkyHanniMod.configManager.saveConfig("Updated known feature flags") if (!SkyHanniMod.feature.storage.hasPlayedBefore) { SkyHanniMod.feature.storage.hasPlayedBefore = true - player.addChatMessage( - ChatComponentText( - "§e[SkyHanni] Looks like this is the first time you are using SkyHanni." + - " Click here to configure default options, or run /shdefaultoptions." - ).setChatStyle( - ChatStyle().setChatClickEvent( - ClickEvent( - ClickEvent.Action.RUN_COMMAND, - "/shdefaultoptions" - ) - ) - ) + LorenzUtils.clickableChat( + "§e[SkyHanni] Looks like this is the first time you are using SkyHanni. " + + "Click here to configure default options, or run /shdefaultoptions.", + "shdefaultoptions" ) - } else if (updated) { - val mostFeatureFulOldVersion = knownToggles.maxByOrNull { if (it.key != SkyHanniMod.version) it.value.size else -1 } + val mostFeatureFulOldVersion = + knownToggles.maxByOrNull { if (it.key != SkyHanniMod.version) it.value.size else -1 } val command = "/shdefaultoptions ${mostFeatureFulOldVersion?.key} ${SkyHanniMod.version}" - player.addChatMessage( - ChatComponentText( - "§e[SkyHanni] Looks like you updated SkyHanni." + - " Click here to configure the newly introduced options, or run $command." - ).setChatStyle( - ChatStyle().setChatClickEvent( - ClickEvent( - ClickEvent.Action.RUN_COMMAND, - command - ) - ) - ) + LorenzUtils.clickableChat( + "§e[SkyHanni] Looks like you updated SkyHanni. " + + "Click here to configure the newly introduced options, or run $command.", + command ) } } @@ -75,21 +57,28 @@ object DefaultConfigFeatures { LorenzUtils.chat("§e[SkyHanni] Unknown version $old") return } - optionList = optionList.mapValues { it.value.filter { (togglesInNewVersion == null || it.path in togglesInNewVersion) && (togglesInOldVersion == null || it.path !in togglesInOldVersion) } } - .filter { it.value.isNotEmpty() } + optionList = optionList + .mapValues { it -> + it.value.filter { + (togglesInNewVersion == null || it.path in togglesInNewVersion) && + (togglesInOldVersion == null || it.path !in togglesInOldVersion) + } + } + .filter { (_, filteredOptions) -> filteredOptions.isNotEmpty() } + SkyHanniMod.screenToOpen = DefaultConfigOptionGui(optionList, old, new) } fun applyCategorySelections( - resetSuggestionState: MutableMap<FeatureToggleProcessor.Category, DefaultConfigOptionGui.ResetSuggestionState>, - orderedOptions: Map<FeatureToggleProcessor.Category, List<FeatureToggleProcessor.FeatureToggleableOption>> + resetSuggestionState: MutableMap<Category, ResetSuggestionState>, + orderedOptions: Map<Category, List<FeatureToggleableOption>> ) { - orderedOptions.forEach { cat, options -> + orderedOptions.forEach { (cat, options) -> for (option in options) { val resetState = option.toggleOverride ?: resetSuggestionState[cat]!! - if (resetState == DefaultConfigOptionGui.ResetSuggestionState.LEAVE_DEFAULTS) continue + if (resetState == ResetSuggestionState.LEAVE_DEFAULTS) continue val onState = option.isTrueEnabled - val setTo = if (resetState == DefaultConfigOptionGui.ResetSuggestionState.TURN_ALL_ON) { + val setTo = if (resetState == ResetSuggestionState.TURN_ALL_ON) { onState } else { !onState @@ -107,6 +96,4 @@ object DefaultConfigFeatures { ) return listOf() } - - } diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/DefaultConfigOptionGui.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/DefaultConfigOptionGui.kt index 00fc88dd4..ac1a497fe 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/DefaultConfigOptionGui.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/DefaultConfigOptionGui.kt @@ -8,9 +8,11 @@ import net.minecraft.client.gui.GuiScreen import net.minecraft.client.gui.ScaledResolution import net.minecraft.client.renderer.GlStateManager import org.lwjgl.input.Mouse +import kotlin.math.max +import kotlin.math.min class DefaultConfigOptionGui( - val orderedOptions: Map<FeatureToggleProcessor.Category, List<FeatureToggleProcessor.FeatureToggleableOption>>, + private val orderedOptions: Map<Category, List<FeatureToggleableOption>>, old: String, new: String ) : @@ -21,45 +23,37 @@ class DefaultConfigOptionGui( else "§5SkyHanni Options In Version $new" } else { - if (new == "null") { + if (new == "null") "§5SkyHanni Options since $old" - } else { + else "§5SkyHanni Options $old → $new" - } } - val xSize = 400 - val ySize = 300 - val barSize = 40 - val padding = 10 - var wasMouseDown = false - val cardHeight = 30 - var currentScrollOffset = 0 + private val xSize = 400 + private val ySize = 300 + private val barSize = 40 + private val padding = 10 + private var wasMouseDown = false + private val cardHeight = 30 - enum class ResetSuggestionState(val label: String) { - TURN_ALL_OFF("§c§lTurn all off"), - TURN_ALL_ON("§a§lTurn all on"), - LEAVE_DEFAULTS("§b§lLeave unchanged"), ; + private var currentScrollOffset = 0 - val next get() = entries[(ordinal + 1) % entries.size] - } - - val resetSuggestionState = + private val resetSuggestionState = orderedOptions.keys.associateWith { ResetSuggestionState.LEAVE_DEFAULTS }.toMutableMap() override fun drawScreen(mouseX: Int, mouseY: Int, partialTicks: Float) { super.drawScreen(mouseX, mouseY, partialTicks) drawDefaultBackground() RenderUtils.drawFloatingRectDark((width - xSize) / 2, (height - ySize) / 2, xSize, ySize) - val sr = ScaledResolution(mc) + val scaledResolution = ScaledResolution(mc) var hoveringTextToDraw: List<String>? = null - var mx = mouseX - ((width - xSize) / 2) - padding + val x = mouseX - ((width - xSize) / 2) - padding val isMouseDown = Mouse.isButtonDown(0) val shouldClick = isMouseDown && !wasMouseDown wasMouseDown = isMouseDown val isMouseInScrollArea = - mx in 0..xSize && mouseY in ((height - ySize) / 2) + barSize..((height + ySize) / 2 - barSize) - var my = mouseY - ((height - ySize) / 2 + barSize) + currentScrollOffset + x in 0..xSize && mouseY in ((height - ySize) / 2) + barSize..((height + ySize) / 2 - barSize) + var y = mouseY - ((height - ySize) / 2 + barSize) + currentScrollOffset GlStateManager.pushMatrix() GlStateManager.translate(width / 2F, (height - ySize) / 2F, 0F) @@ -82,45 +76,51 @@ class DefaultConfigOptionGui( 0F ) var i = 0 - fun bt(title: String, tooltip: List<String>, func: () -> Unit) { - val lw = mc.fontRendererObj.getStringWidth(title) - var s = false - if (mouseX - ((width - xSize) / 2 + padding) in i..(i + lw) + fun button(title: String, tooltip: List<String>, func: () -> Unit) { + val width = mc.fontRendererObj.getStringWidth(title) + var overMouse = false + if (mouseX - ((this.width - xSize) / 2 + padding) in i..(i + width) && mouseY - (height + ySize) / 2 in -barSize..0 ) { - s = true + overMouse = true hoveringTextToDraw = tooltip if (shouldClick) { func() } } - RenderUtils.drawFloatingRectDark(i - 1, -3, lw + 4, 14) - mc.fontRendererObj.drawString(title, 2 + i.toFloat(), 0F, if (s) 0xFF00FF00.toInt() else -1, s) - i += lw + 12 + RenderUtils.drawFloatingRectDark(i - 1, -3, width + 4, 14) + mc.fontRendererObj.drawString( + title, + 2 + i.toFloat(), + 0F, + if (overMouse) 0xFF00FF00.toInt() else -1, + overMouse + ) + i += width + 12 } - bt("Apply choices", listOf()) { + button("Apply choices", listOf()) { DefaultConfigFeatures.applyCategorySelections(resetSuggestionState, orderedOptions) mc.displayGuiScreen(null) } - bt("Turn all on", listOf()) { - resetSuggestionState.entries.forEach { - it.setValue(ResetSuggestionState.TURN_ALL_ON); - orderedOptions[it.key]!!.forEach { it.toggleOverride = null } + button("Turn all on", listOf()) { + resetSuggestionState.entries.forEach { entry -> + entry.setValue(ResetSuggestionState.TURN_ALL_ON) + orderedOptions[entry.key]!!.forEach { it.toggleOverride = null } } } - bt("Turn all off", listOf()) { - resetSuggestionState.entries.forEach { - it.setValue(ResetSuggestionState.TURN_ALL_OFF) - orderedOptions[it.key]!!.forEach { it.toggleOverride = null } + button("Turn all off", listOf()) { + resetSuggestionState.entries.forEach { entry -> + entry.setValue(ResetSuggestionState.TURN_ALL_OFF) + orderedOptions[entry.key]!!.forEach { it.toggleOverride = null } } } - bt("Leave all untouched", listOf()) { - resetSuggestionState.entries.forEach { - it.setValue(ResetSuggestionState.LEAVE_DEFAULTS) - orderedOptions[it.key]!!.forEach { it.toggleOverride = null } + button("Leave all untouched", listOf()) { + resetSuggestionState.entries.forEach { entry -> + entry.setValue(ResetSuggestionState.LEAVE_DEFAULTS) + orderedOptions[entry.key]!!.forEach { it.toggleOverride = null } } } - bt("Cancel", listOf()) { + button("Cancel", listOf()) { mc.displayGuiScreen(null) } GlStateManager.popMatrix() @@ -131,7 +131,7 @@ class DefaultConfigOptionGui( (height - ySize) / 2 + barSize, (width + xSize) / 2, (height + ySize) / 2 - barSize, - sr + scaledResolution ) GlStateManager.translate( (width - xSize) / 2F + padding, @@ -141,48 +141,52 @@ class DefaultConfigOptionGui( for ((cat) in orderedOptions.entries) { val suggestionState = resetSuggestionState[cat]!! + drawRect(0, 0, xSize - padding * 2, 1, 0xFF808080.toInt()) drawRect(0, 30, xSize - padding * 2, cardHeight + 1, 0xFF808080.toInt()) drawRect(0, 0, 1, cardHeight, 0xFF808080.toInt()) drawRect(xSize - padding * 2 - 1, 0, xSize - padding * 2, cardHeight, 0xFF808080.toInt()) + mc.fontRendererObj.drawString("§e${cat.name} ${suggestionState.label}", 4, 4, -1) mc.fontRendererObj.drawSplitString("§7${cat.description}", 4, 14, xSize - padding * 2 - 8, -1) - if (isMouseInScrollArea && my in 0..cardHeight) { - hoveringTextToDraw = - listOf( - "§e${cat.name}", - "§7${cat.description}", - "§7Current plan: ${suggestionState.label}", - "§aClick to toggle!", - "§7Hold shift to show all options" - ) + + if (isMouseInScrollArea && y in 0..cardHeight) { + hoveringTextToDraw = listOf( + "§e${cat.name}", + "§7${cat.description}", + "§7Current plan: ${suggestionState.label}", + "§aClick to toggle!", + "§7Hold shift to show all options" + ) + if (isShiftKeyDown()) { hoveringTextToDraw = listOf( "§e${cat.name}", - "§7${cat.description}", + "§7${cat.description}" ) + orderedOptions[cat]!!.map { "§7 - §a" + it.name } } + if (shouldClick) { resetSuggestionState[cat] = suggestionState.next orderedOptions[cat]!!.forEach { it.toggleOverride = null } } } - my -= cardHeight + + y -= cardHeight GlStateManager.translate(0F, cardHeight.toFloat(), 0F) } - GlStateManager.popMatrix() - GlScissorStack.pop(sr) + GlScissorStack.pop(scaledResolution) if (hoveringTextToDraw != null) { Utils.drawHoveringText(hoveringTextToDraw, mouseX, mouseY, width, height, 100, mc.fontRendererObj) } } - fun scroll(s: Int) { + private fun scroll(s: Int) { currentScrollOffset = - Math.max(0, Math.min(s, (orderedOptions.size + 1) * cardHeight - ySize + barSize + padding * 2)) + max(0, min(s, (orderedOptions.size + 1) * cardHeight - ySize + barSize + padding * 2)) } override fun handleMouseInput() { diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/FeatureToggleProcessor.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/FeatureToggleProcessor.kt index c6b1beea5..8d431a64e 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/FeatureToggleProcessor.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/FeatureToggleProcessor.kt @@ -17,16 +17,6 @@ class FeatureToggleProcessor : ConfigStructureReader { allOptions.groupBy { it.category } } - data class FeatureToggleableOption( - val name: String, val description: String, val previouslyEnabled: Boolean, - val isTrueEnabled: Boolean, val category: Category, - val setter: (Boolean) -> Unit, - val path: String, - var toggleOverride: DefaultConfigOptionGui.ResetSuggestionState? = null - ) - - data class Category(val name: String, val description: String) - override fun beginCategory(baseObject: Any?, field: Field?, name: String, description: String) { latestCategory = Category(name, description) } diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/FeatureToggleableOption.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/FeatureToggleableOption.kt new file mode 100644 index 000000000..88b9f1a8e --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/FeatureToggleableOption.kt @@ -0,0 +1,9 @@ +package at.hannibal2.skyhanni.features.misc.massconfiguration + +data class FeatureToggleableOption( + val name: String, val description: String, val previouslyEnabled: Boolean, + val isTrueEnabled: Boolean, val category: Category, + val setter: (Boolean) -> Unit, + val path: String, + var toggleOverride: ResetSuggestionState? = null +)
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/ResetSuggestionState.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/ResetSuggestionState.kt new file mode 100644 index 000000000..30a78a7b2 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/massconfiguration/ResetSuggestionState.kt @@ -0,0 +1,10 @@ +package at.hannibal2.skyhanni.features.misc.massconfiguration + +enum class ResetSuggestionState(val label: String) { + TURN_ALL_OFF("§c§lTurn all off"), + TURN_ALL_ON("§a§lTurn all on"), + LEAVE_DEFAULTS("§b§lLeave unchanged"), + ; + + val next get() = entries[(ordinal + 1) % entries.size] +}
\ No newline at end of file |