PostEffectCommand .java

net.minecraft.server.commands.PostEffectCommand (class): members: ERROR_CLEAR_FAILED, ERROR_REMOVE_FAILED, ERROR_ADD_FAILED, RESPONSE_REMOVE, RESPONSE_ADD.

net.minecraft.server.commandsclass121 linesserver5.8 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)

Fields & Constants (6)

ERROR_ADD_FAILED
ERROR_CLEAR_FAILED
ERROR_REMOVE_FAILED
RESPONSE_ADD
RESPONSE_CLEAR
RESPONSE_REMOVE

String literals (17)

"commands.posteffect.add.failed"
"commands.posteffect.clear.failed"
"commands.posteffect.remove.failed"
"commands.posteffect.add.success.single"
"commands.posteffect.add.success.multiple"
"commands.posteffect.clear.success.single"
"commands.posteffect.clear.success.multiple"
"commands.posteffect.remove.success.single"
"commands.posteffect.remove.success.multiple"
"posteffect"
"targets"
"clear"
"list"
"target"
"remove"
"commands.posteffect.list.empty"
"commands.posteffect.list.success"

Full Source

PostEffectCommand.java121 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.exceptions.CommandSyntaxException;
5
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
6
import java.util.Collection;
7
import java.util.List;
8
import java.util.stream.Collectors;
9
import net.minecraft.commands.CommandSourceStack;
10
import net.minecraft.commands.Commands;
11
import net.minecraft.commands.arguments.EntityArgument;
12
import net.minecraft.commands.arguments.IdentifierArgument;
13
import net.minecraft.commands.synchronization.SuggestionProviders;
14
import net.minecraft.network.chat.Component;
15
import net.minecraft.resources.Identifier;
16
import net.minecraft.server.level.ServerPlayer;
17
18
public class PostEffectCommand {
19
	private static final SimpleCommandExceptionType ERROR_ADD_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.posteffect.add.failed"));
20
	private static final SimpleCommandExceptionType ERROR_CLEAR_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.posteffect.clear.failed"));
21
	private static final SimpleCommandExceptionType ERROR_REMOVE_FAILED = new SimpleCommandExceptionType(
22
		Component.translatable("commands.posteffect.remove.failed")
23
	);
24
	private static final CommandResponseTracker.MessagesWithArg<ServerPlayer, Identifier> RESPONSE_ADD = CommandResponseTracker.messages(
25
		ERROR_ADD_FAILED,
26
		(entity, var1, posteffect) -> Component.translatable("commands.posteffect.add.success.single", Component.translationArg(posteffect), entity.getDisplayName()),
27
		(entityCount, var1, posteffect) -> Component.translatable("commands.posteffect.add.success.multiple", Component.translationArg(posteffect), entityCount)
28
	);
29
	private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_CLEAR = CommandResponseTracker.messages(
30
		ERROR_CLEAR_FAILED,
31
		(entity, var1) -> Component.translatable("commands.posteffect.clear.success.single", entity.getDisplayName()),
32
		(entityCount, var1) -> Component.translatable("commands.posteffect.clear.success.multiple", entityCount)
33
	);
34
	private static final CommandResponseTracker.MessagesWithArg<ServerPlayer, Identifier> RESPONSE_REMOVE = CommandResponseTracker.messages(
35
		ERROR_REMOVE_FAILED,
36
		(entity, var1, posteffect) -> Component.translatable(
37
			"commands.posteffect.remove.success.single", Component.translationArg(posteffect), entity.getDisplayName()
38
		),
39
		(entityCount, var1, posteffect) -> Component.translatable("commands.posteffect.remove.success.multiple", Component.translationArg(posteffect), entityCount)
40
	);
41
42
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
43
		dispatcher.register(
44
			Commands.literal("posteffect")
45
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
46
				.then(
47
					Commands.literal("add")
48
						.then(
49
							Commands.argument("targets", EntityArgument.players())
50
								.then(
51
									Commands.argument("posteffect", IdentifierArgument.id())
52
										.suggests(SuggestionProviders.cast(SuggestionProviders.POST_EFFECTS))
53
										.executes(c -> addPostEffect(c.getSource(), EntityArgument.getPlayers(c, "targets"), IdentifierArgument.getId(c, "posteffect")))
54
								)
55
						)
56
				)
57
				.then(
58
					Commands.literal("clear")
59
						.then(Commands.argument("targets", EntityArgument.players()).executes(c -> clearPostEffect(c.getSource(), EntityArgument.getPlayers(c, "targets"))))
60
				)
61
				.then(
62
					Commands.literal("list")
63
						.then(Commands.argument("target", EntityArgument.player()).executes(c -> listPostEffects(c.getSource(), EntityArgument.getPlayer(c, "target"))))
64
				)
65
				.then(
66
					Commands.literal("remove")
67
						.then(
68
							Commands.argument("targets", EntityArgument.players())
69
								.then(
70
									Commands.argument("posteffect", IdentifierArgument.id())
71
										.suggests(SuggestionProviders.cast(SuggestionProviders.POST_EFFECTS))
72
										.executes(c -> removePostEffect(c.getSource(), EntityArgument.getPlayers(c, "targets"), IdentifierArgument.getId(c, "posteffect")))
73
								)
74
						)
75
				)
76
		);
77
	}
78
79
	private static int addPostEffect(final CommandSourceStack source, final Collection<ServerPlayer> players, final Identifier posteffect) throws CommandSyntaxException {
80
		CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
81
82
		for (ServerPlayer player : players) {
83
			tracker.track(player, player.addPostEffect(posteffect));
84
		}
85
86
		return tracker.sendFeedback(source, true, RESPONSE_ADD, posteffect);
87
	}
88
89
	private static int clearPostEffect(final CommandSourceStack source, final Collection<ServerPlayer> players) throws CommandSyntaxException {
90
		CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
91
92
		for (ServerPlayer player : players) {
93
			tracker.track(player, player.clearPostEffects());
94
		}
95
96
		return tracker.sendFeedback(source, true, RESPONSE_CLEAR);
97
	}
98
99
	private static int listPostEffects(final CommandSourceStack source, final ServerPlayer player) {
100
		List<Identifier> postEffects = player.getPostEffects();
101
		if (postEffects.isEmpty()) {
102
			source.sendSuccess(() -> Component.translatable("commands.posteffect.list.empty", player.getDisplayName()), false);
103
		} else {
104
			String names = postEffects.stream().map(Identifier::toString).collect(Collectors.joining(", "));
105
			source.sendSuccess(() -> Component.translatable("commands.posteffect.list.success", player.getDisplayName(), postEffects.size(), names), false);
106
		}
107
108
		return postEffects.size();
109
	}
110
111
	private static int removePostEffect(final CommandSourceStack source, final Collection<ServerPlayer> players, final Identifier posteffect) throws CommandSyntaxException {
112
		CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
113
114
		for (ServerPlayer player : players) {
115
			tracker.track(player, player.removePostEffect(posteffect));
116
		}
117
118
		return tracker.sendFeedback(source, true, RESPONSE_REMOVE, posteffect);
119
	}
120
}
121

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