Solved Balance Command

Discussion in 'Spigot Plugin Development' started by Pvpeon, Jun 26, 2018.

Thread Status:
Not open for further replies.
  1. So, I was able to override the Essentials Balance command but anytime a user just enters /bal or it's aliases without an arg it errors. I've tried everything best to my knowledge. Honestly feel this is easy and I'm missing something simple to fix it.
    Command should be able to take no arg and take a arg if necessary.
    Command use would be /balance (Player Name Optional)


    Here's the code
    Code (Text):
    if(cmd.getName().equalsIgnoreCase("balance")) {
                Player target = Bukkit.getPlayerExact(args[0]);
             
                      if(args[0] == null || args[0] == "") {
                        p.sendMessage(colorize(PREFIX + "&aYour current Balance is: &c$" + commas(econ.getBalance(sender.getName()))));
                      }else {
                      if(target != null) {
                          p.sendMessage(colorize(PREFIX + target.getName() + "'s &acurrent Balance is &c$" + commas(econ.getBalance(target.getName()))));
                      }else {
                          p.sendMessage(colorize(PREFIX + "&cUser doesn't seem to be online at the moment!"));
                      }
                  }
              }
     
  2. Dont do this if(args[0] == null || args[0] == "") {
    You use == for ints or boolean not strings.
     
    • Agree Agree x 1
  3. Code (Java):

    if(cmd.getName().equalsIgnoreCase("balance")) {

             // You are not checking if the length is > 0 so as not to get NPE when using args[0]

                Player target = Bukkit.getPlayerExact(args[0]);

                      if(args[0] == null || args[0] == "") { // Totally unnecessary, you shouldn't be using args[0] == "" (Don't compare strings with == or != )

                        p.sendMessage(colorize(PREFIX + "&aYour current Balance is: &c$" + commas(econ.getBalance(sender.getName()))));

                      }else {

                      if(target != null) {

                          p.sendMessage(colorize(PREFIX + target.getName() + "'s &acurrent Balance is &c$" + commas(econ.getBalance(target.getName()))));

                      }else {

                          p.sendMessage(colorize(PREFIX + "&cUser doesn't seem to be online at the moment!"));

                      }
                  }


              }
     
    Try using this:
    Code (Java):

    if(cmd.getName().equalsIgnoreCase("balance")) {

            if (args.length==0){
         // check the player's money
    }
    if (args.length == 1) {
    Player target = bukkit...

    if (target==null) {
    // your code
    return true;
    }
    // check the target's money
    }
     
     
    • Agree Agree x 1
    • Informative Informative x 1
  4. I've tried args.length > 0 and it didn't work either.
     
  5. Could you define didn't work?
     
    • Informative Informative x 1
  6. I just tried what you provided. It still throws and error also when no arg is entered. /balance Pvpeon works fine.
    I remember I have does this once in a old plugin I created but since my Windows was Corrupted due to the Creators Update I had to restart.
     
  7. What kind of error does it throw? Not providing us with detailed info will not allow us to help you further. We won't and can't guess your issue.
     
    • Informative Informative x 1
  8. Well, I fixed it. I made a mistake in not adding Vault as a dependency.
    Thanks for the help!
     
  9. check ur code again.
     
    • Like Like x 1
Thread Status:
Not open for further replies.