Plugin Help (Solved) problems

Discussion in 'Plugin Help/Development/Requests' started by Randomguy, Jul 26, 2015.

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

    Randomguy

    Hi, I have some problems with my plugin. Code:
    Code:
    package com.Zach.Soundmaker;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Sound;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.Command;
    import org.bukkit.entity.Player;
    
    public class Main extends JavaPlugin { 
           
            public void onEnable() {
                getLogger().info("Soundmaker 3000 playing sounds successfully!! Oh no! It's a zombie! Oh wait thats just this plugin...");
            }
            public void onDisable() {
                getLogger().info("Soundmaker 3000 shutting dowwwwwwn");
            }
            public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args ) {
            if(commandLabel.equalsIgnoreCase("GhastSound")) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length > 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments. /GhastSound (Player). Try /Soundmaker help for more help!");
                }
                else if (args.length == 0) {
                int volume = 1;
                float pitch = 1.0f;
                target.playSound(player.getLocation(), Sound.GHAST_SCREAM, volume, pitch );
                }
                return true;
            }
            if(commandLabel.equalsIgnoreCase("ZombieSound")) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length > 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments. /ZombieSound (Player). Try /Soundmaker help for more help!");
                }
                else if (args.length == 0) {
                int volume = 1;
                float pitch = 1.0f;
                target.playSound(player.getLocation(), Sound.ZOMBIE_IDLE, volume, pitch );
                }
                return true;
            }
           
            if(commandLabel.equalsIgnoreCase("SkeletonSound")) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length > 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments. /SkeletonSound (Player). Try /Soundmaker help for more help!");
                }
                else if (args.length == 0) {
                int volume = 1;
                float pitch = 1.0f;
                target.playSound(player.getLocation(), Sound.SKELETON_IDLE, volume, pitch );
                }
                return true;
            }
            if(commandLabel.equalsIgnoreCase("SpiderSound")) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length > 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments. /SpiderSound (Player). Try /Soundmaker help for more help!");
                }
                else if (args.length == 0) {
                int volume = 1;
                float pitch = 1.0f;
                target.playSound(player.getLocation(), Sound.SPIDER_IDLE, volume, pitch );
                }
                return true;
            }
            if(commandLabel.equalsIgnoreCase("SlimeSound")) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length > 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments. /SlimeSound (Player). Try /Soundmaker help for more help!");
                }
                else if (args.length == 0) {
                int volume = 1;
                float pitch = 1.0f;
                target.playSound(player.getLocation(), Sound.SLIME_WALK, volume, pitch );
                }
                return true;
            }
           
            if(commandLabel.equalsIgnoreCase("BlazeSound")) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length > 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments. /BlazeSound (Player). Try /Soundmaker help for more help!");
                }
                else if (args.length == 0) {
                int volume = 1;
                float pitch = 1.0f;
                target.playSound(player.getLocation(), Sound.BLAZE_BREATH, volume, pitch );
                }
                return true;
            }
            if(commandLabel.equalsIgnoreCase("EnderDragonSound")) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length > 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments. /EnderDragonSound (Player). Try /Soundmaker help for more help!");
                }
                else if (args.length == 0) {
                int volume = 1;
                float pitch = 1.0f;
                target.playSound(player.getLocation(), Sound.ENDERDRAGON_GROWL, volume, pitch );
                }
                return true;
            }
            if(commandLabel.equalsIgnoreCase("EndermanSound")) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length > 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments. /EndermanSound (Player). Try /Soundmaker help for more help!");
                }
                else if (args.length == 0) {
                int volume = 1;
                float pitch = 1.0f;
                target.playSound(player.getLocation(), Sound.ENDERMAN_SCREAM, volume, pitch );
                target.playSound(player.getLocation(), Sound.ENDERMAN_TELEPORT, volume, pitch );
                }
                return true;
            }
            if(commandLabel.equalsIgnoreCase("SilverfishSound")) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length > 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments. /SilverfishSound (Player). Try /Soundmaker help for more help!");
                }
                else if (args.length == 0) {
                int volume = 1;
                float pitch = 1.0f;
                target.playSound(player.getLocation(), Sound.SILVERFISH_WALK, volume, pitch );
                }
                return true;
            }
            if(commandLabel.equalsIgnoreCase("MagmaCubeSound")) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length > 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments. /MagmaCubeSound (Player). Try /Soundmaker help for more help!");
                }
                else if (args.length == 0) {
                int volume = 1;
                float pitch = 1.0f;
                target.playSound(player.getLocation(), Sound.MAGMACUBE_JUMP, volume, pitch );
                }
                return true;
            }
            if(commandLabel.equalsIgnoreCase("CreeperSound")) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length > 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments. /CreeperSound (Player). Try /Soundmaker help for more help!");
                }
                else if (args.length == 0) {
                int volume = 1;
                float pitch = 1.0f;
                target.playSound(player.getLocation(), Sound.CREEPER_HISS, volume, pitch );
                target.playSound(player.getLocation(), Sound.EXPLODE, volume, pitch );
                }
                return true;
            }
            if(commandLabel.equalsIgnoreCase("WitherSound")) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length > 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments. /WitherSound (Player). Try /Soundmaker help for more help!");
                }
                else if (args.length == 0) {
                int volume = 1;
                float pitch = 1.0f;
                target.playSound(player.getLocation(), Sound.WITHER_SPAWN, volume, pitch );
                target.playSound(player.getLocation(), Sound.WITHER_SHOOT, volume, pitch );
                }
                return true;
            }
            if (commandLabel.equalsIgnoreCase("Soundmaker") && sender instanceof Player) {
                Player player = (Player) sender;
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getPlayerExact(args[0]);
                if(target == null) {
                    player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
                }
                if(args.length >= 1) {
                    player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too many/little arguments!");
                    }
                else if(args.length == 0) {
                if(args[0].equalsIgnoreCase("Help")); {
                player.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "Welcome To Soundmaker 3000, the ultimate trolling plugin! By ZachAttack1170!!");
                player.sendMessage(ChatColor.YELLOW + "Type /(Sound) (Player) to start!"
                                        + ChatColor.YELLOW + "\n" + ChatColor.BOLD + "Options:"
                                        + ChatColor.YELLOW + "\n* /GhastSound"
                                        + ChatColor.YELLOW + "\n* /CreeperSound"
                                        + ChatColor.YELLOW + "\n* /WitherSound"
                                        + ChatColor.YELLOW + "\n* /MagmaCubeSound"
                                        + ChatColor.YELLOW + "\n* /SilverfishSound"
                                        + ChatColor.YELLOW + "\n* /EndermanSound"
                                        + ChatColor.YELLOW + "\n* /EnderDragonSound"
                                        + ChatColor.YELLOW + "\n* /BlazeSound"
                                        + ChatColor.YELLOW + "\n* /SlimeSound"
                                        + ChatColor.YELLOW + "\n* /SkeletonSound"
                                        + ChatColor.YELLOW + "\n* /ZombieSound"
                                        + ChatColor.YELLOW + "\n* /SpiderSound");                                           
                }
                    }
                return true;
            }
            return false;
        }
    }
    this is what happens...
    prints too many args and player offline if not enough args...if player not on it gives too many/little args. Doing correct command prints out too many/little args. /Soundmaker gives a arrayindexoutofboundsexception: 0 error. and /Soundmaker help gives too many/little args and that player is either offline or doesnt exist.

    I know this is a lot but I learn from my mistakes and am learning Java currently... I am learning Java but for bukkit problems I come here... TY for taking time to look over this... I really appreciate it!

    plz help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  2. Offline

    Ruptur

    @Randomguy
    Well where to start ...

    Lets identify whats wrong:
    1. You havnt register the command executor so i dont quite understand how the commands work
    2. Your order is entirely wrong. just wrong ...
    3. Your checks are likely to throw errors so they fail as checks
    4. You could have done everythin in 50+ lines shorter
    5. Returning false will relay the command back to the sender.

    Then to adress the problems and find a solution:
    1. Register the command in onEnable()
    Code:java
    1.  
    2. public void onEnable() {
    3. getCommand("acommand").setExecutor(this);
    4. getCommand("soundplayer").setExecutor(this);
    5. // .... more commands here ...
    6. }
    7.  


    2. You will need to check if the args length is equal to or more than 1(not 0) before checking if the player is online
    Code:java
    1.  
    2. if(args.length != 1) {
    3. player.sendMessage(ChatColor.RED + "Invalid number of arguments!");
    4. }
    5.  
    6. Player player = getPlayer(args[0]);
    7.  


    3. Dont use getPlayerExact because it deprecated instead you can use getPlayer which is more or less the same thing
    Code:java
    1.  
    2. Player target = Bukkit.getPlayer(args[0]);
    3.  
    4. if(target == null) {
    5. player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
    6. return true;
    7. }
    8.  


    4. You dont need to check if the commandLabal is xxx at the start instead only check it when you are getting to the playsound bit
    An example of this is:
    Code:java
    1.  
    2. if(commandLabel.equalsIgnoreCase("soundplayer")) {
    3. // do something
    4. return true;
    5. }
    6.  
    7. if(args.length != 1) {
    8. player.sendMessage(ChatColor.RED + "Invalid number of arguments!");
    9. }
    10.  
    11. Player target = Bukkit.getPlayer(args[0]);
    12.  
    13. if(target == null) {
    14. player.sendMessage(ChatColor.RED + "That player is either offline or doesn't exist!!");
    15. return true;
    16. }
    17.  
    18. float volume = 1.0f;
    19. float pitch = 1.0f;
    20.  
    21. if(commandLabel.equalsIgnoreCase("endermansound")) {
    22. target.playSound(player.getLocation(), Sound.ENDERMAN_SCREAM, volume, pitch);
    23. target.playSound(player.getLocation(), Sound.ENDERMAN_TELEPORT, volume, pitch);
    24.  
    25. } else if (commandLabel.equalsIgnoreCase("enderdragonsound")) {
    26. target.playSound(player.getLocation(), Sound.ENDERDRAGON_GROWL, volume, pitch);
    27. } else if (commandLabel.equalsIgnoreCase("blazesound")) {
    28. target.playSound(player.getLocation(), Sound.BLAZE_BREATH, volume, pitch);
    29. } else if(something) {
    30. // do something
    31. } else if (something) {
    32. // You get the drift
    33. }
    34.  


    5. Dont return false.

    Hope i helped, leave a like if you feel i did :)
     
    Last edited: Jul 27, 2015
    Randomguy likes this.
  3. Offline

    Randomguy

    TY SO MUCH
     
Thread Status:
Not open for further replies.

Share This Page