| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.arguments.FloatArgumentType;
|
| 5 | import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
| 6 | import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
| 7 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 8 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 9 | import java.util.Collection;
|
| 10 | import java.util.List;
|
| 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.IdentifierArgument;
|
| 15 | import net.minecraft.commands.arguments.coordinates.Vec3Argument;
|
| 16 | import net.minecraft.commands.synchronization.SuggestionProviders;
|
| 17 | import net.minecraft.core.Holder;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.network.protocol.game.ClientboundSoundPacket;
|
| 20 | import net.minecraft.resources.Identifier;
|
| 21 | import net.minecraft.server.level.ServerLevel;
|
| 22 | import net.minecraft.server.level.ServerPlayer;
|
| 23 | import net.minecraft.sounds.SoundEvent;
|
| 24 | import net.minecraft.sounds.SoundSource;
|
| 25 | import net.minecraft.util.Mth;
|
| 26 | import net.minecraft.world.phys.Vec3;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 |
|
| 29 | public class PlaySoundCommand {
|
| 30 | private static final SimpleCommandExceptionType ERROR_TOO_FAR = new SimpleCommandExceptionType(Component.translatable("commands.playsound.failed"));
|
| 31 | private static final CommandResponseTracker.MessagesWithArg<ServerPlayer, Identifier> RESPONSE_PLAY = CommandResponseTracker.messages(
|
| 32 | ERROR_TOO_FAR,
|
| 33 | (player, var1, sound) -> Component.translatable("commands.playsound.success.single", Component.translationArg(sound), player.getDisplayName()),
|
| 34 | (playerCount, var1, sound) -> Component.translatable("commands.playsound.success.multiple", Component.translationArg(sound), playerCount)
|
| 35 | );
|
| 36 |
|
| 37 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 38 | RequiredArgumentBuilder<CommandSourceStack, Identifier> name = Commands.argument("sound", IdentifierArgument.id())
|
| 39 | .suggests(SuggestionProviders.cast(SuggestionProviders.AVAILABLE_SOUNDS))
|
| 40 | .executes(
|
| 41 | c -> playSound(
|
| 42 | c.getSource(),
|
| 43 | getCallingPlayerAsCollection(c.getSource().getPlayer()),
|
| 44 | IdentifierArgument.getId(c, "sound"),
|
| 45 | SoundSource.MASTER,
|
| 46 | c.getSource().getPosition(),
|
| 47 | 1.0F,
|
| 48 | 1.0F,
|
| 49 | 0.0F
|
| 50 | )
|
| 51 | );
|
| 52 |
|
| 53 | for (SoundSource source : SoundSource.values()) {
|
| 54 | name.then(source(source));
|
| 55 | }
|
| 56 |
|
| 57 | dispatcher.register(Commands.literal("playsound").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS)).then(name));
|
| 58 | }
|
| 59 |
|
| 60 | private static LiteralArgumentBuilder<CommandSourceStack> source(final SoundSource source) {
|
| 61 | return Commands.literal(source.getName())
|
| 62 | .executes(
|
| 63 | c -> playSound(
|
| 64 | c.getSource(),
|
| 65 | getCallingPlayerAsCollection(c.getSource().getPlayer()),
|
| 66 | IdentifierArgument.getId(c, "sound"),
|
| 67 | source,
|
| 68 | c.getSource().getPosition(),
|
| 69 | 1.0F,
|
| 70 | 1.0F,
|
| 71 | 0.0F
|
| 72 | )
|
| 73 | )
|
| 74 | .then(
|
| 75 | Commands.argument("targets", EntityArgument.players())
|
| 76 | .executes(
|
| 77 | c -> playSound(
|
| 78 | c.getSource(), EntityArgument.getPlayers(c, "targets"), IdentifierArgument.getId(c, "sound"), source, c.getSource().getPosition(), 1.0F, 1.0F, 0.0F
|
| 79 | )
|
| 80 | )
|
| 81 | .then(
|
| 82 | Commands.argument("pos", Vec3Argument.vec3())
|
| 83 | .executes(
|
| 84 | c -> playSound(
|
| 85 | c.getSource(), EntityArgument.getPlayers(c, "targets"), IdentifierArgument.getId(c, "sound"), source, Vec3Argument.getVec3(c, "pos"), 1.0F, 1.0F, 0.0F
|
| 86 | )
|
| 87 | )
|
| 88 | .then(
|
| 89 | Commands.argument("volume", FloatArgumentType.floatArg(0.0F))
|
| 90 | .executes(
|
| 91 | c -> playSound(
|
| 92 | c.getSource(),
|
| 93 | EntityArgument.getPlayers(c, "targets"),
|
| 94 | IdentifierArgument.getId(c, "sound"),
|
| 95 | source,
|
| 96 | Vec3Argument.getVec3(c, "pos"),
|
| 97 | c.getArgument("volume", Float.class),
|
| 98 | 1.0F,
|
| 99 | 0.0F
|
| 100 | )
|
| 101 | )
|
| 102 | .then(
|
| 103 | Commands.argument("pitch", FloatArgumentType.floatArg(0.0F, 2.0F))
|
| 104 | .executes(
|
| 105 | c -> playSound(
|
| 106 | c.getSource(),
|
| 107 | EntityArgument.getPlayers(c, "targets"),
|
| 108 | IdentifierArgument.getId(c, "sound"),
|
| 109 | source,
|
| 110 | Vec3Argument.getVec3(c, "pos"),
|
| 111 | c.getArgument("volume", Float.class),
|
| 112 | c.getArgument("pitch", Float.class),
|
| 113 | 0.0F
|
| 114 | )
|
| 115 | )
|
| 116 | .then(
|
| 117 | Commands.argument("minVolume", FloatArgumentType.floatArg(0.0F, 1.0F))
|
| 118 | .executes(
|
| 119 | c -> playSound(
|
| 120 | c.getSource(),
|
| 121 | EntityArgument.getPlayers(c, "targets"),
|
| 122 | IdentifierArgument.getId(c, "sound"),
|
| 123 | source,
|
| 124 | Vec3Argument.getVec3(c, "pos"),
|
| 125 | c.getArgument("volume", Float.class),
|
| 126 | c.getArgument("pitch", Float.class),
|
| 127 | c.getArgument("minVolume", Float.class)
|
| 128 | )
|
| 129 | )
|
| 130 | )
|
| 131 | )
|
| 132 | )
|
| 133 | )
|
| 134 | );
|
| 135 | }
|
| 136 |
|
| 137 | private static Collection<ServerPlayer> getCallingPlayerAsCollection(final @Nullable ServerPlayer player) {
|
| 138 | return player != null ? List.of(player) : List.of();
|
| 139 | }
|
| 140 |
|
| 141 | private static int playSound(
|
| 142 | final CommandSourceStack source,
|
| 143 | final Collection<ServerPlayer> players,
|
| 144 | final Identifier sound,
|
| 145 | final SoundSource soundSource,
|
| 146 | final Vec3 position,
|
| 147 | final float volume,
|
| 148 | final float pitch,
|
| 149 | final float minVolume
|
| 150 | ) throws CommandSyntaxException {
|
| 151 | Holder<SoundEvent> soundHolder = Holder.direct(SoundEvent.createVariableRangeEvent(sound));
|
| 152 | double maxDistSqr = Mth.square(soundHolder.value().getRange(volume));
|
| 153 | ServerLevel level = source.getLevel();
|
| 154 | long seed = level.getRandom().nextLong();
|
| 155 | CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
|
| 156 |
|
| 157 | for (ServerPlayer player : players) {
|
| 158 | if (player.level() == level) {
|
| 159 | double deltaX = position.x - player.getX();
|
| 160 | double deltaY = position.y - player.getY();
|
| 161 | double deltaZ = position.z - player.getZ();
|
| 162 | double distSqr = deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ;
|
| 163 | Vec3 localPosition = position;
|
| 164 | float localVolume = volume;
|
| 165 | if (distSqr > maxDistSqr) {
|
| 166 | if (minVolume <= 0.0F) {
|
| 167 | continue;
|
| 168 | }
|
| 169 |
|
| 170 | double distance = Math.sqrt(distSqr);
|
| 171 | localPosition = new Vec3(player.getX() + deltaX / distance * 2.0, player.getY() + deltaY / distance * 2.0, player.getZ() + deltaZ / distance * 2.0);
|
| 172 | localVolume = minVolume;
|
| 173 | }
|
| 174 |
|
| 175 | player.connection
|
| 176 | .send(new ClientboundSoundPacket(soundHolder, soundSource, localPosition.x(), localPosition.y(), localPosition.z(), localVolume, pitch, seed));
|
| 177 | tracker.track(player);
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | return tracker.sendFeedback(source, true, RESPONSE_PLAY, sound);
|
| 182 | }
|
| 183 | }
|
| 184 |
|