| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
| 5 | import java.util.Collection;
|
| 6 | import net.minecraft.commands.CommandSourceStack;
|
| 7 | import net.minecraft.commands.Commands;
|
| 8 | import net.minecraft.commands.arguments.EntityArgument;
|
| 9 | import net.minecraft.commands.arguments.IdentifierArgument;
|
| 10 | import net.minecraft.commands.arguments.selector.EntitySelector;
|
| 11 | import net.minecraft.commands.synchronization.SuggestionProviders;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.network.protocol.game.ClientboundStopSoundPacket;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.server.level.ServerPlayer;
|
| 16 | import net.minecraft.sounds.SoundSource;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | public class StopSoundCommand {
|
| 20 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 21 | RequiredArgumentBuilder<CommandSourceStack, EntitySelector> target = Commands.argument("targets", EntityArgument.players())
|
| 22 | .executes(c -> stopSound(c.getSource(), EntityArgument.getPlayers(c, "targets"), null, null))
|
| 23 | .then(
|
| 24 | Commands.literal("*")
|
| 25 | .then(
|
| 26 | Commands.argument("sound", IdentifierArgument.id())
|
| 27 | .suggests(SuggestionProviders.cast(SuggestionProviders.AVAILABLE_SOUNDS))
|
| 28 | .executes(c -> stopSound(c.getSource(), EntityArgument.getPlayers(c, "targets"), null, IdentifierArgument.getId(c, "sound")))
|
| 29 | )
|
| 30 | );
|
| 31 |
|
| 32 | for (SoundSource source : SoundSource.values()) {
|
| 33 | target.then(
|
| 34 | Commands.literal(source.getName())
|
| 35 | .executes(c -> stopSound(c.getSource(), EntityArgument.getPlayers(c, "targets"), source, null))
|
| 36 | .then(
|
| 37 | Commands.argument("sound", IdentifierArgument.id())
|
| 38 | .suggests(SuggestionProviders.cast(SuggestionProviders.AVAILABLE_SOUNDS))
|
| 39 | .executes(c -> stopSound(c.getSource(), EntityArgument.getPlayers(c, "targets"), source, IdentifierArgument.getId(c, "sound")))
|
| 40 | )
|
| 41 | );
|
| 42 | }
|
| 43 |
|
| 44 | dispatcher.register(Commands.literal("stopsound").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS)).then(target));
|
| 45 | }
|
| 46 |
|
| 47 | private static int stopSound(
|
| 48 | final CommandSourceStack source, final Collection<ServerPlayer> targets, final @Nullable SoundSource soundSource, final @Nullable Identifier sound
|
| 49 | ) {
|
| 50 | ClientboundStopSoundPacket packet = new ClientboundStopSoundPacket(sound, soundSource);
|
| 51 |
|
| 52 | for (ServerPlayer player : targets) {
|
| 53 | player.connection.send(packet);
|
| 54 | }
|
| 55 |
|
| 56 | if (soundSource != null) {
|
| 57 | if (sound != null) {
|
| 58 | source.sendSuccess(() -> Component.translatable("commands.stopsound.success.source.sound", Component.translationArg(sound), soundSource.getName()), true);
|
| 59 | } else {
|
| 60 | source.sendSuccess(() -> Component.translatable("commands.stopsound.success.source.any", soundSource.getName()), true);
|
| 61 | }
|
| 62 | } else if (sound != null) {
|
| 63 | source.sendSuccess(() -> Component.translatable("commands.stopsound.success.sourceless.sound", Component.translationArg(sound)), true);
|
| 64 | } else {
|
| 65 | source.sendSuccess(() -> Component.translatable("commands.stopsound.success.sourceless.any"), true);
|
| 66 | }
|
| 67 |
|
| 68 | return targets.size();
|
| 69 | }
|
| 70 | }
|
| 71 |
|