Skip to content

Commit 416eb7e

Browse files
committed
update eat module logic
1 parent eb78139 commit 416eb7e

File tree

4 files changed

+31
-39
lines changed

4 files changed

+31
-39
lines changed

src/main/java/com/zenith/feature/inventory/InventoryManager.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,21 @@ public InventoryManager() {
3333
);
3434
}
3535

36+
static long t = 0;
3637
/**
3738
* Requests inventory actions to be executed at the end of the current tick, and subsequent ticks if applicable
3839
*/
3940
public synchronized RequestFuture submit(final InventoryActionRequest actionRequest) {
40-
if (actionRequest.getPriority() <= currentActionRequest.getPriority() && hasActiveRequest())
41+
if (actionRequest.getPriority() <= currentActionRequest.getPriority() || hasActiveRequest())
4142
return RequestFuture.rejected;
42-
if (currentActionRequest.isExecuting()) return RequestFuture.rejected;
4343
currentRequestFuture.complete(false);
4444
currentActionRequest = actionRequest;
4545
currentRequestFuture = new RequestFuture();
4646
return currentRequestFuture;
4747
}
4848

4949
private synchronized void handleTick(final ClientBotTick event) {
50+
t++;
5051
if (currentActionRequest == DEFAULT_ACTION_REQUEST) return;
5152
int delay = requireNonNullElse(
5253
currentActionRequest.getActionDelayTicks(),
@@ -67,7 +68,8 @@ private void executeNextAction() {
6768
var action = currentActionRequest.next();
6869
if (action != null) {
6970
if (action.containerId() != CACHE.getPlayerCache().getInventoryCache().getOpenContainerId()) {
70-
CLIENT_LOG.debug("[Inventory Manager] Skipping action {} requester: {} requested container: {} != {}",
71+
CLIENT_LOG.debug("[Inventory Manager] [{}] Skipping action {} requester: {} requested container: {} != {}",
72+
t,
7173
action,
7274
currentActionRequest.getOwnerName(),
7375
action.containerId(),
@@ -76,7 +78,8 @@ private void executeNextAction() {
7678
var packet = action.packet();
7779
if (packet != null) {
7880
// todo: setting for toggling inv debug logging
79-
CLIENT_LOG.debug("[Inventory Manager] Executing action: {} requester: {}",
81+
CLIENT_LOG.debug("[Inventory Manager] [{}] Executing action: {} requester: {}",
82+
t,
8083
action,
8184
currentActionRequest.getOwnerName());
8285
Proxy.getInstance().getClient().sendAwait(packet);
@@ -87,6 +90,8 @@ private void executeNextAction() {
8790
}
8891
}
8992
}
93+
} else {
94+
CLIENT_LOG.debug("[Inventory Manager] [{}] Executing no action, requester: {}", t, currentActionRequest.getOwnerName());
9095
}
9196
if (currentActionRequest.isCompleted()) {
9297
currentRequestFuture.complete(true);
@@ -108,6 +113,6 @@ public int requestedContainerId() {
108113
}
109114

110115
public boolean hasActiveRequest() {
111-
return currentActionRequest != DEFAULT_ACTION_REQUEST;
116+
return currentActionRequest != DEFAULT_ACTION_REQUEST && currentActionRequest.isExecuting();
112117
}
113118
}

src/main/java/com/zenith/module/impl/AbstractInventoryModule.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
import java.util.ArrayList;
1717
import java.util.List;
1818

19-
import static com.zenith.Globals.CACHE;
20-
import static com.zenith.Globals.INVENTORY;
19+
import static com.zenith.Globals.*;
2120
import static java.util.Objects.nonNull;
2221

2322
/**
@@ -65,15 +64,15 @@ public InventoryActionResult doInventoryActionsV2() {
6564
return new InventoryActionResult(ActionState.ITEM_IN_HAND, RequestFuture.rejected, 0);
6665
var swapFuture = switchToItem();
6766
if (swapFuture != null)
68-
return new InventoryActionResult(ActionState.SWAPPING, swapFuture, 5);
67+
return new InventoryActionResult(ActionState.SWAPPING, swapFuture, CONFIG.client.inventory.actionDelayTicks);
6968
return new InventoryActionResult(ActionState.NO_ITEM, RequestFuture.rejected, 0);
7069
}
7170

7271
// returns delay (if any) before next action
7372
public int doInventoryActions() {
7473
if (isItemEquipped()) return 0;
7574
var switchResult = switchToItem();
76-
if (switchResult != null) return 5;
75+
if (switchResult != null) return CONFIG.client.inventory.actionDelayTicks;
7776
return 0;
7877
}
7978

src/main/java/com/zenith/module/impl/AutoEat.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ void handleClientTick(final ClientBotTick e) {
6060
if (CACHE.getPlayerCache().getThePlayer().isAlive()
6161
&& CACHE.getPlayerCache().getGameMode() != GameMode.CREATIVE
6262
&& CACHE.getPlayerCache().getGameMode() != GameMode.SPECTATOR
63-
&& playerHealthBelowThreshold()
6463
) {
6564
if (delay > 0) {
6665
delay--;
67-
if (isEating || !swapFuture.isDone()) {
66+
if (isEating) {
6867
INPUTS.submit(InputRequest.noInput(this, getPriority()));
6968
INVENTORY.submit(InventoryActionRequest.noAction(this, getPriority()));
7069
}
@@ -75,11 +74,15 @@ && playerHealthBelowThreshold()
7574
INPUTS.submit(InputRequest.noInput(this, getPriority()));
7675
return;
7776
}
77+
if (!playerHealthBelowThreshold()) {
78+
return;
79+
}
7880
var invActionResult = doInventoryActionsV2();
7981
switch (invActionResult.state()) {
8082
case ITEM_IN_HAND -> {
8183
startEating();
8284
INVENTORY.submit(InventoryActionRequest.noAction(this, getPriority()));
85+
delay = invActionResult.expectedDelay();
8386
}
8487
case NO_ITEM -> {
8588
if (CONFIG.client.extra.autoEat.warning
@@ -97,19 +100,20 @@ && playerHealthBelowThreshold()
97100
}
98101
default -> throw new IllegalStateException("Unexpected action state: " + invActionResult.state());
99102
}
100-
delay = invActionResult.expectedDelay();
101103
} else {
102104
isEating = false;
105+
delay = 0;
103106
}
104107
}
105108

106109
void startEating() {
110+
if (!isItemEquipped()) return;
107111
var hand = getHand();
108-
if (hand == null) return;
109112
INPUTS.submit(InputRequest.builder()
110113
.owner(this)
111114
.input(Input.builder()
112115
.rightClick(true)
116+
.hand(hand)
113117
.clickTarget(ClickTarget.None.INSTANCE)
114118
.clickRequiresRotation(false)
115119
.build())

src/main/java/com/zenith/module/impl/AutoOmen.java

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66
import com.zenith.feature.player.ClickTarget;
77
import com.zenith.feature.player.Input;
88
import com.zenith.feature.player.InputRequest;
9-
import com.zenith.mc.block.Direction;
109
import com.zenith.mc.item.ItemData;
1110
import com.zenith.mc.item.ItemRegistry;
1211
import com.zenith.util.RequestFuture;
1312
import net.kyori.adventure.text.Component;
1413
import net.kyori.adventure.text.TranslatableComponent;
1514
import org.geysermc.mcprotocollib.protocol.data.game.entity.Effect;
1615
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
17-
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.PlayerAction;
1816
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
19-
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundPlayerActionPacket;
2017

2118
import java.util.List;
2219
import java.util.Objects;
@@ -68,14 +65,12 @@ public void handleClientTick(final ClientBotTick e) {
6865
lastRaidActive = System.nanoTime();
6966
}
7067
if (CACHE.getPlayerCache().getThePlayer().isAlive()
71-
&& (CONFIG.client.extra.autoOmen.whileRaidActive || (TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - lastRaidActive) > CONFIG.client.extra.autoOmen.raidCooldownMs))
72-
&& (CONFIG.client.extra.autoOmen.whileOmenActive || (TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - lastHadOmen) > CONFIG.client.extra.autoOmen.omenCooldownMs))
7368
&& CACHE.getPlayerCache().getGameMode() != GameMode.CREATIVE
7469
&& CACHE.getPlayerCache().getGameMode() != GameMode.SPECTATOR
7570
) {
7671
if (delay > 0) {
7772
delay--;
78-
if (isEating || !swapFuture.isDone()) {
73+
if (isEating) {
7974
INPUTS.submit(InputRequest.noInput(this, getPriority()));
8075
INVENTORY.submit(InventoryActionRequest.noAction(this, getPriority()));
8176
}
@@ -86,49 +81,38 @@ public void handleClientTick(final ClientBotTick e) {
8681
INPUTS.submit(InputRequest.noInput(this, getPriority()));
8782
return;
8883
}
84+
var raidActiveCondition = CONFIG.client.extra.autoOmen.whileRaidActive || TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - lastRaidActive) > CONFIG.client.extra.autoOmen.raidCooldownMs;
85+
var omenActiveCondition = CONFIG.client.extra.autoOmen.whileOmenActive || TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - lastHadOmen) > CONFIG.client.extra.autoOmen.omenCooldownMs;
86+
if (!raidActiveCondition || !omenActiveCondition) {
87+
return;
88+
}
8989
var invActionResult = doInventoryActionsV2();
9090
switch (invActionResult.state()) {
9191
case ITEM_IN_HAND -> {
9292
startEating();
9393
INVENTORY.submit(InventoryActionRequest.noAction(this, getPriority()));
94+
delay = invActionResult.expectedDelay();
9495
}
9596
case NO_ITEM -> {}
9697
case SWAPPING -> {
9798
swapFuture = invActionResult.inventoryActionFuture();
9899
}
99100
default -> throw new IllegalStateException("Unexpected action state: " + invActionResult.state());
100101
}
101-
delay = invActionResult.expectedDelay();
102102
} else {
103-
if (isEating) {
104-
if (delay > 0) { // we got interrupted during drinking
105-
// todo: have bot automatically cancel eats if not confirmed every tick?
106-
sendClientPacketAsync(new ServerboundPlayerActionPacket(
107-
PlayerAction.RELEASE_USE_ITEM,
108-
0, 0, 0,
109-
Direction.DOWN.mcpl(),
110-
CACHE.getPlayerCache().getSeqId().incrementAndGet()
111-
));
112-
debug("Got interrupted during omen drink");
113-
delay = 0;
114-
} else {
115-
delay = 20;
116-
debug("Omen drink completed");
117-
}
118-
} else {
119-
delay = 0;
120-
}
121103
isEating = false;
104+
delay = 0;
122105
}
123106
}
124107

125108
public void startEating() {
109+
if (!isItemEquipped()) return;
126110
var hand = getHand();
127-
if (hand == null) return;
128111
INPUTS.submit(InputRequest.builder()
129112
.owner(this)
130113
.input(Input.builder()
131114
.rightClick(true)
115+
.hand(hand)
132116
.clickTarget(ClickTarget.None.INSTANCE)
133117
.clickRequiresRotation(false)
134118
.build())

0 commit comments

Comments
 (0)