Solved Command Problem

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

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

    moe097

    I am making a plugin where the players types:
    /BuyKit Berserker - To buy the kit.
    /KitPvP Berserker - To select the kit after they bought it.
    And I do the same commands except replace "Berserker" with rusher, but whenever I type:
    /BuyKit Rusher, It buys the Berserker kit.

    I am using Vault 1.4.1,
    and the Bukkit API.

    I get no errors in console.

    Here is my code (Sorry for the un-organized code, I kinda wrote this in a hurry, sorry):
    Code:java
    1. // Kit you must buy
    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. player.getEquipment().clear();
    20.  
    21. player.sendMessage(ChatColor.RED + "You have chosen the Berserker Kit!");
    22.  
    23. player.setItemInHand(sword);
    24. player.getInventory().addItem(helm);
    25. player.getInventory().addItem(chest);
    26. player.getInventory().addItem(leg);
    27. player.getInventory().addItem(boots);
    28. player.getInventory().addItem(potionstack);
    29.  
    30.  
    31. return true;
    32. }
    33.  
    34. }
    35. //Kit you must buy
    36. if(args[0].equalsIgnoreCase("Rusher") && (rusher)) {
    37.  
    38. String playername = player.getName();
    39.  
    40. ItemStack helm = new ItemStack(Material.GOLD_HELMET);
    41. ItemStack chest = new ItemStack(Material.GOLD_CHESTPLATE);
    42. ItemStack leg = new ItemStack(Material.LEATHER_LEGGINGS);
    43. ItemStack boots = new ItemStack(Material.LEATHER_BOOTS);
    44.  
    45. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    46. ItemMeta meta1 = sword.getItemMeta();
    47. meta1.setDisplayName(playername + " Sword");
    48.  
    49. Potion potion = new Potion(PotionType.SPEED, 1, false, false);
    50. ItemStack potionstack = potion.toItemStack(3);
    51.  
    52. player.getInventory().clear();
    53. player.getEquipment().clear();
    54.  
    55. player.sendMessage(ChatColor.RED + "You have chosen the Rusher Kit!");
    56.  
    57. player.setItemInHand(sword);
    58. player.getInventory().addItem(helm);
    59. player.getInventory().addItem(chest);
    60. player.getInventory().addItem(leg);
    61. player.getInventory().addItem(boots);
    62. player.getInventory().addItem(potionstack);
    63.  
    64. return true;
    65.  
    66. }
    67.  
    68. if(args[0].equalsIgnoreCase("Help")) {
    69. 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!");
    70. return true;
    71. }
    72. if(args[0].equalsIgnoreCase("Kits")) {
    73. player.sendMessage(ChatColor.GREEN + playerkits);
    74. return true;
    75. }
    76. if(args[0].equalsIgnoreCase("Food")) {
    77. ItemStack food = new ItemStack(Material.BREAD);
    78. food.setAmount(64);
    79. player.getInventory().addItem(food);
    80. player.sendMessage(ChatColor.GREEN + "You have been given 64 loafs of bread!");
    81. }
    82. if(cmd.getName().equalsIgnoreCase("BuyKit")) {
    83.  
    84. if(args[0].equalsIgnoreCase("Berserker") && economy.getBalance(pn) == berserkerprice && pb == 200 && !(pb < 200) || pb >= 200) {
    85. EconomyResponse er = economy.withdrawPlayer(pn, berserkerprice);
    86. if(er.transactionSuccess()) {
    87.  
    88. berserker = true;
    89.  
    90. if((rusher)) {
    91. playerkits = "Your Kits: Archer, PvP, Rusher, Berserker";
    92. } else if(!(rusher)) {
    93. playerkits = "Your Kits: Archer, PvP, Berserker";
    94. }
    95.  
    96. player.sendMessage(ChatColor.GREEN + "You have bought the Berserker Kit for: 200 MineCoins! - Type '/KitPvP Berserker' to get your kit!");
    97. }
    98. return true;
    99.  
    100. }
    101.  
    102. if(args[0].equalsIgnoreCase("Rusher") && economy.getBalance(pn) == rusherprice && pb == 400 && !(pb < 400) || pb >= 400) {
    103.  
    104. EconomyResponse er = economy.withdrawPlayer(pn, rusherprice);
    105. if(er.transactionSuccess()) {
    106. rusher = true;
    107. if((berserker)) {
    108. playerkits = "Your Kits: Archer, PvP, Berserker, Rusher";
    109. } else if(!(berserker)) {
    110. playerkits = "Your Kits: Archer, PvP, Rusher";
    111. }
    112. player.sendMessage(ChatColor.BLUE + "You have bought the Rusher Kit for: 400 MineCoins! - Type '/KitPvP Rusher' to get your kit!");
    113. }
    114.  
    115. return true;
    116. }
    117. }


    Pastebin code: http://pastebin.com/VV1zvJiJ

    That is the code for the commands. The "playerkits" String is somewhere else, that is not the problem (I did'nt feel like posting the whole code.)

    Thanks!
     
  2. Offline

    Traks

    The logical conditional-AND has a higher precedence than the logical conditional-OR in Java. So in a statement with both these operators, Java will first get the boolean value of all subsequent AND statements and afterwards combine it with the ORs, if no brackets are used. However, if brackets are used, the boolean value within them will be calculated primarily.
    Code:java
    1. if(args[0].equalsIgnoreCase("Berserker") && economy.getBalance(pn) == berserkerprice && pb == 200 && !(pb <200) || pb >= 200)

    We can conclude from the previously stated, that this if-clause will also pass (aka, the then-clause will be called) if pb >= 200 returns true. I think what you would want, is to put everything behind the first AND operator between brackets. Consequently, that will make sure both the complete right hand side of the first AND operator and the left hand side are obliged to be true, to let the if-clause pass.
     
  3. Offline

    moe097

    Traks
    Thanks for the response but what do I do? Everything Behind the AND operator would be:
    if(args[0].equalsIgnoreCase("Berserker")
     
  4. Offline

    Traks

    Everything behind the first AND operator is the following
    Code:java
    1. economy.getBalance(pn) == berserkerprice && pb ==200 && !(pb < 200) || (pb >= 200)

    Just put that in-between brackets...
     
  5. Offline

    moe097

    Traks
    Like This:
    Code:java
    1. if(args[0].equalsIgnoreCase("Berserker") { economy.getBalance(pn) == berserkerprice && pb ==200 && !(pb < 200) || (pb >= 200)
    2.  
    3. EconomyResponse er = economy.withdrawPlayer(pn, berserkerprice);
    4. if(er.transactionSuccess()) {
    5.  
    6. berserker = true;
    7.  
    8. if((rusher)) {
    9. playerkits = "Your Kits: Archer, PvP, Rusher, Berserker";
    10. } else if(!(rusher)) {
    11. playerkits = "Your Kits: Archer, PvP, Berserker";
    12. }
    13.  
    14. player.sendMessage(ChatColor.GREEN + "You have bought the Berserker Kit for: 200 MineCoins! - Type '/KitPvP Berserker' to get your kit!");
    15. }
    16. return true;
    17.  
    18. }
    19.  

    I get an error in my IDE if I do that.
    Those operators can only go inside an if statment right?
     
  6. Offline

    Traks

    I meant round brackets (aka parentheses), sorry, I thought the implied bracket type was obvious...
     
  7. Offline

    moe097

    Traks

    Wow... That was stupid of me, your right.
    Thanks! Problem Solved!
     
Thread Status:
Not open for further replies.

Share This Page