Help with command Arguments

Discussion in 'Plugin Development' started by ChristolisTV, May 18, 2016.

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

    ChristolisTV

    Hello there! I have a problem with my code can you help me please? I want a command (/setgroup [Player] [Group]) where you can make a player move into another group. Here is what I tried.

    Code:java
    1.  
    2. @SuppressWarnings("deprecation")
    3. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    4. String ranks[] = {
    5. "Default",
    6. "MVP",
    7. "VIP",
    8. "Moderator",
    9. "Admin",
    10. "Owner",
    11. "Developer"
    12. };
    13. if(commandLabel.equalsIgnoreCase("setgroup")) {
    14. if(args.length != 0) {
    15. Player targetPlayer = Bukkit.getPlayer(args[0]);
    16. if(args.length == 1 && targetPlayer != null) {
    17. if(args[1].equals(ranks)) {
    18. try {
    19. Connection conn = DriverManager.getConnection(url,user,pass);
    20. Statement st = conn.createStatement();
    21. ResultSet rs = st.executeQuery("SELECT * FROM `db_9fd6c2_pdata`.`hubstats`;");
    22. if(rs.next()) {
    23. st.executeUpdate("UPDATE `hubstats` SET rank='"+ranks+"' WHERE name='"+targetPlayer+"';");
    24. return true;
    25. } else {
    26. sender.sendMessage(ChatColor.YELLOW + "Seems that the player can't be found!");
    27. }
    28. } catch(Exception e) {
    29. sender.sendMessage(ChatColor.DARK_RED + "Failed to update" + targetPlayer + "'s rank! If you believe its an error, contact Christolis, the Developer of this server!");
    30. System.out.println("["+sender+"] Failed to update " + targetPlayer + "'s rank! Printing stacktrace:");
    31. e.printStackTrace();
    32. }
    33. } else if(!args[1].equals(ranks)) {
    34. sender.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.WHITE + "/setgroup [Player] [Group]");
    35. sender.sendMessage(ChatColor.YELLOW + "You have to put a correct rank! To see a list of ranks type " + ChatColor.AQUA + "/ranklist" + ChatColor.YELLOW + "!");
    36. return false;
    37. }
    38. } else if(targetPlayer == null) {
    39. sender.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.WHITE + "/setgroup [Player] [Group]");
    40. sender.sendMessage(ChatColor.YELLOW + "Seems that this player is not connected or you typed it incorrectly!");
    41. return false;
    42. }
    43. } else if(args.length == 0) {
    44. sender.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.WHITE + "/setgroup [Player] [Group]");
    45. return false;
    46. }
    47. }
    48. return false;
    49. }
    50.  

    Help me please? :(
     
  2. You can't compare a string to an array like that. Loop through the array contents and check if the current string is equalsIgnoreCase the arg[1] and at the end return or break from the loop
     
  3. Offline

    ChristolisTV

    Tried but it didn't work. My code:
    Code:java
    1.  
    2. @SuppressWarnings("deprecation")
    3. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    4. Player targetPlayer = Bukkit.getPlayer(args[0]);
    5. String[] ranks = {
    6. "Default",
    7. "MVP",
    8. "VIP",
    9. "Moderator",
    10. "Admin",
    11. "Owner",
    12. "Developer"
    13. };
    14. if(commandLabel.equalsIgnoreCase("setgroup")) {
    15. if(args.length != 0) {
    16. if(args.length == 1 && targetPlayer != null) {
    17. for(String s : ranks) {
    18. if(s.equalsIgnoreCase("Default")) {
    19. try {
    20. Connection conn = DriverManager.getConnection(url,user,pass);
    21. Statement st = conn.createStatement();
    22. ResultSet rs = st.executeQuery("SELECT * FROM `db_9fd6c2_pdata`.`hubstats`;");
    23. if(rs.next()) {
    24. st.executeUpdate("UPDATE `hubstats` SET rank='"+ranks+"' WHERE name='"+targetPlayer+"';");
    25. return true;
    26. } else {
    27. sender.sendMessage(ChatColor.YELLOW + "Seems that the player can't be found!");
    28. }
    29. } catch(Exception e) {
    30. sender.sendMessage(ChatColor.DARK_RED + "Failed to update" + targetPlayer + "'s rank! If you believe its an error, contact Christolis, the Developer of this server!");
    31. System.out.println("["+sender+"] Failed to update " + targetPlayer + "'s rank! Printing stacktrace:");
    32. e.printStackTrace();
    33. }
    34. } else if(!args[1].equals(ranks)) {
    35. sender.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.WHITE + "/setgroup [Player] [Group]");
    36. sender.sendMessage(ChatColor.YELLOW + "You have to put a correct rank! To see a list of ranks type " + ChatColor.AQUA + "/ranklist" + ChatColor.YELLOW + "!");
    37. return false;
    38. } else if(targetPlayer == null) {
    39. sender.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.WHITE + "/setgroup [Player] [Group]");
    40. sender.sendMessage(ChatColor.YELLOW + "Seems that this player is not connected or you typed it incorrectly!");
    41. return false;
    42. }
    43. }
    44. } else if(args.length == 0) {
    45. sender.sendMessage(ChatColor.GOLD + "Usage: " + ChatColor.WHITE + "/setgroup [Player] [Group]");
    46. return false;
    47. }
    48. }
    49. }
    50. return false;
    51. }
    52.  
     
  4. Offline

    WolfMage1

    @ChristolisTV
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            String ranks[]={"Default", "MVP", "VIP", "Moderator", "Admin", "Owner", "Developer"};
            if(commandLabel.equalsIgnoreCase("setgroup")){ //Use cmd.getName() instead to support aliases.
                if(args.length!=0){ //I'd use if(args.length < 2) send message "Insufficient args."
                    Player targetPlayer=Bukkit.getPlayer(args[0]);
                    if(args.length==1&&targetPlayer!=null){
                        if(args[1].equals(ranks)){ //This is always going to be false (afaik)
                                                                //args[1].equalsIgnoreCase(rank); as @FisheyLP said.
                            try{ //No, just no. Use AsyncTasks for sending/retrieving data from mysql.
                                Connection conn=DriverManager.getConnection(url, user, pass); //I'd just create an sql class and use an existing connection, or better yet use connection caching (HikariCP) if it's for a massive project.
                                Statement st=conn.createStatement(); //Use preparedstatements instead to avoid sql injection.
                                ResultSet rs=st.executeQuery("SELECT * FROM `db_9fd6c2_pdata`.`hubstats`;"); //You're selecting everything in your database which if you have a lot of data, it's going to take a while, add a where clause with whatever is the primary key.
                                if(rs.next()){
                                    st.executeUpdate("UPDATE `hubstats` SET rank='"+ranks+"' WHERE name='"+targetPlayer+"';"); //You're setting the name equal to a player, not the players name. and use the value you got from the for loop,
                                    return true;
                                }
                                else{
                                    sender.sendMessage(ChatColor.YELLOW+"Seems that the player can't be found!");
                                }
                            }
                            catch(Exception e){
                                sender.sendMessage(ChatColor.DARK_RED+"Failed to update"+targetPlayer+"'s rank! If you believe its an error, contact Christolis, the Developer of this server!"); //again, use targetPlayer.getName()
                                System.out.println("["+sender+"] Failed to update "+targetPlayer+"'s rank! Printing stacktrace:");
                                e.printStackTrace(); //and again, targetPlayer.getName() and sender.getName().
                            }
                        }
                        else if(!args[1].equals(ranks)){
                            sender.sendMessage(ChatColor.GOLD+"Usage: "+ChatColor.WHITE+"/setgroup [Player] [Group]");
                            sender.sendMessage(ChatColor.YELLOW+"You have to put a correct rank! To see a list of ranks type "+ChatColor.AQUA+"/ranklist"+ChatColor.YELLOW+"!");
                            return false;
                        }
                    }
                    else if(targetPlayer==null){
                        sender.sendMessage(ChatColor.GOLD+"Usage: "+ChatColor.WHITE+"/setgroup [Player] [Group]");
                        sender.sendMessage(ChatColor.YELLOW+"Seems that this player is not connected or you typed it incorrectly!");
                        return false;
                    }
                }
                else if(args.length==0){
                    sender.sendMessage(ChatColor.GOLD+"Usage: "+ChatColor.WHITE+"/setgroup [Player] [Group]");
                    return false;
                }
            }
            return false;
        }
    There's so so so so so much that can be improved with your code but. here's the extremely obvious things.

    Oh and [arg] is optional, <arg> is a requirement, at least that's what people go by.
     
  5. And another reason to learn java before bukkit -.-

    You need to compare the argument to the current String s
     
    Zombie_Striker and WolfMage1 like this.
  6. Offline

    MadMaxCookie

    @ChristolisTV like what @FisheyLP said you made it wrong try to revise your code :D you're just confused

    compare the 2 codes of yours
    the new ones and the old ones

    first it says

    if the args[0] is equals to ranks array { // does something

    currently

    if the ranks array. to string is equalsignorecase default :(

    do you know anything about the difference ?
     
Thread Status:
Not open for further replies.

Share This Page