CommandSourceStack .java

net.minecraft.commands.CommandSourceStack (class, 628 lines): members: getChatMessageChainer, withMaximumPermission, getEntityOrException, signingContext.

net.minecraft.commandsclass628 linescommon17.4 KB

Implements

API Surface (49)

CommandSourceStack withSource(final CommandSource source)
CommandSourceStack withEntity(final Entity entity)
CommandSourceStack withPosition(final Vec3 pos)
CommandSourceStack withRotation(final Vec2 rotation)
CommandSourceStack withCallback(final CommandResultCallback resultCallback)
CommandSourceStack withCallback(final CommandResultCallback newCallback, final BinaryOperator<CommandResultCallback> combiner)
CommandSourceStack withSuppressedOutput()
CommandSourceStack withPermission(final PermissionSet permissions)
CommandSourceStack withMaximumPermission(final PermissionSet newPermissions)
CommandSourceStack withAnchor(final EntityAnchorArgument.Anchor anchor)
CommandSourceStack withLevel(final ServerLevel level)
CommandSourceStack facing(final Entity entity, final EntityAnchorArgument.Anchor anchor)
CommandSourceStack facing(final Vec3 pos)
CommandSourceStack withSigningContext(final CommandSigningContext signingContext, final TaskChainer chatMessageChainer)
Component getDisplayName()
String getTextName()
PermissionSet permissions()
Vec3 getPosition()
ServerLevel getLevel()
Entity getEntityOrException()
ServerPlayer getPlayerOrException()
boolean isPlayer()
Vec2 getRotation()
MinecraftServer getServer()
EntityAnchorArgument.Anchor getAnchor()
CommandSigningContext getSigningContext()
TaskChainer getChatMessageChainer()
boolean shouldFilterMessageTo(final ServerPlayer receiver)
void sendChatMessage(final OutgoingChatMessage message, final boolean filtered, final ChatType.Bound chatType)
void sendSystemMessage(final Component message)
void sendSuccess(final Supplier<Component> messageSupplier, final boolean broadcast)
void sendFailure(final Component message)
CommandResultCallback callback()
Collection<String> getOnlinePlayerNames()
Collection<String> getAllTeams()
Stream<Identifier> getAvailableSounds()
Stream<Identifier> getAvailablePostEffects()
CompletableFuture<Suggestions> customSuggestion(final CommandContext<?> context)
<E> CompletableFuture<Suggestions> suggestRegistryElements(final ResourceKey<? extends Registry<E>> key, final SharedSuggestionProvider.ElementSuggestionType elements, final SuggestionsBuilder builder, final CommandContext<?> context, final Predicate<E> filter)
Set<ResourceKey<Level>> levels()
RegistryAccess registryAccess()
FeatureFlagSet enabledFeatures()
CommandDispatcher<CommandSourceStack> dispatcher()
void handleError(final CommandExceptionType type, final Message message, final boolean forked, final @Nullable TraceCallbacks tracer)
boolean isSilent()
Component displayName(final @Nullable Entity entity)
String textName(final @Nullable Entity entity)
Component displayName(final @Nullable Entity entity)
String textName(final @Nullable Entity entity)

Fields & Constants (21)

ERROR_NOT_PLAYER
ERROR_NOT_ENTITY
source
worldPosition
level
permissions
namesProvider
server
silent
entity
resultCallback
anchor
rotation
signingContext
chatMessageChainer
position
name
key
elements
builder
context

String literals (3)

"permissions.requires.player"
"permissions.requires.entity"
"chat.type.admin"

Full Source

CommandSourceStack.java628 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.commands;
2
3
import com.google.common.collect.Lists;
4
import com.mojang.brigadier.CommandDispatcher;
5
import com.mojang.brigadier.Message;
6
import com.mojang.brigadier.context.CommandContext;
7
import com.mojang.brigadier.exceptions.CommandExceptionType;
8
import com.mojang.brigadier.exceptions.CommandSyntaxException;
9
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
10
import com.mojang.brigadier.suggestion.Suggestions;
11
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
12
import java.util.Collection;
13
import java.util.Objects;
14
import java.util.Optional;
15
import java.util.Set;
16
import java.util.concurrent.CompletableFuture;
17
import java.util.function.BinaryOperator;
18
import java.util.function.Predicate;
19
import java.util.function.Supplier;
20
import java.util.stream.Stream;
21
import net.fabricmc.fabric.api.permission.v1.PermissionContextOwner;
22
import net.minecraft.ChatFormatting;
23
import net.minecraft.commands.arguments.EntityAnchorArgument;
24
import net.minecraft.commands.execution.TraceCallbacks;
25
import net.minecraft.core.HolderLookup;
26
import net.minecraft.core.Registry;
27
import net.minecraft.core.RegistryAccess;
28
import net.minecraft.core.registries.BuiltInRegistries;
29
import net.minecraft.network.chat.ChatType;
30
import net.minecraft.network.chat.CommonComponents;
31
import net.minecraft.network.chat.Component;
32
import net.minecraft.network.chat.ComponentUtils;
33
import net.minecraft.network.chat.OutgoingChatMessage;
34
import net.minecraft.resources.Identifier;
35
import net.minecraft.resources.ResourceKey;
36
import net.minecraft.server.MinecraftServer;
37
import net.minecraft.server.level.ServerLevel;
38
import net.minecraft.server.level.ServerPlayer;
39
import net.minecraft.server.permissions.PermissionSet;
40
import net.minecraft.sounds.SoundEvent;
41
import net.minecraft.util.Mth;
42
import net.minecraft.util.TaskChainer;
43
import net.minecraft.world.entity.Entity;
44
import net.minecraft.world.flag.FeatureFlagSet;
45
import net.minecraft.world.level.Level;
46
import net.minecraft.world.level.dimension.DimensionType;
47
import net.minecraft.world.level.gamerules.GameRules;
48
import net.minecraft.world.phys.Vec2;
49
import net.minecraft.world.phys.Vec3;
50
import org.jspecify.annotations.Nullable;
51
52
public class CommandSourceStack implements SharedSuggestionProvider, ExecutionCommandSource<CommandSourceStack>, PermissionContextOwner {
53
	public static final SimpleCommandExceptionType ERROR_NOT_PLAYER = new SimpleCommandExceptionType(Component.translatable("permissions.requires.player"));
54
	public static final SimpleCommandExceptionType ERROR_NOT_ENTITY = new SimpleCommandExceptionType(Component.translatable("permissions.requires.entity"));
55
	private final CommandSource source;
56
	private final Vec3 worldPosition;
57
	private final ServerLevel level;
58
	private final PermissionSet permissions;
59
	private final CommandSourceStack.NamesProvider namesProvider;
60
	private final MinecraftServer server;
61
	private final boolean silent;
62
	private final @Nullable Entity entity;
63
	private final CommandResultCallback resultCallback;
64
	private final EntityAnchorArgument.Anchor anchor;
65
	private final Vec2 rotation;
66
	private final CommandSigningContext signingContext;
67
	private final TaskChainer chatMessageChainer;
68
69
	public CommandSourceStack(
70
		final CommandSource source,
71
		final Vec3 position,
72
		final Vec2 rotation,
73
		final ServerLevel level,
74
		final PermissionSet permissions,
75
		final MinecraftServer server,
76
		final Entity entity
77
	) {
78
		this(source, position, rotation, level, permissions, CommandSourceStack.NamesProvider.FOR_ENTITY, server, entity);
79
	}
80
81
	public CommandSourceStack(
82
		final CommandSource source,
83
		final Vec3 position,
84
		final Vec2 rotation,
85
		final ServerLevel level,
86
		final PermissionSet permissions,
87
		final Component name,
88
		final MinecraftServer server
89
	) {
90
		this(source, position, rotation, level, permissions, CommandSourceStack.NamesProvider.constant(name), server, null);
91
	}
92
93
	private CommandSourceStack(
94
		final CommandSource source,
95
		final Vec3 position,
96
		final Vec2 rotation,
97
		final ServerLevel level,
98
		final PermissionSet permissions,
99
		final CommandSourceStack.NamesProvider namesProvider,
100
		final MinecraftServer server,
101
		final @Nullable Entity entity
102
	) {
103
		this(
104
			source,
105
			position,
106
			rotation,
107
			level,
108
			permissions,
109
			namesProvider,
110
			server,
111
			entity,
112
			false,
113
			CommandResultCallback.EMPTY,
114
			EntityAnchorArgument.Anchor.FEET,
115
			CommandSigningContext.ANONYMOUS,
116
			TaskChainer.immediate(server)
117
		);
118
	}
119
120
	private CommandSourceStack(
121
		final CommandSource source,
122
		final Vec3 position,
123
		final Vec2 rotation,
124
		final ServerLevel level,
125
		final PermissionSet permissions,
126
		final CommandSourceStack.NamesProvider namesProvider,
127
		final MinecraftServer server,
128
		final @Nullable Entity entity,
129
		final boolean silent,
130
		final CommandResultCallback resultCallback,
131
		final EntityAnchorArgument.Anchor anchor,
132
		final CommandSigningContext signingContext,
133
		final TaskChainer chatMessageChainer
134
	) {
135
		this.source = source;
136
		this.worldPosition = position;
137
		this.level = level;
138
		this.silent = silent;
139
		this.entity = entity;
140
		this.permissions = permissions;
141
		this.namesProvider = namesProvider;
142
		this.server = server;
143
		this.resultCallback = resultCallback;
144
		this.anchor = anchor;
145
		this.rotation = rotation;
146
		this.signingContext = signingContext;
147
		this.chatMessageChainer = chatMessageChainer;
148
	}
149
150
	public CommandSourceStack withSource(final CommandSource source) {
151
		return this.source == source
152
			? this
153
			: new CommandSourceStack(
154
				source,
155
				this.worldPosition,
156
				this.rotation,
157
				this.level,
158
				this.permissions,
159
				this.namesProvider,
160
				this.server,
161
				this.entity,
162
				this.silent,
163
				this.resultCallback,
164
				this.anchor,
165
				this.signingContext,
166
				this.chatMessageChainer
167
			);
168
	}
169
170
	public CommandSourceStack withEntity(final Entity entity) {
171
		return this.entity == entity
172
			? this
173
			: new CommandSourceStack(
174
				this.source,
175
				this.worldPosition,
176
				this.rotation,
177
				this.level,
178
				this.permissions,
179
				CommandSourceStack.NamesProvider.FOR_ENTITY,
180
				this.server,
181
				entity,
182
				this.silent,
183
				this.resultCallback,
184
				this.anchor,
185
				this.signingContext,
186
				this.chatMessageChainer
187
			);
188
	}
189
190
	public CommandSourceStack withPosition(final Vec3 pos) {
191
		return this.worldPosition.equals(pos)
192
			? this
193
			: new CommandSourceStack(
194
				this.source,
195
				pos,
196
				this.rotation,
197
				this.level,
198
				this.permissions,
199
				this.namesProvider,
200
				this.server,
201
				this.entity,
202
				this.silent,
203
				this.resultCallback,
204
				this.anchor,
205
				this.signingContext,
206
				this.chatMessageChainer
207
			);
208
	}
209
210
	public CommandSourceStack withRotation(final Vec2 rotation) {
211
		return this.rotation.equals(rotation)
212
			? this
213
			: new CommandSourceStack(
214
				this.source,
215
				this.worldPosition,
216
				rotation,
217
				this.level,
218
				this.permissions,
219
				this.namesProvider,
220
				this.server,
221
				this.entity,
222
				this.silent,
223
				this.resultCallback,
224
				this.anchor,
225
				this.signingContext,
226
				this.chatMessageChainer
227
			);
228
	}
229
230
	public CommandSourceStack withCallback(final CommandResultCallback resultCallback) {
231
		return Objects.equals(this.resultCallback, resultCallback)
232
			? this
233
			: new CommandSourceStack(
234
				this.source,
235
				this.worldPosition,
236
				this.rotation,
237
				this.level,
238
				this.permissions,
239
				this.namesProvider,
240
				this.server,
241
				this.entity,
242
				this.silent,
243
				resultCallback,
244
				this.anchor,
245
				this.signingContext,
246
				this.chatMessageChainer
247
			);
248
	}
249
250
	public CommandSourceStack withCallback(final CommandResultCallback newCallback, final BinaryOperator<CommandResultCallback> combiner) {
251
		CommandResultCallback newCompositeCallback = combiner.apply(this.resultCallback, newCallback);
252
		return this.withCallback(newCompositeCallback);
253
	}
254
255
	public CommandSourceStack withSuppressedOutput() {
256
		return !this.silent && !this.source.alwaysAccepts()
257
			? new CommandSourceStack(
258
				this.source,
259
				this.worldPosition,
260
				this.rotation,
261
				this.level,
262
				this.permissions,
263
				this.namesProvider,
264
				this.server,
265
				this.entity,
266
				true,
267
				this.resultCallback,
268
				this.anchor,
269
				this.signingContext,
270
				this.chatMessageChainer
271
			)
272
			: this;
273
	}
274
275
	public CommandSourceStack withPermission(final PermissionSet permissions) {
276
		return permissions == this.permissions
277
			? this
278
			: new CommandSourceStack(
279
				this.source,
280
				this.worldPosition,
281
				this.rotation,
282
				this.level,
283
				permissions,
284
				this.namesProvider,
285
				this.server,
286
				this.entity,
287
				this.silent,
288
				this.resultCallback,
289
				this.anchor,
290
				this.signingContext,
291
				this.chatMessageChainer
292
			);
293
	}
294
295
	public CommandSourceStack withMaximumPermission(final PermissionSet newPermissions) {
296
		return this.withPermission(this.permissions.union(newPermissions));
297
	}
298
299
	public CommandSourceStack withAnchor(final EntityAnchorArgument.Anchor anchor) {
300
		return anchor == this.anchor
301
			? this
302
			: new CommandSourceStack(
303
				this.source,
304
				this.worldPosition,
305
				this.rotation,
306
				this.level,
307
				this.permissions,
308
				this.namesProvider,
309
				this.server,
310
				this.entity,
311
				this.silent,
312
				this.resultCallback,
313
				anchor,
314
				this.signingContext,
315
				this.chatMessageChainer
316
			);
317
	}
318
319
	public CommandSourceStack withLevel(final ServerLevel level) {
320
		if (level == this.level) {
321
			return this;
322
		}
323
324
		double scale = DimensionType.getTeleportationScale(this.level.dimensionType(), level.dimensionType());
325
		Vec3 pos = new Vec3(this.worldPosition.x * scale, this.worldPosition.y, this.worldPosition.z * scale);
326
		return new CommandSourceStack(
327
			this.source,
328
			pos,
329
			this.rotation,
330
			level,
331
			this.permissions,
332
			this.namesProvider,
333
			this.server,
334
			this.entity,
335
			this.silent,
336
			this.resultCallback,
337
			this.anchor,
338
			this.signingContext,
339
			this.chatMessageChainer
340
		);
341
	}
342
343
	public CommandSourceStack facing(final Entity entity, final EntityAnchorArgument.Anchor anchor) {
344
		return this.facing(anchor.apply(entity));
345
	}
346
347
	public CommandSourceStack facing(final Vec3 pos) {
348
		Vec3 from = this.anchor.apply(this);
349
		double xd = pos.x - from.x;
350
		double yd = pos.y - from.y;
351
		double zd = pos.z - from.z;
352
		double sd = Math.sqrt(xd * xd + zd * zd);
353
		float xRot = Mth.wrapDegrees((float)(-(Mth.atan2(yd, sd) * 180.0F / (float)Math.PI)));
354
		float yRot = Mth.wrapDegrees((float)(Mth.atan2(zd, xd) * 180.0F / (float)Math.PI) - 90.0F);
355
		return this.withRotation(new Vec2(xRot, yRot));
356
	}
357
358
	public CommandSourceStack withSigningContext(final CommandSigningContext signingContext, final TaskChainer chatMessageChainer) {
359
		return signingContext == this.signingContext && chatMessageChainer == this.chatMessageChainer
360
			? this
361
			: new CommandSourceStack(
362
				this.source,
363
				this.worldPosition,
364
				this.rotation,
365
				this.level,
366
				this.permissions,
367
				this.namesProvider,
368
				this.server,
369
				this.entity,
370
				this.silent,
371
				this.resultCallback,
372
				this.anchor,
373
				signingContext,
374
				chatMessageChainer
375
			);
376
	}
377
378
	public Component getDisplayName() {
379
		return this.namesProvider.displayName(this.entity);
380
	}
381
382
	public String getTextName() {
383
		return this.namesProvider.textName(this.entity);
384
	}
385
386
	@Override
387
	public PermissionSet permissions() {
388
		return this.permissions;
389
	}
390
391
	public Vec3 getPosition() {
392
		return this.worldPosition;
393
	}
394
395
	public ServerLevel getLevel() {
396
		return this.level;
397
	}
398
399
	public @Nullable Entity getEntity() {
400
		return this.entity;
401
	}
402
403
	public Entity getEntityOrException() throws CommandSyntaxException {
404
		if (this.entity == null) {
405
			throw ERROR_NOT_ENTITY.create();
406
		} else {
407
			return this.entity;
408
		}
409
	}
410
411
	public ServerPlayer getPlayerOrException() throws CommandSyntaxException {
412
		if (this.entity instanceof ServerPlayer player) {
413
			return player;
414
		} else {
415
			throw ERROR_NOT_PLAYER.create();
416
		}
417
	}
418
419
	public @Nullable ServerPlayer getPlayer() {
420
		return this.entity instanceof ServerPlayer player ? player : null;
421
	}
422
423
	public boolean isPlayer() {
424
		return this.entity instanceof ServerPlayer;
425
	}
426
427
	public Vec2 getRotation() {
428
		return this.rotation;
429
	}
430
431
	public MinecraftServer getServer() {
432
		return this.server;
433
	}
434
435
	public EntityAnchorArgument.Anchor getAnchor() {
436
		return this.anchor;
437
	}
438
439
	public CommandSigningContext getSigningContext() {
440
		return this.signingContext;
441
	}
442
443
	public TaskChainer getChatMessageChainer() {
444
		return this.chatMessageChainer;
445
	}
446
447
	public boolean shouldFilterMessageTo(final ServerPlayer receiver) {
448
		ServerPlayer player = this.getPlayer();
449
		return receiver == player ? false : player != null && player.isTextFilteringEnabled() || receiver.isTextFilteringEnabled();
450
	}
451
452
	public void sendChatMessage(final OutgoingChatMessage message, final boolean filtered, final ChatType.Bound chatType) {
453
		if (!this.silent) {
454
			ServerPlayer player = this.getPlayer();
455
			if (player != null) {
456
				player.sendChatMessage(message, filtered, chatType);
457
			} else {
458
				this.source.sendSystemMessage(chatType.decorate(message.content()));
459
			}
460
		}
461
	}
462
463
	public void sendSystemMessage(final Component message) {
464
		if (!this.silent) {
465
			ServerPlayer player = this.getPlayer();
466
			if (player != null) {
467
				player.sendSystemMessage(message);
468
			} else {
469
				this.source.sendSystemMessage(message);
470
			}
471
		}
472
	}
473
474
	public void sendSuccess(final Supplier<Component> messageSupplier, final boolean broadcast) {
475
		boolean shouldSendSystemMessage = this.source.acceptsSuccess() && !this.silent;
476
		boolean shouldBroadcast = broadcast && this.source.shouldInformAdmins() && !this.silent;
477
		if (shouldSendSystemMessage || shouldBroadcast) {
478
			Component message = messageSupplier.get();
479
			if (shouldSendSystemMessage) {
480
				this.source.sendSystemMessage(message);
481
			}
482
483
			if (shouldBroadcast) {
484
				this.broadcastToAdmins(message);
485
			}
486
		}
487
	}
488
489
	private void broadcastToAdmins(final Component message) {
490
		Component broadcast = Component.translatable("chat.type.admin", this.getDisplayName(), message).withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC);
491
		GameRules gameRules = this.level.getGameRules();
492
		if (gameRules.get(GameRules.SEND_COMMAND_FEEDBACK)) {
493
			for (ServerPlayer player : this.server.getPlayerList().getPlayers()) {
494
				if (player.commandSource() != this.source && this.server.getPlayerList().isOp(player.nameAndId())) {
495
					player.sendSystemMessage(broadcast);
496
				}
497
			}
498
		}
499
500
		if (this.source != this.server && gameRules.get(GameRules.LOG_ADMIN_COMMANDS)) {
501
			this.server.sendSystemMessage(broadcast);
502
		}
503
	}
504
505
	public void sendFailure(final Component message) {
506
		if (this.source.acceptsFailure() && !this.silent) {
507
			this.source.sendSystemMessage(Component.empty().append(message).withStyle(ChatFormatting.RED));
508
		}
509
	}
510
511
	@Override
512
	public CommandResultCallback callback() {
513
		return this.resultCallback;
514
	}
515
516
	@Override
517
	public Collection<String> getOnlinePlayerNames() {
518
		return Lists.newArrayList(this.server.getPlayerNames());
519
	}
520
521
	@Override
522
	public Collection<String> getAllTeams() {
523
		return this.server.getScoreboard().getTeamNames();
524
	}
525
526
	@Override
527
	public Stream<Identifier> getAvailableSounds() {
528
		return BuiltInRegistries.SOUND_EVENT.stream().map(SoundEvent::location);
529
	}
530
531
	@Override
532
	public Stream<Identifier> getAvailablePostEffects() {
533
		return Stream.empty();
534
	}
535
536
	@Override
537
	public CompletableFuture<Suggestions> customSuggestion(final CommandContext<?> context) {
538
		return Suggestions.empty();
539
	}
540
541
	@Override
542
	public <E> CompletableFuture<Suggestions> suggestRegistryElements(
543
		final ResourceKey<? extends Registry<E>> key,
544
		final SharedSuggestionProvider.ElementSuggestionType elements,
545
		final SuggestionsBuilder builder,
546
		final CommandContext<?> context,
547
		final Predicate<E> filter
548
	) {
549
		return this.getLookup(key).map(registry -> {
550
			this.suggestRegistryElements((HolderLookup<E>)registry, elements, builder, filter);
551
			return builder.buildFuture();
552
		}).orElseGet(Suggestions::empty);
553
	}
554
555
	private <E> Optional<? extends HolderLookup<E>> getLookup(final ResourceKey<? extends Registry<E>> key) {
556
		Optional<? extends Registry<E>> lookup = this.registryAccess().lookup(key);
557
		return lookup.isPresent() ? lookup : this.server.reloadableRegistries().lookup().lookup(key);
558
	}
559
560
	@Override
561
	public Set<ResourceKey<Level>> levels() {
562
		return this.server.levelKeys();
563
	}
564
565
	@Override
566
	public RegistryAccess registryAccess() {
567
		return this.server.registryAccess();
568
	}
569
570
	@Override
571
	public FeatureFlagSet enabledFeatures() {
572
		return this.level.enabledFeatures();
573
	}
574
575
	@Override
576
	public CommandDispatcher<CommandSourceStack> dispatcher() {
577
		return this.getServer().getFunctions().getDispatcher();
578
	}
579
580
	@Override
581
	public void handleError(final CommandExceptionType type, final Message message, final boolean forked, final @Nullable TraceCallbacks tracer) {
582
		if (tracer != null) {
583
			tracer.onError(message.getString());
584
		}
585
586
		if (!forked) {
587
			this.sendFailure(ComponentUtils.fromMessage(message));
588
		}
589
	}
590
591
	@Override
592
	public boolean isSilent() {
593
		return this.silent;
594
	}
595
596
	public interface NamesProvider {
597
		CommandSourceStack.NamesProvider FOR_ENTITY = new CommandSourceStack.NamesProvider() {
598
			@Override
599
			public Component displayName(final @Nullable Entity entity) {
600
				return entity != null ? entity.getDisplayName() : CommonComponents.EMPTY;
601
			}
602
603
			@Override
604
			public String textName(final @Nullable Entity entity) {
605
				return entity != null ? entity.getPlainTextName() : "";
606
			}
607
		};
608
609
		Component displayName(@Nullable Entity entity);
610
611
		String textName(@Nullable Entity entity);
612
613
		static CommandSourceStack.NamesProvider constant(final Component value) {
614
			return new CommandSourceStack.NamesProvider() {
615
				@Override
616
				public Component displayName(final @Nullable Entity entity) {
617
					return value;
618
				}
619
620
				@Override
621
				public String textName(final @Nullable Entity entity) {
622
					return value.getString();
623
				}
624
			};
625
		}
626
	}
627
}
628

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