forked from iguanaman/HungerOverhaul
/
FoodEventHandler.java
191 lines (164 loc) · 6.05 KB
/
FoodEventHandler.java
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package iguanaman.hungeroverhaul.module.food;
import iguanaman.hungeroverhaul.HungerOverhaul;
import iguanaman.hungeroverhaul.common.config.Config;
import net.minecraft.potion.PotionEffect;
import net.minecraft.world.EnumDifficulty;
import net.minecraftforge.event.entity.living.LivingEntityUseItemEvent;
import net.minecraftforge.fml.common.eventhandler.Event.Result;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import squeek.applecore.api.AppleCoreAPI;
import squeek.applecore.api.food.FoodEvent;
import squeek.applecore.api.food.FoodValues;
import squeek.applecore.api.hunger.ExhaustionEvent;
import squeek.applecore.api.hunger.HealthRegenEvent;
import squeek.applecore.api.hunger.StarvationEvent;
public class FoodEventHandler
{
@SubscribeEvent
public void onFoodEaten(FoodEvent.FoodEaten event)
{
if (!event.player.world.isRemote && event.player.world.getGameRules().getBoolean("naturalRegeneration") && Config.healthRegenRatePercentage > 0)
{
if (Config.addWellFedEffect)
{
int duration = (int) (Math.pow(event.foodValues.hunger * 100, 1.2) * Config.wellFedDurationMultiplier);
if (duration >= 30)
{
PotionEffect currentEffect = event.player.getActivePotionEffect(HungerOverhaul.potionWellFed);
if (currentEffect != null)
{
duration += currentEffect.getDuration();
}
event.player.addPotionEffect(new PotionEffect(HungerOverhaul.potionWellFed, duration, 0, true, true));
}
}
}
if (Config.foodRegensHealth)
{
float toHeal = Math.round(event.foodValues.hunger / (float) Config.foodHealDivider);
float canHeal = event.player.getMaxHealth() - event.player.getHealth();
if (toHeal > canHeal)
{
toHeal = canHeal;
}
if (toHeal > 0f)
{
event.player.heal(toHeal);
}
}
}
@SubscribeEvent
public void allowExhaustion(ExhaustionEvent.AllowExhaustion event)
{
if (Config.hungerLossRatePercentage == 0)
{
AppleCoreAPI.mutator.setHunger(event.player, 19);
AppleCoreAPI.mutator.setSaturation(event.player, 0f);
AppleCoreAPI.mutator.setExhaustion(event.player, 0f);
event.setResult(Result.DENY);
}
}
@SubscribeEvent
public void getMaxExhaustion(ExhaustionEvent.GetMaxExhaustion event)
{
EnumDifficulty difficulty = event.player.world.getDifficulty();
float hungerLossRate = event.maxExhaustionLevel / (Config.hungerLossRatePercentage / 100F);
if (Config.difficultyScalingHunger)
{
if (difficulty == EnumDifficulty.PEACEFUL)
{
hungerLossRate *= 5F / 3F;
}
else if (difficulty == EnumDifficulty.EASY)
{
hungerLossRate *= 4F / 3F;
}
}
event.maxExhaustionLevel = hungerLossRate;
}
@SubscribeEvent
public void onExhausted(ExhaustionEvent.Exhausted event)
{
// decrease hunger in peaceful
if (event.player.getFoodStats().getSaturationLevel() <= 0)
{
event.deltaHunger = -1;
}
}
@SubscribeEvent
public void allowHealthRegen(HealthRegenEvent.AllowRegen event)
{
if (event.player.getFoodStats().getFoodLevel() >= Config.minHungerToHeal && Config.healthRegenRatePercentage > 0 && event.player.world.getGameRules().getBoolean("naturalRegeneration") && event.player.shouldHeal())
{
event.setResult(Result.ALLOW);
}
else
{
event.setResult(Result.DENY);
}
}
@SubscribeEvent
public void onHealthRegenTick(HealthRegenEvent.GetRegenTickPeriod event)
{
float wellfedModifier = 1.0F;
if (event.player.isPotionActive(HungerOverhaul.potionWellFed))
{
wellfedModifier = 1F - Config.wellFedEffectiveness;
}
EnumDifficulty difficulty = event.player.world.getDifficulty();
float difficultyModifierHealing = 1.0F;
if (Config.difficultyScalingHealing)
{
if (difficulty.getDifficultyId() <= EnumDifficulty.EASY.getDifficultyId())
{
difficultyModifierHealing = 0.75F;
}
else if (difficulty == EnumDifficulty.HARD)
{
difficultyModifierHealing = 1.5F;
}
}
float lowHealthModifier = 1.0F;
if (Config.modifyRegenRateOnLowHealth)
{
lowHealthModifier = event.player.getMaxHealth() - event.player.getHealth();
lowHealthModifier *= Config.lowHealthRegenRateModifier / 100F;
lowHealthModifier *= difficultyModifierHealing;
lowHealthModifier = (float) Math.pow(lowHealthModifier + 1F, 1.5F);
}
event.regenTickPeriod = Math.round(80.0F * difficultyModifierHealing * wellfedModifier * lowHealthModifier / (Config.healthRegenRatePercentage / 100F));
}
@SubscribeEvent
public void onHealthRegen(HealthRegenEvent.Regen event)
{
if (Config.disableHealingHungerDrain)
{
event.deltaExhaustion = 0;
}
}
@SubscribeEvent
public void onStarve(StarvationEvent.Starve event)
{
event.starveDamage = Config.damageOnStarve;
}
@SubscribeEvent
public void onFoodStatsAddition(FoodEvent.FoodStatsAddition event)
{
if (Config.hungerLossRatePercentage == 0)
{
event.setCanceled(true);
}
}
@SubscribeEvent
public void onFoodStartEating(LivingEntityUseItemEvent.Start event)
{
if (Config.modifyFoodEatingSpeed && AppleCoreAPI.accessor.isFood(event.getItem()))
{
int hunger = FoodValues.get(event.getItem()).hunger;
if (hunger > 0)
{
event.setDuration(hunger * 8 + 8);
}
}
}
}