Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AutoTool): Settings refactoring #5260

Open
wants to merge 2 commits into
base: nextgen
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
package net.ccbluex.liquidbounce.features.module.modules.world

import it.unimi.dsi.fastutil.ints.IntObjectImmutablePair
import net.ccbluex.liquidbounce.config.types.Choice
import net.ccbluex.liquidbounce.config.types.ChoiceConfigurable
import net.ccbluex.liquidbounce.event.events.BlockBreakingProgressEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.ClientModule
import net.ccbluex.liquidbounce.utils.client.SilentHotbar
import net.ccbluex.liquidbounce.utils.item.isNothing
import net.ccbluex.liquidbounce.utils.inventory.findBestToolToMineBlock
import net.minecraft.block.BlockState
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.item.ItemStack
Expand All @@ -36,20 +38,37 @@ import net.minecraft.util.math.BlockPos
* Automatically chooses the best tool in your inventory to mine a block.
*/
object ModuleAutoTool : ClientModule("AutoTool", Category.WORLD) {
val toolSelector =
choices(
"ToolSelector",
DynamicSelectMode,
arrayOf(DynamicSelectMode, StaticSelectMode)
)

// Ignore items with low durability
private val ignoreDurability by boolean("IgnoreDurability", false)
sealed class ToolSelectorMode(name: String) : Choice(name) {
abstract fun getTool(inventory: PlayerInventory, blockState: BlockState?): IntObjectImmutablePair<ItemStack>?
}

private object DynamicSelectMode : ToolSelectorMode("Dynamic") {
override val parent: ChoiceConfigurable<*>
get() = toolSelector

private val ignoreDurability by boolean("IgnoreDurability", false)

override fun getTool(inventory: PlayerInventory, blockState: BlockState?) =
inventory.findBestToolToMineBlock(blockState, ignoreDurability)
}

// Automatic search for the best weapon
private val search by boolean("Search", true)
private object StaticSelectMode : ToolSelectorMode("Static") {
override val parent: ChoiceConfigurable<*>
get() = toolSelector

/* Slot with the best tool
* Useful if the tool has special effects
* cannot be determined
*
* NOTE: option [search] must be disabled
*/
private val slot by int("Slot", 0, 0..8)
private val slot by int("Slot", 0, 0..8)

override fun getTool(inventory: PlayerInventory, blockState: BlockState?): IntObjectImmutablePair<ItemStack> {
return IntObjectImmutablePair(slot, inventory.getStack(slot))
}
Comment on lines +66 to +70
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we want to choose a static slot though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already there, I didn't change the functionality of AutoTool itself

Copy link
Contributor Author

@sqlerrorthing sqlerrorthing Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at what's already available

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see.

}

private val swapPreviousDelay by int("SwapPreviousDelay", 20, 1..100, "ticks")

Expand All @@ -67,32 +86,8 @@ object ModuleAutoTool : ClientModule("AutoTool", Category.WORLD) {

val blockState = world.getBlockState(pos)
val inventory = player.inventory
val index = getTool(inventory, blockState)?.firstInt() ?: return
val index = toolSelector.activeChoice.getTool(inventory, blockState)?.firstInt() ?: return
SilentHotbar.selectSlotSilently(this, index, swapPreviousDelay)
}

fun getTool(inventory: PlayerInventory, blockState: BlockState?): IntObjectImmutablePair<ItemStack>? {
if (search || !running) {
val (hotbarSlot, stack) =
(0..8).map {
it to inventory.getStack(it)
}.filter { (_, stack) ->
val durabilityCheck = (stack.damage < (stack.maxDamage - 2) || ignoreDurability)
(stack.isNothing() || (!player.isCreative && durabilityCheck))
}.maxByOrNull { (_, stack) ->
stack.getMiningSpeedMultiplier(blockState)
} ?: return null

val miningSpeedMultiplier = stack.getMiningSpeedMultiplier(blockState)

// The current slot already matches the best
if (miningSpeedMultiplier == player.inventory.mainHandStack.getMiningSpeedMultiplier(blockState)) {
return null
}
return IntObjectImmutablePair(hotbarSlot, stack)
} else {
return IntObjectImmutablePair(slot, inventory.getStack(slot))
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ object ModulePacketMine : ClientModule("PacketMine", Category.WORLD) {
return null
}

return ModuleAutoTool.getTool(player.inventory, state)
return ModuleAutoTool.toolSelector.activeChoice.getTool(player.inventory, state)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.viaversion.viaversion.api.Via
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper
import com.viaversion.viaversion.api.type.Types
import com.viaversion.viaversion.protocols.v1_9_1to1_9_3.packet.ServerboundPackets1_9_3
import it.unimi.dsi.fastutil.ints.IntObjectImmutablePair
import net.ccbluex.liquidbounce.config.types.Configurable
import net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.*
import net.ccbluex.liquidbounce.features.module.modules.world.scaffold.ModuleScaffold
Expand All @@ -36,15 +37,16 @@ import net.ccbluex.liquidbounce.utils.client.*
import net.ccbluex.liquidbounce.utils.input.shouldSwingHand
import net.ccbluex.liquidbounce.utils.item.isNothing
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.block.BlockState
import net.minecraft.block.Blocks
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen
import net.minecraft.component.type.DyedColorComponent
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.network.packet.c2s.play.CloseHandledScreenC2SPacket
import net.minecraft.registry.Registries
import net.minecraft.registry.tag.ItemTags
import net.minecraft.util.ActionResult
import net.minecraft.util.Hand
import kotlin.math.abs

Expand Down Expand Up @@ -247,6 +249,29 @@ fun ItemStack.getArmorColor(): Int? {
}
}

fun PlayerInventory.findBestToolToMineBlock(
blockState: BlockState?,
ignoreDurability: Boolean = true
): IntObjectImmutablePair<ItemStack>? {
val (hotbarSlot, stack) =
(0..8).map {
it to getStack(it)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IntObjectImmutablePair(it, getStack(it))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesnt make sense

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int to any? -> Pair<Integer, @Nullable Object> while u has IntObjectImmutablePair as result type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the end it's not my code (I moved it). If you want to change something, change it and create a pr.

}.filter { (_, stack) ->
val durabilityCheck = (stack.damage < (stack.maxDamage - 2) || ignoreDurability)
(stack.isNothing() || (!player.isCreative && durabilityCheck))
}.maxByOrNull { (_, stack) ->
stack.getMiningSpeedMultiplier(blockState)
} ?: return null

val miningSpeedMultiplier = stack.getMiningSpeedMultiplier(blockState)

// The current slot already matches the best
if (miningSpeedMultiplier == player.inventory.mainHandStack.getMiningSpeedMultiplier(blockState)) {
return null
}
return IntObjectImmutablePair(hotbarSlot, stack)
}
Comment on lines +252 to +273
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason to move this into another class when it's not being used multiple times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did it 'cause of the confusion in this code. Logically, I don't think PacketMine should inherit the AutoTool settings.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it still does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm confused, make your own verdict, if it has to inherit AutoTool settings, then yes, there is no point in a separate [findBestToolToMineBlock] method.


/**
* A list of blocks which may not be placed (apart from the usual checks), so inv cleaner and scaffold
* won't count them as blocks
Expand Down
Loading