ScheduleCommand .java

net.minecraft.server.commands.ScheduleCommand (class): members: ERROR_CANT_REMOVE, SUGGEST_SCHEDULE, ERROR_SAME_TICK, ERROR_MACRO, time, callback, source.

net.minecraft.server.commandsclass122 linesserver5.1 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)

Fields & Constants (7)

ERROR_SAME_TICK
ERROR_CANT_REMOVE
ERROR_MACRO
SUGGEST_SCHEDULE
source
callback
time

String literals (12)

"commands.schedule.same_tick"
"commands.schedule.cleared.failure"
"commands.schedule.macro"
"schedule"
"function"
"time"
"append"
"replace"
"clear"
"commands.schedule.created.function"
"commands.schedule.created.tag"
"commands.schedule.cleared.success"

Full Source

ScheduleCommand.java122 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.arguments.IntegerArgumentType;
5
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
7
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
8
import com.mojang.brigadier.suggestion.SuggestionProvider;
9
import com.mojang.datafixers.util.Either;
10
import com.mojang.datafixers.util.Pair;
11
import java.util.Collection;
12
import java.util.Optional;
13
import net.minecraft.commands.CommandSourceStack;
14
import net.minecraft.commands.Commands;
15
import net.minecraft.commands.SharedSuggestionProvider;
16
import net.minecraft.commands.arguments.IdentifierArgument;
17
import net.minecraft.commands.arguments.TimeArgument;
18
import net.minecraft.commands.arguments.item.FunctionArgument;
19
import net.minecraft.commands.functions.CommandFunction;
20
import net.minecraft.commands.functions.MacroFunction;
21
import net.minecraft.network.chat.Component;
22
import net.minecraft.resources.Identifier;
23
import net.minecraft.server.MinecraftServer;
24
import net.minecraft.world.level.timers.FunctionCallback;
25
import net.minecraft.world.level.timers.FunctionTagCallback;
26
import net.minecraft.world.level.timers.TimerQueue;
27
28
public class ScheduleCommand {
29
	private static final SimpleCommandExceptionType ERROR_SAME_TICK = new SimpleCommandExceptionType(Component.translatable("commands.schedule.same_tick"));
30
	private static final DynamicCommandExceptionType ERROR_CANT_REMOVE = new DynamicCommandExceptionType(
31
		s -> Component.translatableEscape("commands.schedule.cleared.failure", s)
32
	);
33
	private static final SimpleCommandExceptionType ERROR_MACRO = new SimpleCommandExceptionType(Component.translatableEscape("commands.schedule.macro"));
34
	private static final SuggestionProvider<CommandSourceStack> SUGGEST_SCHEDULE = (c, p) -> SharedSuggestionProvider.suggestResource(
35
		c.getSource().getServer().getScheduledEvents().getEventsIds().stream().map(Identifier::parse), p
36
	);
37
38
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
39
		dispatcher.register(
40
			Commands.literal("schedule")
41
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
42
				.then(
43
					Commands.literal("function")
44
						.then(
45
							Commands.argument("function", FunctionArgument.functions())
46
								.suggests(FunctionCommand.SUGGEST_FUNCTION)
47
								.then(
48
									Commands.argument("time", TimeArgument.time())
49
										.executes(c -> schedule(c.getSource(), FunctionArgument.getFunctionOrTag(c, "function"), IntegerArgumentType.getInteger(c, "time"), true))
50
										.then(
51
											Commands.literal("append")
52
												.executes(c -> schedule(c.getSource(), FunctionArgument.getFunctionOrTag(c, "function"), IntegerArgumentType.getInteger(c, "time"), false))
53
										)
54
										.then(
55
											Commands.literal("replace")
56
												.executes(c -> schedule(c.getSource(), FunctionArgument.getFunctionOrTag(c, "function"), IntegerArgumentType.getInteger(c, "time"), true))
57
										)
58
								)
59
						)
60
				)
61
				.then(
62
					Commands.literal("clear")
63
						.then(
64
							Commands.argument("function", IdentifierArgument.id())
65
								.suggests(SUGGEST_SCHEDULE)
66
								.executes(c -> remove(c.getSource(), IdentifierArgument.getId(c, "function")))
67
						)
68
				)
69
		);
70
	}
71
72
	private static int schedule(
73
		final CommandSourceStack source,
74
		final Pair<Identifier, Either<CommandFunction<CommandSourceStack>, Collection<CommandFunction<CommandSourceStack>>>> callback,
75
		final int time,
76
		final boolean replace
77
	) throws CommandSyntaxException {
78
		if (time == 0) {
79
			throw ERROR_SAME_TICK.create();
80
		}
81
82
		long tickTime = source.getLevel().getGameTime() + time;
83
		Identifier callbackId = callback.getFirst();
84
		TimerQueue<MinecraftServer> queue = source.getServer().getScheduledEvents();
85
		Optional<CommandFunction<CommandSourceStack>> function = callback.getSecond().left();
86
		if (function.isPresent()) {
87
			if (function.get() instanceof MacroFunction) {
88
				throw ERROR_MACRO.create();
89
			}
90
91
			String scheduleId = callbackId.toString();
92
			if (replace) {
93
				queue.remove(scheduleId);
94
			}
95
96
			queue.schedule(scheduleId, tickTime, new FunctionCallback(callbackId));
97
			source.sendSuccess(() -> Component.translatable("commands.schedule.created.function", Component.translationArg(callbackId), time, tickTime), true);
98
		} else {
99
			String scheduleId = "#" + callbackId;
100
			if (replace) {
101
				queue.remove(scheduleId);
102
			}
103
104
			queue.schedule(scheduleId, tickTime, new FunctionTagCallback(callbackId));
105
			source.sendSuccess(() -> Component.translatable("commands.schedule.created.tag", Component.translationArg(callbackId), time, tickTime), true);
106
		}
107
108
		return Math.floorMod(tickTime, Integer.MAX_VALUE);
109
	}
110
111
	private static int remove(final CommandSourceStack source, final Identifier id) throws CommandSyntaxException {
112
		String stringId = id.toString();
113
		int count = source.getServer().getScheduledEvents().remove(stringId);
114
		if (count == 0) {
115
			throw ERROR_CANT_REMOVE.create(stringId);
116
		}
117
118
		source.sendSuccess(() -> Component.translatable("commands.schedule.cleared.success", count, stringId), true);
119
		return count;
120
	}
121
}
122

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