-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathEatConfig.kt
More file actions
114 lines (101 loc) · 4.38 KB
/
EatConfig.kt
File metadata and controls
114 lines (101 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright 2025 Lambda
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.lambda.config.groups
import com.lambda.context.Automated
import com.lambda.context.AutomatedSafeContext
import com.lambda.interaction.material.StackSelection.Companion.selectStack
import com.lambda.threading.runSafe
import com.lambda.util.Describable
import com.lambda.util.NamedEnum
import com.lambda.util.item.ItemUtils.nutrition
import net.minecraft.entity.effect.StatusEffects
import net.minecraft.item.Item
import net.minecraft.item.ItemStack
interface EatConfig {
val eatOnHunger: Boolean
val minFoodLevel: Int
val nutritiousFood: List<Item>
val saturated: Saturation
val eatOnFire: Boolean
val resistanceFood: List<Item>
val eatOnDamage: Boolean
val minDamage: Int
val regenerationFood: List<Item>
val selectionPriority: SelectionPriority
val ignoreBadFood: Boolean
val badFood: List<Item>
enum class Saturation(
override val displayName: String,
override val description: String
): NamedEnum, Describable {
EatSmart("Eat Smart", "Eats until the next food would exceed the hunger limit."),
EatUntilFull("Eat Until Full", "Eats food until the hunger bar is completely full. May waste some food."),
}
enum class SelectionPriority(
val comparator: Comparator<ItemStack>,
override val displayName: String,
override val description: String
): NamedEnum, Describable {
LeastNutritious(
compareBy { it.item.nutrition },
"Least Nutritious",
"Eats food items with the least nutritional value."
),
MostNutritious(
compareByDescending { it.item.nutrition },
"Most Nutritious",
"Eats food items with the most nutritional value."
)
}
enum class Reason(val message: (ItemStack) -> String) {
None({ "Waiting for reason to eat..." }),
Hunger({ "Eating ${it.item.name.string} due to Hunger" }),
Damage({ "Eating ${it.item.name.string} due to Damage" }),
Fire({ "Eating ${it.item.name.string} due to Fire" });
fun shouldEat() = this != None
context(c: Automated)
fun shouldKeepEating(stack: ItemStack?) = runSafe {
if (stack == null || stack.isEmpty) return@runSafe false
when(this@Reason) {
Hunger -> when(c.eatConfig.saturated) {
Saturation.EatSmart -> stack.item.nutrition + player.hungerManager.foodLevel <= 20
Saturation.EatUntilFull -> player.hungerManager.isNotFull
}
Damage -> !player.hasStatusEffect(StatusEffects.REGENERATION)
Fire -> !player.hasStatusEffect(StatusEffects.FIRE_RESISTANCE)
None -> false
}
} ?: false
context(c: Automated)
fun selector() = selectStack(sorter = c.eatConfig.selectionPriority.comparator) {
when(this@Reason) {
None -> any()
Hunger -> isOneOfItems(c.eatConfig.nutritiousFood)
Damage -> isOneOfItems(c.eatConfig.regenerationFood)
Fire -> isOneOfItems(c.eatConfig.resistanceFood)
} and if (c.eatConfig.ignoreBadFood) isNoneOfItems(c.eatConfig.badFood) else any()
}
}
companion object {
fun AutomatedSafeContext.reasonEating() = when {
eatConfig.eatOnHunger && player.hungerManager.foodLevel <= eatConfig.minFoodLevel -> Reason.Hunger
eatConfig.eatOnDamage && player.health <= eatConfig.minDamage && !player.hasStatusEffect(StatusEffects.REGENERATION) -> Reason.Damage
eatConfig.eatOnFire && player.isOnFire && !player.hasStatusEffect(StatusEffects.FIRE_RESISTANCE) -> Reason.Fire
else -> Reason.None
}
}
}