TickCommand .java

net.minecraft.server.commands.TickCommand (class, 167 lines): members: ERROR_SPRINT_STOP_NOT_SPRINTING, ERROR_STEP_STOP_NOT_STEPPING, DEFAULT_TICKRATE.

net.minecraft.server.commandsclass167 linesserver7.1 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)

Fields & Constants (5)

MAX_TICKRATE
DEFAULT_TICKRATE
ERROR_TICK_STEP_NOT_FROZEN
ERROR_STEP_STOP_NOT_STEPPING
ERROR_SPRINT_STOP_NOT_SPRINTING

String literals (23)

"commands.tick.step.fail"
"commands.tick.step.stop.fail"
"commands.tick.sprint.stop.fail"
"tick"
"query"
"rate"
"step"
"stop"
"time"
"sprint"
"unfreeze"
"freeze"
"commands.tick.rate.success"
"commands.tick.status.sprinting"
"commands.tick.query.rate.sprinting"
"commands.tick.status.frozen"
"commands.tick.status.lagging"
"commands.tick.status.running"
"commands.tick.query.rate.running"
"commands.tick.query.percentiles"
"commands.tick.sprint.stop.success"
"commands.tick.step.success"
"commands.tick.step.stop.success"

Full Source

TickCommand.java167 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.arguments.FloatArgumentType;
5
import com.mojang.brigadier.arguments.IntegerArgumentType;
6
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
8
import java.util.Arrays;
9
import java.util.Locale;
10
import net.minecraft.commands.CommandSourceStack;
11
import net.minecraft.commands.Commands;
12
import net.minecraft.commands.SharedSuggestionProvider;
13
import net.minecraft.commands.arguments.TimeArgument;
14
import net.minecraft.network.chat.Component;
15
import net.minecraft.server.ServerTickRateManager;
16
import net.minecraft.util.TimeUtil;
17
18
public class TickCommand {
19
	private static final float MAX_TICKRATE = 10000.0F;
20
	private static final String DEFAULT_TICKRATE = String.valueOf(20);
21
	private static final SimpleCommandExceptionType ERROR_TICK_STEP_NOT_FROZEN = new SimpleCommandExceptionType(Component.translatable("commands.tick.step.fail"));
22
	private static final SimpleCommandExceptionType ERROR_STEP_STOP_NOT_STEPPING = new SimpleCommandExceptionType(
23
		Component.translatable("commands.tick.step.stop.fail")
24
	);
25
	private static final SimpleCommandExceptionType ERROR_SPRINT_STOP_NOT_SPRINTING = new SimpleCommandExceptionType(
26
		Component.translatable("commands.tick.sprint.stop.fail")
27
	);
28
29
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
30
		dispatcher.register(
31
			Commands.literal("tick")
32
				.requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
33
				.then(Commands.literal("query").executes(c -> tickQuery(c.getSource())))
34
				.then(
35
					Commands.literal("rate")
36
						.then(
37
							Commands.argument("rate", FloatArgumentType.floatArg(1.0F, 10000.0F))
38
								.suggests((c, b) -> SharedSuggestionProvider.suggest(new String[]{DEFAULT_TICKRATE}, b))
39
								.executes(c -> setTickingRate(c.getSource(), FloatArgumentType.getFloat(c, "rate")))
40
						)
41
				)
42
				.then(
43
					Commands.literal("step")
44
						.executes(c -> step(c.getSource(), 1))
45
						.then(Commands.literal("stop").executes(c -> stopStepping(c.getSource())))
46
						.then(
47
							Commands.argument("time", TimeArgument.time(1))
48
								.suggests((c, b) -> SharedSuggestionProvider.suggest(new String[]{"1t", "1s"}, b))
49
								.executes(c -> step(c.getSource(), IntegerArgumentType.getInteger(c, "time")))
50
						)
51
				)
52
				.then(
53
					Commands.literal("sprint")
54
						.then(Commands.literal("stop").executes(c -> stopSprinting(c.getSource())))
55
						.then(
56
							Commands.argument("time", TimeArgument.time(1))
57
								.suggests((c, b) -> SharedSuggestionProvider.suggest(new String[]{"60s", "1d", "3d"}, b))
58
								.executes(c -> sprint(c.getSource(), IntegerArgumentType.getInteger(c, "time")))
59
						)
60
				)
61
				.then(Commands.literal("unfreeze").executes(c -> setFreeze(c.getSource(), false)))
62
				.then(Commands.literal("freeze").executes(c -> setFreeze(c.getSource(), true)))
63
		);
64
	}
65
66
	private static String nanosToMilisString(final long nanos) {
67
		return String.format(Locale.ROOT, "%.1f", (float)nanos / (float)TimeUtil.NANOSECONDS_PER_MILLISECOND);
68
	}
69
70
	private static int setTickingRate(final CommandSourceStack source, final float rate) {
71
		ServerTickRateManager manager = source.getServer().tickRateManager();
72
		manager.setTickRate(rate);
73
		String tickRateString = String.format(Locale.ROOT, "%.1f", rate);
74
		source.sendSuccess(() -> Component.translatable("commands.tick.rate.success", tickRateString), true);
75
		return (int)rate;
76
	}
77
78
	private static int tickQuery(final CommandSourceStack source) {
79
		ServerTickRateManager manager = source.getServer().tickRateManager();
80
		String busyTime = nanosToMilisString(source.getServer().getAverageTickTimeNanos());
81
		float tickRate = manager.tickrate();
82
		String tickRateString = String.format(Locale.ROOT, "%.1f", tickRate);
83
		if (manager.isSprinting()) {
84
			source.sendSuccess(() -> Component.translatable("commands.tick.status.sprinting"), false);
85
			source.sendSuccess(() -> Component.translatable("commands.tick.query.rate.sprinting", tickRateString, busyTime), false);
86
		} else {
87
			if (manager.isFrozen()) {
88
				source.sendSuccess(() -> Component.translatable("commands.tick.status.frozen"), false);
89
			} else if (manager.nanosecondsPerTick() < source.getServer().getAverageTickTimeNanos()) {
90
				source.sendSuccess(() -> Component.translatable("commands.tick.status.lagging"), false);
91
			} else {
92
				source.sendSuccess(() -> Component.translatable("commands.tick.status.running"), false);
93
			}
94
95
			String milliSecondsPerTickTarget = nanosToMilisString(manager.nanosecondsPerTick());
96
			source.sendSuccess(() -> Component.translatable("commands.tick.query.rate.running", tickRateString, busyTime, milliSecondsPerTickTarget), false);
97
		}
98
99
		long[] samples = Arrays.copyOf(source.getServer().getTickTimesNanos(), source.getServer().getTickTimesNanos().length);
100
		Arrays.sort(samples);
101
		String p50 = nanosToMilisString(samples[samples.length / 2]);
102
		String p95 = nanosToMilisString(samples[(int)(samples.length * 0.95)]);
103
		String p99 = nanosToMilisString(samples[(int)(samples.length * 0.99)]);
104
		source.sendSuccess(() -> Component.translatable("commands.tick.query.percentiles", p50, p95, p99, samples.length), false);
105
		return (int)tickRate;
106
	}
107
108
	private static int sprint(final CommandSourceStack source, final int time) {
109
		boolean interrupted = source.getServer().tickRateManager().requestGameToSprint(time);
110
		if (interrupted) {
111
			source.sendSuccess(() -> Component.translatable("commands.tick.sprint.stop.success"), true);
112
		}
113
114
		source.sendSuccess(() -> Component.translatable("commands.tick.status.sprinting"), true);
115
		return 1;
116
	}
117
118
	private static int setFreeze(final CommandSourceStack source, final boolean freeze) {
119
		ServerTickRateManager manager = source.getServer().tickRateManager();
120
		if (freeze) {
121
			if (manager.isSprinting()) {
122
				manager.stopSprinting();
123
			}
124
125
			if (manager.isSteppingForward()) {
126
				manager.stopStepping();
127
			}
128
		}
129
130
		manager.setFrozen(freeze);
131
		if (freeze) {
132
			source.sendSuccess(() -> Component.translatable("commands.tick.status.frozen"), true);
133
		} else {
134
			source.sendSuccess(() -> Component.translatable("commands.tick.status.running"), true);
135
		}
136
137
		return freeze ? 1 : 0;
138
	}
139
140
	private static int step(final CommandSourceStack source, final int advance) throws CommandSyntaxException {
141
		if (!source.getServer().tickRateManager().stepGameIfPaused(advance)) {
142
			throw ERROR_TICK_STEP_NOT_FROZEN.create();
143
		}
144
145
		source.sendSuccess(() -> Component.translatable("commands.tick.step.success", advance), true);
146
		return 1;
147
	}
148
149
	private static int stopStepping(final CommandSourceStack source) throws CommandSyntaxException {
150
		if (!source.getServer().tickRateManager().stopStepping()) {
151
			throw ERROR_STEP_STOP_NOT_STEPPING.create();
152
		}
153
154
		source.sendSuccess(() -> Component.translatable("commands.tick.step.stop.success"), true);
155
		return 1;
156
	}
157
158
	private static int stopSprinting(final CommandSourceStack source) throws CommandSyntaxException {
159
		if (!source.getServer().tickRateManager().stopSprinting()) {
160
			throw ERROR_SPRINT_STOP_NOT_SPRINTING.create();
161
		}
162
163
		source.sendSuccess(() -> Component.translatable("commands.tick.sprint.stop.success"), true);
164
		return 1;
165
	}
166
}
167

Decompiled Minecraft 26.3 source of TickCommand (net/minecraft/server/commands/TickCommand.java) with Mojang official mappings, stored verbatim on the site.