Solved Playing sounds

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

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

    andrewmariocast

    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:
    Code:
    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(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    6)
            at org.bukkit.craftbukkit.v1_4_R1.CraftServer.dispatchCommand(CraftServe
    r.java:514)
            at net.minecraft.server.v1_4_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:980)
            at net.minecraft.server.v1_4_R1.PlayerConnection.chat(PlayerConnection.j
    ava:898)
            at net.minecraft.server.v1_4_R1.PlayerConnection.a(PlayerConnection.java
    :853)
            at net.minecraft.server.v1_4_R1.Packet3Chat.handle(Packet3Chat.java:44)
            at net.minecraft.server.v1_4_R1.NetworkManager.b(NetworkManager.java:290
    )
            at net.minecraft.server.v1_4_R1.PlayerConnection.d(PlayerConnection.java
    :113)
            at net.minecraft.server.v1_4_R1.ServerConnection.b(SourceFile:39)
            at net.minecraft.server.v1_4_R1.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_4_R1.MinecraftServer.r(MinecraftServer.java:5
    98)
            at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:2
    24)
            at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:4
    94)
            at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java
    :427)
            at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:8
    49)
    Caused by: java.lang.Error: Unresolved compilation problem:
            BURP cannot be resolved to a variable
     
            at com.AnditVideos.Sparticles.ScareExecutor.onCommand(ScareExecutor.java
    :26)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 15 more
    My command executer looks like this:

    Code:
    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) {
    }
     
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
     
    sender.sendMessage(ChatColor.RED + "Playing burp sound");
     
     
    sender.playSound(BURP,getBlockX(),getBlockY(),getBlockZ());
     
     
    return true;
    }
     
    }
    
    Please Help, Thanks :D
     
  2. Offline

    DarkBladee12

  3. Offline

    andrewmariocast

    DarkBladee12 Thanks but it still doesn't work. I think its something to do with:
    Code:
    getBlockX(),getBlockY(),getBlockZ()
    Eclipse puts red lines under it
     
  4. Offline

    the_merciless

    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

    andrewmariocast

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

    the_merciless

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

    minoneer

    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

    the_merciless

    Code:
    @Override
    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

    andrewmariocast

    Thanks you guys it worked :D
     
  10. Offline

    the_merciless

    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

    andrewmariocast

    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:
    Code:
    p.playSound(p.getLocation(), Sound.args[1], 2F, args[2]);
    But that doesn't seem to work
     
  12. Offline

    TomTheDeveloper

    andrewmariocast
    Code:
    Player p = (Player)sender;
    Put that after this:
    Code:
    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

    andrewmariocast

    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

    andrewmariocast

    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

    the_merciless

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

    Code:
    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;
                    }else{
                        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

    andrewmariocast

    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)
    Code:
    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;
                        }else{
                            PitchNo = 1;
                        }
                        if (Bukkit.getPlayerExact(args[2]) == null){
                            sender.sendMessage(ChatColor.RED + "Player: " + args[2] + " is not online!");
                        }else{
                            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");
                            }
                    }
                    }else{
                        sender.sendMessage(ChatColor.DARK_RED + "Nope");
                    }
    This (and some other code) makes it so you can do /playsound <sound> <pitch> <player>
     
  21. Offline

    the_merciless

    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

    andrewmariocast

  24. Offline

    the_merciless

    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

    andrewmariocast

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

    TrilisconPvP

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

    the_merciless

    Code:
    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