diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/bukkit/command/SimpleCommandMap.java | 1 | ||||
-rw-r--r-- | src/main/java/org/bukkit/command/defaults/PlaySoundCommand.java | 87 | ||||
-rw-r--r-- | src/main/java/org/bukkit/entity/Player.java | 14 |
3 files changed, 102 insertions, 0 deletions
diff --git a/src/main/java/org/bukkit/command/SimpleCommandMap.java b/src/main/java/org/bukkit/command/SimpleCommandMap.java index df5f6ef1..5e1e233d 100644 --- a/src/main/java/org/bukkit/command/SimpleCommandMap.java +++ b/src/main/java/org/bukkit/command/SimpleCommandMap.java @@ -58,6 +58,7 @@ public class SimpleCommandMap implements CommandMap { fallbackCommands.add(new TestForCommand()); fallbackCommands.add(new EffectCommand()); fallbackCommands.add(new ScoreboardCommand()); + fallbackCommands.add(new PlaySoundCommand()); } public SimpleCommandMap(final Server server) { diff --git a/src/main/java/org/bukkit/command/defaults/PlaySoundCommand.java b/src/main/java/org/bukkit/command/defaults/PlaySoundCommand.java new file mode 100644 index 00000000..4caf2bbe --- /dev/null +++ b/src/main/java/org/bukkit/command/defaults/PlaySoundCommand.java @@ -0,0 +1,87 @@ +package org.bukkit.command.defaults; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class PlaySoundCommand extends VanillaCommand { + public PlaySoundCommand() { + super("playsound"); + this.description = "Plays a sound to a given player"; + this.usageMessage = "/playsound <sound> <player> [x] [y] [z] [volume] [pitch] [minimumVolume]"; + this.setPermission("bukkit.command.playsound"); + } + + @Override + public boolean execute(CommandSender sender, String currentAlias, String[] args) { + if (!testPermission(sender)) { + return true; + } + + if (args.length < 2) { + sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); + return false; + } + final String soundArg = args[0]; + final String playerArg = args[1]; + + final Player player = Bukkit.getPlayerExact(playerArg); + if (player == null) { + sender.sendMessage(ChatColor.RED + "Can't find player %s" + playerArg); + return false; + } + + final Location location = player.getLocation(); + + double x = Math.floor(location.getX()); + double y = Math.floor(location.getY() + 0.5D); + double z = Math.floor(location.getZ()); + double volume = 1.0D; + double pitch = 1.0D; + double minimumVolume = 0.0D; + + switch (args.length) { + default: + case 8: + minimumVolume = getDouble(sender, args[7], 0.0D, 1.0D); + case 7: + pitch = getDouble(sender, args[6], 0.0D, 2.0D); + case 6: + volume = getDouble(sender, args[5], 0.0D, Float.MAX_VALUE); + case 5: + z = getDouble(sender, args[4]); + case 4: + y = getDouble(sender, args[3]); + case 3: + x = getDouble(sender, args[2]); + case 2: + // Noop + } + + final double fixedVolume = volume > 1.0D ? volume * 16.0D : 16.0D; + final Location soundLocation = new Location(player.getWorld(), x, y, z); + if (location.distanceSquared(soundLocation) > fixedVolume * fixedVolume) { + if (minimumVolume <= 0.0D) { + sender.sendMessage(ChatColor.RED + playerArg + " is too far away to hear the sound"); + return false; + } + + final double deltaX = x - location.getX(); + final double deltaY = y - location.getY(); + final double deltaZ = z - location.getZ(); + final double delta = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ) / 2.0D; + + if (delta > 0.0D) { + soundLocation.add(deltaX / delta, deltaY / delta, deltaZ / delta); + } + + player.playSound(soundLocation, soundArg, (float) minimumVolume, (float) pitch); + } else { + player.playSound(soundLocation, soundArg, (float) volume, (float) pitch); + } + sender.sendMessage(String.format("Played '%s' to %s", soundArg, playerArg)); + return true; + } +} diff --git a/src/main/java/org/bukkit/entity/Player.java b/src/main/java/org/bukkit/entity/Player.java index 3b58d120..d492790f 100644 --- a/src/main/java/org/bukkit/entity/Player.java +++ b/src/main/java/org/bukkit/entity/Player.java @@ -215,6 +215,20 @@ public interface Player extends HumanEntity, Conversable, CommandSender, Offline public void playSound(Location location, Sound sound, float volume, float pitch); /** + * Play a sound for a player at the location. + * <p> + * This function will fail silently if Location or Sound are null. No + * sound will be heard by the player if their client does not have the + * respective sound for the value passed. + * + * @param location the location to play the sound + * @param sound the internal sound name to play + * @param volume the volume of the sound + * @param pitch the pitch of the sound + */ + public void playSound(Location location, String sound, float volume, float pitch); + + /** * Plays an effect to just this player. * * @param loc the location to play the effect at |