StopwatchCommand .java

net.minecraft.server.commands.StopwatchCommand (class, 116 lines): members: ERROR_DOES_NOT_EXIST, SUGGEST_STOPWATCHES, ERROR_ALREADY_EXISTS, register.

net.minecraft.server.commandsclass116 linesserver4.7 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)

Fields & Constants (3)

ERROR_ALREADY_EXISTS
ERROR_DOES_NOT_EXIST
SUGGEST_STOPWATCHES

String literals (12)

"commands.stopwatch.already_exists"
"commands.stopwatch.does_not_exist"
"stopwatch"
"create"
"query"
"scale"
"restart"
"remove"
"commands.stopwatch.create.success"
"commands.stopwatch.query"
"commands.stopwatch.restart.success"
"commands.stopwatch.remove.success"

Full Source

StopwatchCommand.java116 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.arguments.DoubleArgumentType;
5
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
7
import com.mojang.brigadier.suggestion.SuggestionProvider;
8
import net.minecraft.commands.CommandSourceStack;
9
import net.minecraft.commands.Commands;
10
import net.minecraft.commands.SharedSuggestionProvider;
11
import net.minecraft.commands.arguments.IdentifierArgument;
12
import net.minecraft.network.chat.Component;
13
import net.minecraft.resources.Identifier;
14
import net.minecraft.server.MinecraftServer;
15
import net.minecraft.world.Stopwatch;
16
import net.minecraft.world.Stopwatches;
17
18
public class StopwatchCommand {
19
	private static final DynamicCommandExceptionType ERROR_ALREADY_EXISTS = new DynamicCommandExceptionType(
20
		id -> Component.translatableEscape("commands.stopwatch.already_exists", id)
21
	);
22
	public static final DynamicCommandExceptionType ERROR_DOES_NOT_EXIST = new DynamicCommandExceptionType(
23
		id -> Component.translatableEscape("commands.stopwatch.does_not_exist", id)
24
	);
25
	public static final SuggestionProvider<CommandSourceStack> SUGGEST_STOPWATCHES = (c, p) -> SharedSuggestionProvider.suggestResource(
26
		c.getSource().getServer().getStopwatches().ids(), p
27
	);
28
29
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
30
		dispatcher.register(
31
			Commands.literal("stopwatch")
32
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
33
				.then(
34
					Commands.literal("create")
35
						.then(Commands.argument("id", IdentifierArgument.id()).executes(c -> createStopwatch(c.getSource(), IdentifierArgument.getId(c, "id"))))
36
				)
37
				.then(
38
					Commands.literal("query")
39
						.then(
40
							Commands.argument("id", IdentifierArgument.id())
41
								.suggests(SUGGEST_STOPWATCHES)
42
								.then(
43
									Commands.argument("scale", DoubleArgumentType.doubleArg())
44
										.executes(c -> queryStopwatch(c.getSource(), IdentifierArgument.getId(c, "id"), DoubleArgumentType.getDouble(c, "scale")))
45
								)
46
								.executes(c -> queryStopwatch(c.getSource(), IdentifierArgument.getId(c, "id"), 1.0))
47
						)
48
				)
49
				.then(
50
					Commands.literal("restart")
51
						.then(
52
							Commands.argument("id", IdentifierArgument.id())
53
								.suggests(SUGGEST_STOPWATCHES)
54
								.executes(c -> restartStopwatch(c.getSource(), IdentifierArgument.getId(c, "id")))
55
						)
56
				)
57
				.then(
58
					Commands.literal("remove")
59
						.then(
60
							Commands.argument("id", IdentifierArgument.id())
61
								.suggests(SUGGEST_STOPWATCHES)
62
								.executes(c -> removeStopwatch(c.getSource(), IdentifierArgument.getId(c, "id")))
63
						)
64
				)
65
		);
66
	}
67
68
	private static int createStopwatch(final CommandSourceStack source, final Identifier id) throws CommandSyntaxException {
69
		MinecraftServer server = source.getServer();
70
		Stopwatches stopwatches = server.getStopwatches();
71
		Stopwatch now = new Stopwatch(Stopwatches.currentTime());
72
		if (!stopwatches.add(id, now)) {
73
			throw ERROR_ALREADY_EXISTS.create(id);
74
		}
75
76
		source.sendSuccess(() -> Component.translatable("commands.stopwatch.create.success", Component.translationArg(id)), true);
77
		return 1;
78
	}
79
80
	private static int queryStopwatch(final CommandSourceStack source, final Identifier id, final double scale) throws CommandSyntaxException {
81
		MinecraftServer server = source.getServer();
82
		Stopwatches stopwatches = server.getStopwatches();
83
		Stopwatch stopwatch = stopwatches.get(id);
84
		if (stopwatch == null) {
85
			throw ERROR_DOES_NOT_EXIST.create(id);
86
		}
87
88
		long currentTime = Stopwatches.currentTime();
89
		double elapsedSeconds = stopwatch.elapsedSeconds(currentTime);
90
		source.sendSuccess(() -> Component.translatable("commands.stopwatch.query", Component.translationArg(id), elapsedSeconds), true);
91
		return (int)(elapsedSeconds * scale);
92
	}
93
94
	private static int restartStopwatch(final CommandSourceStack source, final Identifier id) throws CommandSyntaxException {
95
		MinecraftServer server = source.getServer();
96
		Stopwatches stopwatches = server.getStopwatches();
97
		if (!stopwatches.update(id, stopwatch -> new Stopwatch(Stopwatches.currentTime()))) {
98
			throw ERROR_DOES_NOT_EXIST.create(id);
99
		}
100
101
		source.sendSuccess(() -> Component.translatable("commands.stopwatch.restart.success", Component.translationArg(id)), true);
102
		return 1;
103
	}
104
105
	private static int removeStopwatch(final CommandSourceStack source, final Identifier id) throws CommandSyntaxException {
106
		MinecraftServer server = source.getServer();
107
		Stopwatches stopwatches = server.getStopwatches();
108
		if (!stopwatches.remove(id)) {
109
			throw ERROR_DOES_NOT_EXIST.create(id);
110
		}
111
112
		source.sendSuccess(() -> Component.translatable("commands.stopwatch.remove.success", Component.translationArg(id)), true);
113
		return 1;
114
	}
115
}
116

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