-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathAutoGap.java
More file actions
193 lines (166 loc) · 6.33 KB
/
AutoGap.java
File metadata and controls
193 lines (166 loc) · 6.33 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
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
192
193
package com.zenith.module.impl;
import com.github.rfresh2.EventConsumer;
import com.zenith.event.client.ClientBotTick;
import com.zenith.feature.inventory.InventoryActionRequest;
import com.zenith.feature.player.ClickTarget;
import com.zenith.feature.player.Input;
import com.zenith.feature.player.InputRequest;
import com.zenith.mc.food.FoodRegistry;
import com.zenith.util.RequestFuture;
import org.geysermc.mcprotocollib.protocol.data.game.entity.Effect;
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.MetadataTypes;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
import java.util.List;
import java.util.Objects;
import static com.github.rfresh2.EventConsumer.of;
import static com.zenith.Globals.CACHE;
import static com.zenith.Globals.CONFIG;
import static com.zenith.Globals.INPUTS;
import static com.zenith.Globals.INVENTORY;
public class AutoGap extends AbstractInventoryModule {
private int delay = 0;
private boolean isEating = false;
private RequestFuture swapFuture = RequestFuture.rejected;
public AutoGap() {
super(HandRestriction.EITHER, 0);
}
@Override
public List<EventConsumer<?>> registerEvents() {
return List.of(
of(ClientBotTick.class, this::handleClientTick),
of(ClientBotTick.Starting.class, this::handleBotTickStarting),
of(ClientBotTick.Stopped.class, this::handleBotTickStopped)
);
}
@Override
public int getPriority() {
return Objects.requireNonNullElse(CONFIG.client.extra.autoGap.priority, 11500);
}
@Override
public boolean enabledSetting() {
return CONFIG.client.extra.autoGap.enabled;
}
public boolean isEating() {
return enabledSetting() && isEating;
}
void handleClientTick(final ClientBotTick e) {
if (CACHE.getPlayerCache().getThePlayer().isAlive()
&& CACHE.getPlayerCache().getGameMode() != GameMode.CREATIVE
&& CACHE.getPlayerCache().getGameMode() != GameMode.SPECTATOR
) {
if (delay > 0) {
delay--;
if (isEating) {
INPUTS.submit(InputRequest.noInput(this, getPriority()));
INVENTORY.submit(InventoryActionRequest.noAction(this, getPriority()));
}
return;
}
isEating = false;
if (!swapFuture.isDone()) {
INPUTS.submit(InputRequest.noInput(this, getPriority()));
return;
}
if (!shouldEatGap()) {
return;
}
var invActionResult = doInventoryActionsV2();
switch (invActionResult.state()) {
case ITEM_IN_HAND -> {
delay = invActionResult.expectedDelay();
startEating();
INVENTORY.submit(InventoryActionRequest.noAction(this, getPriority()));
}
case SWAPPING -> swapFuture = invActionResult.inventoryActionFuture();
case NO_ITEM -> {
}
default -> throw new IllegalStateException("Unexpected action state: " + invActionResult.state());
}
} else {
isEating = false;
delay = 0;
}
}
void startEating() {
if (!isItemEquipped()) return;
var hand = getHand();
INPUTS.submit(InputRequest.builder()
.owner(this)
.input(Input.builder()
.rightClick(true)
.hand(hand)
.clickTarget(ClickTarget.None.INSTANCE)
.clickRequiresRotation(false)
.build())
.priority(getPriority())
.build())
.addInputExecutedListener(future -> {
isEating = true;
delay = 50;
});
}
public void onEnable() {
reset();
}
public void onDisable() {
reset();
}
void handleBotTickStarting(final ClientBotTick.Starting event) {
reset();
}
void handleBotTickStopped(final ClientBotTick.Stopped event) {
reset();
}
void reset() {
delay = 0;
isEating = false;
swapFuture = RequestFuture.rejected;
}
boolean shouldEatGap() {
return isLowHealth() || shouldEatFromOnFire();
}
boolean isLowHealth() {
return CACHE.getPlayerCache().getThePlayer().getHealth() <= CONFIG.client.extra.autoGap.healthThreshold;
}
boolean hasFireResistance() {
return CACHE.getPlayerCache().getThePlayer().getPotionEffectMap().containsKey(Effect.FIRE_RESISTANCE);
}
boolean shouldEatFromOnFire() {
return CONFIG.client.extra.autoGap.onFire && isPlayerOnFire() && !hasFireResistance();
}
boolean isPlayerOnFire() {
Byte flagsByte = CACHE.getPlayerCache().getThePlayer().getMetadataValue(0, MetadataTypes.BYTE, Byte.class);
return isOnFireFlags(flagsByte);
}
static boolean isOnFireFlags(final Byte flagsByte) {
return flagsByte != null && (flagsByte & 0x01) != 0;
}
@Override
public boolean itemPredicate(final ItemStack itemStack) {
final boolean requireEnchantedGap = shouldEatFromOnFire() && !isLowHealth();
return shouldUseItem(itemStack, requireEnchantedGap, hasEnchantedGapInInventory());
}
boolean shouldUseItem(final ItemStack itemStack, final boolean requireEnchantedGap, final boolean hasEnchantedGapInInventory) {
if (requireEnchantedGap) {
return isEnchantedGap(itemStack);
}
if (isEnchantedGap(itemStack)) return true;
return isRegularGap(itemStack) && !hasEnchantedGapInInventory;
}
boolean hasEnchantedGapInInventory() {
var inventory = CACHE.getPlayerCache().getPlayerInventory();
for (int i = 9; i <= 44; i++) {
if (isEnchantedGap(inventory.get(i))) {
return true;
}
}
return false;
}
boolean isRegularGap(ItemStack itemStack) {
return itemStack != null && itemStack.getId() == FoodRegistry.GOLDEN_APPLE.id();
}
boolean isEnchantedGap(ItemStack itemStack) {
return itemStack != null && itemStack.getId() == FoodRegistry.ENCHANTED_GOLDEN_APPLE.id();
}
}