Skip to content

Commit

Permalink
Fix removing the previous task
Browse files Browse the repository at this point in the history
  • Loading branch information
xpple committed Feb 6, 2025
1 parent 1bc0636 commit 8845baf
Showing 1 changed file with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CTickCommand {

private static final String TASK_NAME = "ctick";

private static TickMeasuringTask currentMeasurer = null;
private static CurrentTask currentTask = null;

public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
dispatcher.register(literal("ctick")
Expand All @@ -42,8 +42,8 @@ private static int getClientTps(FabricClientCommandSource source) throws Command
stopPreviousTask();

TickMeasuringTask measurer = new TickMeasuringTask(true, false);
TaskManager.addTask(TASK_NAME, measurer);
currentMeasurer = measurer;
String name = TaskManager.addTask(TASK_NAME, measurer);
currentTask = new CurrentTask(name, measurer);

float tps = source.getWorld().tickRateManager().tickrate();
source.sendFeedback(Component.translatable("commands.ctick.client.tps.expectedTps", tps));
Expand All @@ -54,8 +54,8 @@ private static int getClientMspt(FabricClientCommandSource source) throws Comman
stopPreviousTask();

TickMeasuringTask measurer = new TickMeasuringTask(false, false);
TaskManager.addTask(TASK_NAME, measurer);
currentMeasurer = measurer;
String name = TaskManager.addTask(TASK_NAME, measurer);
currentTask = new CurrentTask(name, measurer);

float mspt = TimeUtil.MILLISECONDS_PER_SECOND / source.getWorld().tickRateManager().tickrate();
source.sendFeedback(Component.translatable("commands.ctick.client.mspt.expectedMspt", mspt));
Expand All @@ -67,8 +67,8 @@ private static int getServerTps(FabricClientCommandSource source) throws Command

boolean isIntegratedServer = source.getClient().hasSingleplayerServer();
TickMeasuringTask measurer = new TickMeasuringTask(true, !isIntegratedServer);
TaskManager.addTask(TASK_NAME, measurer);
currentMeasurer = measurer;
String name = TaskManager.addTask(TASK_NAME, measurer);
currentTask = new CurrentTask(name, measurer);

float tps = source.getWorld().tickRateManager().tickrate();
source.sendFeedback(Component.translatable("commands.ctick.server.tps.expectedTps", tps));
Expand All @@ -80,19 +80,18 @@ private static int getServerMspt(FabricClientCommandSource source) throws Comman

boolean isIntegratedServer = source.getClient().hasSingleplayerServer();
TickMeasuringTask measurer = new TickMeasuringTask(false, !isIntegratedServer);
TaskManager.addTask(TASK_NAME, measurer);
currentMeasurer = measurer;
String name = TaskManager.addTask(TASK_NAME, measurer);
currentTask = new CurrentTask(name, measurer);

float mspt = TimeUtil.MILLISECONDS_PER_SECOND / source.getWorld().tickRateManager().tickrate();
source.sendFeedback(Component.translatable("commands.ctick.server.mspt.expectedMspt", mspt));
return (int) mspt;
}

private static void stopPreviousTask() {
if (currentMeasurer != null) {
currentMeasurer._break();
TaskManager.removeTask(TASK_NAME);
currentMeasurer = null;
if (currentTask != null) {
TaskManager.removeTask(currentTask.name);
currentTask = null;
}
}

Expand Down Expand Up @@ -180,14 +179,14 @@ public boolean condition() {

public static void registerEvents() {
ClientTickEvents.START_CLIENT_TICK.register(minecraft -> {
if (currentMeasurer != null && !currentMeasurer.isCompleted() && !currentMeasurer.forceInaccurate) {
currentMeasurer.startTick();
if (currentTask != null && !currentTask.measurer.isCompleted() && !currentTask.measurer.forceInaccurate) {
currentTask.measurer.startTick();
}
});

ClientTickEvents.END_CLIENT_TICK.register(minecraft -> {
if (currentMeasurer != null && !currentMeasurer.isCompleted() && !currentMeasurer.forceInaccurate) {
currentMeasurer.endTick();
if (currentTask != null && !currentTask.measurer.isCompleted() && !currentTask.measurer.forceInaccurate) {
currentTask.measurer.endTick();
}
});

Expand All @@ -196,15 +195,18 @@ public static void registerEvents() {

@Override
public void onTimeSync(ClientboundSetTimePacket packet) {
if (currentMeasurer != null && !currentMeasurer.isCompleted() && currentMeasurer.forceInaccurate) {
if (currentTask != null && !currentTask.measurer.isCompleted() && currentTask.measurer.forceInaccurate) {
long tick = packet.gameTime();
if (lastTick != -1) {
int deltaTick = (int) (tick - lastTick);
currentMeasurer.incrTickCount(deltaTick);
currentTask.measurer.incrTickCount(deltaTick);
}
lastTick = tick;
}
}
});
}

private record CurrentTask(String name, TickMeasuringTask measurer) {
}
}

0 comments on commit 8845baf

Please sign in to comment.