Solved If statement help

Discussion in 'Plugin Development' started by moe097, Jul 25, 2014.

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

    moe097

    I am making a kit pvp plugin on my server. This is how I want the commands to work:
    /KitPvP Berserker
    If they have bought the kit already, then give it to the player. If not say:
    "You do not have this kit! Type: '/BuyKit Berserker' to buy the kit!"

    /BuyKit Berserker
    If they have enough money take away 200 MineCoins, and say this to the player:
    "You have bought the Berserker kit with 200 MineCoins! Type: '/KitPvP Berserker' to get the kit!"
    If they don't have enough money say this:
    "You do not have enough MineCoins to buy this kit!"

    This is what its doing and I cant figure out why:
    It try's to buy the kit whenever I type /KitPvP Berserker.
    So If I don't have the kit and I don't have enough money it says this:
    Code:
    "You do not have this kit!  Type:  '/BuyKit Berserker' to buy the kit!" 
    "You do not have enough MineCoins to buy this kit!" 
    If I do have enough money it buys the kit for me. I think its a problem with one of my if statements but I don't know what.

    Here is the code:
    Code:java
    1. public boolean berserker = false; // This is not actually right here it is above my onEnable() but I didn't want to paste the whole thing
    2. if(args[0].equalsIgnoreCase("Berserker") && (berserker)) {
    3.  
    4. String playername = player.getName();
    5.  
    6. ItemStack helm = new ItemStack(Material.CHAINMAIL_HELMET);
    7. ItemStack chest = new ItemStack(Material.CHAINMAIL_CHESTPLATE);
    8. ItemStack leg = new ItemStack(Material.LEATHER_LEGGINGS);
    9. ItemStack boots = new ItemStack(Material.LEATHER_BOOTS);
    10.  
    11. ItemStack sword = new ItemStack(Material.IRON_AXE);
    12. ItemMeta meta1 = sword.getItemMeta();
    13. meta1.setDisplayName(playername + " Sword");
    14.  
    15. Potion potion = new Potion(PotionType.STRENGTH, 1, false, false);
    16. ItemStack potionstack = potion.toItemStack(3);
    17.  
    18. player.getInventory().clear();
    19.  
    20. player.sendMessage(ChatColor.RED + "You have chosen the Berserker Kit!");
    21.  
    22. player.setItemInHand(sword);
    23. player.getInventory().addItem(helm);
    24. player.getInventory().addItem(chest);
    25. player.getInventory().addItem(leg);
    26. player.getInventory().addItem(boots);
    27. player.getInventory().addItem(potionstack);
    28.  
    29. }else {
    30. player.sendMessage(ChatColor.DARK_RED + "You do not have this kit! You can buy it by typing: /BuyKit Berserker");
    31. }
    32. if(args[0].equalsIgnoreCase("Help")) {
    33. player.sendMessage(ChatColor.GREEN + "Commands: /KitPvP <Kit> - Chooses a kit - /KitPvP Kits - Displays all kits and kits you can buy - /BuyKit <Kit> - Buys the selected kit!");
    34. return true;
    35. }
    36. if(args[0].equalsIgnoreCase("Kits")) {
    37. player.sendMessage(playerkits);
    38. return true;
    39. }
    40. if(cmd.getName().equalsIgnoreCase("BuyKit")) {
    41.  
    42. }
    43. if(args[0].equalsIgnoreCase("Berserker") && economy.getBalance(pn) == berserkerprice && pb == 200 && !(pb < 200)) {
    44.  
    45. EconomyResponse er = economy.withdrawPlayer(pn, berserkerprice);
    46. if(er.transactionSuccess()) {
    47. berserker = true;
    48. playerkits = "Your Kits: Archer, PvP, Berserker";
    49. player.sendMessage(ChatColor.GREEN + "You have bought the Berserker Kit for: 200 MineCoins! - Type '/KitPvP Berserker' to get your kit!");
    50. }
    51. }
    52. else if(pb < 200 && pb < 200){
    53. player.sendMessage(ChatColor.RED + "You don't have enough MineCoins to buy this kit!");
    54. }


    Pastebin Code: http://pastebin.com/9umqjkgx

    I get no error.

    Sorry if this was unclear, I didn't really know how to explain the problem.
    Thanks!
     
  2. Offline

    DannyDog

    Line 40.
    You need to have if(args[0]... nested inside the check for "BuyKit".
     
  3. Offline

    DevilMental

    moe097 Also what if the player has 201 Minecoins, 340 ? The berserker kit would not work because in the if statement, you wrote that the kit could be given if the player just had 200Minecoins.And why checking two times nb < 200, and the money == 200 and !money < 200 ? You need to organize your code, the kit should be in a method, not in the command method !
     
  4. Offline

    moe097

    DannyDog

    Thanks it fixed it kinda, whenever I type: "/BuyKit Beserker" and I don't have enough money, It says:
    "You do not have this kit..."
    "You don't have enough MineCoins..."

    Here is my new code just in case I did it wrong (I don't use nesting usually, so it's likely):
    Code:java
    1. if(args[0].equalsIgnoreCase("Berserker") && (berserker)) {
    2.  
    3. String playername = player.getName();
    4.  
    5. ItemStack helm = new ItemStack(Material.CHAINMAIL_HELMET);
    6. ItemStack chest = new ItemStack(Material.CHAINMAIL_CHESTPLATE);
    7. ItemStack leg = new ItemStack(Material.LEATHER_LEGGINGS);
    8. ItemStack boots = new ItemStack(Material.LEATHER_BOOTS);
    9.  
    10. ItemStack sword = new ItemStack(Material.IRON_AXE);
    11. ItemMeta meta1 = sword.getItemMeta();
    12. meta1.setDisplayName(playername + " Sword");
    13.  
    14. Potion potion = new Potion(PotionType.STRENGTH, 1, false, false);
    15. ItemStack potionstack = potion.toItemStack(3);
    16.  
    17. player.getInventory().clear();
    18.  
    19. player.sendMessage(ChatColor.RED + "You have chosen the Berserker Kit!");
    20.  
    21. player.setItemInHand(sword);
    22. player.getInventory().addItem(helm);
    23. player.getInventory().addItem(chest);
    24. player.getInventory().addItem(leg);
    25. player.getInventory().addItem(boots);
    26. player.getInventory().addItem(potionstack);
    27.  
    28. }else {
    29. player.sendMessage(ChatColor.DARK_RED + "You do not have this kit! You can buy it by typing: /BuyKit Berserker");
    30. }
    31. if(args[0].equalsIgnoreCase("Help")) {
    32. player.sendMessage(ChatColor.GREEN + "Commands: /KitPvP <Kit> - Chooses a kit - /KitPvP Kits - Displays all kits and kits you can buy - /BuyKit <Kit> - Buys the selected kit!");
    33. return true;
    34. }
    35. if(args[0].equalsIgnoreCase("Kits")) {
    36. player.sendMessage(playerkits);
    37. return true;
    38. }
    39. if(cmd.getName().equalsIgnoreCase("BuyKit")) {
    40.  
    41. if(args[0].equalsIgnoreCase("Berserker") && economy.getBalance(pn) == berserkerprice && pb == 200 && !(pb < 200)) {
    42.  
    43. EconomyResponse er = economy.withdrawPlayer(pn, berserkerprice);
    44. if(er.transactionSuccess()) {
    45. berserker = true;
    46. playerkits = "Your Kits: Archer, PvP, Berserker";
    47. player.sendMessage(ChatColor.GREEN + "You have bought the Berserker Kit for: 200 MineCoins! - Type '/KitPvP Berserker' to get your kit!");
    48. }
    49. }
    50. else if(pb < 200 && pb < 200 && !(berserker)){
    51. player.sendMessage(ChatColor.RED + "You don't have enough MineCoins to buy this kit!");
    52. }
    53. }



    DevilMental
    I wanted it to be only 200 for a reason. I did it twice for my own reason that would take to long to explain.
    Yes you are right, the code is very un-organized and formatted terribly, I was in a hurry when I wrote this.

    I don't really like organization, I kinda just know where everything is now.

    I have fixed the problem. Thanks Everyone!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page