EffectCommands .java

net.minecraft.server.commands.EffectCommands (class): members: ERROR_CLEAR_EVERYTHING_FAILED, ERROR_CLEAR_SPECIFIC_FAILED, RESPONSE_CLEAR_SINGLE, seconds.

net.minecraft.server.commandsclass207 linesserver8.8 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context)

Fields & Constants (11)

ERROR_GIVE_FAILED
ERROR_CLEAR_EVERYTHING_FAILED
ERROR_CLEAR_SPECIFIC_FAILED
RESPONSE_GIVE
RESPONSE_CLEAR_ALL
RESPONSE_CLEAR_SINGLE
source
entities
effectHolder
seconds
amplifier

String literals (16)

"commands.effect.give.failed"
"commands.effect.clear.everything.failed"
"commands.effect.clear.specific.failed"
"commands.effect.give.success.single"
"commands.effect.give.success.multiple"
"commands.effect.clear.everything.success.single"
"commands.effect.clear.specific.success.single"
"commands.effect.clear.specific.success.multiple"
"effect"
"clear"
"targets"
"give"
"seconds"
"amplifier"
"hideParticles"
"infinite"

Full Source

EffectCommands.java207 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.google.common.collect.ImmutableList;
4
import com.mojang.brigadier.CommandDispatcher;
5
import com.mojang.brigadier.arguments.BoolArgumentType;
6
import com.mojang.brigadier.arguments.IntegerArgumentType;
7
import com.mojang.brigadier.exceptions.CommandSyntaxException;
8
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
9
import java.util.Collection;
10
import net.minecraft.commands.CommandBuildContext;
11
import net.minecraft.commands.CommandSourceStack;
12
import net.minecraft.commands.Commands;
13
import net.minecraft.commands.arguments.EntityArgument;
14
import net.minecraft.commands.arguments.ResourceArgument;
15
import net.minecraft.core.Holder;
16
import net.minecraft.core.registries.Registries;
17
import net.minecraft.network.chat.Component;
18
import net.minecraft.world.effect.MobEffect;
19
import net.minecraft.world.effect.MobEffectInstance;
20
import net.minecraft.world.entity.Entity;
21
import net.minecraft.world.entity.LivingEntity;
22
import org.jspecify.annotations.Nullable;
23
24
public class EffectCommands {
25
	private static final SimpleCommandExceptionType ERROR_GIVE_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.effect.give.failed"));
26
	private static final SimpleCommandExceptionType ERROR_CLEAR_EVERYTHING_FAILED = new SimpleCommandExceptionType(
27
		Component.translatable("commands.effect.clear.everything.failed")
28
	);
29
	private static final SimpleCommandExceptionType ERROR_CLEAR_SPECIFIC_FAILED = new SimpleCommandExceptionType(
30
		Component.translatable("commands.effect.clear.specific.failed")
31
	);
32
	private static final CommandResponseTracker.MessagesWithArgs<LivingEntity, MobEffect, Integer> RESPONSE_GIVE = CommandResponseTracker.messages(
33
		ERROR_GIVE_FAILED,
34
		(entity, var1, effect, duration) -> Component.translatable(
35
			"commands.effect.give.success.single", effect.getDisplayName(), entity.getDisplayName(), duration / 20
36
		),
37
		(entityCount, var1, effect, duration) -> Component.translatable("commands.effect.give.success.multiple", effect.getDisplayName(), entityCount, duration / 20)
38
	);
39
	private static final CommandResponseTracker.Messages<Entity> RESPONSE_CLEAR_ALL = CommandResponseTracker.messages(
40
		ERROR_CLEAR_EVERYTHING_FAILED,
41
		(entity, var1) -> Component.translatable("commands.effect.clear.everything.success.single", entity.getDisplayName()),
42
		(entityCount, var1) -> Component.translatable("commands.effect.clear.everything.success.multiple", entityCount)
43
	);
44
	private static final CommandResponseTracker.MessagesWithArg<Entity, MobEffect> RESPONSE_CLEAR_SINGLE = CommandResponseTracker.messages(
45
		ERROR_CLEAR_SPECIFIC_FAILED,
46
		(entity, var1, effect) -> Component.translatable("commands.effect.clear.specific.success.single", effect.getDisplayName(), entity.getDisplayName()),
47
		(entityCount, var1, effect) -> Component.translatable("commands.effect.clear.specific.success.multiple", effect.getDisplayName(), entityCount)
48
	);
49
50
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
51
		dispatcher.register(
52
			Commands.literal("effect")
53
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
54
				.then(
55
					Commands.literal("clear")
56
						.executes(c -> clearEffects(c.getSource(), ImmutableList.of(c.getSource().getEntityOrException())))
57
						.then(
58
							Commands.argument("targets", EntityArgument.entities())
59
								.executes(c -> clearEffects(c.getSource(), EntityArgument.getEntities(c, "targets")))
60
								.then(
61
									Commands.argument("effect", ResourceArgument.resource(context, Registries.MOB_EFFECT))
62
										.executes(c -> clearEffect(c.getSource(), EntityArgument.getEntities(c, "targets"), ResourceArgument.getMobEffect(c, "effect")))
63
								)
64
						)
65
				)
66
				.then(
67
					Commands.literal("give")
68
						.then(
69
							Commands.argument("targets", EntityArgument.entities())
70
								.then(
71
									Commands.argument("effect", ResourceArgument.resource(context, Registries.MOB_EFFECT))
72
										.executes(c -> giveEffect(c.getSource(), EntityArgument.getEntities(c, "targets"), ResourceArgument.getMobEffect(c, "effect"), null, 0, true))
73
										.then(
74
											Commands.argument("seconds", IntegerArgumentType.integer(1, 1000000))
75
												.executes(
76
													c -> giveEffect(
77
														c.getSource(),
78
														EntityArgument.getEntities(c, "targets"),
79
														ResourceArgument.getMobEffect(c, "effect"),
80
														IntegerArgumentType.getInteger(c, "seconds"),
81
														0,
82
														true
83
													)
84
												)
85
												.then(
86
													Commands.argument("amplifier", IntegerArgumentType.integer(0, 255))
87
														.executes(
88
															c -> giveEffect(
89
																c.getSource(),
90
																EntityArgument.getEntities(c, "targets"),
91
																ResourceArgument.getMobEffect(c, "effect"),
92
																IntegerArgumentType.getInteger(c, "seconds"),
93
																IntegerArgumentType.getInteger(c, "amplifier"),
94
																true
95
															)
96
														)
97
														.then(
98
															Commands.argument("hideParticles", BoolArgumentType.bool())
99
																.executes(
100
																	c -> giveEffect(
101
																		c.getSource(),
102
																		EntityArgument.getEntities(c, "targets"),
103
																		ResourceArgument.getMobEffect(c, "effect"),
104
																		IntegerArgumentType.getInteger(c, "seconds"),
105
																		IntegerArgumentType.getInteger(c, "amplifier"),
106
																		!BoolArgumentType.getBool(c, "hideParticles")
107
																	)
108
																)
109
														)
110
												)
111
										)
112
										.then(
113
											Commands.literal("infinite")
114
												.executes(c -> giveEffect(c.getSource(), EntityArgument.getEntities(c, "targets"), ResourceArgument.getMobEffect(c, "effect"), -1, 0, true))
115
												.then(
116
													Commands.argument("amplifier", IntegerArgumentType.integer(0, 255))
117
														.executes(
118
															c -> giveEffect(
119
																c.getSource(),
120
																EntityArgument.getEntities(c, "targets"),
121
																ResourceArgument.getMobEffect(c, "effect"),
122
																-1,
123
																IntegerArgumentType.getInteger(c, "amplifier"),
124
																true
125
															)
126
														)
127
														.then(
128
															Commands.argument("hideParticles", BoolArgumentType.bool())
129
																.executes(
130
																	c -> giveEffect(
131
																		c.getSource(),
132
																		EntityArgument.getEntities(c, "targets"),
133
																		ResourceArgument.getMobEffect(c, "effect"),
134
																		-1,
135
																		IntegerArgumentType.getInteger(c, "amplifier"),
136
																		!BoolArgumentType.getBool(c, "hideParticles")
137
																	)
138
																)
139
														)
140
												)
141
										)
142
								)
143
						)
144
				)
145
		);
146
	}
147
148
	private static int giveEffect(
149
		final CommandSourceStack source,
150
		final Collection<? extends Entity> entities,
151
		final Holder<MobEffect> effectHolder,
152
		final @Nullable Integer seconds,
153
		final int amplifier,
154
		final boolean particles
155
	) throws CommandSyntaxException {
156
		MobEffect effect = effectHolder.value();
157
		int duration = computeDurationInTicks(seconds, effect);
158
		CommandResponseTracker<LivingEntity> tracker = CommandResponseTracker.create();
159
160
		for (Entity entity : entities) {
161
			if (entity instanceof LivingEntity livingEntity) {
162
				MobEffectInstance instance = new MobEffectInstance(effectHolder, duration, amplifier, false, particles);
163
				tracker.track(livingEntity, livingEntity.addEffect(instance, source.getEntity()));
164
			}
165
		}
166
167
		return tracker.sendFeedback(source, true, RESPONSE_GIVE, effect, duration);
168
	}
169
170
	private static int computeDurationInTicks(final @Nullable Integer seconds, final MobEffect effect) {
171
		if (seconds != null) {
172
			if (effect.isInstantaneous()) {
173
				return seconds;
174
			} else {
175
				return seconds == -1 ? -1 : seconds * 20;
176
			}
177
		} else {
178
			return effect.isInstantaneous() ? 1 : 600;
179
		}
180
	}
181
182
	private static int clearEffects(final CommandSourceStack source, final Collection<? extends Entity> entities) throws CommandSyntaxException {
183
		CommandResponseTracker<LivingEntity> tracker = CommandResponseTracker.create();
184
185
		for (Entity entity : entities) {
186
			if (entity instanceof LivingEntity livingEntity) {
187
				tracker.track(livingEntity, livingEntity.removeAllEffects());
188
			}
189
		}
190
191
		return tracker.sendFeedback(source, true, RESPONSE_CLEAR_ALL);
192
	}
193
194
	private static int clearEffect(final CommandSourceStack source, final Collection<? extends Entity> entities, final Holder<MobEffect> effectHolder) throws CommandSyntaxException {
195
		MobEffect effect = effectHolder.value();
196
		CommandResponseTracker<LivingEntity> tracker = CommandResponseTracker.create();
197
198
		for (Entity entity : entities) {
199
			if (entity instanceof LivingEntity livingEntity) {
200
				tracker.track(livingEntity, livingEntity.removeEffect(effectHolder));
201
			}
202
		}
203
204
		return tracker.sendFeedback(source, true, RESPONSE_CLEAR_SINGLE, effect);
205
	}
206
}
207

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