aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/PlaySoundCommand.h
blob: 1f43143b5c667bf7839d4198c658dedf2e395ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
package net.minecraft.commands.common;

import net.minecraft.commands.BaseCommand;
import net.minecraft.commands.CommandSender;
import net.minecraft.commands.exceptions.CommandException;
import net.minecraft.commands.exceptions.UsageException;
import net.minecraft.network.packet.LevelSoundPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;

public class PlaySoundCommand extends BaseCommand {
    @Override
    public String getName() {
        return "playsound";
    }

    @Override
    public int getPermissionLevel() {
        return LEVEL_GAMEMASTERS;
    }

    @Override
    public String getUsage(CommandSender source) {
        return "commands.playsound.usage";
    }

    @Override
    public void execute(CommandSender source, String[] args) {
        if (args.length < 2) {
            throw new UsageException(getUsage(source));
        }

        int index = 0;
        String sound = args[index++];
        ServerPlayer player = convertToPlayer(source, args[index++]);
        double x = player.getCommandSenderWorldPosition().x;
        double y = player.getCommandSenderWorldPosition().y;
        double z = player.getCommandSenderWorldPosition().z;
        double volume = 1;
        double pitch = 1;
        double minVolume = 0;

        if (args.length > index) x = convertArgToCoordinate(source, x, args[index++]);
        if (args.length > index) y = convertArgToCoordinate(source, y, args[index++], 0, 0);
        if (args.length > index) z = convertArgToCoordinate(source, z, args[index++]);

        if (args.length > index) volume = convertArgToDouble(source, args[index++], 0, Float.MAX_VALUE);
        if (args.length > index) pitch = convertArgToDouble(source, args[index++], 0, 2);
        if (args.length > index) minVolume = convertArgToDouble(source, args[index++], 0, 1);

        double maxDist = volume > 1 ? volume * 16 : 16;
        double dist = player.distanceTo(x, y, z);

        if (dist > maxDist) {
            if (minVolume > 0) {
                double deltaX = x - player.x;
                double deltaY = y - player.y;
                double deltaZ = z - player.z;
                double length = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
                double soundX = player.x;
                double soundY = player.y;
                double soundZ = player.z;

                if (length > 0) {
                    soundX += deltaX / length * 2;
                    soundY += deltaY / length * 2;
                    soundZ += deltaZ / length * 2;
                }

                player.connection.send(new LevelSoundPacket(sound, soundX, soundY, soundZ, (float) minVolume, (float) pitch));
            } else {
                throw new CommandException("commands.playsound.playerTooFar", player.getAName());
            }
        } else {
            player.connection.send(new LevelSoundPacket(sound, x, y, z, (float) volume, (float) pitch));
        }

        logAdminAction(source, "commands.playsound.success", sound, player.getAName());
    }

    @Override
    public boolean isValidWildcardPlayerArgument(String[] args, int argumentIndex) {
        return argumentIndex == 1;
    }
}

*/