What Am I doing wrong?

Discussion in 'Plugin Development' started by ArthurMaker, Sep 18, 2013.

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

    ArthurMaker

    I'm trying to make a "/money send" command, but it is not working! D:
    What am I doing wrong?

    Code:java
    1. if(cmd.getName().equalsIgnoreCase("money") || cmd.getName().equalsIgnoreCase("balance")){
    2.  
    3.  
    4.  
    5. if (args.length >= 1) {
    6.  
    7. if (args[0] == "send"){
    8.  
    9. if (args[1] == ""){
    10.  
    11. player.sendMessage(ChatColor.RED + "Correct usage: " + ChatColor.WHITE + "/money send <player> <value>");
    12. player.sendMessage(ChatColor.GRAY + "Please, put the name of the player and a value.");
    13.  
    14. } else {
    15. Player target = Bukkit.getServer().getPlayer(args[1]);
    16. if (args[2] == "" || args[2] == "0"){
    17.  
    18. player.sendMessage(ChatColor.RED + "Correct usage: " + ChatColor.WHITE + "/money send <player> <value>");
    19. player.sendMessage(ChatColor.GRAY + "Put a value to send money to the player " + ChatColor.ITALIC + target.getName());
    20.  
    21. } else {
    22.  
    23. int emeralds_value = Integer.parseInt(args[2]);
    24.  
    25. if (getConfig().contains(target.getName())){
    26. if (getConfig().getInt(player.getName()+".balance") >= emeralds_value){
    27.  
    28. //sender
    29. plugin.getConfig().set(player.getName()+".balance", plugin.getConfig().getInt(player.getName()+".balance") - emeralds_value);
    30. plugin.saveConfig();
    31.  
    32. //receiver
    33. plugin.getConfig().set(target.getName()+".balance", plugin.getConfig().getInt(target.getName()+".balance") + emeralds_value);
    34. plugin.saveConfig();
    35.  
    36. player.sendMessage(ChatColor.GREEN + "You sent " + ChatColor.BLUE + "" + ChatColor.ITALIC + emeralds_value + ChatColor.GREEN + "emerald to " + ChatColor.BLUE + target.getName());
    37. player.playSound(player.getLocation(), Sound.ORB_PICKUP, 20.0F, 5.0F);
    38.  
    39.  
    40. } else {
    41.  
    42. //balance -
    43. player.sendMessage(ChatColor.RED + "Error: " + ChatColor.GRAY + "you don't have all these emeralds to send!");
    44.  
    45. }
    46.  
    47. }else{
    48. //player does not exist
    49. player.sendMessage(ChatColor.RED + "Error: " + ChatColor.GRAY + "player doesn't exist!");
    50.  
    51. }
    52.  
    53. }
    54.  
    55. }
    56.  
    57. } else {
    58.  
    59. //no know command, so, it shows the player's balance:
    60. player.playEffect(player.getLocation(), Effect.CLICK2, 3);
    61. player.playSound(player.getLocation(), Sound.ORB_PICKUP, 20.0F, 5.0F);
    62.  
    63. player.sendMessage(ChatColor.GREEN + "[Balance]: " + ChatColor.GRAY + "" + ChatColor.ITALIC + "" + getConfig().getInt(player.getName()+".balance") + " emeralds.");
    64.  
    65.  
    66. }
    67.  
    68. }else{
    69.  
    70. //args 0 // show only money
    71. player.playEffect(player.getLocation(), Effect.CLICK2, 3);
    72. player.playSound(player.getLocation(), Sound.ORB_PICKUP, 20.0F, 5.0F);
    73.  
    74. player.sendMessage(ChatColor.GREEN + "[Balance]: " + ChatColor.GRAY + "" + ChatColor.ITALIC + "" + getConfig().getInt(player.getName()+".balance") + " emeralds.");
    75.  
    76. }
    77.  
    78.  
    79. }


    Please, help! D:
     
  2. Offline

    1Rogue

    Are you getting any errors, what isn't working?
     
  3. ArthurMaker
    To make a money plugin command you should try using vault!!!
     
  4. Offline

    chasechocolate

    Compare strings using .equals() or .equalsIgnoreCase().
     
  5. Offline

    ArthurMaker

    1Rogue

    I'm not getting errors, I'm getting only the "[Balance]: x emeralds." message :/

    thatdubstepgamer

    I'm not going to use Vault, because it is not necessary to do this...

    chasechocolate

    I'm already doing this :/
     
  6. Offline

    chasechocolate

    ArthurMaker how come you use == in the code you had? Anyways, your code will be run if args.length is 0. It's all logical, I recommend reviewing your code.
     
  7. Offline

    Technius

    No, you aren't.
    Code:
    args[0] == "send"
    The '==' compares if two instances of an object are the same, or if two raw types have the equal value. Since a String is an object, comparing two different String instances will '==' will never return true since they are different instances. You have to use
    Code:
    args[0].equals("send")
    instead, since the String's equals method compares the contents of the String.
     
    Chinwe likes this.
  8. Offline

    JPG2000

    thatdubstepgamer Vaut is for hooking into plugins, not making your own.

    You can't make a money pugin "with vault"
     
  9. Offline

    SkillSam

    Perhaps try this? I was looking into your code, tried to recode it myself to see where the error was. But I agree with Technius about the args portion.

    Code:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("money")) {
    2. if (args.length == 0) {
    3. p.playEffect(p.getLocation(), Effect.CLICK1, Type.SOUND);
    4. p.playSound(p.getLocation(), Sound.ORB_PICKUP, 20.0F, 5.0F);
    5. p.sendMessage(ChatColor.GREEN +"[Balance]: " + ChatColor.BLUE + "" + ChatColor.ITALIC + "" + this.getConfig().getInt(p.getName() + ".balance"));
    6. return true;
    7. }
    8. if (args.length == 1) {
    9. if (args[0].equalsIgnoreCase("send")) {
    10. p.sendMessage(ChatColor.RED + "Error: " + ChatColor.GRAY + "Please Specify a Player.");
    11. return true;
    12. }
    13. else {
    14. p.sendMessage(ChatColor.RED + "Correct usage: " + ChatColor.WHITE + "/money send <player> <value>");
    15. return true;
    16. }
    17. }
    18. if (args.length == 2) {
    19. Player target = Bukkit.getServer().getPlayer(args[1]);
    20. if (target == null) {
    21. p.sendMessage(ChatColor.RED + "Error: " + ChatColor.GRAY + "Invalid player.");
    22. }
    23. }
    24. if (args.length == 3) {
    25. Player target = Bukkit.getServer().getPlayer(args[1]);
    26. int money = Integer.parseInt(args[2]);
    27. if (getConfig().getInt(p.getName() + ".balance") < money) {
    28. p.sendMessage(ChatColor.RED + "You do not have enough emeralds to send!");
    29. return true;
    30. }
    31. //Sender
    32. this.getConfig().set(p.getName() + ".balance", this.getConfig().getInt(p.getName() + ".balance") - money);
    33. this.saveConfig();
    34.  
    35. //Receiver
    36. this.getConfig().set(target.getName() + ".balance", this.getConfig().getInt(target.getName() + ".balance") + money);
    37. this.saveConfig();
    38.  
    39. p.sendMessage(ChatColor.GREEN + "You sent " + ChatColor.BLUE + "" + ChatColor.ITALIC + money + ChatColor.GREEN + "emerald(s) to " + ChatColor.BLUE + target.getName());
    40. p.playSound(p.getLocation(), Sound.ORB_PICKUP, 20.0F, 5.0F);
    41. return true;
    42. }
    43. }
     
Thread Status:
Not open for further replies.

Share This Page