Confues about return statement

Discussion in 'Spigot Plugin Development' started by Owner_jomzaza, Jun 18, 2018.

  1. Did i wrong ?

    Code (Java):
    if(args.length == 3) {
                if(sender.hasPermission("TNM.admin")) {
                    if(args[0].equalsIgnoreCase("set")) {
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.prefix + " &cกรุณาใช้คำสั่ง &7/money set <player> <amount> &aเพื่อปรับเปลี่ยนจำนวนเงินของผู้เเล่น"));
                        if(args.length < 2) {
                            player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.prefix + " &cกรุณาใช้คำสั่ง &7/money set <player> <amount> &aเพื่อปรับเปลี่ยนจำนวนเงินของผู้เเล่น"));
                            try {
                                Player target = Bukkit.getPlayer(args[0]);
                                double balance_set = Double.parseDouble(args[1]);
                                if(target == null) {
                                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.noplayerfound.replace("{prefix}", Message.prefix).replace("{player}", target.getName())));
                                } else {
                                    econ.withdrawPlayer(target, econ.getBalance(target));
                                    econ.depositPlayer(target, balance_set);
                                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.moneyset.replace("{prefix}", Message.prefix).replace("{balance}", balance_set + "").replace("{player}", target.getName())));
                                    return false;
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    }
                }
            }
     
  2. Well, what's the problem? Did the stacktrace print? If so, please post a link to it using https://pastebin.com/.
    Not sure what I'm supposed to look for.
     
  3. Yeah if you need a too ask a question you should probably give more information.
     
  4. To begin with you don't check if the sender if instanceof a player so check this using:
    Code (Text):
    if (!(sender instanceof Player)) return;
    Just to help neaten things up it's best to use negative statements as opposed to normal if statements, for example using:
    Code (Text):

    if (!player.hasPermission("permission.use")) {
    player.sendMessage("You don't have permission!");
    return true;
    }
     
    This allows you to easily send invalid permissions messages.
    As for using the if for the args length, perhaps take a look at switch statements? I find that they're incredibly useful for helping to keep code neat, example:
    Code (Text):
    switch(args.length) {
    case 1:
            player.sendMessage("args Length is 1");
            break;
    case 2:
            player.sendMessage("args Length is 2");
            break;
    default:
            player.sendMessage("invalid argument length");
            break;
     
     
    • Agree Agree x 3
  5. All of my thing, I want to do like /money set <name> <amount> to set a money for player

    This is all of my code (i use commandexecutor on main class)

    Code (Java):
     @Override
            public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
            if (!(sender instanceof Player)) {
                Log.info(ChatColor.translateAlternateColorCodes('&', Message.prefix + " &cOnly players are supported for this Example Plugin, but you should not do this!!!"));
                return true;
            }  
            Player player = (Player) sender;
            if (args.length == 0) {
                double balance = (double) econ.getBalance(player);
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.balance.replace("{prefix}", Message.prefix).replace("{balance}", balance + "")));
            } else if(args[0].equalsIgnoreCase("set")) {
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.prefix + " &cกรุณาใช้คำสั่ง &7/money set <player> <amount> &aเพื่อปรับเปลี่ยนจำนวนเงินของผู้เเล่น"));
                if(args.length == 3) {
                    Player target = Bukkit.getPlayer(args[1]);
                    double balance_set = Double.parseDouble(args[2]);
                    switch(args.length) {
                    case 0:
                      player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.prefix + " &cกรุณาใช้คำสั่ง &7/money set <player> <amount> &aTEST"));
                      break;
                    case 1:
                        if (target == null) {
                            player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.noplayerfound.replace("{prefix}", Message.prefix).replace("{player}", target.getName())));
                            return true;
                        }
                        econ.withdrawPlayer(target, econ.getBalance(target));
                        econ.depositPlayer(target, balance_set);
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.moneyset.replace("{prefix}", Message.prefix).replace("{balance}", balance_set + "").replace("{player}", target.getName())));
                        break;  
                    }
                }
                return true;
            }
           
           
           
            return false;

            }        
     
  6. Code (Java):

    @Override
       public boolean onCommand(CommandSender cs, Command cmd, String lb, String[] args) {
           Player player = (Player) cs;
           if (args.length < 1) {
               // whatever you wish (it's length == 0) meaning the player typed just the
               // command.
               return true;
           }
    // /money set <target> <amount>
    // args[x] 0     1       2
    // == 0    1     2       3
           String target = args[1]; // our target player.
           String amount = args.length == 3 ? args[2] : null; // if length is 3 meaning an amount has been set then allow it to return as args[2] else null.
           if (amount == null) {
               // if true then something's missing.
               // checking if the player typed what ever is necessary. (the command, set, a
               // player and the amount)
               return true;
           }

           if (!args[0].equalsIgnoreCase("set")) {
               return true;
           }
           Player targetp = Bukkit.getPlayer(target);

           if (targetp == null){
               // player is null (offline?)
               return true;
    }
           double balance_set;
           try {
               balance_set = Double.valueOf(amount);
           } catch (Exception e) {
               // not a double?
               return true;
           }
           // double playerbalance = vault.getBalance(player);
           // vault.withdraw(player, balance_set-playerbalance);
    return false;
       }

     
    I think this is enough help. You'll easily understand what's happening here. You gotta work a little though since this is not the full code.
     
    #6 darklazerog, Jun 19, 2018
    Last edited: Jun 19, 2018
  7. Always return true unless you want the registered command usage (in plugin.yml) to show in chat.
     
  8. Well since the OP had return false on his code I assumed he wanted it this way, hence why I am returning false as well. Also, if I remember correctly, not adding a usage on your command in plugin.yml will result in showing the label of the command or the command's name in the chat. Not 100% sure though.
     
  9. OK. Now solved, thanks you guy but i have a little question about

    if player type /pay and it will send player to /money pay

    My new Code

    Code (Java):
    if(args[0].equalsIgnoreCase("set")) {
                if(sender.hasPermission("TNM.admin")) {
                   if(args.length == 3) {
                    String target = args[1];
                    Player targetp = Bukkit.getPlayer(target);
                    Double balance_set = Double.valueOf(args[2]);
                    try {
                        if (targetp == null) {
                            player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.noplayerfound.replace("{prefix}", Message.prefix).replace("{player}", target)));
                            return true;
                        }
                        econ.withdrawPlayer(targetp, econ.getBalance(targetp));
                        econ.depositPlayer(targetp, balance_set);
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.moneyset.replace("{prefix}", Message.prefix).replace("{balance}", balance_set + "").replace("{player}", target)));
                    } catch (Exception e) {
                        return true;
                    }
                } else {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.prefix + " &cกรุณาใช้คำสั่ง &7/money set <player> <amount> &aเพื่อเปลี่ยนจำนวนเงินของผู้เล่น"));
                    return true;
                }
                } else {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message.noPerm.replace("{prefix}", Message.prefix)));
                    return true;
                }
            }
     
  10. your args[2] will throw an numberformatexception if it isn't indeed a double. add a try catch on it.
    Also you can simply do
    Code (Java):

    econ.depositPlayer(targetp, balance_set-econ.getBalance(targetp));
     
    and this will set the new balance to the player.
     
  11. Here's a tip, learn some java first.
    This is not meant to be aggresive but just a helpful comment
     
    • Funny Funny x 1