Skip to content

Commit

Permalink
BPS bug fixes
Browse files Browse the repository at this point in the history
BPS Tracker:
= Possibly fixed Lower Average BPS failsafe false positives
+ Added pausing during lane switch, inventory open, etc.
  • Loading branch information
onixiya1337 committed Jul 8, 2024
1 parent 3367644 commit 5069750
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 71 deletions.
22 changes: 19 additions & 3 deletions src/main/java/com/jelly/farmhelperv2/config/FarmHelperConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.jelly.farmhelperv2.config.page.FailsafeNotificationsPage;
import com.jelly.farmhelperv2.config.struct.Rewarp;
import com.jelly.farmhelperv2.failsafe.FailsafeManager;
import com.jelly.farmhelperv2.failsafe.impl.LowerAvgBpsFailsafe;
import com.jelly.farmhelperv2.feature.impl.*;
import com.jelly.farmhelperv2.gui.AutoUpdaterGUI;
import com.jelly.farmhelperv2.handler.GameStateHandler;
Expand Down Expand Up @@ -1950,11 +1949,28 @@ public enum SPRAYONATOR_ITEM {
)
public static boolean streamerModeInfo2;
@HUD(
name = "Status HUD", category = HUD
name = "Status HUD - Visual Settings", category = HUD
)
public static StatusHUD statusHUD = new StatusHUD();

@Switch(
name = "Count RNG to $/Hr in Profit Calculator", category = HUD, subcategory = "Profit Calculator",
description = "Count RNG to $/Hr"
)
public static boolean countRNGToProfitCalc = false;
@Switch(
name = "Reset stats between disabling", category = HUD, subcategory = "Profit Calculator"
)
public static boolean resetStatsBetweenDisabling = false;
@Button(
name = "Reset Profit Calculator", category = HUD, subcategory = "Profit Calculator",
text = "Reset Now", size = 2
)
public void resetStats() {
ProfitCalculator.getInstance().resetProfits();
}
@HUD(
name = "Profit Calculator HUD", category = HUD, subcategory = " "
name = "Profit Calculator HUD - Visual Settings", category = HUD, subcategory = " "
)
public static ProfitCalculatorHUD profitHUD = new ProfitCalculatorHUD();
//</editor-fold>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
package com.jelly.farmhelperv2.failsafe.impl;

import cc.polyfrost.oneconfig.utils.Multithreading;
import com.jelly.farmhelperv2.config.FarmHelperConfig;
import com.jelly.farmhelperv2.config.page.CustomFailsafeMessagesPage;
import com.jelly.farmhelperv2.config.page.FailsafeNotificationsPage;
import com.jelly.farmhelperv2.event.ReceivePacketEvent;
import com.jelly.farmhelperv2.failsafe.Failsafe;
import com.jelly.farmhelperv2.failsafe.FailsafeManager;
import com.jelly.farmhelperv2.feature.FeatureManager;
import com.jelly.farmhelperv2.feature.impl.AntiStuck;
import com.jelly.farmhelperv2.feature.impl.AutoSell;
import com.jelly.farmhelperv2.feature.impl.LagDetector;
import com.jelly.farmhelperv2.feature.impl.MovRecPlayer;
import com.jelly.farmhelperv2.handler.BaritoneHandler;
import com.jelly.farmhelperv2.handler.GameStateHandler;
import com.jelly.farmhelperv2.handler.MacroHandler;
import com.jelly.farmhelperv2.handler.RotationHandler;
import com.jelly.farmhelperv2.pathfinder.FlyPathFinderExecutor;
import com.jelly.farmhelperv2.util.AngleUtils;
import com.jelly.farmhelperv2.util.KeyBindUtils;
import com.jelly.farmhelperv2.util.LogUtils;
import com.jelly.farmhelperv2.util.PlayerUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import com.jelly.farmhelperv2.failsafe.FailsafeManager;
import com.jelly.farmhelperv2.feature.FeatureManager;
import com.jelly.farmhelperv2.feature.impl.BPSTracker;
import com.jelly.farmhelperv2.feature.impl.ProfitCalculator;
import com.jelly.farmhelperv2.handler.GameStateHandler;
import com.jelly.farmhelperv2.handler.MacroHandler;
import com.jelly.farmhelperv2.handler.RotationHandler;
import com.jelly.farmhelperv2.util.KeyBindUtils;
import com.jelly.farmhelperv2.util.LogUtils;
import com.jelly.farmhelperv2.util.helper.Clock;
import com.jelly.farmhelperv2.util.helper.FifoQueue;
import com.jelly.farmhelperv2.util.helper.Rotation;
import com.jelly.farmhelperv2.util.helper.RotationConfiguration;
import net.minecraftforge.fml.common.gameevent.TickEvent;
Expand Down Expand Up @@ -72,7 +70,7 @@ public void onTickDetection(TickEvent.ClientTickEvent event) {
}

if (BPSTracker.getInstance().getBPSFloat() - lastBPS < 0) {
if(!clock.isScheduled())
if (!clock.isScheduled())
clock.schedule(FarmHelperConfig.BPSDropThreshold * 1000L);
} else if (BPSTracker.getInstance().getBPSFloat() - lastBPS > 0) {
clock.reset();
Expand Down
93 changes: 69 additions & 24 deletions src/main/java/com/jelly/farmhelperv2/feature/impl/BPSTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.jelly.farmhelperv2.feature.IFeature;
import com.jelly.farmhelperv2.handler.GameStateHandler;
import com.jelly.farmhelperv2.handler.MacroHandler;
import com.jelly.farmhelperv2.util.LogUtils;
import com.jelly.farmhelperv2.util.helper.Clock;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.BlockNetherWart;
import net.minecraft.block.BlockReed;
Expand All @@ -15,14 +13,14 @@
import net.minecraftforge.fml.common.gameevent.TickEvent;

import java.text.NumberFormat;
import java.util.*;
import java.util.LinkedList;
import java.util.Locale;

public class BPSTracker implements IFeature {
private static BPSTracker instance;
private final LinkedList<Tuple<Long, Long>> bpsQueue = new LinkedList<>();
private long blocksBroken = 0;

private long totalBlocksBroken = 0;
public final LinkedList<Tuple<Long, Long>> bpsQueue = new LinkedList<>();
public long blocksBroken = 0;
public long totalBlocksBroken = 0;
private final NumberFormat oneDecimalDigitFormatter = NumberFormat.getNumberInstance(Locale.US);

private BPSTracker() {
Expand All @@ -36,10 +34,36 @@ public static BPSTracker getInstance() {
return instance;
}

public boolean isPaused = false;
private long pauseStartTime = 0;
private float lastKnownBPS = 0;


public void pause() {
if (!isPaused) {
isPaused = true;
pauseStartTime = System.currentTimeMillis();
lastKnownBPS = getBPSFloat();
}
}

private void adjustQueueTimestamps(long pauseDuration) {
LinkedList<Tuple<Long, Long>> adjustedQueue = new LinkedList<>();
for (Tuple<Long, Long> element : bpsQueue) {
adjustedQueue.add(new Tuple<>(element.getFirst(), element.getSecond() + pauseDuration));
}
bpsQueue.clear();
bpsQueue.addAll(adjustedQueue);
}

@Override
public void resume() {
bpsQueue.clear();
totalBlocksBroken = 0;
if (isPaused) {
long pauseDuration = System.currentTimeMillis() - pauseStartTime;
adjustQueueTimestamps(pauseDuration);
isPaused = false;
pauseStartTime = 0;
}
}

@Override
Expand All @@ -54,40 +78,58 @@ public void onTickCheckBPS(TickEvent.ClientTickEvent event) {
if (!MacroHandler.getInstance().isMacroToggled()) return;
if (!MacroHandler.getInstance().isCurrentMacroEnabled()) return;
if (event.phase != TickEvent.Phase.START) return;

bpsQueue.add(new Tuple<>(blocksBroken, System.currentTimeMillis()));
if (MacroHandler.getInstance().getMacro().checkForBPS())
BPSTracker.getInstance().resume();
else
BPSTracker.getInstance().pause();
if (isPaused) return;

long currentTime = System.currentTimeMillis();
bpsQueue.add(new Tuple<>(blocksBroken, currentTime));
blocksBroken = 0;

float elapsedTime = (bpsQueue.getLast().getSecond() - bpsQueue.getFirst().getSecond()) / 1000f;
while (elapsedTime > 10f) {
bpsQueue.pollFirst();
elapsedTime = (bpsQueue.getLast().getSecond() - bpsQueue.getFirst().getSecond()) / 1000f;
if (bpsQueue.size() > 1) {
float elapsedTime = (currentTime - bpsQueue.getFirst().getSecond()) / 1000f;
while (elapsedTime > 10f && bpsQueue.size() > 1) {
Tuple<Long, Long> removed = bpsQueue.pollFirst();
elapsedTime = (currentTime - bpsQueue.getFirst().getSecond()) / 1000f;
}

totalBlocksBroken = 0;
for (Tuple<Long, Long> element : bpsQueue) {
totalBlocksBroken += element.getFirst();
}
totalBlocksBroken -= bpsQueue.getFirst().getFirst();
}

totalBlocksBroken = 0;
for (Tuple<Long, Long> element : bpsQueue) {
totalBlocksBroken += element.getFirst();
}
totalBlocksBroken -= bpsQueue.getFirst().getFirst();
}

public String getBPS() {
if (!MacroHandler.getInstance().getMacroingTimer().isScheduled()) return "0.0 BPS";
return oneDecimalDigitFormatter.format(getBPSFloat()) + " BPS";
}

public boolean dontCheckForBPS() {
return !MacroHandler.getInstance().getMacroingTimer().isScheduled()
|| MacroHandler.getInstance().isCurrentMacroPaused()
|| !MacroHandler.getInstance().getMacro().checkForBPS()
|| isPaused;
}

public float getBPSFloat() {
if (!MacroHandler.getInstance().getMacroingTimer().isScheduled()) return 0;
if (bpsQueue.isEmpty()) return 0;
if (dontCheckForBPS() || isPaused || bpsQueue.size() < 2) {
return lastKnownBPS;
}

float elapsedTime = (bpsQueue.getLast().getSecond() - bpsQueue.getFirst().getSecond()) / 1000f;
return ((int) ((double) this.totalBlocksBroken / elapsedTime * 10.0D)) / 10.0F;
lastKnownBPS = ((int) ((double) this.totalBlocksBroken / elapsedTime * 10.0D)) / 10.0F;
return lastKnownBPS;
}

@SubscribeEvent
public void onBlockChange(PlayerDestroyBlockEvent event) {
if (!MacroHandler.getInstance().isMacroToggled()) return;
if (!GameStateHandler.getInstance().inGarden()) return;
if (dontCheckForBPS()) return;

switch (MacroHandler.getInstance().getCrop()) {
case NETHER_WART:
Expand Down Expand Up @@ -163,6 +205,9 @@ public void resetStatesAfterMacroDisabled() {
bpsQueue.clear();
totalBlocksBroken = 0;
blocksBroken = 0;
lastKnownBPS = 0;
isPaused = false;
pauseStartTime = 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.jelly.farmhelperv2.feature.IFeature;
import com.jelly.farmhelperv2.handler.GameStateHandler;
import com.jelly.farmhelperv2.handler.MacroHandler;
import com.jelly.farmhelperv2.hud.ProfitCalculatorHUD;
import com.jelly.farmhelperv2.util.APIUtils;
import com.jelly.farmhelperv2.util.LogUtils;
import com.jelly.farmhelperv2.util.helper.Clock;
Expand Down Expand Up @@ -163,9 +162,10 @@ public boolean shouldStartAtMacroStart() {

@Override
public void start() {
if (ProfitCalculatorHUD.resetStatsBetweenDisabling) {
if (FarmHelperConfig.resetStatsBetweenDisabling) {
resetProfits();
}
previousCurrentPurse = GameStateHandler.getInstance().getCurrentPurse();
IFeature.super.start();
}

Expand Down Expand Up @@ -259,7 +259,7 @@ public void onTickUpdateProfit(TickEvent.PlayerTickEvent event) {
realProfit = profit;
realProfit += rngPrice;

if (ProfitCalculatorHUD.countRNGToProfitCalc) {
if (FarmHelperConfig.countRNGToProfitCalc) {
realHourlyProfit = (realProfit / (MacroHandler.getInstance().getMacroingTimer().getElapsedTime() / 1000f / 60 / 60));
} else {
realHourlyProfit = profit / (MacroHandler.getInstance().getMacroingTimer().getElapsedTime() / 1000f / 60 / 60);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.jelly.farmhelperv2.failsafe.impl.WorldChangeFailsafe;
import com.jelly.farmhelperv2.feature.FeatureManager;
import com.jelly.farmhelperv2.feature.impl.*;
import com.jelly.farmhelperv2.hud.ProfitCalculatorHUD;
import com.jelly.farmhelperv2.macro.AbstractMacro;
import com.jelly.farmhelperv2.macro.impl.*;
import com.jelly.farmhelperv2.pathfinder.FlyPathFinderExecutor;
Expand Down Expand Up @@ -185,7 +184,7 @@ public void enableMacro() {

analyticsTimer.reset();
Multithreading.schedule(() -> {
if (!macroingTimer.isScheduled() || ProfitCalculatorHUD.resetStatsBetweenDisabling) {
if (!macroingTimer.isScheduled() || FarmHelperConfig.resetStatsBetweenDisabling) {
macroingTimer.schedule();
macroingTimer.pause();
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/jelly/farmhelperv2/hud/DebugHUD.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.jelly.farmhelperv2.config.FarmHelperConfig;
import com.jelly.farmhelperv2.failsafe.FailsafeManager;
import com.jelly.farmhelperv2.failsafe.impl.GuestVisitFailsafe;
import com.jelly.farmhelperv2.failsafe.impl.LowerAvgBpsFailsafe;
import com.jelly.farmhelperv2.feature.FeatureManager;
import com.jelly.farmhelperv2.feature.IFeature;
import com.jelly.farmhelperv2.feature.impl.*;
Expand Down Expand Up @@ -165,5 +164,13 @@ protected void getLines(List<String> lines, boolean example) {
lines.add(" Clock: " + AutoPestExchange.getInstance().getDelayClock().getRemainingTime());
lines.add(" Stuck clock: " + AutoPestExchange.getInstance().getStuckClock().getRemainingTime());
}
if (BPSTracker.getInstance().isRunning()) {
lines.add("BPSTracker");
lines.add(" BPS: " + BPSTracker.getInstance().getBPS());
lines.add(" BPS Queue Size: " + BPSTracker.getInstance().bpsQueue.size());
lines.add(" Blocks Broken: " + BPSTracker.getInstance().blocksBroken);
lines.add(" Total Blocks Broken: " + BPSTracker.getInstance().totalBlocksBroken);
lines.add(" isPaused: " + BPSTracker.getInstance().isPaused);
}
}
}
20 changes: 0 additions & 20 deletions src/main/java/com/jelly/farmhelperv2/hud/ProfitCalculatorHUD.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.jelly.farmhelperv2.hud;

import cc.polyfrost.oneconfig.config.annotations.Button;
import cc.polyfrost.oneconfig.config.annotations.Color;
import cc.polyfrost.oneconfig.config.annotations.Dropdown;
import cc.polyfrost.oneconfig.config.annotations.Switch;
import cc.polyfrost.oneconfig.config.core.OneColor;
import cc.polyfrost.oneconfig.hud.BasicHud;
import cc.polyfrost.oneconfig.libs.universal.UMatrixStack;
Expand All @@ -14,7 +12,6 @@
import com.jelly.farmhelperv2.feature.impl.BPSTracker;
import com.jelly.farmhelperv2.feature.impl.ProfitCalculator;
import com.jelly.farmhelperv2.handler.GameStateHandler;
import com.jelly.farmhelperv2.handler.MacroHandler;
import com.jelly.farmhelperv2.util.LogUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.util.Tuple;
Expand All @@ -24,23 +21,6 @@
import java.util.List;

public class ProfitCalculatorHUD extends BasicHud {
@Switch(
name = "Count RNG to $/Hr in Profit Calculator", category = "Miscellaneous",
description = "Count RNG to $/Hr"
)
public static boolean countRNGToProfitCalc = false;
@Switch(
name = "Reset stats between disabling",
category = "Miscellaneous"
)
public static boolean resetStatsBetweenDisabling = false;
@Button(
name = "Reset Profit Calculator",
category = "Miscellaneous",
text = "Reset Now",
size = 2
)

private final float iconWidth = 12 * scale;
private final float iconHeight = 12 * scale;
protected transient ArrayList<Tuple<String, String>> lines = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public void changeState(State state) {
public abstract void actionAfterTeleport();

public boolean checkForBPS() {
return currentState != State.NONE && currentState != State.DROPPING && currentState != State.SWITCHING_SIDE && currentState != State.SWITCHING_LANE || mc.currentScreen == null;
return currentState != State.NONE && currentState != State.DROPPING && currentState != State.SWITCHING_SIDE && currentState != State.SWITCHING_LANE && mc.currentScreen == null;
}

public State calculateDirection() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.jelly.farmhelperv2.macro.impl;

import com.jelly.farmhelperv2.config.FarmHelperConfig;
import com.jelly.farmhelperv2.feature.impl.AntiStuck;
import com.jelly.farmhelperv2.handler.GameStateHandler;
import com.jelly.farmhelperv2.handler.MacroHandler;
import com.jelly.farmhelperv2.macro.AbstractMacro;
import com.jelly.farmhelperv2.util.*;
import com.jelly.farmhelperv2.util.AngleUtils;
import com.jelly.farmhelperv2.util.KeyBindUtils;
import com.jelly.farmhelperv2.util.LogUtils;
import com.jelly.farmhelperv2.util.helper.Rotation;
import com.jelly.farmhelperv2.util.helper.RotationConfiguration;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;

import java.util.Optional;

Expand Down

0 comments on commit 5069750

Please sign in to comment.