Add 1.6 PlaySound command. Fixes BUKKIT-4489

By: h31ix <zero_gravity@outlook.com>
This commit is contained in:
Bukkit/Spigot 2013-07-19 18:02:40 -04:00
parent 133585430c
commit 06abcc1323
3 changed files with 102 additions and 0 deletions

View file

@ -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) {

View file

@ -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;
}
}

View file

@ -214,6 +214,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.
*