AutoMobHeal module

AutoMobHeal: Minecraft 26.3 cheat module, full source for every implementation (1 client: LiquidBounce (Kotlin)).

1 implementationWORLD

Clients with this module

Full module source: 1 implementation

Every implementation below is the complete module file, verbatim. Expand a client to read its code.

LiquidBounce (Kotlin)WORLDsrc/main/kotlin/net/ccbluex/liquidbounce/features/module/modules/world/automobheal/AutoMobHeal.ktKotlin · 460 lines · 16.7 KB

Automatically heals nearby vanilla-healable mobs using their vanilla repair or feeding items.

SettingsRangeDelaySlotResetDelaySwingModeHealthThresholdNoWasteAllowBucketFoodAllowGoldenCarrotAllowGoldenAppleAllowEnchantedGoldenApple
AutoMobHeal.ktKotlin · Minecraft 26.2 · 460 lines · 16.7 KB · complete file, verbatimOriginal ↗
1
/*
2
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
3
 *
4
 * Copyright (c) 2015 - 2026 CCBlueX
5
 *
6
 * LiquidBounce is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * LiquidBounce is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
package net.ccbluex.liquidbounce.features.module.modules.world.automobheal
21
22
import net.ccbluex.liquidbounce.config.types.group.ToggleableValueGroup
23
import net.ccbluex.liquidbounce.event.tickHandler
24
import net.ccbluex.liquidbounce.event.waitTicks
25
import net.ccbluex.liquidbounce.features.module.ClientModule
26
import net.ccbluex.liquidbounce.features.module.ModuleCategories
27
import net.ccbluex.liquidbounce.features.module.modules.world.automobheal.MobFoodOption.Companion.foodNutritionHeal
28
import net.ccbluex.liquidbounce.utils.block.SwingMode
29
import net.ccbluex.liquidbounce.utils.client.SilentHotbar
30
import net.ccbluex.liquidbounce.utils.entity.interactEntity
31
import net.ccbluex.liquidbounce.utils.entity.squaredBoxedDistanceTo
32
import net.ccbluex.liquidbounce.utils.inventory.HotbarItemSlot
33
import net.ccbluex.liquidbounce.utils.inventory.Slots
34
import net.ccbluex.liquidbounce.utils.inventory.findClosestSlot
35
import net.minecraft.tags.ItemTags
36
import net.minecraft.util.ToFloatFunction
37
import net.minecraft.world.entity.Entity
38
import net.minecraft.world.entity.LivingEntity
39
import net.minecraft.world.entity.animal.camel.Camel
40
import net.minecraft.world.entity.animal.camel.CamelHusk
41
import net.minecraft.world.entity.animal.equine.AbstractHorse
42
import net.minecraft.world.entity.animal.equine.Llama
43
import net.minecraft.world.entity.animal.equine.SkeletonHorse
44
import net.minecraft.world.entity.animal.equine.ZombieHorse
45
import net.minecraft.world.entity.animal.feline.Cat
46
import net.minecraft.world.entity.animal.golem.IronGolem
47
import net.minecraft.world.entity.animal.nautilus.AbstractNautilus
48
import net.minecraft.world.entity.animal.wolf.Wolf
49
import net.minecraft.world.item.ItemStack
50
import net.minecraft.world.item.Items
51
import net.minecraft.world.phys.Vec3
52
import kotlin.math.abs
53
54
/**
55
 * Automatically heals nearby vanilla-healable mobs using their vanilla repair or feeding items.
56
 */
57
object AutoMobHeal : ClientModule(
58
    "AutoMobHeal",
59
    ModuleCategories.WORLD,
60
    aliases = listOf("AutoGolemRepair"),
61
) {
62
63
    private val range by float("Range", 4.5f, 1f..6f)
64
    private val delay by intRange("Delay", 4..4, 0..40, "ticks")
65
    private val slotResetDelay by int("SlotResetDelay", 2, 0..20, "ticks")
66
    private val swingMode by enumChoice("SwingMode", SwingMode.DO_NOT_HIDE)
67
68
    private data class HealPlan(
69
        val target: LivingEntity,
70
        val slot: HotbarItemSlot,
71
        val distanceSq: Double,
72
    )
73
74
    private data class FoodCandidate(
75
        val slot: HotbarItemSlot,
76
        val option: MobFoodOption,
77
        val healAmount: Float,
78
    )
79
80
    private sealed class HealTarget<T : LivingEntity>(
81
        name: String,
82
        private val entityClass: Class<T>,
83
    ) : ToggleableValueGroup(this@AutoMobHeal, name, enabled = true) {
84
85
        private val healthThreshold by float("HealthThreshold", 75f, 1f..100f, "%")
86
87
        @Suppress("LoopWithTooManyJumpStatements")
88
        fun findPlan(maxRangeSq: Double, eyePosition: Vec3): HealPlan? {
89
            if (!running) {
90
                return null
91
            }
92
93
            val minHealthRatio = (healthThreshold / 100f).coerceIn(0f, 1f)
94
            var bestPlan: HealPlan? = null
95
96
            for (entity in world.entitiesForRendering()) {
97
                val typedEntity = cast(entity) ?: continue
98
                if (!player.hasLineOfSight(typedEntity) ||
99
                    !canInteract(typedEntity) ||
100
                    !shouldHeal(typedEntity, minHealthRatio)
101
                ) {
102
                    continue
103
                }
104
105
                val distanceSq = typedEntity.squaredBoxedDistanceTo(eyePosition)
106
                if (distanceSq > maxRangeSq || distanceSq >= (bestPlan?.distanceSq ?: Double.POSITIVE_INFINITY)) {
107
                    continue
108
                }
109
110
                val slot = findSlot(typedEntity) ?: continue
111
                bestPlan = HealPlan(typedEntity, slot, distanceSq)
112
            }
113
114
            return bestPlan
115
        }
116
117
        protected abstract fun findSlot(entity: T): HotbarItemSlot?
118
119
        protected open fun canInteract(entity: T): Boolean {
120
            return !isBlockedBySecondaryUse(entity)
121
        }
122
123
        protected open fun shouldHeal(entity: T, minHealthRatio: Float): Boolean {
124
            return entity.isAlive && entity.health <= entity.maxHealth * minHealthRatio
125
        }
126
127
        @Suppress("UNCHECKED_CAST")
128
        private fun cast(entity: Entity): T? {
129
            return if (entityClass.isInstance(entity)) {
130
                entity as T
131
            } else {
132
                null
133
            }
134
        }
135
136
        protected open class FoodHealTarget<T : LivingEntity>(
137
            name: String,
138
            entityClass: Class<T>,
139
        ) : HealTarget<T>(name, entityClass) {
140
            private val noWaste by boolean("NoWaste", true)
141
142
            protected open val preferNonBucketFoodAlways: Boolean = false
143
            protected open val allowBucketFood: Boolean
144
                get() = true
145
146
            protected open fun foodOptions(entity: T): List<MobFoodOption> = emptyList()
147
148
            final override fun findSlot(entity: T): HotbarItemSlot? {
149
                val missingHealth = (entity.maxHealth - entity.health).coerceAtLeast(0f)
150
                val candidates = Slots.OffhandWithHotbar.mapNotNull { slot ->
151
                    val option = foodOptions(entity).firstOrNull {
152
                        it.test.test(slot.itemStack)
153
                    } ?: return@mapNotNull null
154
                    if (option.isBucket && !allowBucketFood) {
155
                        return@mapNotNull null
156
                    }
157
158
                    FoodCandidate(
159
                        slot = slot,
160
                        option = option,
161
                        healAmount = option.healAmount.applyAsFloat(slot.itemStack),
162
                    )
163
                }
164
165
                if (candidates.isEmpty()) {
166
                    return null
167
                }
168
169
                val allowedCandidates = if (noWaste) {
170
                    candidates.filterNot { wouldWasteHealing(it, missingHealth) }
171
                } else {
172
                    candidates
173
                }
174
175
                return allowedCandidates.minWithOrNull(foodCandidateComparator(missingHealth))?.slot
176
            }
177
178
            private fun foodCandidateComparator(missingHealth: Float): Comparator<FoodCandidate> {
179
                return Comparator.comparingInt(::bucketPenalty)
180
                    .thenComparing { wouldWasteHealing(it, missingHealth) }
181
                    .thenComparingDouble { healingDelta(it, missingHealth).toDouble() }
182
                    .thenComparing({ it.slot }, HotbarItemSlot.PREFER_NEARBY)
183
            }
184
185
            private fun wouldWasteHealing(candidate: FoodCandidate, missingHealth: Float): Boolean {
186
                return candidate.healAmount > missingHealth
187
            }
188
189
            private fun healingDelta(candidate: FoodCandidate, missingHealth: Float): Float {
190
                return abs(candidate.healAmount - missingHealth)
191
            }
192
193
            private fun bucketPenalty(candidate: FoodCandidate): Int {
194
                return if (preferNonBucketFoodAlways && candidate.option.isBucket) 1 else 0
195
            }
196
        }
197
198
        protected sealed class BucketFoodHealTarget<T : LivingEntity>(
199
            name: String,
200
            entityClass: Class<T>,
201
        ) : FoodHealTarget<T>(name, entityClass) {
202
            override val allowBucketFood by boolean("AllowBucketFood", false)
203
        }
204
205
        protected class SimpleHealFoodTarget<T : LivingEntity>(
206
            name: String,
207
            entityClass: Class<T>,
208
            private val options: List<MobFoodOption>,
209
        ) : FoodHealTarget<T>(name, entityClass) {
210
            override fun foodOptions(entity: T): List<MobFoodOption> = options
211
        }
212
213
        /**
214
         * @see net.minecraft.world.entity.animal.golem.IronGolem.mobInteract
215
         */
216
        object IronGolemTarget : HealTarget<IronGolem>(
217
            "IronGolem",
218
            IronGolem::class.java,
219
        ) {
220
            override fun findSlot(entity: IronGolem): HotbarItemSlot? =
221
                Slots.OffhandWithHotbar.findClosestSlot(Items.IRON_INGOT)
222
        }
223
224
        /**
225
         * @see net.minecraft.world.entity.animal.wolf.Wolf.mobInteract
226
         */
227
        object WolfTarget : FoodHealTarget<Wolf>(
228
            "Wolf",
229
            Wolf::class.java,
230
        ) {
231
            private val options = listOf(
232
                MobFoodOption(ItemTags.WOLF_FOOD, healAmount2xNutrition),
233
            )
234
235
            override fun foodOptions(entity: Wolf): List<MobFoodOption> = options
236
237
            override fun shouldHeal(entity: Wolf, minHealthRatio: Float): Boolean {
238
                return entity.isTame && super.shouldHeal(entity, minHealthRatio)
239
            }
240
        }
241
242
        /**
243
         * @see net.minecraft.world.entity.animal.feline.Cat.mobInteract
244
         */
245
        object CatTarget : FoodHealTarget<Cat>(
246
            "Cat",
247
            Cat::class.java,
248
        ) {
249
            private val options = listOf(
250
                MobFoodOption(ItemTags.CAT_FOOD, healAmount1xNutrition),
251
            )
252
253
            override fun foodOptions(entity: Cat): List<MobFoodOption> = options
254
255
            override fun shouldHeal(entity: Cat, minHealthRatio: Float): Boolean {
256
                return entity.isOwnedBy(player) && super.shouldHeal(entity, minHealthRatio)
257
            }
258
        }
259
260
        /**
261
         * @see net.minecraft.world.entity.animal.equine.Horse.mobInteract
262
         * @see net.minecraft.world.entity.animal.equine.AbstractHorse.handleEating
263
         */
264
        object HorseFamilyTarget : FoodHealTarget<AbstractHorse>(
265
            "HorseFamily",
266
            AbstractHorse::class.java,
267
        ) {
268
            private val allowGoldenCarrot by boolean("AllowGoldenCarrot", false)
269
            private val allowGoldenApple by boolean("AllowGoldenApple", false)
270
            private val allowEnchantedGoldenApple by boolean("AllowEnchantedGoldenApple", false)
271
272
            override fun foodOptions(entity: AbstractHorse): List<MobFoodOption> = buildList {
273
                add(MobFoodOption(Items.SUGAR, 1f))
274
                add(MobFoodOption(Items.WHEAT, 2f))
275
                add(MobFoodOption(Items.APPLE, 3f))
276
                add(MobFoodOption(Items.CARROT, 3f))
277
                add(MobFoodOption(Items.HAY_BLOCK, 20f))
278
279
                if (allowGoldenCarrot) {
280
                    add(MobFoodOption(Items.GOLDEN_CARROT, 4f))
281
                }
282
                if (allowGoldenApple) {
283
                    add(MobFoodOption(Items.GOLDEN_APPLE, 10f))
284
                }
285
                if (allowEnchantedGoldenApple) {
286
                    add(MobFoodOption(Items.ENCHANTED_GOLDEN_APPLE, 10f))
287
                }
288
            }
289
290
            override fun shouldHeal(
291
                entity: AbstractHorse,
292
                minHealthRatio: Float,
293
            ): Boolean {
294
                return entity !is Llama &&
295
                    entity !is Camel &&
296
                    entity !is ZombieHorse &&
297
                    entity !is SkeletonHorse &&
298
                    super.shouldHeal(entity, minHealthRatio)
299
            }
300
        }
301
302
        /**
303
         * @see Camel.handleEating
304
         */
305
        object CamelTarget : FoodHealTarget<Camel>(
306
            "Camel",
307
            Camel::class.java,
308
        ) {
309
            private val options = listOf(
310
                MobFoodOption(Items.CACTUS, 2f),
311
            )
312
313
            override fun foodOptions(entity: Camel): List<MobFoodOption> = options
314
315
            override fun shouldHeal(entity: Camel, minHealthRatio: Float): Boolean {
316
                return entity !is CamelHusk && super.shouldHeal(entity, minHealthRatio)
317
            }
318
        }
319
320
        /**
321
         * @see net.minecraft.world.entity.animal.nautilus.AbstractNautilus.mobInteract
322
         */
323
        object NautilusTarget : BucketFoodHealTarget<AbstractNautilus>(
324
            "Nautilus",
325
            AbstractNautilus::class.java,
326
        ) {
327
            override val preferNonBucketFoodAlways: Boolean
328
                get() = true
329
330
            private val options = listOf(
331
                MobFoodOption(Items.COD, healAmount2xNutrition),
332
                MobFoodOption(Items.SALMON, healAmount2xNutrition),
333
                MobFoodOption(Items.TROPICAL_FISH, healAmount2xNutrition),
334
                MobFoodOption(Items.PUFFERFISH, healAmount2xNutrition),
335
                MobFoodOption(Items.COOKED_COD, healAmount2xNutrition),
336
                MobFoodOption(Items.COOKED_SALMON, healAmount2xNutrition),
337
                MobFoodOption.ofBucket(Items.COD_BUCKET),
338
                MobFoodOption.ofBucket(Items.SALMON_BUCKET),
339
                MobFoodOption.ofBucket(Items.TROPICAL_FISH_BUCKET),
340
                MobFoodOption.ofBucket(Items.PUFFERFISH_BUCKET),
341
            )
342
343
            override fun foodOptions(entity: AbstractNautilus): List<MobFoodOption> = options
344
345
            override fun shouldHeal(
346
                entity: AbstractNautilus,
347
                minHealthRatio: Float,
348
            ): Boolean {
349
                return !entity.isBaby && entity.isTame && super.shouldHeal(entity, minHealthRatio)
350
            }
351
        }
352
353
        companion object {
354
            private val healAmount1xNutrition = ToFloatFunction<ItemStack> { stack -> foodNutritionHeal(stack, 1f) }
355
            private val healAmount2xNutrition = ToFloatFunction<ItemStack> { stack -> foodNutritionHeal(stack, 2f) }
356
357
            /**
358
             * @see ZombieHorse.mobInteract
359
             * @see AbstractHorse.handleEating
360
             */
361
            @JvmField
362
            val ZombieHorseTarget = SimpleHealFoodTarget(
363
                "ZombieHorse",
364
                ZombieHorse::class.java,
365
                listOf(
366
                    MobFoodOption(Items.RED_MUSHROOM, 3f),
367
                ),
368
            )
369
370
            /**
371
             * @see Llama.handleEating
372
             */
373
            @JvmField
374
            val LlamaTarget = SimpleHealFoodTarget(
375
                "Llama",
376
                Llama::class.java,
377
                listOf(
378
                    MobFoodOption(Items.WHEAT, 2f),
379
                    MobFoodOption(Items.HAY_BLOCK, 10f),
380
                ),
381
            )
382
383
            /**
384
             * @see CamelHusk.isFood
385
             * @see Camel.handleEating
386
             */
387
            @JvmField
388
            val CamelHuskTarget = SimpleHealFoodTarget(
389
                "CamelHusk",
390
                CamelHusk::class.java,
391
                listOf(
392
                    MobFoodOption(Items.RABBIT_FOOT, 2f),
393
                ),
394
            )
395
        }
396
    }
397
398
    private val healTargets = arrayOf(
399
        HealTarget.IronGolemTarget,
400
        HealTarget.WolfTarget,
401
        HealTarget.CatTarget,
402
        HealTarget.HorseFamilyTarget,
403
        HealTarget.ZombieHorseTarget,
404
        HealTarget.LlamaTarget,
405
        HealTarget.CamelTarget,
406
        HealTarget.CamelHuskTarget,
407
        HealTarget.NautilusTarget,
408
    )
409
410
    init {
411
        healTargets.forEach { tree(it) }
412
    }
413
414
    @Suppress("unused")
415
    private val repeatable = tickHandler {
416
        if (player.isUsingItem || mc.gui.screen() != null) {
417
            return@tickHandler
418
        }
419
420
        val maxRange = minOf(range.toDouble(), player.entityInteractionRange())
421
        val maxRangeSq = maxRange * maxRange
422
        val eyePosition = player.eyePosition
423
424
        val plan = healTargets
425
            .mapNotNull { it.findPlan(maxRangeSq, eyePosition) }
426
            .minByOrNull { it.distanceSq }
427
            ?: return@tickHandler
428
429
        SilentHotbar.selectSlotSilently(this, plan.slot, slotResetDelay)
430
431
        val result = interactEntity(
432
            plan.target,
433
            hand = plan.slot.useHand,
434
            swingMode = swingMode,
435
        )
436
437
        if (result?.consumesAction() == true) {
438
            waitTicks(delay.random())
439
        }
440
    }
441
442
    override fun onDisabled() {
443
        SilentHotbar.resetSlot(this)
444
    }
445
446
    private fun isBlockedBySecondaryUse(entity: LivingEntity): Boolean {
447
        if (!player.isSecondaryUseActive) {
448
            return false
449
        }
450
451
        return when (entity) {
452
            is Camel -> !entity.isBaby
453
            is AbstractHorse -> entity.isTamed && !entity.isBaby
454
            is AbstractNautilus -> entity.isTame && !entity.isBaby
455
            else -> false
456
        }
457
    }
458
459
}
460