-
-
Notifications
You must be signed in to change notification settings - Fork 126
Add the ctick command #712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: fabric
Are you sure you want to change the base?
Changes from all commits
0518b86
a7a1fdf
1bc0636
8845baf
a61803f
a816065
9217766
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
package net.earthcomputer.clientcommands.command; | ||
|
||
import com.mojang.brigadier.CommandDispatcher; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import net.earthcomputer.clientcommands.event.MoreClientEvents; | ||
import net.earthcomputer.clientcommands.task.SimpleTask; | ||
import net.earthcomputer.clientcommands.task.TaskManager; | ||
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; | ||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.network.protocol.game.ClientboundSetTimePacket; | ||
import net.minecraft.util.TimeUtil; | ||
|
||
import java.text.DecimalFormat; | ||
|
||
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*; | ||
|
||
public class CTickCommand { | ||
|
||
private static final DecimalFormat DEC_FMT = new DecimalFormat("0.00"); | ||
|
||
private static final String TASK_NAME = "ctick"; | ||
|
||
private static CurrentTask currentTask = null; | ||
|
||
public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) { | ||
dispatcher.register(literal("ctick") | ||
.then(literal("client") | ||
.then(literal("tps") | ||
.executes(ctx -> getClientTps(ctx.getSource()))) | ||
.then(literal("mspt") | ||
.executes(ctx -> getClientMspt(ctx.getSource())))) | ||
.then(literal("server") | ||
.then(literal("tps") | ||
.executes(ctx -> getServerTps(ctx.getSource()))) | ||
.then(literal("mspt") | ||
.executes(ctx -> getServerMspt(ctx.getSource()))))); | ||
} | ||
|
||
private static int getClientTps(FabricClientCommandSource source) throws CommandSyntaxException { | ||
stopPreviousTask(); | ||
|
||
TickMeasuringTask measurer = new TickMeasuringTask(true, false); | ||
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)); | ||
return (int) tps; | ||
} | ||
|
||
private static int getClientMspt(FabricClientCommandSource source) throws CommandSyntaxException { | ||
stopPreviousTask(); | ||
|
||
TickMeasuringTask measurer = new TickMeasuringTask(false, false); | ||
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)); | ||
return (int) mspt; | ||
} | ||
|
||
private static int getServerTps(FabricClientCommandSource source) throws CommandSyntaxException { | ||
stopPreviousTask(); | ||
|
||
boolean isIntegratedServer = source.getClient().hasSingleplayerServer(); | ||
TickMeasuringTask measurer = new TickMeasuringTask(true, !isIntegratedServer); | ||
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)); | ||
return (int) tps; | ||
} | ||
|
||
private static int getServerMspt(FabricClientCommandSource source) throws CommandSyntaxException { | ||
stopPreviousTask(); | ||
|
||
boolean isIntegratedServer = source.getClient().hasSingleplayerServer(); | ||
TickMeasuringTask measurer = new TickMeasuringTask(false, !isIntegratedServer); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not seeing where there is any accurate measurements from the integrated server. Be careful about thread safety with that, though. |
||
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 (currentTask != null) { | ||
TaskManager.removeTask(currentTask.name); | ||
currentTask = null; | ||
} | ||
} | ||
|
||
private static class TickMeasuringTask extends SimpleTask { | ||
|
||
private static final int PERIOD = 100; | ||
|
||
private int tickCount = 0; | ||
private long totalTickTime = 0; | ||
private long startTickTime; | ||
private boolean hadFirstTick = false; | ||
private long firstTickStart; | ||
private long lastTickStart; | ||
|
||
private final boolean tps; | ||
private final boolean forceInaccurate; | ||
|
||
public TickMeasuringTask(boolean tps, boolean forceInaccurate) { | ||
this.tps = tps; | ||
this.forceInaccurate = forceInaccurate; | ||
} | ||
|
||
public void incrTickCount(int count) { | ||
if (!hadFirstTick) { | ||
firstTickStart = System.nanoTime(); | ||
hadFirstTick = true; | ||
} else { | ||
tickCount += count; | ||
} | ||
} | ||
|
||
public void startTick() { | ||
startTickTime = System.nanoTime(); | ||
if (!hadFirstTick) { | ||
firstTickStart = startTickTime; | ||
hadFirstTick = true; | ||
} | ||
} | ||
|
||
public void endTick() { | ||
if (hadFirstTick) { | ||
totalTickTime += System.nanoTime() - startTickTime; | ||
tickCount++; | ||
} | ||
} | ||
|
||
@Override | ||
protected void onTick() { | ||
if (tickCount >= PERIOD) { | ||
lastTickStart = System.nanoTime(); | ||
_break(); | ||
} | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
ClientCommandHelper.sendFeedback(Component.translatable("commands.ctick.measuring")); | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
if (tps) { | ||
long totalTime = lastTickStart - firstTickStart; | ||
double tps = 1_000_000_000D * tickCount / totalTime; | ||
ClientCommandHelper.sendFeedback(Component.translatable("commands.ctick.tps", totalTime == 0 ? Component.translatable("commands.ctick.tps.immeasurable") : DEC_FMT.format(tps))); | ||
} else if (forceInaccurate) { | ||
long totalTime = lastTickStart - firstTickStart; | ||
double mspt = totalTime / (1_000_000D * tickCount); | ||
ClientCommandHelper.sendFeedback(Component.translatable("commands.ctick.mspt", DEC_FMT.format(mspt))); | ||
ClientCommandHelper.sendHelp(Component.translatable("commands.ctick.mspt.inaccurate")); | ||
} else { | ||
double mspt = totalTickTime / (1_000_000D * tickCount); | ||
ClientCommandHelper.sendFeedback(Component.translatable("commands.ctick.mspt", DEC_FMT.format(mspt))); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean condition() { | ||
return tickCount <= 1200; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wasn't a problem in 1.12 since the client tick rate was always expected to be 20 and hardly ever fell below 20, but now if the TPS is set to 1 then this could take a long time to complete. I would suggest using real time instead. Just be extra careful about there being zero ticks elapsed and dividing by zero. |
||
} | ||
} | ||
|
||
public static void registerEvents() { | ||
ClientTickEvents.START_CLIENT_TICK.register(minecraft -> { | ||
if (currentTask != null && !currentTask.measurer.isCompleted() && !currentTask.measurer.forceInaccurate) { | ||
currentTask.measurer.startTick(); | ||
} | ||
}); | ||
|
||
ClientTickEvents.END_CLIENT_TICK.register(minecraft -> { | ||
if (currentTask != null && !currentTask.measurer.isCompleted() && !currentTask.measurer.forceInaccurate) { | ||
currentTask.measurer.endTick(); | ||
} | ||
}); | ||
|
||
MoreClientEvents.TIME_SYNC.register(new MoreClientEvents.TimeSync() { | ||
private long lastTick = -1; | ||
|
||
@Override | ||
public void onTimeSync(ClientboundSetTimePacket packet) { | ||
if (currentTask != null && !currentTask.measurer.isCompleted() && currentTask.measurer.forceInaccurate) { | ||
long tick = packet.gameTime(); | ||
if (lastTick != -1) { | ||
int deltaTick = (int) (tick - lastTick); | ||
currentTask.measurer.incrTickCount(deltaTick); | ||
} | ||
lastTick = tick; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private record CurrentTask(String name, TickMeasuringTask measurer) { | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These expected TPS and MSPT messages shouldn't be printed while the game is sprinting