Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
PlaySoundCommand.java
1 package org.bukkit.command.defaults;
2 
3 import org.bukkit.Bukkit;
4 import org.bukkit.ChatColor;
5 import org.bukkit.Location;
7 import org.bukkit.entity.Player;
8 
9 public class PlaySoundCommand extends VanillaCommand {
10  public PlaySoundCommand() {
11  super("playsound");
12  this.description = "Plays a sound to a given player";
13  this.usageMessage = "/playsound <sound> <player> [x] [y] [z] [volume] [pitch] [minimumVolume]";
14  this.setPermission("bukkit.command.playsound");
15  }
16 
17  @Override
18  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
19  if (!testPermission(sender)) {
20  return true;
21  }
22 
23  if (args.length < 2) {
24  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
25  return false;
26  }
27  final String soundArg = args[0];
28  final String playerArg = args[1];
29 
30  final Player player = Bukkit.getPlayerExact(playerArg);
31  if (player == null) {
32  sender.sendMessage(ChatColor.RED + "Can't find player " + playerArg);
33  return false;
34  }
35 
36  final Location location = player.getLocation();
37 
38  double x = Math.floor(location.getX());
39  double y = Math.floor(location.getY() + 0.5D);
40  double z = Math.floor(location.getZ());
41  double volume = 1.0D;
42  double pitch = 1.0D;
43  double minimumVolume = 0.0D;
44 
45  switch (args.length) {
46  default:
47  case 8:
48  minimumVolume = getDouble(sender, args[7], 0.0D, 1.0D);
49  case 7:
50  pitch = getDouble(sender, args[6], 0.0D, 2.0D);
51  case 6:
52  volume = getDouble(sender, args[5], 0.0D, Float.MAX_VALUE);
53  case 5:
54  z = getRelativeDouble(z, sender, args[4]);
55  case 4:
56  y = getRelativeDouble(y, sender, args[3]);
57  case 3:
58  x = getRelativeDouble(x, sender, args[2]);
59  case 2:
60  // Noop
61  }
62 
63  final double fixedVolume = volume > 1.0D ? volume * 16.0D : 16.0D;
64  final Location soundLocation = new Location(player.getWorld(), x, y, z);
65  if (location.distanceSquared(soundLocation) > fixedVolume * fixedVolume) {
66  if (minimumVolume <= 0.0D) {
67  sender.sendMessage(ChatColor.RED + playerArg + " is too far away to hear the sound");
68  return false;
69  }
70 
71  final double deltaX = x - location.getX();
72  final double deltaY = y - location.getY();
73  final double deltaZ = z - location.getZ();
74  final double delta = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ) / 2.0D;
75 
76  if (delta > 0.0D) {
77  location.add(deltaX / delta, deltaY / delta, deltaZ / delta);
78  }
79 
80  player.playSound(location, soundArg, (float) minimumVolume, (float) pitch);
81  } else {
82  player.playSound(soundLocation, soundArg, (float) volume, (float) pitch);
83  }
84  sender.sendMessage(String.format("Played '%s' to %s", soundArg, playerArg));
85  return true;
86  }
87 }
boolean testPermission(CommandSender target)
void sendMessage(String message)
Location add(Location vec)
Definition: Location.java:301
double distanceSquared(Location o)
Definition: Location.java:442
void playSound(Location location, Sound sound, float volume, float pitch)
void setPermission(String permission)
static Player getPlayerExact(String name)
Definition: Bukkit.java:436