Skip to content

Commit

Permalink
refactor(legacy): API cleanup for Fonts/FileManager/ModuleManager/Pac…
Browse files Browse the repository at this point in the history
…ketUtils/MinecraftInstance (#4979)
  • Loading branch information
MukjepScarlet authored Dec 20, 2024
1 parent 9053c5f commit 6964caa
Show file tree
Hide file tree
Showing 88 changed files with 342 additions and 398 deletions.
10 changes: 5 additions & 5 deletions src/main/java/net/ccbluex/liquidbounce/LiquidBounce.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ object LiquidBounce {
// TODO: make it configurable
UIManager.setLookAndFeel(FlatMacLightLaf())

SharedScopes

// Load languages
loadLanguages()

SharedScopes
// Load client fonts
loadFonts()

// Register listeners
RotationUtils
Expand All @@ -143,9 +146,6 @@ object LiquidBounce {
WaitMsUtils
BlinkUtils

// Load client fonts
loadFonts()

// Load settings
loadSettings(false) {
LOGGER.info("Successfully loaded ${it.size} settings.")
Expand Down Expand Up @@ -185,7 +185,7 @@ object LiquidBounce {
HeadsTab()
}

// Disable optifine fastrender
// Disable Optifine FastRender
disableFastRender()

// Load alt generators
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/ccbluex/liquidbounce/cape/CapeAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.io.File
import java.util.*
import net.ccbluex.liquidbounce.utils.client.ClientUtils.LOGGER

object CapeAPI : MinecraftInstance() {
object CapeAPI : MinecraftInstance {

private val capesCache = File(dir, "capes").apply {
mkdir()
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/ccbluex/liquidbounce/cape/CapeService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import java.util.concurrent.atomic.AtomicLong
* We know this might cause sometimes users to not have their capes shown immediately when account switches, but we can reduce the stress
* on the API and the connection of the user.
*/
object CapeService : Listenable, MinecraftInstance() {
object CapeService : Listenable, MinecraftInstance {

/**
* The client cape user
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/ccbluex/liquidbounce/chat/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import net.ccbluex.liquidbounce.utils.login.UserUtils
import java.net.URI
import java.util.*

abstract class Client : ClientListener, MinecraftInstance() {
abstract class Client : ClientListener, MinecraftInstance {

internal var channel: Channel? = null
var username = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object SettingsUtils {
* @param script The script to apply.
*/
fun applyScript(script: String) {
script.lines().forEachIndexed { index, s ->
script.lineSequence().forEachIndexed { index, s ->
if (s.isEmpty() || s.startsWith('#')) {
return@forEachIndexed
}
Expand Down Expand Up @@ -185,7 +185,7 @@ object SettingsUtils {
val all = values && binds && states

return buildString {
for (module in moduleManager.modules) {
for (module in moduleManager) {
if (all || !module.subjective) {
if (values) {
for (value in module.values) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/ccbluex/liquidbounce/config/Value.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import net.minecraft.client.gui.FontRenderer
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

abstract class Value<T>(
sealed class Value<T>(
val name: String,
protected open var value: T,
val subjective: Boolean = false,
Expand Down Expand Up @@ -309,7 +309,7 @@ open class FontValue(
val valueObject = JsonObject()
valueObject.run {
addProperty("fontName", fontDetails.name)
addProperty("fontSize", fontDetails.fontSize)
addProperty("fontSize", fontDetails.size)
}
return valueObject
}
Expand All @@ -327,7 +327,7 @@ open class FontValue(
else -> {
val fontInfo = Fonts.getFontDetails(value)
fontInfo?.let {
"${it.name}${if (it.fontSize != -1) " - ${it.fontSize}" else ""}"
"${it.name}${if (it.size != -1) " - ${it.size}" else ""}"
} ?: "Font: Unknown"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import net.ccbluex.liquidbounce.utils.client.MinecraftInstance
import net.ccbluex.liquidbounce.utils.client.asResourceLocation
import net.ccbluex.liquidbounce.utils.client.playSound

abstract class Command(val command: String, vararg val alias: String) : MinecraftInstance() {
abstract class Command(val command: String, vararg val alias: String) : MinecraftInstance {
/**
* Execute commands with provided [args]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object AutoDisableCommand : Command("autodisable") {
}

val moduleName = args[2]
val module = ModuleManager.getModule(moduleName)
val module = ModuleManager[moduleName]

if (module != null) {
if (AutoDisable.getModules().contains(module)) {
Expand All @@ -49,7 +49,7 @@ object AutoDisableCommand : Command("autodisable") {
}

val moduleName = args[2]
val module = ModuleManager.getModule(moduleName)
val module = ModuleManager[moduleName]

if (module != null) {
if (AutoDisable.getModules().contains(module)) {
Expand Down Expand Up @@ -84,7 +84,7 @@ object AutoDisableCommand : Command("autodisable") {
when (args[0].lowercase()) {
"add" -> {
val input = args[1].lowercase()
ModuleManager.modules.filter { it.name.lowercase().startsWith(input) }.map { it.name }
ModuleManager.filter { it.name.lowercase().startsWith(input) }.map { it.name }
}

"remove" -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object BindCommand : Command("bind") {
val moduleName = args[0]

return when (args.size) {
1 -> moduleManager.modules
1 -> moduleManager
.map { it.name }
.filter { it.startsWith(moduleName, true) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object BindsCommand : Command("binds") {
override fun execute(args: Array<String>) {
if (args.size > 1) {
if (args[1].equals("clear", true)) {
for (module in moduleManager.modules)
for (module in moduleManager)
module.keyBind = Keyboard.KEY_NONE

chat("Removed all binds.")
Expand All @@ -25,7 +25,7 @@ object BindsCommand : Command("binds") {
}

chat("§c§lBinds")
moduleManager.modules.forEach {
moduleManager.forEach {
if (it.keyBind != Keyboard.KEY_NONE)
chat("§6> §c${it.getName()}: §a§l${Keyboard.getKeyName(it.keyBind)}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ object HideCommand : Command("hide") {
when (args[1].lowercase()) {
"list" -> {
chat("§c§lHidden")
moduleManager.modules.forEach {
moduleManager.forEach {
if (!it.inArray) chat("§6> §c${it.getName()}")
}
}

"clear" -> {
for (module in moduleManager.modules)
for (module in moduleManager)
module.inArray = true

chat("Cleared hidden modules.")
}

"reset" -> {
for (module in moduleManager.modules)
for (module in moduleManager)
module.inArray = module.defaultInArray

chat("Reset hidden modules.")
Expand Down Expand Up @@ -70,7 +70,7 @@ object HideCommand : Command("hide") {
val moduleName = args[0]

return when (args.size) {
1 -> moduleManager.modules.mapNotNull { it.name.takeIf { it.startsWith(moduleName, true) == true } }
1 -> moduleManager.mapNotNull { it.name.takeIf { it.startsWith(moduleName, true) == true } }
else -> emptyList()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object PanicCommand : Command("panic") {
* Execute commands with provided [args]
*/
override fun execute(args: Array<String>) {
var modules = moduleManager.modules.filter { it.state }
var modules = moduleManager.filter { it.state }
val msg: String

if (args.size > 1 && args[1].isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object ReloadCommand : Command("reload", "configreload") {
disableScripts()
unloadScripts()

for (module in moduleManager.modules)
for (module in moduleManager)
moduleManager.generateCommand(module)

chat("§c§lReloading scripts...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ object ScriptManagerCommand : Command("scriptmanager", "scripts") {

reloadScripts()

for (module in moduleManager.modules) moduleManager.generateCommand(module)
for (module in moduleManager) moduleManager.generateCommand(module)
loadConfig(modulesConfig)

isStarting = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object ToggleCommand : Command("toggle", "t") {
val moduleName = args[0]

return when (args.size) {
1 -> moduleManager.modules
1 -> moduleManager
.map { it.name }
.filter { it.startsWith(moduleName, true) }
.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import net.ccbluex.liquidbounce.utils.timing.TickedActions.TickScheduler
import org.lwjgl.input.Keyboard
import java.util.concurrent.CopyOnWriteArraySet

private val SPLIT_REGEX = "(?<=[a-z])(?=[A-Z])".toRegex()

open class Module(
val name: String,
val category: Category,
Expand All @@ -35,12 +37,12 @@ open class Module(
private val forcedDescription: String? = null,

// Adds spaces between lowercase and uppercase letters (KillAura -> Kill Aura)
val spacedName: String = name.split("(?<=[a-z])(?=[A-Z])".toRegex()).joinToString(separator = " "),
val spacedName: String = name.splitToSequence(SPLIT_REGEX).joinToString(separator = " "),
val subjective: Boolean = category == Category.RENDER,
val gameDetecting: Boolean = canBeEnabled,
val hideModule: Boolean = false,

) : MinecraftInstance(), Listenable {
) : MinecraftInstance, Listenable {

// Value that determines whether the module should depend on GameDetector
private val onlyInGameValue = boolean("OnlyInGame", true, subjective = true) { GameDetector.state }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import net.ccbluex.liquidbounce.features.module.modules.world.*
import net.ccbluex.liquidbounce.features.module.modules.world.Timer
import net.ccbluex.liquidbounce.features.module.modules.world.scaffolds.Scaffold
import net.ccbluex.liquidbounce.utils.client.ClientUtils.LOGGER
import net.ccbluex.liquidbounce.utils.inventory.InventoryManager
import java.util.*

object ModuleManager : Listenable {
private val MODULE_REGISTRY = TreeSet(Comparator.comparing(Module::name))

val modules = TreeSet<Module> { module1, module2 -> module1.name.compareTo(module2.name) }
private val moduleClassMap = hashMapOf<Class<*>, Module>()
object ModuleManager : Listenable, Collection<Module> by MODULE_REGISTRY {

/**
* Register all modules
Expand Down Expand Up @@ -224,30 +222,10 @@ object ModuleManager : Listenable {
* Register [module]
*/
fun registerModule(module: Module) {
modules += module
moduleClassMap[module.javaClass] = module

MODULE_REGISTRY += module
generateCommand(module)
}

/**
* Register [moduleClass] with new instance
*/
private fun registerModule(moduleClass: Class<out Module>) {
try {
registerModule(moduleClass.newInstance())
} catch (e: Throwable) {
LOGGER.error("Failed to load module: ${moduleClass.name} (${e.javaClass.name}: ${e.message})")
}
}

/**
* Register a list of modules
*/
@SafeVarargs
fun registerModules(vararg modules: Class<out Module>) = modules.forEach(this::registerModule)


/**
* Register a list of modules
*/
Expand All @@ -258,8 +236,7 @@ object ModuleManager : Listenable {
* Unregister module
*/
fun unregisterModule(module: Module) {
modules.remove(module)
moduleClassMap.remove(module::class.java)
MODULE_REGISTRY.remove(module)
unregisterListener(module)
}

Expand All @@ -275,29 +252,16 @@ object ModuleManager : Listenable {
registerCommand(ModuleCommand(module, values))
}

/**
* Get module by [moduleClass]
*/
fun getModule(moduleClass: Class<*>) = moduleClassMap[moduleClass]!!

operator fun get(clazz: Class<*>) = getModule(clazz)

/**
* Get module by [moduleName]
*/
fun getModule(moduleName: String?) = modules.find { it.name.equals(moduleName, ignoreCase = true) }

operator fun get(name: String) = getModule(name)

/**
* Module related events
*/
operator fun get(moduleName: String) = MODULE_REGISTRY.find { it.name.equals(moduleName, ignoreCase = true) }

/**
* Handle incoming key presses
*/
private val onKey = handler<KeyEvent> { event ->
modules.forEach { if (it.keyBind == event.key) it.toggle() }
MODULE_REGISTRY.forEach { if (it.keyBind == event.key) it.toggle() }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ object Backtrack : Module("Backtrack", Category.COMBAT, hideModule = false) {
get() = synchronized(packetQueue) { packetQueue.isEmpty() }

val areQueuedPacketsEmpty
get() = PacketUtils.queuedPackets?.run { synchronized(this) { isEmpty() } } == true
get() = PacketUtils.isQueueEmpty()

val onPacket = handler<PacketEvent> { event ->
val packet = event.packet
Expand Down Expand Up @@ -472,7 +472,7 @@ object Backtrack : Module("Backtrack", Category.COMBAT, hideModule = false) {
synchronized(packetQueue) {
packetQueue.removeAll { (packet, timestamp) ->
if (timestamp <= System.currentTimeMillis() - supposedDelay) {
schedulePacketProcess(packet)
PacketUtils.schedulePacketProcess(packet)
true
} else false
}
Expand All @@ -494,7 +494,7 @@ object Backtrack : Module("Backtrack", Category.COMBAT, hideModule = false) {
synchronized(packetQueue) {
packetQueue.removeAll { (packet, timestamp) ->
if (timestamp <= time) {
schedulePacketProcess(packet)
PacketUtils.schedulePacketProcess(packet)
true
} else false
}
Expand Down Expand Up @@ -533,7 +533,7 @@ object Backtrack : Module("Backtrack", Category.COMBAT, hideModule = false) {
synchronized(packetQueue) {
packetQueue.removeAll {
if (handlePackets) {
schedulePacketProcess(it.packet)
PacketUtils.schedulePacketProcess(it.packet)
}

true
Expand Down
Loading

0 comments on commit 6964caa

Please sign in to comment.