Skip to content

Commit

Permalink
Better Auto Eat threshold
Browse files Browse the repository at this point in the history
Select "health", "hunger", "any", "both" thresholds to trigger auto eat.
  • Loading branch information
DesiCow authored and Wide-Cat committed Aug 20, 2023
1 parent 7c00a9c commit 1101e84
Showing 1 changed file with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiPredicate;

public class AutoEat extends Module {
private static final Class<? extends Module>[] AURAS = new Class[] { KillAura.class, CrystalAura.class, AnchorAura.class, BedAura.class };

private final SettingGroup sgGeneral = settings.getDefaultGroup();
private final SettingGroup sgHunger = settings.createGroup("Hunger");
private final SettingGroup sgThreshold = settings.createGroup("Threshold");

// General

Expand Down Expand Up @@ -67,14 +68,32 @@ public class AutoEat extends Module {
.build()
);

// Hunger
// Threshold

private final Setting<Integer> hungerThreshold = sgHunger.add(new IntSetting.Builder()
private final Setting<ThresholdMode> thresholdMode = sgThreshold.add(new EnumSetting.Builder<ThresholdMode>()
.name("threshold-mode")
.description("The threshold mode to trigger auto eat.")
.defaultValue(ThresholdMode.Any)
.build()
);

private final Setting<Double> healthThreshold = sgThreshold.add(new DoubleSetting.Builder()
.name("health-threshold")
.description("The level of health you eat at.")
.defaultValue(10)
.range(1, 19)
.sliderRange(1, 19)
.visible(() -> thresholdMode.get() != ThresholdMode.Hunger)
.build()
);

private final Setting<Integer> hungerThreshold = sgThreshold.add(new IntSetting.Builder()
.name("hunger-threshold")
.description("The level of hunger you eat at.")
.defaultValue(16)
.range(1, 19)
.sliderRange(1, 19)
.visible(() -> thresholdMode.get() != ThresholdMode.Health)
.build()
);

Expand Down Expand Up @@ -207,8 +226,11 @@ private void changeSlot(int slot) {
this.slot = slot;
}

private boolean shouldEat() {
return mc.player.getHungerManager().getFoodLevel() <= hungerThreshold.get();
public boolean shouldEat() {
boolean health = mc.player.getHealth() <= healthThreshold.get();
boolean hunger = mc.player.getHungerManager().getFoodLevel() <= hungerThreshold.get();

return thresholdMode.get().test(health, hunger);
}

private int findSlot() {
Expand Down Expand Up @@ -237,4 +259,21 @@ private int findSlot() {

return slot;
}

public enum ThresholdMode {
Health((health, hunger) -> health),
Hunger((health, hunger) -> hunger),
Any((health, hunger) -> health || hunger),
Both((health, hunger) -> health && hunger);

private final BiPredicate<Boolean, Boolean> predicate;

ThresholdMode(BiPredicate<Boolean, Boolean> predicate) {
this.predicate = predicate;
}

public boolean test(boolean health, boolean hunger) {
return predicate.test(health, hunger);
}
}
}

0 comments on commit 1101e84

Please sign in to comment.