Sub Commands

Discussion in 'Plugin Development' started by CactusComboPvP, Jan 5, 2014.

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

    CactusComboPvP

    So, I made a plugin. It does /kit pvp. I want it so when they do /kit it sends a message saying:
    Kits: PvP and when they do /kit pvp it does the command
    I got this so far.
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("pvp")){
    2. if (!sender.hasPermission("clashpvp.pvp")){
    3. p.sendMessage(ChatColor.RED + "You do not have permission!");
    4. return true;
    5. }
    6. if(cooldown.contains(p.getName())) {
    7. p.sendMessage(ChatColor.RED + "You can get this kit again in 10 minutes!");
    8. return false;
    9. } else {
    10. p.removePotionEffect(PotionEffectType.SPEED);
    11. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    12. sword.addEnchantment(Enchantment.DAMAGE_ALL, 2);
    13. ItemStack helmet = new ItemStack(Material.IRON_HELMET);
    14. ItemStack chestplate = new ItemStack(Material.IRON_CHESTPLATE);
    15. ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS);
    16. ItemStack boots = new ItemStack(Material.IRON_BOOTS);
    17. Potion pot = new Potion(PotionType.STRENGTH, 2);
    18. ItemStack potion = pot.toItemStack(1);
    19. Potion pot1 = new Potion(PotionType.SPEED, 2);
    20. ItemStack potion1 = pot1.toItemStack(1);
    21. pi.addItem(helmet);
    22. pi.addItem(chestplate);
    23. pi.addItem(leggings);
    24. pi.addItem(boots);
    25. pi.addItem(sword);
    26. pi.addItem(potion);
    27. pi.addItem(potion1);
    28. pi.addItem(new ItemStack (Material.MUSHROOM_SOUP, 26));
    29. p.sendMessage("§7You have chosen the PvP kit.");
    30. cooldown.add(p.getName());
    31.  
    32. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    33.  
    34. @Override
    35. public void run() {
    36. cooldown.remove(p.getName());
    37. }
    38. }, 12000);
    39. }
    40. return true;
    41.  
    42. }


    How can i make it into /kit pvp and when they do /kit it says: Kits: PVP

    Any help appreciated

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

    Slideroller

    Post the full code where it shows, the kit command please.
     
  3. Offline

    CactusComboPvP

    Slideroller
    All right, here it is:
    Code:java
    1. if ((cmd.getName().equalsIgnoreCase("kits")) &&
    2. (sender.hasPermission("clashpvp.clear"))) {
    3. Bukkit.getServer().broadcastMessage(ChatColor.DARK_PURPLE + "Kits: PvP" );
    4. }
    5. if (cmd.getName().equalsIgnoreCase("pvp")){
    6. if (!sender.hasPermission("clashpvp.pvp")){
    7. p.sendMessage(ChatColor.RED + "You do not have permission!");
    8. return true;
    9. }
    10. if(cooldown.contains(p.getName())) {
    11. p.sendMessage(ChatColor.RED + "You can get this kit again in 10 minutes!");
    12. return false;
    13. } else {
    14. p.removePotionEffect(PotionEffectType.SPEED);
    15. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    16. sword.addEnchantment(Enchantment.DAMAGE_ALL, 2);
    17. ItemStack helmet = new ItemStack(Material.IRON_HELMET);
    18. ItemStack chestplate = new ItemStack(Material.IRON_CHESTPLATE);
    19. ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS);
    20. ItemStack boots = new ItemStack(Material.IRON_BOOTS);
    21. Potion pot = new Potion(PotionType.STRENGTH, 2);
    22. ItemStack potion = pot.toItemStack(1);
    23. Potion pot1 = new Potion(PotionType.SPEED, 2);
    24. ItemStack potion1 = pot1.toItemStack(1);
    25. pi.addItem(helmet);
    26. pi.addItem(chestplate);
    27. pi.addItem(leggings);
    28. pi.addItem(boots);
    29. pi.addItem(sword);
    30. pi.addItem(potion);
    31. pi.addItem(potion1);
    32. pi.addItem(new ItemStack (Material.MUSHROOM_SOUP, 26));
    33. p.sendMessage("§7You have chosen the PvP kit.");
    34. cooldown.add(p.getName());
    35.  
    36. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    37.  
    38. @Override
    39. public void run() {
    40. cooldown.remove(p.getName());
    41. }
    42. }, 12000);
    43. }
    44. return true;
    45. }
     
  4. Offline

    bigteddy98

    The (String[] args) of the command are the arguments, if a player excecutes "/kit", the args.length == 0. If a player excecutes /kit pvp, the args.length == 1 && args[0].equalsIgnoreCase("pvp"). Hope it helps.

    If you don't know what arguments are, here an example (the bold text are arguments):
    /kit pvp create.
    In my example, args[0] will return "pvp", and args[1], will return "create".
     
  5. Offline

    CactusComboPvP

    bigteddy98
    I'm getting syntax errors, i probably added it wrong, can you try adding it? Thanks
     
  6. Offline

    bigteddy98

    Could you post your stacktrace please?
     
  7. Offline

    CactusComboPvP

    bigteddy98
    Actually, I done this much. But it gives me internal error when i do the command, do i have to register the sub command?
    I registered: kits and pvp in plugin.yml

    Code:java
    1. if (args.length == 0 && args[0].equalsIgnoreCase("kits") &&
    2. (sender.hasPermission("clashpvp.clear"))) {
    3. Bukkit.getServer().broadcastMessage(ChatColor.DARK_PURPLE + "Kits: PvP" );
    4. }
    5. if (args.length == 1 && args[0].equalsIgnoreCase("pvp")) {
    6. if (!sender.hasPermission("clashpvp.pvp")){
    7. p.sendMessage(ChatColor.RED + "You do not have permission!");
    8. return true;
    9. }
    10. if(cooldown.contains(p.getName())) {
    11. p.sendMessage(ChatColor.RED + "You can get this kit again in 10 minutes!");
    12. return false;
    13. } else {
    14. p.removePotionEffect(PotionEffectType.SPEED);
    15. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    16. sword.addEnchantment(Enchantment.DAMAGE_ALL, 2);
    17. ItemStack helmet = new ItemStack(Material.IRON_HELMET);
    18. ItemStack chestplate = new ItemStack(Material.IRON_CHESTPLATE);
    19. ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS);
    20. ItemStack boots = new ItemStack(Material.IRON_BOOTS);
    21. Potion pot = new Potion(PotionType.STRENGTH, 2);
    22. ItemStack potion = pot.toItemStack(1);
    23. Potion pot1 = new Potion(PotionType.SPEED, 2);
    24. ItemStack potion1 = pot1.toItemStack(1);
    25. pi.addItem(helmet);
    26. pi.addItem(chestplate);
    27. pi.addItem(leggings);
    28. pi.addItem(boots);
    29. pi.addItem(sword);
    30. pi.addItem(potion);
    31. pi.addItem(potion1);
    32. pi.addItem(new ItemStack (Material.MUSHROOM_SOUP, 26));
    33. p.sendMessage("§7You have chosen the PvP kit.");
    34. cooldown.add(p.getName());
    35.  
    36. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    37.  
    38. @Override
    39. public void run() {
    40. cooldown.remove(p.getName());
    41. }
    42. }, 12000);
    43. }
    44. return true;
    45. }


    bigteddy98

    Okay, so when I do /kits pvp it works
    But when I do /kits alone it gives me internal error occured.

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

    bigteddy98

    Code:java
    1. if (args.length == 0 && args[0].equalsIgnoreCase("kits"))

    This isn't possible anyway, because if the args.length == 0, you check if it doesn't have any arguments. But right after that, you check if the argument (which you just checked it doesn't excists) equals ("kit").
     
  9. Offline

    CactusComboPvP

  10. Offline

    bigteddy98


    Changed it for you, untested, should work:
    Code:java
    1. if (args.length == 0) {
    2. Bukkit.getServer().broadcastMessage(ChatColor.DARK_PURPLE + "Kits: PvP");
    3. return true;
    4. }
    5. if (args.length == 1 && args[0].equalsIgnoreCase("pvp")) {
    6. if (!sender.hasPermission("clashpvp.pvp")) {
    7. p.sendMessage(ChatColor.RED + "You do not have permission!");
    8. return true;
    9. }
    10. if (cooldown.contains(p.getName())) {
    11. p.sendMessage(ChatColor.RED + "You can get this kit again in 10 minutes!");
    12. return false;
    13. } else {
    14. Inventory pi = p.getInventory();
    15. p.removePotionEffect(PotionEffectType.SPEED);
    16. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    17. sword.addEnchantment(Enchantment.DAMAGE_ALL, 2);
    18. ItemStack helmet = new ItemStack(Material.IRON_HELMET);
    19. ItemStack chestplate = new ItemStack(Material.IRON_CHESTPLATE);
    20. ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS);
    21. ItemStack boots = new ItemStack(Material.IRON_BOOTS);
    22. Potion pot = new Potion(PotionType.STRENGTH, 2);
    23. ItemStack potion = pot.toItemStack(1);
    24. Potion pot1 = new Potion(PotionType.SPEED, 2);
    25. ItemStack potion1 = pot1.toItemStack(1);
    26. pi.addItem(helmet);
    27. pi.addItem(chestplate);
    28. pi.addItem(leggings);
    29. pi.addItem(boots);
    30. pi.addItem(sword);
    31. pi.addItem(potion);
    32. pi.addItem(potion1);
    33. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 26));
    34. p.sendMessage("§7You have chosen the PvP kit.");
    35. cooldown.add(p.getName());
    36.  
    37. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    38.  
    39. @Override
    40. public void run() {
    41. cooldown.remove(p.getName());
    42. }
    43. }, 12000);
    44. }
    45. return true;
    46. }
     
  11. Offline

    CactusComboPvP

  12. Offline

    bigteddy98

    You're welcome, feel free to ask if you have any more questions :).
     
Thread Status:
Not open for further replies.

Share This Page