SmartEat module

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

1 implementationPLAYER

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)PLAYERsrc/main/kotlin/net/ccbluex/liquidbounce/features/module/modules/player/ModuleSmartEat.ktKotlin · 262 lines · 9.8 KB

Automatically eats food when your hunger is low.

SettingsSwapBackDelayPreferGappleHealthThresholdPreferNotchAppleHealthThresholdPreferHealthPotHealthThresholdCombatPauseTimeNotDuringCombatOffsetMinHunger
ModuleSmartEat.ktKotlin · Minecraft 26.2 · 262 lines · 9.8 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
package net.ccbluex.liquidbounce.features.module.modules.player
20
21
import net.ccbluex.liquidbounce.config.types.group.ToggleableValueGroup
22
import net.ccbluex.liquidbounce.event.events.GameTickEvent
23
import net.ccbluex.liquidbounce.event.events.KeybindIsPressedEvent
24
import net.ccbluex.liquidbounce.event.events.OverlayRenderEvent
25
import net.ccbluex.liquidbounce.event.events.PlayerInteractedItemEvent
26
import net.ccbluex.liquidbounce.event.handler
27
import net.ccbluex.liquidbounce.event.tickHandler
28
import net.ccbluex.liquidbounce.event.tickUntil
29
import net.ccbluex.liquidbounce.features.module.ClientModule
30
import net.ccbluex.liquidbounce.features.module.ModuleCategories
31
import net.ccbluex.liquidbounce.utils.client.SilentHotbar
32
import net.ccbluex.liquidbounce.utils.combat.CombatManager
33
import net.ccbluex.liquidbounce.utils.inventory.HotbarItemSlot
34
import net.ccbluex.liquidbounce.utils.inventory.Slots
35
import net.ccbluex.liquidbounce.utils.item.foodComponent
36
import net.ccbluex.liquidbounce.utils.item.getPotionEffects
37
import net.ccbluex.liquidbounce.utils.item.isMiningTool
38
import net.ccbluex.liquidbounce.utils.sorting.ComparatorChain
39
import net.minecraft.client.gui.render.GuiRenderer
40
import net.minecraft.client.renderer.RenderPipelines
41
import net.minecraft.resources.Identifier
42
import net.minecraft.world.InteractionResult
43
import net.minecraft.world.effect.MobEffects
44
import net.minecraft.world.item.ItemStack
45
import net.minecraft.world.item.ItemUseAnimation
46
import net.minecraft.world.item.Items
47
48
/**
49
 * SmartEat module
50
 *
51
 * Makes it easier to eat
52
 */
53
54
object ModuleSmartEat : ClientModule("SmartEat", ModuleCategories.PLAYER) {
55
56
    private val HOTBAR_OFFHAND_LEFT_TEXTURE = Identifier.parse("hud/hotbar_offhand_left")
57
58
    private val swapBackDelay by int("SwapBackDelay", 5, 1..20)
59
60
    private val preferGappleHealth by float("PreferGappleHealthThreshold", 9f, 0f..20f)
61
    private val preferNotchAppleHealth by float("PreferNotchAppleHealthThreshold", 2f, 0f..20f)
62
    private val preferHealthPotHealth by float("PreferHealthPotHealthThreshold", 12f, 0f..20f)
63
64
    private val combatPauseTime by int("CombatPauseTime", 0, 0..40, "ticks")
65
    private val notDuringCombat by boolean("NotDuringCombat", false)
66
67
    private object Estimator {
68
        private val comparator = ComparatorChain<Pair<HotbarItemSlot, FoodEstimationData>>(
69
            // If there is an indication for a special item, we should use it. Items with lower health threshold
70
            // are preferred since their usage is probably more urgent.
71
            compareByDescending { it.second.healthThreshold },
72
            compareBy { it.second.restoredHunger },
73
            // Use the closest slot
74
            Comparator.comparing({ it.first }, HotbarItemSlot.PREFER_NEARBY),
75
            // Just for stabilization reasons
76
            compareBy { SilentHotbar.serversideSlot }
77
        )
78
79
        fun findBestFood(): HotbarItemSlot? {
80
            return Slots.OffhandWithHotbar
81
                .mapNotNull { slot -> getFoodEstimationData(slot.itemStack)?.let { slot to it } }
82
                .maxWithOrNull(comparator)?.first
83
        }
84
85
        private class FoodEstimationData(val restoredHunger: Int = 0, val healthThreshold: Int = 20)
86
87
        private fun getFoodEstimationData(itemStack: ItemStack): FoodEstimationData? {
88
            val item = itemStack.item
89
90
            val prefersGapples = player.health <= preferGappleHealth
91
            val prefersNotchApple = player.health <= preferNotchAppleHealth
92
            val prefersHealthPot = player.health <= preferHealthPotHealth
93
94
            return when {
95
                prefersGapples && item == Items.POTION -> {
96
                    val hasHealthEffect =
97
                        itemStack.getPotionEffects().any {
98
                            it.effect == MobEffects.INSTANT_HEALTH
99
                        }
100
101
                    if (hasHealthEffect) {
102
                        FoodEstimationData(healthThreshold = preferHealthPotHealth.toInt())
103
                    } else {
104
                        null
105
                    }
106
                }
107
                prefersHealthPot && item == Items.GOLDEN_APPLE -> {
108
                    FoodEstimationData(
109
                        healthThreshold = preferHealthPotHealth.toInt(),
110
                        restoredHunger = itemStack.foodComponent!!.nutrition
111
                    )
112
                }
113
                prefersNotchApple && item == Items.ENCHANTED_GOLDEN_APPLE -> {
114
                    FoodEstimationData(
115
                        healthThreshold = preferNotchAppleHealth.toInt(),
116
                        restoredHunger = itemStack.foodComponent!!.nutrition
117
                    )
118
                }
119
                itemStack.foodComponent != null ->
120
                    FoodEstimationData(restoredHunger = itemStack.foodComponent!!.nutrition)
121
                else -> null
122
            }
123
        }
124
    }
125
126
    private object SilentOffhand : ToggleableValueGroup(this, "SilentOffhand", true) {
127
        private object RenderSlot : ToggleableValueGroup(this, "RenderSlot", true) {
128
129
            private val offset by int("Offset", 40, 30..70)
130
131
            @Suppress("unused")
132
            private val renderHandler = handler<OverlayRenderEvent> { event ->
133
                // MC-Rendering code for off-hand
134
135
                val currentFood = Estimator.findBestFood() ?: return@handler
136
                val dc = event.context
137
                val scaledWidth = dc.guiWidth()
138
                val scaledHeight = dc.guiHeight()
139
                val i: Int = scaledWidth / 2
140
                val x = i - 91 - 26 - offset
141
                val y = scaledHeight - GuiRenderer.DEFAULT_ITEM_SIZE - 3
142
                dc.itemDecorations(mc.font, currentFood.itemStack, x, y)
143
                dc.item(currentFood.itemStack, x, y)
144
                dc.blitSprite(
145
                    RenderPipelines.GUI_TEXTURED,
146
                    HOTBAR_OFFHAND_LEFT_TEXTURE, i - 91 - 29 - offset,
147
                    scaledHeight - 23, 29, 24
148
                )
149
            }
150
151
        }
152
153
        @Suppress("unused")
154
        private val interactionHandler = handler<PlayerInteractedItemEvent> { event ->
155
            if (!enabled) {
156
                return@handler
157
            }
158
159
            if (event.actionResult != InteractionResult.PASS) {
160
                return@handler
161
            }
162
163
            val currentFood = Estimator.findBestFood() ?: return@handler
164
165
            val alwaysEdible = currentFood.itemStack.foodComponent?.canAlwaysEat == false
166
167
            if (!player.canEat(false) && alwaysEdible) {
168
                return@handler
169
            }
170
171
            if (notDuringCombat && CombatManager.isInCombat) {
172
                return@handler
173
            }
174
175
            // Only use silent offhand if we have tools in hand.
176
            if (!player.mainHandItem.isMiningTool) {
177
                return@handler
178
            }
179
180
            CombatManager.pauseCombatForAtLeast(combatPauseTime)
181
            SilentHotbar.selectSlotSilently(
182
                this@SilentOffhand,
183
                currentFood,
184
                swapBackDelay.coerceAtLeast(5)
185
            )
186
        }
187
188
        @Suppress("unused")
189
        private val tickHandler = handler<GameTickEvent> {
190
            val useAction = player.useItem.useAnimation
191
192
            if (useAction != ItemUseAnimation.EAT && useAction != ItemUseAnimation.DRINK) {
193
                return@handler
194
            }
195
196
            if (!SilentHotbar.isSlotModifiedBy(this@SilentOffhand)) {
197
                return@handler
198
            }
199
200
            // if we are already eating, we want to keep the silent slot
201
            SilentHotbar.selectSlotSilently(this@SilentOffhand, SilentHotbar.serversideSlot, swapBackDelay)
202
        }
203
204
        init {
205
            tree(RenderSlot)
206
        }
207
208
        override fun onDisabled() {
209
            SilentHotbar.resetSlot(this)
210
        }
211
    }
212
213
    private object AutoEat : ToggleableValueGroup(this, "AutoEat", true) {
214
215
        private val minHunger by int("MinHunger", 15, 0..20)
216
        private var forceUseKey = false
217
218
        @Suppress("unused")
219
        private val tickHandler = tickHandler {
220
            if (player.foodData.foodLevel < minHunger) {
221
                if (notDuringCombat && CombatManager.isInCombat) {
222
                    return@tickHandler
223
                }
224
225
                CombatManager.pauseCombatForAtLeast(combatPauseTime)
226
                tickUntil {
227
                    eat()
228
                    player.foodData.foodLevel > minHunger
229
                }
230
231
                forceUseKey = false
232
            }
233
        }
234
235
        @Suppress("unused")
236
        private val keyBindIsPressedHandler = handler<KeybindIsPressedEvent> { event ->
237
            if (event.keyBinding == mc.options.keyUse && forceUseKey) {
238
                CombatManager.pauseCombatForAtLeast(combatPauseTime)
239
                event.isPressed = true
240
            }
241
        }
242
243
        override fun onDisabled() {
244
            SilentHotbar.resetSlot(this)
245
        }
246
247
        fun eat() {
248
            val currentBestFood = Estimator.findBestFood() ?: return
249
250
            SilentHotbar.selectSlotSilently(AutoEat, currentBestFood, swapBackDelay)
251
            forceUseKey = true
252
        }
253
254
    }
255
256
    init {
257
        tree(SilentOffhand)
258
        tree(AutoEat)
259
    }
260
261
}
262