| 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.builder.ArgumentBuilder;
|
| 7 | import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
| 8 | import com.mojang.brigadier.context.CommandContext;
|
| 9 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 10 | import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
|
| 11 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 12 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 13 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 14 | import java.util.concurrent.CompletableFuture;
|
| 15 | import java.util.stream.Stream;
|
| 16 | import net.minecraft.commands.CommandBuildContext;
|
| 17 | import net.minecraft.commands.CommandSourceStack;
|
| 18 | import net.minecraft.commands.Commands;
|
| 19 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 20 | import net.minecraft.commands.arguments.IdentifierArgument;
|
| 21 | import net.minecraft.commands.arguments.ResourceArgument;
|
| 22 | import net.minecraft.commands.arguments.TimeArgument;
|
| 23 | import net.minecraft.core.Holder;
|
| 24 | import net.minecraft.core.registries.Registries;
|
| 25 | import net.minecraft.network.chat.Component;
|
| 26 | import net.minecraft.resources.ResourceKey;
|
| 27 | import net.minecraft.world.clock.ClockTimeMarker;
|
| 28 | import net.minecraft.world.clock.ClockTimeMarkers;
|
| 29 | import net.minecraft.world.clock.ServerClockManager;
|
| 30 | import net.minecraft.world.clock.WorldClock;
|
| 31 | import net.minecraft.world.level.dimension.DimensionType;
|
| 32 | import net.minecraft.world.timeline.Timeline;
|
| 33 |
|
| 34 | public class TimeCommand {
|
| 35 | private static final DynamicCommandExceptionType ERROR_NO_DEFAULT_CLOCK = new DynamicCommandExceptionType(
|
| 36 | dimension -> Component.translatableEscape("commands.time.no_default_clock", dimension)
|
| 37 | );
|
| 38 | private static final Dynamic2CommandExceptionType ERROR_NO_TIME_MARKER_FOUND = new Dynamic2CommandExceptionType(
|
| 39 | (clock, timeMarker) -> Component.translatableEscape("commands.time.no_time_marker_found", timeMarker, clock)
|
| 40 | );
|
| 41 | private static final Dynamic2CommandExceptionType ERROR_WRONG_TIMELINE_FOR_CLOCK = new Dynamic2CommandExceptionType(
|
| 42 | (clock, timeline) -> Component.translatableEscape("commands.time.wrong_timeline_for_clock", timeline, clock)
|
| 43 | );
|
| 44 | private static final Dynamic2CommandExceptionType ERROR_ALREADY_AT_TIME_MARKER = new Dynamic2CommandExceptionType(
|
| 45 | (clock, marker) -> Component.translatableEscape("commands.time.set.already_at_time_marker", clock, marker)
|
| 46 | );
|
| 47 | private static final Dynamic2CommandExceptionType ERROR_ALREADY_AT_TIME = new Dynamic2CommandExceptionType(
|
| 48 | (clock, time) -> Component.translatableEscape("commands.time.set.already_at_time", clock, time)
|
| 49 | );
|
| 50 | private static final DynamicCommandExceptionType ERROR_ALREADY_PAUSED = new DynamicCommandExceptionType(
|
| 51 | clock -> Component.translatableEscape("commands.time.pause.already_paused", clock)
|
| 52 | );
|
| 53 | private static final DynamicCommandExceptionType ERROR_ALREADY_RUNNING = new DynamicCommandExceptionType(
|
| 54 | clock -> Component.translatableEscape("commands.time.pause.already_running", clock)
|
| 55 | );
|
| 56 | private static final Dynamic2CommandExceptionType ERROR_ALREADY_SAME_RATE = new Dynamic2CommandExceptionType(
|
| 57 | (clock, rate) -> Component.translatableEscape("commands.time.rate.already_same", clock, rate)
|
| 58 | );
|
| 59 | private static final int MAX_CLOCK_RATE = 1000;
|
| 60 |
|
| 61 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 62 | LiteralArgumentBuilder<CommandSourceStack> baseCommand = Commands.literal("time").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS));
|
| 63 | dispatcher.register(addClockNodes(context, baseCommand, c -> getDefaultClock(c.getSource())));
|
| 64 | dispatcher.register(
|
| 65 | baseCommand.then(Commands.literal("query").then(Commands.literal("gametime").executes(c -> queryGameTime(c.getSource()))))
|
| 66 | .then(
|
| 67 | Commands.literal("of")
|
| 68 | .then(
|
| 69 | addClockNodes(
|
| 70 | context, Commands.argument("clock", ResourceArgument.resource(context, Registries.WORLD_CLOCK)), c -> ResourceArgument.getClock(c, "clock")
|
| 71 | )
|
| 72 | )
|
| 73 | )
|
| 74 | );
|
| 75 | }
|
| 76 |
|
| 77 | private static <A extends ArgumentBuilder<CommandSourceStack, A>> A addClockNodes(
|
| 78 | final CommandBuildContext context, final A node, final TimeCommand.ClockGetter clockGetter
|
| 79 | ) {
|
| 80 | return node.then(
|
| 81 | Commands.literal("set")
|
| 82 | .then(
|
| 83 | Commands.argument("time", TimeArgument.time())
|
| 84 | .executes(c -> setTotalTicks(c.getSource(), clockGetter.getClock(c), IntegerArgumentType.getInteger(c, "time")))
|
| 85 | )
|
| 86 | .then(
|
| 87 | Commands.argument("timemarker", IdentifierArgument.id())
|
| 88 | .suggests((c, p) -> suggestTimeMarkers(c.getSource(), p, clockGetter.getClock(c)))
|
| 89 | .executes(
|
| 90 | c -> setTimeToTimeMarker(
|
| 91 | c.getSource(), clockGetter.getClock(c), ResourceKey.create(ClockTimeMarkers.ROOT_ID, IdentifierArgument.getId(c, "timemarker"))
|
| 92 | )
|
| 93 | )
|
| 94 | )
|
| 95 | )
|
| 96 | .then(
|
| 97 | Commands.literal("add")
|
| 98 | .then(
|
| 99 | Commands.argument("time", TimeArgument.time(Integer.MIN_VALUE))
|
| 100 | .executes(c -> addTime(c.getSource(), clockGetter.getClock(c), IntegerArgumentType.getInteger(c, "time")))
|
| 101 | )
|
| 102 | )
|
| 103 | .then(Commands.literal("pause").executes(c -> setPaused(c.getSource(), clockGetter.getClock(c), true)))
|
| 104 | .then(Commands.literal("resume").executes(c -> setPaused(c.getSource(), clockGetter.getClock(c), false)))
|
| 105 | .then(
|
| 106 | Commands.literal("rate")
|
| 107 | .then(
|
| 108 | Commands.argument("rate", FloatArgumentType.floatArg(1.0E-5F, 1000.0F))
|
| 109 | .executes(c -> setRate(c.getSource(), clockGetter.getClock(c), FloatArgumentType.getFloat(c, "rate")))
|
| 110 | )
|
| 111 | )
|
| 112 | .then(
|
| 113 | Commands.literal("query")
|
| 114 | .then(Commands.literal("time").executes(c -> queryTime(c.getSource(), clockGetter.getClock(c))))
|
| 115 | .then(
|
| 116 | Commands.argument("timeline", ResourceArgument.resource(context, Registries.TIMELINE))
|
| 117 | .suggests((c, p) -> suggestTimelines(c.getSource(), p, clockGetter.getClock(c)))
|
| 118 | .executes(c -> queryTimelineTicks(c.getSource(), clockGetter.getClock(c), ResourceArgument.getTimeline(c, "timeline")))
|
| 119 | .then(
|
| 120 | Commands.literal("repetition")
|
| 121 | .executes(c -> queryTimelineRepetitions(c.getSource(), clockGetter.getClock(c), ResourceArgument.getTimeline(c, "timeline")))
|
| 122 | )
|
| 123 | )
|
| 124 | );
|
| 125 | }
|
| 126 |
|
| 127 | private static CompletableFuture<Suggestions> suggestTimeMarkers(
|
| 128 | final CommandSourceStack source, final SuggestionsBuilder builder, final Holder<WorldClock> clock
|
| 129 | ) {
|
| 130 | return SharedSuggestionProvider.suggestResource(source.getServer().clockManager().commandTimeMarkersForClock(clock).map(ResourceKey::identifier), builder);
|
| 131 | }
|
| 132 |
|
| 133 | private static CompletableFuture<Suggestions> suggestTimelines(
|
| 134 | final CommandSourceStack source, final SuggestionsBuilder builder, final Holder<WorldClock> clock
|
| 135 | ) {
|
| 136 | Stream<ResourceKey<Timeline>> timelines = source.registryAccess()
|
| 137 | .lookupOrThrow(Registries.TIMELINE)
|
| 138 | .listElements()
|
| 139 | .filter(timeline -> timeline.value().clock().equals(clock))
|
| 140 | .map(Holder.Reference::key);
|
| 141 | return SharedSuggestionProvider.suggestResource(timelines.map(ResourceKey::identifier), builder);
|
| 142 | }
|
| 143 |
|
| 144 | private static int queryGameTime(final CommandSourceStack source) {
|
| 145 | long gameTime = source.getLevel().getGameTime();
|
| 146 | source.sendSuccess(() -> Component.translatable("commands.time.query.gametime", gameTime), false);
|
| 147 | return wrapTime(gameTime);
|
| 148 | }
|
| 149 |
|
| 150 | private static int queryTime(final CommandSourceStack source, final Holder<WorldClock> clock) {
|
| 151 | ServerClockManager clockManager = source.getServer().clockManager();
|
| 152 | long totalTicks = clockManager.getInstance(clock).totalTicks();
|
| 153 | source.sendSuccess(() -> Component.translatable("commands.time.query.absolute", clock.getRegisteredName(), totalTicks), false);
|
| 154 | return wrapTime(totalTicks);
|
| 155 | }
|
| 156 |
|
| 157 | private static int queryTimelineTicks(final CommandSourceStack source, final Holder<WorldClock> clock, final Holder<Timeline> timeline) throws CommandSyntaxException {
|
| 158 | if (!clock.equals(timeline.value().clock())) {
|
| 159 | throw ERROR_WRONG_TIMELINE_FOR_CLOCK.create(clock.getRegisteredName(), timeline.getRegisteredName());
|
| 160 | }
|
| 161 |
|
| 162 | ServerClockManager clockManager = source.getServer().clockManager();
|
| 163 | long currentTicks = timeline.value().getCurrentTicks(clockManager);
|
| 164 | source.sendSuccess(() -> Component.translatable("commands.time.query.timeline", timeline.getRegisteredName(), currentTicks), false);
|
| 165 | return wrapTime(currentTicks);
|
| 166 | }
|
| 167 |
|
| 168 | private static int queryTimelineRepetitions(final CommandSourceStack source, final Holder<WorldClock> clock, final Holder<Timeline> timeline) throws CommandSyntaxException {
|
| 169 | if (!clock.equals(timeline.value().clock())) {
|
| 170 | throw ERROR_WRONG_TIMELINE_FOR_CLOCK.create(clock.getRegisteredName(), timeline.getRegisteredName());
|
| 171 | }
|
| 172 |
|
| 173 | ServerClockManager clockManager = source.getServer().clockManager();
|
| 174 | long repetitions = timeline.value().getPeriodCount(clockManager);
|
| 175 | source.sendSuccess(() -> Component.translatable("commands.time.query.timeline.repetitions", timeline.getRegisteredName(), repetitions), false);
|
| 176 | return wrapTime(repetitions);
|
| 177 | }
|
| 178 |
|
| 179 | private static int setTotalTicks(final CommandSourceStack source, final Holder<WorldClock> clock, final int totalTicks) throws CommandSyntaxException {
|
| 180 | ServerClockManager clockManager = source.getServer().clockManager();
|
| 181 | if (clockManager.getInstance(clock).totalTicks() == totalTicks) {
|
| 182 | throw ERROR_ALREADY_AT_TIME.create(clock.getRegisteredName(), totalTicks);
|
| 183 | }
|
| 184 |
|
| 185 | clockManager.setTotalTicks(clock, totalTicks);
|
| 186 | source.sendSuccess(() -> Component.translatable("commands.time.set.absolute", clock.getRegisteredName(), totalTicks), true);
|
| 187 | return totalTicks;
|
| 188 | }
|
| 189 |
|
| 190 | private static int addTime(final CommandSourceStack source, final Holder<WorldClock> clock, final int time) {
|
| 191 | ServerClockManager clockManager = source.getServer().clockManager();
|
| 192 | clockManager.addTicks(clock, time);
|
| 193 | long totalTicks = clockManager.getInstance(clock).totalTicks();
|
| 194 | source.sendSuccess(() -> Component.translatable("commands.time.set.absolute", clock.getRegisteredName(), totalTicks), true);
|
| 195 | return wrapTime(totalTicks);
|
| 196 | }
|
| 197 |
|
| 198 | private static int setTimeToTimeMarker(final CommandSourceStack source, final Holder<WorldClock> clock, final ResourceKey<ClockTimeMarker> timeMarkerId) throws CommandSyntaxException {
|
| 199 | ServerClockManager clockManager = source.getServer().clockManager();
|
| 200 | ServerClockManager.MoveResult moveResult = clockManager.moveToTimeMarker(clock, timeMarkerId);
|
| 201 | String clockName = clock.getRegisteredName();
|
| 202 | String timeMarkerName = timeMarkerId.identifier().toString();
|
| 203 | switch (moveResult) {
|
| 204 | case NO_TIME_MARKER_FOUND:
|
| 205 | throw ERROR_NO_TIME_MARKER_FOUND.create(clockName, timeMarkerName);
|
| 206 | case NOT_MOVED:
|
| 207 | throw ERROR_ALREADY_AT_TIME_MARKER.create(clockName, timeMarkerName);
|
| 208 | case MOVED:
|
| 209 | source.sendSuccess(() -> Component.translatable("commands.time.set.time_marker", clockName, timeMarkerName), true);
|
| 210 | default:
|
| 211 | return wrapTime(clockManager.getInstance(clock).totalTicks());
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | private static int setPaused(final CommandSourceStack source, final Holder<WorldClock> clock, final boolean paused) throws CommandSyntaxException {
|
| 216 | ServerClockManager clockManager = source.getServer().clockManager();
|
| 217 | String clockName = clock.getRegisteredName();
|
| 218 | if (clockManager.getInstance(clock).isPaused() == paused) {
|
| 219 | if (paused) {
|
| 220 | throw ERROR_ALREADY_PAUSED.create(clockName);
|
| 221 | } else {
|
| 222 | throw ERROR_ALREADY_RUNNING.create(clockName);
|
| 223 | }
|
| 224 | } else {
|
| 225 | clockManager.setPaused(clock, paused);
|
| 226 | source.sendSuccess(() -> Component.translatable(paused ? "commands.time.pause" : "commands.time.resume", clockName), true);
|
| 227 | return 1;
|
| 228 | }
|
| 229 | }
|
| 230 |
|
| 231 | private static int setRate(final CommandSourceStack source, final Holder<WorldClock> clock, final float rate) throws CommandSyntaxException {
|
| 232 | ServerClockManager clockManager = source.getServer().clockManager();
|
| 233 | if (clockManager.getInstance(clock).rate() == rate) {
|
| 234 | throw ERROR_ALREADY_SAME_RATE.create(clock.getRegisteredName(), rate);
|
| 235 | }
|
| 236 |
|
| 237 | clockManager.setRate(clock, rate);
|
| 238 | source.sendSuccess(() -> Component.translatable("commands.time.rate", clock.getRegisteredName(), rate), true);
|
| 239 | return 1;
|
| 240 | }
|
| 241 |
|
| 242 | private static int wrapTime(final long ticks) {
|
| 243 | return Math.toIntExact(ticks % 2147483647L);
|
| 244 | }
|
| 245 |
|
| 246 | private static Holder<WorldClock> getDefaultClock(final CommandSourceStack source) throws CommandSyntaxException {
|
| 247 | Holder<DimensionType> dimensionType = source.getLevel().dimensionTypeRegistration();
|
| 248 | return dimensionType.value().defaultClock().orElseThrow(() -> ERROR_NO_DEFAULT_CLOCK.create(dimensionType.getRegisteredName()));
|
| 249 | }
|
| 250 |
|
| 251 | private interface ClockGetter {
|
| 252 | Holder<WorldClock> getClock(CommandContext<CommandSourceStack> context) throws CommandSyntaxException;
|
| 253 | }
|
| 254 | }
|
| 255 |
|