Solved Playing sounds

Discussion in 'Plugin Development' started by andrewmariocast, Mar 3, 2013.

Thread Status:
Not open for further replies.
  1. Offline


    I am trying to make a plugin that plays sounds after the player does a command. However this does not appear to be working. After doing the command (/scare) the console says:
    15:53:34 [INFO] AndrewTowers issued server command: /scare
    15:53:34 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'scar
    e' in plugin AnditSparticles v0.1
            at org.bukkit.command.PluginCommand.execute(
            at org.bukkit.command.SimpleCommandMap.dispatch(
            at org.bukkit.craftbukkit.v1_4_R1.CraftServer.dispatchCommand(CraftServe
            at net.minecraft.server.v1_4_R1.PlayerConnection.handleCommand(PlayerCon
            at net.minecraft.server.v1_4_R1.PlayerConnection.a(
            at net.minecraft.server.v1_4_R1.Packet3Chat.handle(
            at net.minecraft.server.v1_4_R1.NetworkManager.b(
            at net.minecraft.server.v1_4_R1.PlayerConnection.d(
            at net.minecraft.server.v1_4_R1.ServerConnection.b(SourceFile:39)
            at net.minecraft.server.v1_4_R1.DedicatedServerConnection.b(SourceFile:3
            at net.minecraft.server.v1_4_R1.MinecraftServer.r(
            at net.minecraft.server.v1_4_R1.DedicatedServer.r(
            at net.minecraft.server.v1_4_R1.MinecraftServer.q(
    Caused by: java.lang.Error: Unresolved compilation problem:
            BURP cannot be resolved to a variable
            at com.AnditVideos.Sparticles.ScareExecutor.onCommand(
            at org.bukkit.command.PluginCommand.execute(
            ... 15 more
    My command executer looks like this:

    package com.AnditVideos.Sparticles;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Sound;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    public class ScareExecutor implements CommandExecutor {
    public ScareExecutor(Sparticles plugin) {
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    sender.sendMessage(ChatColor.RED + "Playing burp sound");
    return true;
    Please Help, Thanks :D
  2. Offline


  3. Offline


    DarkBladee12 Thanks but it still doesn't work. I think its something to do with:
    Eclipse puts red lines under it
  4. Offline


    sender.playSound(p.getLocation(), Sound.FUSE, 2F, 1F);

    Note: Fuse is the sound the creeper makes before it explodes. I think thats what u were looking for earlier.

    Note: Make sure sender is a player
  5. Offline


    the_merciless it doesn't work the p in p.getLocation has a red line under it
  6. Offline


    Player p = (Player)sender;
    p.playSound(p.getLocation(), Sound.FUSE, 2F, 1F);
  7. Offline


    That is because you didn't Cast your "sender" to a "Player" yet. Don't forget to check if the command sender really is a player, otherwise you will get lots of errors if you use this command elsewhere.
  8. Offline


    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("Scare"){
            Player p = (Player)sender;
            if (sender instanceof Player){
                p.sendMessage(ChatColor.RED + "Playing burp sound");
                p.playSound(p.getLocation(), Sound.FUSE, 2F, 1F);
                return true;
    return false;
  9. Offline


    Thanks you guys it worked :D
  10. Offline


    Only the player specified will hear the sound, something i just worked out.

    edit: player.playSound() will play the sound for only that player, if you want it to play at a location so all players can hear it u should use:

    player.getWorld().playSound(player.getLocation(), Sound.FUSE, 3F, 1F);
    Maulss likes this.
  11. Offline


    the_merciless Have you any idea how I would do it with arguments EG: /playsound private burp 1
    'burp' being the sound and '1' being the pitch. I have tried:
    p.playSound(p.getLocation(), Sound.args[1], 2F, args[2]);
    But that doesn't seem to work
  12. Offline


    Player p = (Player)sender;
    Put that after this:
    if (sender instanceof Player)

    Because now you are saying (if the command is executed by the console) that the console is a player, and it will give you an error then, but when you put that corretly, like i said, you won't get an error
  13. andrewmariocast
    You need to learn basic Java first, you can't just add [] to something and magically make it an argument.

    So, using arguments first requires you to check if args.length is actually bigger than the index you're using... args.length > 0 to safely use args[0], and so on, you must always check them.

    You'll need to use a try-catch surrounding Sound.valueOf() and in the catch tell the sender the sound does not exist.
    For volume and pitch you'll need Float.valueOf(), the same try-catch required because if sender does not enter a number it will throw errors, and you need to catch them because they're unavoidable - but if you can avoid errors you should ALWAYS avoid them instead of try-catch!

    And if you want them optional you need to set some default values and then in some args.length conditions check the arguments if they're there and overwrite the defaults, otherwise leave them be.
    haamome likes this.
  14. Offline


    Digi Thanks, I am just starting Java and will buy myself a book on the weekend :D.

    Also do you have any idea how to play the sound to a specified player (args[3]) rather than just the sender.
  15. Bukkit.getPlayer() to get an online player with partial name matching, may return null so check that too.
  16. Offline


    Digi one last question, how would you tell the sender if the sound exists? otherwise it throws an error
  17. If you wanna learn more about Java, I recommend thenewboston's tutorials they're pretty much the best around, and he does many other tutorials, some of note are the Java Game Development and Java Game Development with Slick series.
  18. sender.sendMessage() ?
    Or do you mean the try-catch ? Search for it, it's a Java general thing, not Bukkit specific.
    Basically you're trying a code and catching any Exception object that it throws so you can use a more user-friendly message.
  19. Offline


    Not 100%, (im still learning java myself) but my first guess would be something like this:

    if (cmd.getName().equalsIgnoreCase("scare")){
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null){
                    sender.sendMessage("Player is unknown or not online");
                    return true;
                Sound sound = null;
                for (Sound s : Sound.values()){
                    if (s.toString().contains(args[1])){
                        sound = s;
                        sender.sendMessage("Sound unknown");
                            return true;
                String svol = args[2] + "F";
                int volume = Integer.parseInt(svol);
                String spitch = args[3] + "F";
                int pitch = Integer.parseInt(spitch);
                target.playSound(target.getLocation(), sound, volume, pitch);
    Using the command /scare (player) (sound) (volume) (pitch)
    Eg: /scare The_Merciless burp 3 3

    THIS DOESNT WORK, testing it now.
  20. Offline


    the_merciless Thanks and I have found a way to make it work (I have shortened it a bit as the actual file is huge)
    if (args.length == 3){
                    if (sender.hasPermission("anditsparticles.playsound.others")) {
                        String inputsound = args[0];
                        String InputSound = inputsound.toUpperCase();
                        if (args[1].equalsIgnoreCase("low")){
                            PitchNo = 0;
                        } else if (args[1].equalsIgnoreCase("normal")) {
                            PitchNo = 1;
                        } else if (args[1].equalsIgnoreCase("high")) {
                            PitchNo = 2;
                            PitchNo = 1;
                        if (Bukkit.getPlayerExact(args[2]) == null){
                            sender.sendMessage(ChatColor.RED + "Player: " + args[2] + " is not online!");
                            try {
                                Player p = Bukkit.getPlayerExact(args[2]);
                                p.playSound(p.getLocation(), Sound.valueOf(InputSound), 2F, Float.valueOf(PitchNo));
                                sender.sendMessage(ChatColor.GREEN + "Now playing: " + InputSound + " at pitch: " + args[1] + " To Player: " + args[2]);
                            } catch (Exception e) {
                                sender.sendMessage(ChatColor.RED + "sound does not exist");
                        sender.sendMessage(ChatColor.DARK_RED + "Nope");
    This (and some other code) makes it so you can do /playsound <sound> <pitch> <player>
  21. Offline


    Thanks, i may make use of this code in the plugin im working on atm.
  22. That code will only work for the first value, because if the first is not correct then you're making it fail.
    You should either use sound = s then break and then check if it's not null after the loop... or try-catch on Sound.valueOf();
  23. Offline


  24. Offline


    I realised that when i printed the values.

    I know but im assuming the rest is just 'if's' and 'else's'?
  25. You don't need Float.valueOf() on PitchNo if it's already a float value.

    Also, move "Player p = Bukkit.getPlayerExact(args[2]);" before the "if (Bukkit.getPlayerExact(args[2]) == null){" condition and also edit the condition to check if p == null instead, to avoid calling that method twice.

    And also use getPlayer() instead of getPlayerExact() to allow partial name matching since it's user input.
    You must use getPlayerExact() when you have the exact name, e.g. stored player.getName()

    You should mark it as Solved if it's solved :p
  26. Offline


    Digi Thanks, and thread now prefixed as solved :D
  27. Offline


    How do i play a sound so everyone in the server can hear it?
  28. Offline


    public void playSoundToAllPlayers(){
            for (Player p : Bukkit.getOnlinePlayers()){
                p.playSound(p.getLocation(), Sound.BURP, 1F, 1F);
Thread Status:
Not open for further replies.

Share This Page