Skip to content

Commit

Permalink
refactor(legacy): ClickGUI Panel (#5551)
Browse files Browse the repository at this point in the history
  • Loading branch information
MukjepScarlet authored Feb 7, 2025
1 parent ddf1ec3 commit fbcf872
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ object ModuleManager : Listenable, Collection<Module> by MODULE_REGISTRY {
*/
operator fun get(moduleName: String) = MODULE_REGISTRY.find { it.name.equals(moduleName, ignoreCase = true) }

/**
* Get modules by [category]
*/
operator fun get(category: Category) = MODULE_REGISTRY.filter { it.category === category }

@Deprecated(message = "Only for outdated scripts", replaceWith = ReplaceWith("get(moduleClass)"))
fun getModule(moduleClass: Class<out Module>) = get(moduleClass)

Expand Down
141 changes: 71 additions & 70 deletions src/main/java/net/ccbluex/liquidbounce/ui/client/clickgui/ClickGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
*/
package net.ccbluex.liquidbounce.ui.client.clickgui

import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.ccbluex.liquidbounce.LiquidBounce.CLIENT_NAME
import net.ccbluex.liquidbounce.LiquidBounce.moduleManager
import net.ccbluex.liquidbounce.api.ClientApi
import net.ccbluex.liquidbounce.api.autoSettingsList
import net.ccbluex.liquidbounce.api.loadSettings
import net.ccbluex.liquidbounce.config.SettingsUtils
import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.modules.render.ClickGUI
Expand Down Expand Up @@ -49,7 +48,8 @@ import kotlin.math.roundToInt

object ClickGui : GuiScreen() {

val panels = mutableListOf<Panel>()
// Note: hash key = [Panel.name]
val panels = linkedSetOf<Panel>()
private val hudIcon = ResourceLocation("${CLIENT_NAME.lowercase()}/custom_hud_icon.png")
var style: Style = LiquidBounceStyle
private var mouseX = 0
Expand All @@ -75,12 +75,15 @@ object ClickGui : GuiScreen() {
var yPos = 5

for (category in Category.entries) {
panels += object : Panel(category.displayName, 100, yPos, width, height, false) {
override val elements = moduleManager.mapNotNull {
it.takeIf { module -> module.category == category }?.let(::ModuleElement)
}

}
panels += Panel(
category.displayName,
x = 100,
y = yPos,
width,
height,
false,
moduleManager[category].map(::ModuleElement)
)

yPos += 20
}
Expand All @@ -94,69 +97,67 @@ object ClickGui : GuiScreen() {
}

private fun setupTargetsPanel(xPos: Int = 100, yPos: Int, width: Int, height: Int) =
object : Panel("Targets", xPos, yPos, width, height, false) {

override val elements = listOf(
ButtonElement("Players", { if (Targets.player) guiColor else Int.MAX_VALUE }) {
Targets.player = !Targets.player
},
ButtonElement("Mobs", { if (Targets.mob) guiColor else Int.MAX_VALUE }) {
Targets.mob = !Targets.mob
},
ButtonElement("Animals", { if (Targets.animal) guiColor else Int.MAX_VALUE }) {
Targets.animal = !Targets.animal
},
ButtonElement("Invisible", { if (Targets.invisible) guiColor else Int.MAX_VALUE }) {
Targets.invisible = !Targets.invisible
},
ButtonElement("Dead", { if (Targets.dead) guiColor else Int.MAX_VALUE }) {
Targets.dead = !Targets.dead
},
)
Panel("Targets", xPos, yPos, width, height, false, listOf(
ButtonElement("Players", { if (Targets.player) guiColor else Int.MAX_VALUE }) {
Targets.player = !Targets.player
},
ButtonElement("Mobs", { if (Targets.mob) guiColor else Int.MAX_VALUE }) {
Targets.mob = !Targets.mob
},
ButtonElement("Animals", { if (Targets.animal) guiColor else Int.MAX_VALUE }) {
Targets.animal = !Targets.animal
},
ButtonElement("Invisible", { if (Targets.invisible) guiColor else Int.MAX_VALUE }) {
Targets.invisible = !Targets.invisible
},
ButtonElement("Dead", { if (Targets.dead) guiColor else Int.MAX_VALUE }) {
Targets.dead = !Targets.dead
},
))

private fun setupSettingsPanel(xPos: Int = 100, yPos: Int, width: Int, height: Int): Panel {
val list = autoSettingsList?.map { setting ->
ButtonElement(setting.name, { Integer.MAX_VALUE }) {
SharedScopes.IO.launch {
try {
chat("Loading settings...")

// Load settings and apply them
val settings = ClientApi.getSettingsScript(settingId = setting.settingId)

chat("Applying settings...")
SettingsUtils.applyScript(settings)

chat("§6Settings applied successfully.")
HUD.addNotification(Notification.informative("ClickGUI","Updated Settings"))
mc.playSound("random.anvil_use".asResourceLocation())
} catch (e: Exception) {
ClientUtils.LOGGER.error("Failed to load settings", e)
chat("Failed to load settings: ${e.message}")
}
}
}.apply {
this.hoverText = buildString {
appendLine("§7Description: §e${setting.description.ifBlank { "No description available" }}")
appendLine("§7Type: §e${setting.type.displayName}")
appendLine("§7Contributors: §e${setting.contributors}")
appendLine("§7Last updated: §e${setting.date}")
append("§7Status: §e${setting.statusType.displayName} §a(${setting.statusDate})")
}
}
} ?: run {
// Try load settings
loadSettings(useCached = true) {
mc.addScheduledTask {
setupSettingsPanel(xPos, yPos, width, height)
}
}

emptyList()
}

private fun setupSettingsPanel(xPos: Int = 100, yPos: Int, width: Int, height: Int) =
object : Panel("Auto Settings", xPos, yPos, width, height, false) {

/**
* Auto settings list
*/
override val elements = runBlocking {
SharedScopes.IO.async {
autoSettingsList?.map { setting ->
ButtonElement(setting.name, { Integer.MAX_VALUE }) {
SharedScopes.IO.launch {
try {
chat("Loading settings...")

// Load settings and apply them
val settings = ClientApi.getSettingsScript(settingId = setting.settingId)

chat("Applying settings...")
SettingsUtils.applyScript(settings)

chat("§6Settings applied successfully")
HUD.addNotification(Notification.informative("ClickGUI","Updated Settings"))
mc.playSound("random.anvil_use".asResourceLocation())
} catch (e: Exception) {
ClientUtils.LOGGER.error("Failed to load settings", e)
chat("Failed to load settings: ${e.message}")
}
}
}.apply {
this.hoverText = buildString {
appendLine("§7Description: §e${setting.description.ifBlank { "No description available" }}")
appendLine("§7Type: §e${setting.type.displayName}")
appendLine("§7Contributors: §e${setting.contributors}")
appendLine("§7Last updated: §e${setting.date}")
append("§7Status: §e${setting.statusType.displayName} §a(${setting.statusDate})")
}
}
} ?: emptyList()
}.await()
}
}
return Panel("Auto Settings", xPos, yPos, width, height, false, list)
}

override fun drawScreen(x: Int, y: Int, partialTicks: Float) {
// Enable DisplayList optimization
Expand Down Expand Up @@ -258,7 +259,7 @@ object ClickGui : GuiScreen() {
panel.drag = true

// Move dragged panel to top.
panels.removeAt(panels.lastIndex - index)
panels.remove(panel)
panels += panel
return
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/net/ccbluex/liquidbounce/ui/client/clickgui/Panel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ import kotlin.math.min
import kotlin.math.roundToInt

@SideOnly(Side.CLIENT)
abstract class Panel(val name: String, var x: Int, var y: Int, val width: Int, val height: Int, var open: Boolean) : MinecraftInstance {
abstract val elements: List<Element>
class Panel(
val name: String,
var x: Int,
var y: Int,
val width: Int,
val height: Int,
var open: Boolean,
val elements: List<Element>
) : MinecraftInstance {

var x2 = 0
var y2 = 0
Expand Down Expand Up @@ -199,4 +206,8 @@ abstract class Panel(val name: String, var x: Int, var y: Int, val width: Int, v
}

fun isHovered(mouseX: Int, mouseY: Int) = mouseX in x..x + width && mouseY in y..y + height

override fun hashCode(): Int = this.name.hashCode()

override fun equals(other: Any?): Boolean = other is Panel && other.name == this.name
}

0 comments on commit fbcf872

Please sign in to comment.