Skip to content

Commit f4605a2

Browse files
committed
efficient tools only setting for blocks that could be broken faster with tools. They will only allow those tools
1 parent 14080b0 commit f4605a2

File tree

7 files changed

+77
-58
lines changed

7 files changed

+77
-58
lines changed

src/main/kotlin/com/lambda/config/groups/BreakSettings.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class BreakSettings(
7979
override val ignoredBlocks by c.setting("Ignored Blocks", allSigns, description = "Blocks that wont be broken", visibility = vis).group(baseGroup, Group.General)
8080

8181
// Tool
82+
override val efficientOnly by c.setting("Efficient Tools Only", true, "Only use tools suitable for the given block (will get the item drop)") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)
8283
override val suitableToolsOnly by c.setting("Suitable Tools Only", true, "Only use tools suitable for the given block (will get the item drop)") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)
8384
override val forceSilkTouch by c.setting("Force Silk Touch", false, "Force silk touch when breaking blocks") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)
8485
override val forceFortunePickaxe by c.setting("Force Fortune Pickaxe", false, "Force fortune pickaxe when breaking blocks") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)

src/main/kotlin/com/lambda/interaction/construction/processing/ProcessorRegistry.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import com.lambda.util.reflections.getInstances
2323
import net.minecraft.block.BlockState
2424
import net.minecraft.state.property.Properties
2525
import net.minecraft.util.math.BlockPos
26+
import java.util.*
2627

2728
object ProcessorRegistry : Loadable {
2829
private val processors = getInstances<PlacementProcessor>()
29-
private val processorCache = mutableMapOf<BlockState, PreProcessingInfo?>()
30+
private val processorCache = Collections.synchronizedMap<BlockState, PreProcessingInfo?>(mutableMapOf())
3031

3132
val postProcessedProperties = setOf(
3233
Properties.EXTENDED,

src/main/kotlin/com/lambda/interaction/construction/simulation/checks/BreakChecker.kt

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ import com.lambda.interaction.construction.verify.TargetState
3333
import com.lambda.interaction.material.ContainerSelection.Companion.selectContainer
3434
import com.lambda.interaction.material.StackSelection
3535
import com.lambda.interaction.material.StackSelection.Companion.EVERYTHING
36-
import com.lambda.interaction.material.StackSelection.Companion.select
3736
import com.lambda.interaction.material.StackSelection.Companion.selectStack
3837
import com.lambda.interaction.material.container.ContainerManager.containerWithMaterial
3938
import com.lambda.interaction.material.container.MaterialContainer
39+
import com.lambda.interaction.request.hotbar.HotbarManager
4040
import com.lambda.interaction.request.rotating.RotationManager
4141
import com.lambda.interaction.request.rotating.RotationRequest
4242
import com.lambda.interaction.request.rotating.visibilty.lookAt
4343
import com.lambda.interaction.request.rotating.visibilty.lookAtBlock
44-
import com.lambda.threading.runSafe
4544
import com.lambda.util.BlockUtils.blockState
4645
import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
4746
import com.lambda.util.BlockUtils.instantBreakable
4847
import com.lambda.util.BlockUtils.isEmpty
48+
import com.lambda.util.item.ItemStackUtils.inventoryIndex
4949
import com.lambda.util.item.ItemStackUtils.inventoryIndexOrSelected
5050
import com.lambda.util.world.raycast.RayCastUtils.blockResult
5151
import net.minecraft.block.BlockState
@@ -71,40 +71,6 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
7171
: SimChecker<BreakResult>(), Dependable,
7272
ISimInfo by simInfo
7373
{
74-
val stackSelection: StackSelection by lazy {
75-
runSafe {
76-
selectStack(
77-
count = 0,
78-
sorter = compareByDescending<ItemStack> {
79-
it.canBreak(CachedBlockPosition(world, pos, false))
80-
}.thenByDescending {
81-
state.calcItemBlockBreakingDelta(pos, it)
82-
}
83-
) {
84-
EVERYTHING
85-
.andIf(breakConfig.suitableToolsOnly) {
86-
isSuitableForBreaking(state)
87-
}.andIf(breakConfig.forceSilkTouch) {
88-
hasEnchantment(Enchantments.SILK_TOUCH)
89-
}.andIf(breakConfig.forceFortunePickaxe) {
90-
hasEnchantment(Enchantments.FORTUNE)
91-
}.andIf(!breakConfig.useWoodenTools) {
92-
hasTag(WOODEN_TOOL_MATERIALS).not()
93-
}.andIf(!breakConfig.useStoneTools) {
94-
hasTag(STONE_TOOL_MATERIALS).not()
95-
}.andIf(!breakConfig.useIronTools) {
96-
hasTag(IRON_TOOL_MATERIALS).not()
97-
}.andIf(!breakConfig.useDiamondTools) {
98-
hasTag(DIAMOND_TOOL_MATERIALS).not()
99-
}.andIf(!breakConfig.useGoldTools) {
100-
hasTag(GOLD_TOOL_MATERIALS).not()
101-
}.andIf(!breakConfig.useNetheriteTools) {
102-
hasTag(NETHERITE_TOOL_MATERIALS).not()
103-
}
104-
}
105-
} ?: EVERYTHING.select()
106-
}
107-
10874
override fun asDependent(buildResult: BuildResult) =
10975
BreakResult.Dependency(pos, buildResult)
11076

@@ -134,7 +100,7 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
134100

135101
if (breakConfig.avoidLiquids && affectsFluids()) return true
136102

137-
val swapStack = getSwapStack() ?: return true
103+
val (swapStack, stackSelection) = getSwapStack() ?: return true
138104
val instant = instantBreakable(
139105
state, pos,
140106
if (breakConfig.swapMode.isEnabled()) swapStack else player.mainHandStack,
@@ -181,35 +147,67 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
181147
return true
182148
}
183149

184-
private fun AutomatedSafeContext.getSwapStack(): ItemStack? {
150+
private fun AutomatedSafeContext.getSwapStack(): Pair<ItemStack, StackSelection>? {
151+
val stackSelection = selectStack(
152+
count = 0,
153+
sorter = compareByDescending<ItemStack> {
154+
it.canBreak(CachedBlockPosition(world, pos, false))
155+
}.thenByDescending {
156+
state.calcItemBlockBreakingDelta(pos, it)
157+
}.thenByDescending {
158+
it.inventoryIndex == HotbarManager.serverSlot
159+
}
160+
) {
161+
EVERYTHING
162+
.andIf(breakConfig.efficientOnly) {
163+
isEfficientForBreaking(state)
164+
}.andIf(breakConfig.suitableToolsOnly) {
165+
isSuitableForBreaking(state)
166+
}.andIf(breakConfig.forceSilkTouch) {
167+
hasEnchantment(Enchantments.SILK_TOUCH)
168+
}.andIf(breakConfig.forceFortunePickaxe) {
169+
hasEnchantment(Enchantments.FORTUNE)
170+
}.andIf(!breakConfig.useWoodenTools) {
171+
hasTag(WOODEN_TOOL_MATERIALS).not()
172+
}.andIf(!breakConfig.useStoneTools) {
173+
hasTag(STONE_TOOL_MATERIALS).not()
174+
}.andIf(!breakConfig.useIronTools) {
175+
hasTag(IRON_TOOL_MATERIALS).not()
176+
}.andIf(!breakConfig.useDiamondTools) {
177+
hasTag(DIAMOND_TOOL_MATERIALS).not()
178+
}.andIf(!breakConfig.useGoldTools) {
179+
hasTag(GOLD_TOOL_MATERIALS).not()
180+
}.andIf(!breakConfig.useNetheriteTools) {
181+
hasTag(NETHERITE_TOOL_MATERIALS).not()
182+
}
183+
}
184+
185185
val silentSwapSelection = selectContainer {
186186
ofAnyType(MaterialContainer.Rank.HOTBAR)
187187
}
188188

189-
val swapCandidates = stackSelection.containerWithMaterial(silentSwapSelection)
189+
val swapCandidates = stackSelection
190+
.containerWithMaterial(silentSwapSelection)
191+
.map { it.matchingStacks(stackSelection) }
192+
.flatten()
190193
if (swapCandidates.isEmpty()) {
191194
result(GenericResult.WrongItemSelection(pos, stackSelection, player.mainHandStack))
192195
return null
193196
}
194197

195-
return swapCandidates
196-
.map { it.matchingStacks(stackSelection) }
197-
.asSequence()
198-
.flatten()
199-
.let { containerStacks ->
200-
var bestStack = ItemStack.EMPTY
201-
var bestBreakDelta = -1f
202-
containerStacks.forEach { stack ->
203-
val breakDelta = state.calcItemBlockBreakingDelta(pos, stack)
204-
if (breakDelta > bestBreakDelta ||
205-
(stack == player.mainHandStack && breakDelta >= bestBreakDelta)
206-
) {
207-
bestBreakDelta = breakDelta
208-
bestStack = stack
209-
}
210-
}
211-
bestStack
198+
var bestStack = ItemStack.EMPTY
199+
var bestBreakDelta = -1f
200+
swapCandidates.forEach { stack ->
201+
val breakDelta = state.calcItemBlockBreakingDelta(pos, stack)
202+
if (breakDelta > bestBreakDelta ||
203+
(stack == player.mainHandStack && breakDelta >= bestBreakDelta)
204+
) {
205+
bestBreakDelta = breakDelta
206+
bestStack = stack
212207
}
208+
}
209+
return if (bestBreakDelta == -1f) null
210+
else Pair(bestStack, stackSelection)
213211
}
214212

215213
private suspend fun AutomatedSafeContext.affectsFluids(): Boolean {

src/main/kotlin/com/lambda/interaction/material/StackSelection.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package com.lambda.interaction.material
2020
import com.lambda.interaction.material.StackSelection.Companion.StackSelectionDsl
2121
import com.lambda.util.EnchantmentUtils.getEnchantment
2222
import com.lambda.util.item.ItemStackUtils.shulkerBoxContents
23+
import com.lambda.util.item.ItemUtils
2324
import net.minecraft.block.Block
2425
import net.minecraft.block.BlockState
2526
import net.minecraft.component.ComponentType
@@ -31,6 +32,7 @@ import net.minecraft.item.consume.UseAction
3132
import net.minecraft.registry.RegistryKey
3233
import net.minecraft.registry.tag.TagKey
3334
import net.minecraft.screen.slot.Slot
35+
import java.util.*
3436
import kotlin.reflect.KClass
3537

3638
/**
@@ -159,6 +161,14 @@ class StackSelection {
159161
*/
160162
fun isOneOfStacks(stacks: Collection<ItemStack>): (ItemStack) -> Boolean = stacks::contains
161163

164+
fun isEfficientForBreaking(blockState: BlockState): (ItemStack) -> Boolean = { itemStack ->
165+
val hasEfficientTool = efficientToolCache.getOrPut(blockState) {
166+
ItemUtils.tools.any { it.getMiningSpeed(it.defaultStack, blockState) > 1f }
167+
}
168+
if (hasEfficientTool) itemStack.item.getMiningSpeed(itemStack, blockState) > 1f
169+
else false
170+
}
171+
162172
fun isSuitableForBreaking(blockState: BlockState): (ItemStack) -> Boolean = { it.isSuitableFor(blockState) }
163173

164174
fun hasTag(tag: TagKey<Item>): (ItemStack) -> Boolean = { it.isIn(tag) }
@@ -275,6 +285,8 @@ class StackSelection {
275285
val NOTHING: (ItemStack) -> Boolean = { false }
276286
val NO_COMPARE: Comparator<ItemStack> = Comparator { _, _ -> 0 }
277287

288+
val efficientToolCache: MutableMap<BlockState, Boolean> = Collections.synchronizedMap<BlockState, Boolean>(mutableMapOf())
289+
278290
@StackSelectionDsl
279291
fun Item.select() = selectStack { isItem(this@select) }
280292

src/main/kotlin/com/lambda/interaction/request/breaking/BreakConfig.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ interface BreakConfig : RequestConfig {
5757
val avoidSupporting: Boolean
5858
val ignoredBlocks: Set<Block>
5959

60+
val efficientOnly: Boolean
6061
val suitableToolsOnly: Boolean
6162
val forceSilkTouch: Boolean
6263
val forceFortunePickaxe: Boolean

src/main/kotlin/com/lambda/module/modules/player/FastBreak.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ object FastBreak : Module(
6666
editTyped(
6767
::avoidLiquids,
6868
::avoidSupporting,
69+
::efficientOnly,
6970
::suitableToolsOnly,
7071
::rotateForBreak,
7172
::doubleBreak

src/main/kotlin/com/lambda/module/modules/player/PacketMine.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ object PacketMine : Module(
7979
private val endColor by setting("End Color", Color(255, 0, 0, 60).brighter(), "The color of the end (farthest from breaking) of the queue") { renderQueue && dynamicColor }.group(Group.Break, BreakSettings.Group.Cosmetic)
8080

8181
override val breakConfig = BreakSettings(this, Group.Break).apply {
82-
editTyped(::avoidLiquids, ::avoidSupporting, ::suitableToolsOnly) { defaultValue(false) }
82+
editTyped(
83+
::avoidLiquids,
84+
::avoidSupporting,
85+
::efficientOnly,
86+
::suitableToolsOnly
87+
) { defaultValue(false) }
8388
::swing.edit { defaultValue(BreakConfig.SwingMode.Start) }
8489

8590
::rebreak.insert(::rebreakMode, SettingGroup.InsertMode.Below)

0 commit comments

Comments
 (0)