-
-
Notifications
You must be signed in to change notification settings - Fork 513
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(nextgen): SmartEat #1766
feat(nextgen): SmartEat #1766
Changes from 64 commits
7b12026
fedb9a2
aee4508
e0d76d6
4d9dd41
fcdbe7d
0b9e5ed
2cf2ad1
18523e5
0dd5cd6
f29def9
24a08cb
d8b3c79
16405f8
8da283a
48ceb3a
93349a7
b0ccc7a
e4813f8
4688e79
85eb491
82e3b47
1612e25
58e58b6
9947da4
0d0f09b
173b5d6
e70c0c9
42c1fff
685a95a
6e5ad41
c693778
c3e03c8
226eced
aa8df95
90812df
5e4c150
fdfe6d2
d24e38a
aff91a0
4f5e426
83b65bd
56b349d
bce2f53
fd363c2
8f6bd55
190be5e
51232e7
221b9dc
9aff3c8
80030f5
572b557
8c4ebfa
025a87f
76a9131
75311e4
0bbbae4
6ec2cc0
51cb692
7c8de28
6891cd7
d58fb9a
1ad9b2c
d8e160c
9caa8ab
19f37dc
03edff3
7586785
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
/* | ||
* This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce) | ||
* | ||
* Copyright (c) 2015 - 2023 CCBlueX | ||
* | ||
* LiquidBounce is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* LiquidBounce is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package net.ccbluex.liquidbounce.features.module.modules.combat | ||
|
||
import net.ccbluex.liquidbounce.config.ToggleableConfigurable | ||
import net.ccbluex.liquidbounce.event.events.OverlayRenderEvent | ||
import net.ccbluex.liquidbounce.event.events.PlayerInteractedItem | ||
import net.ccbluex.liquidbounce.event.handler | ||
import net.ccbluex.liquidbounce.event.repeatable | ||
import net.ccbluex.liquidbounce.features.module.Category | ||
import net.ccbluex.liquidbounce.features.module.Module | ||
import net.ccbluex.liquidbounce.render.renderEnvironmentForGUI | ||
import net.ccbluex.liquidbounce.utils.client.SilentHotbar | ||
import net.ccbluex.liquidbounce.utils.client.chat | ||
import net.ccbluex.liquidbounce.utils.item.Hotbar | ||
import net.minecraft.client.gui.DrawContext | ||
import net.minecraft.client.option.KeyBinding | ||
import net.minecraft.entity.effect.StatusEffects | ||
import net.minecraft.entity.player.PlayerEntity | ||
import net.minecraft.item.ItemStack | ||
import net.minecraft.item.Items | ||
import net.minecraft.potion.PotionUtil | ||
import net.minecraft.util.ActionResult | ||
import net.minecraft.util.Hand | ||
import net.minecraft.util.Identifier | ||
import net.minecraft.util.UseAction | ||
import kotlin.math.max | ||
|
||
/** | ||
* SmartEat module | ||
* | ||
* Makes it easier to eat | ||
*/ | ||
|
||
object ModuleSmartEat : Module("SmartEat", Category.PLAYER) { | ||
private val HOTBAR_OFFHAND_LEFT_TEXTURE = Identifier("hud/hotbar_offhand_left") | ||
|
||
private val swapBackDelay by int("SwapBackDelay", 5, 1..20) | ||
|
||
private val preferGappleHealth by float("MaxPreferGappleHealth", 9f, 0f..20f) | ||
private val preferNotchAppleHealth by float("MaxPreferNotchAppleHealth", 2f, 0f..20f) | ||
private val preferHealthPotHealth by float("MaxPreferHealthPotHealth", 12f, 0f..20f) | ||
superblaubeere27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private var prefersGapples = false | ||
private var prefersNotchApple = false | ||
private var prefersHealthPot = false | ||
private val food: Pair<Int, ItemStack>? | ||
get() = Hotbar.findBestItem(0) { _, itemStack -> | ||
getItemScore(itemStack) | ||
} | ||
@Suppress("ReturnCount") | ||
private fun getItemScore(itemStack: ItemStack): Int { | ||
val item = itemStack.item | ||
|
||
if (prefersHealthPot && item == Items.POTION) { | ||
val hasHealthEffect = | ||
PotionUtil.getPotionEffects(itemStack).any { | ||
it.effectType == StatusEffects.INSTANT_HEALTH | ||
} | ||
|
||
if (hasHealthEffect) | ||
return 100 - preferHealthPotHealth.toInt() | ||
} | ||
|
||
val foodComp = item.foodComponent ?: return -1 | ||
|
||
val hasHungerEffect = foodComp.statusEffects.any { it.first.effectType == StatusEffects.HUNGER } | ||
if (hasHungerEffect) | ||
return 0 | ||
|
||
if(prefersGapples && item == Items.GOLDEN_APPLE) | ||
return 100 - preferGappleHealth.toInt() | ||
|
||
if (prefersNotchApple && item == Items.ENCHANTED_GOLDEN_APPLE) | ||
return 100 - preferNotchAppleHealth.toInt() | ||
|
||
return foodComp.hunger | ||
} | ||
superblaubeere27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
val tickHandler = repeatable { | ||
prefersGapples = player.health <= preferGappleHealth | ||
prefersNotchApple = player.health <= preferNotchAppleHealth | ||
prefersHealthPot = player.health <= preferHealthPotHealth | ||
} | ||
private object SilentOffhand : ToggleableConfigurable(this, "SilentOffhand", true) { | ||
private object RenderSlot : ToggleableConfigurable(this, "RenderSlot", true) { | ||
private val offset by int("Offset", 26, 0..40) | ||
val renderHandler = handler<OverlayRenderEvent> { | ||
renderEnvironmentForGUI { | ||
val currentFood = food ?: return@renderEnvironmentForGUI | ||
val dc = DrawContext(mc, mc.bufferBuilders.entityVertexConsumers) | ||
val scaledWidth = dc.scaledWindowWidth | ||
val scaledHeight = dc.scaledWindowHeight | ||
val i: Int = scaledWidth / 2 | ||
val x = i - 91 - 26 - offset | ||
val y = scaledHeight - 16 - 3 | ||
dc.drawItemInSlot(mc.textRenderer, currentFood.second, x, y) | ||
dc.drawItem(currentFood.second, x, y) | ||
dc.drawGuiTexture( | ||
HOTBAR_OFFHAND_LEFT_TEXTURE, i - 91 - 29 - offset, | ||
scaledHeight - 23, 29, 24 | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract those magic numbers (like 26, 91 or 3) to constants (like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see where you are coming from, but I don't know if that really is the optimal solution. I tried to simply replicate Minecraft code as exactly as possible, meaning that the only reason the code should be changed is if minecraft would change their code. At this point, the easiest way of updating Liquidbounce's code would be to check in Minecraft's code again and see what has changed. Here it would just be confusing Liquidbounce's version of the code would use variables whilst Minecraft's wouldn't.
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
||
} | ||
} | ||
} | ||
|
||
val InteractionHandler = handler<PlayerInteractedItem> { event -> | ||
if(!enabled) | ||
return@handler | ||
if(event.actionResult != ActionResult.PASS) | ||
return@handler | ||
|
||
val currentFood = food ?: return@handler | ||
|
||
if( | ||
!player.canConsume(false) | ||
&& currentFood.second.item.foodComponent?.isAlwaysEdible() == false | ||
) { | ||
return@handler | ||
} | ||
|
||
|
||
SilentHotbar.selectSlotSilently(this@SilentOffhand, currentFood.first, max(swapBackDelay, 5)) | ||
superblaubeere27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
val tickHandler = repeatable { | ||
|
||
val useAction = player.activeItem.useAction | ||
|
||
if (useAction != UseAction.EAT && useAction != UseAction.DRINK) | ||
return@repeatable | ||
if (!SilentHotbar.isSlotModified(this@SilentOffhand)) | ||
return@repeatable | ||
// if we are already eating, we want to keep the silent slot | ||
SilentHotbar.selectSlotSilently(this@SilentOffhand, SilentHotbar.serversideSlot, swapBackDelay) | ||
} | ||
|
||
init { | ||
tree(RenderSlot) | ||
} | ||
} | ||
|
||
private object AutoEat : ToggleableConfigurable(this, "AutoEat", true) { | ||
private val minHunger by int("MinHunger", 15, 0..20) | ||
|
||
private val tickHandler = repeatable { | ||
|
||
if(player.hungerManager.foodLevel < minHunger) { | ||
|
||
waitUntil { | ||
eat() | ||
player.hungerManager.foodLevel > minHunger | ||
} | ||
KeyBinding.setKeyPressed(mc.options.useKey.boundKey, false) | ||
|
||
} | ||
superblaubeere27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
fun eat() { | ||
val currentBestFood = food ?: return | ||
|
||
val slot = currentBestFood.first | ||
|
||
SilentHotbar.selectSlotSilently(AutoEat, slot, swapBackDelay) | ||
|
||
KeyBinding.setKeyPressed(mc.options.useKey.boundKey, true) | ||
|
||
|
||
} | ||
} | ||
|
||
|
||
init { | ||
tree(SilentOffhand) | ||
tree(AutoEat) | ||
} | ||
|
||
|
||
} |
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs some attention from the Frontend :o @SenkJu