Player.performCommand() not doing anything.

Discussion in 'Plugin Development' started by imjake9, May 18, 2011.

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

    imjake9

    Well, as the title implies, using player.performCommand() does nothing at all. I've tried both with and without slash ("/"), but nothing. Also, I've tried doing player.chat("/" + command), and that works, but not correctly. For example, using /i (General) through that command makes the correct message appear for the player, but the item isn't actually given. Using /me works perfectly, but also says the "Unknown command" message.
    It seems that the CommandSender variable isn't being passed properly, or something like that, but I'd have no idea how to fix that. Any ideas on both issues?
     
  2. Offline

    GardenGnomer

    This worked for me:

    Code:
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
    player.performCommand("give GardenGnomer 2 2");
    }
    (I cut out all the unnecessary parts)
     
  3. Offline

    MrLizardDogMan

    I try this and nothing Happens

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. if (!(sender instanceof Player)) {
    3. sender.sendMessage("No No No, You Didn't Say The Magic Word");
    4. }
    5. Player p = (Player) sender;
    6. Player player = (Player) sender;
    7. if(cmd.getName().equalsIgnoreCase("rustmc")){
    8. }
    9. if (args.length == 2 && args[0].equalsIgnoreCase("weapon") && args[1].equalsIgnoreCase("pistol")){
    10. if(p.getInventory().contains(Material.IRON_INGOT, 40))
    11. if(p.getInventory().contains(Material.WOOD, 30)){
    12. ItemStack[] pistol = {new ItemStack(Material.IRON_INGOT, 40)};
    13. ItemStack[] pistol2 = {new ItemStack(Material.WOOD, 30)};
    14. player.getInventory().removeItem(pistol);
    15. player.getInventory().removeItem(pistol2);
    16. player.performCommand("mw give M16A3");
    17. }
     
  4. Offline

    ResultStatic

    MrLizardDogMan well to start your brackets } are messed up.


    this is just a mess.
    ^^^^^^
    Code:
     if (args.length == 2 && args[0].equalsIgnoreCase("weapon") && args[1].equalsIgnoreCase("pistol")){

    forgot a { at the end.
    ^^^^^^
    Code:
    if(p.getInventory().contains(Material.IRON_INGOT, 40))

    basically what your telling java with this is "ok if the command equals "rustmc " i should do nothing and basically ignore this line of code and keep reading onto the next line.
    ^^^^^^^^^^
    Code:
    if(cmd.getName().equalsIgnoreCase("rustmc")){
                          }
    MrLizardDogMan
    same thing right here. when u add that end bracket } it just tells java to ignore the checker and to keep reading and do what ever is inside the { and }.

    Code:
    if (!(sender instanceof Player)) {
                      sender.sendMessage("No No No, You Didn't Say The Magic Word");
                    }

    instead it should be

    Code:
    if (sender instanceof Player) {
        all of your code that you want checked to make sure the
        sender is a player.
     
     
    then at the end of your code.
                    }else{
    player.sendMessage("must be a player to use this command);
    }
    }
    MrLizardDogMan
    Player p = (Player) sender; this is so broken too.
    Player player = (Player) sender; they are the same thing.

    this should work
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        if (sender instanceof Player) {
          Player player = (Player) sender;
          if(cmd.getName().equalsIgnoreCase("rustmc")){
          if (args.length == 2){
          if (args[0].equalsIgnoreCase("weapon")){
          if (args[1].equalsIgnoreCase("pistol")){
              if(player.getInventory().contains(Material.IRON_INGOT, 40)){
              if(player.getInventory().contains(Material.WOOD, 30)){
                  ItemStack[] pistol = {new ItemStack(Material.IRON_INGOT, 40)};
                  ItemStack[] pistol2 = {new ItemStack(Material.WOOD, 30)};
                  player.getInventory().removeItem(pistol);
                  player.getInventory().removeItem(pistol2);
                  player.performCommand("mw give M16A3");
              }
            }
          }
          }
          }
          }
        }else{
            sender.sendMessage("No No No, You Didn't Say The Magic Word");
        }
        return false;
    }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 16, 2016
  5. Offline

    MrLizardDogMan

    I feel reel dumb...

    Ok, so i did that, and added all my stuff, And i know that it is with the brackets again >.<

    Crappy Code:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. if (sender instanceof Player) {
    3. Player player = (Player) sender;
    4. if(cmd.getName().equalsIgnoreCase("rustmc")){
    5. if (args.length == 2){
    6. if (args[0].equalsIgnoreCase("weapon")){
    7. if (args[1].equalsIgnoreCase("pistol")){
    8. if(player.getInventory().contains(Material.IRON_INGOT, 40)){
    9. if(player.getInventory().contains(Material.WOOD, 30)){
    10. ItemStack[] pistol = {new ItemStack(Material.IRON_INGOT, 40)};
    11. ItemStack[] pistol2 = {new ItemStack(Material.WOOD, 30)};
    12. player.getInventory().removeItem(pistol);
    13. player.getInventory().removeItem(pistol2);
    14. player.performCommand("mw give pistol");
    15. }
    16. else{
    17. player.sendMessage(ChatColor.RED + "You do not have enough materials.");
    18. }
    19. }
    20. }
    21. }
    22. }
    23. if (args[0].equalsIgnoreCase("weapon")){
    24. if (args[1].equalsIgnoreCase("shotgun")){
    25. if(player.getInventory().contains(Material.IRON_INGOT, 100)){
    26. if(player.getInventory().contains(Material.WOOD, 60)){
    27. ItemStack[] pistol = {new ItemStack(Material.IRON_INGOT, 100)};
    28. ItemStack[] pistol2 = {new ItemStack(Material.WOOD, 60)};
    29. player.getInventory().removeItem(pistol);
    30. player.getInventory().removeItem(pistol2);
    31. player.performCommand("mw give shotgun");
    32. }
    33. else{
    34. player.sendMessage(ChatColor.RED + "You do not have enough materials.");
    35. }
    36. if (args[0].equalsIgnoreCase("weapon")){
    37. if (args[1].equalsIgnoreCase("smg")){
    38. if(player.getInventory().contains(Material.IRON_INGOT, 150)){
    39. if(player.getInventory().contains(Material.WOOD, 40)){
    40. ItemStack[] pistol = {new ItemStack(Material.IRON_INGOT, 150)};
    41. ItemStack[] pistol2 = {new ItemStack(Material.WOOD, 40)};
    42. player.getInventory().removeItem(pistol);
    43. player.getInventory().removeItem(pistol2);
    44. player.performCommand("mw give smg");
    45. }
    46. else{
    47. player.sendMessage(ChatColor.RED + "You do not have enough materials.");
    48. }
    49. if (args[0].equalsIgnoreCase("weapon")){
    50. if (args[1].equalsIgnoreCase("rifle")){
    51. if(player.getInventory().contains(Material.IRON_INGOT, 200)){
    52. if(player.getInventory().contains(Material.WOOD, 70)){
    53. ItemStack[] pistol = {new ItemStack(Material.IRON_INGOT, 200)};
    54. ItemStack[] pistol2 = {new ItemStack(Material.WOOD, 70)};
    55. player.getInventory().removeItem(pistol);
    56. player.getInventory().removeItem(pistol2);
    57. player.performCommand("mw give rifel");
    58. }
    59. else{
    60. player.sendMessage(ChatColor.RED + "You do not have enough materials.");
    61. }
    62. if (args[0].equalsIgnoreCase("weapon")){
    63. if (args[1].equalsIgnoreCase("carbine")){
    64. if(player.getInventory().contains(Material.IRON_INGOT, 600)){
    65. if(player.getInventory().contains(Material.WOOD, 400)){
    66. ItemStack[] pistol = {new ItemStack(Material.IRON_INGOT, 600)};
    67. ItemStack[] pistol2 = {new ItemStack(Material.WOOD, 400)};
    68. player.getInventory().removeItem(pistol);
    69. player.getInventory().removeItem(pistol2);
    70. player.performCommand("mw give carbine");
    71. }
    72. else{
    73. player.sendMessage(ChatColor.RED + "You do not have enough materials.");
    74. }
    75. if (args[0].equalsIgnoreCase("weapon")){
    76. if (args[1].equalsIgnoreCase("ammo")){
    77. if(player.getInventory().contains(Material.SULPHUR, 10)){
    78. if(player.getInventory().contains(Material.COBBLESTONE, 5)){
    79. if(player.getInventory().contains(Material.IRON_INGOT, 5)){
    80. ItemStack[] pistol = {new ItemStack(Material.IRON_INGOT, 5)};
    81. ItemStack[] pistol2 = {new ItemStack(Material.COBBLESTONE, 5)};
    82. ItemStack[] pistol3 = {new ItemStack(Material.SULPHUR, 10)};
    83. player.getInventory().removeItem(pistol);
    84. player.getInventory().removeItem(pistol2);
    85. player.getInventory().removeItem(pistol3);
    86. player.performCommand("mw give ammo");
    87. }
    88.  
    89. }else{
    90. player.sendMessage(ChatColor.RED + "You do not have enough materials.");
    91. }
    92. if (args[0].equalsIgnoreCase("weapon")){
    93. if (args[1].equalsIgnoreCase("bow")){
    94. if(player.getInventory().contains(Material.LOG, 30)){
    95. if(player.getInventory().contains(Material.LEATHER, 30)){
    96. ItemStack[] pistol = {new ItemStack(Material.LOG, 30)};
    97. ItemStack[] pistol2 = {new ItemStack(Material.LEATHER, 30)};
    98. player.getInventory().removeItem(pistol);
    99. player.getInventory().removeItem(pistol2);
    100. ItemStack bow = new ItemStack(Material.BOW, 1);
    101. bow.addEnchantment(Enchantment.ARROW_DAMAGE, 0);
    102. player.getInventory().addItem(bow);
    103. }
    104. else{
    105. player.sendMessage(ChatColor.RED + "You do not have enough materials.");
    106. }
    107. if (args[0].equalsIgnoreCase("weapon")){
    108. if (args[1].equalsIgnoreCase("arrow")){
    109. if(player.getInventory().contains(Material.COBBLESTONE, 5)){
    110. if(player.getInventory().contains(Material.WOOD, 3)){
    111. ItemStack[] pistol = {new ItemStack(Material.COBBLESTONE, 5)};
    112. ItemStack[] pistol2 = {new ItemStack(Material.WOOD, 3)};
    113. player.getInventory().removeItem(pistol);
    114. player.getInventory().removeItem(pistol2);
    115. ItemStack bow = new ItemStack(Material.ARROW, 2);
    116. player.getInventory().addItem(bow);
    117. }
    118. else{
    119. player.sendMessage(ChatColor.RED + "You do not have enough materials.");
    120. }
    121. }
    122. if (args[0].equalsIgnoreCase("weapon")){
    123. if (args[1].equalsIgnoreCase("hatchet")){
    124. if(player.getInventory().contains(Material.IRON_INGOT, 30)){
    125. if(player.getInventory().contains(Material.WOOD, 20)){
    126. ItemStack[] pistol = {new ItemStack(Material.IRON_INGOT, 30)};
    127. ItemStack[] pistol2 = {new ItemStack(Material.WOOD, 20)};
    128. player.getInventory().removeItem(pistol);
    129. player.getInventory().removeItem(pistol2);
    130. ItemStack bow = new ItemStack(Material.IRON_AXE);
    131. player.getInventory().addItem(bow);
    132. }
    133. else{
    134. player.sendMessage(ChatColor.RED + "You do not have enough materials.");
    135. }
    136. }else{
    137. sender.sendMessage("No No No, You Didn't Say The Magic Word");
    138. }
    139. }
    140. }
    141. }
    142. }
    143. }
    144. }
    145. }
    146.  
    147. }
    148. }
    149. }
    150. }
    151. }
    152. }
    153. }
    154. }
    155. }
    156. }
    157. }
    158. }
    159. }
    160. }
    161. }
    162. }
    163. }
    164. return false;
    165. }
    166. }
    167.  

    166 Lines of crap

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 16, 2016
  6. Offline

    MoeMix

    lol how old is this thread xD
     
  7. Offline

    MrLizardDogMan

    Oh i posted this is the wrong thread MoeMix Crap...
     
Thread Status:
Not open for further replies.

Share This Page