Solved Give certain amount (1) of items when unspecified args.

Discussion in 'Plugin Development' started by Ezrab_, Mar 15, 2018.

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

    Ezrab_

    So what I would like to do is, if you don't specify args[3] here:

    Code:java
    1.  
    2. if (Material.getMaterial(args[2].toUpperCase()) != null) {
    3. ItemStack is = new ItemStack(Material.valueOf(args[2].toUpperCase()),
    4. Integer.parseInt(args[3])); // THIS ONE
    5. p.getInventory().addItem(is);
    6. } else {
    7. p.sendMessage(ChatColor.RED + "DIDN'T WORK!");
    8. }
    9.  

    You only get 1 item. So if I do "/kitpvp add knight wood" it'll just give me 1 wood that's it. And if I were to enter a value for example: "/kitpvp add knight wood 64" it'll give me 64 wood.

    My class:

    Code:java
    1.  
    2. package nl.ezrab.kitpvp.cmds;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.inventory.ItemStack;
    11.  
    12. import nl.ezrab.kitpvp.Main;
    13. import nl.ezrab.kitpvp.listeners.InteractListener;
    14.  
    15. public class MainCommand implements CommandExecutor {
    16.  
    17. @Override
    18. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    19. if (sender instanceof Player) {
    20. Player p = (Player) sender;
    21. if (cmd.getName().equalsIgnoreCase("kitpvp")) {
    22. if (args.length == 0) {
    23. p.sendMessage(Main.msg + "Commands for /kitpvp");
    24. p.sendMessage(Main.msg + "/kitpvp open");
    25. p.sendMessage(Main.msg + "/kitpvp create <kit of your choise>");
    26. p.sendMessage(Main.msg + "/kitpvp add <created kit>");
    27. p.sendMessage(Main.msg + "/kitpvp reload");
    28. return true;
    29. }
    30. if (args[0].equalsIgnoreCase("open")) {
    31. if (args.length == 1) {
    32. InteractListener.openGUI(p);
    33. return true;
    34. }
    35. } else if (args[0].equalsIgnoreCase("create")) {
    36. if (args.length == 1) {
    37. p.sendMessage(
    38. Main.msg + ChatColor.RESET + ChatColor.RED + "You have to specify more arguments.");
    39. return true;
    40. }
    41. String kits = "";
    42. kits += args[1];
    43.  
    44. Main.plugin.getConfig().createSection("kits." + kits);
    45. Main.plugin.getConfig().set("kits." + kits, "");
    46. Main.plugin.saveConfig();
    47. p.sendMessage(ChatColor.GREEN + "New kit: " + kits + " created!");
    48. // TODO add stuff to created kits.
    49.  
    50. } else if (args[0].equalsIgnoreCase("add")) {
    51. if (args.length == 1) {
    52. p.sendMessage(
    53. Main.msg + ChatColor.RESET + ChatColor.RED + "You have to specify more arguments.");
    54. return true;
    55. }
    56. if (Main.plugin.getConfig().contains("kits." + args[1])) {
    57. if (args.length == 2) {
    58. p.sendMessage(Main.msg + ChatColor.RESET + ChatColor.RED + "Specify a material please.");
    59. return true;
    60. }
    61. if (Material.getMaterial(args[2].toUpperCase()) != null) {
    62. ItemStack is = new ItemStack(Material.valueOf(args[2].toUpperCase()),
    63. Integer.parseInt(args[3]));
    64. p.getInventory().addItem(is);
    65. } else {
    66. p.sendMessage(ChatColor.RED + "DIDN'T WORK!");
    67. }
    68. } else {
    69. p.sendMessage(Main.msg + ChatColor.RESET + ChatColor.RED + args[1] + " is not a valid kit.");
    70. }
    71. } else if (args[0].equalsIgnoreCase("reload")) {
    72. p.sendMessage(Main.msg + "config reloaded.");
    73. Main.plugin.reloadConfig();
    74. } else {
    75. p.sendMessage(Main.msg + ChatColor.RESET + ChatColor.RED + "That command doesn't exist.");
    76. }
    77. }
    78. } else {
    79. sender.sendMessage("Only players can do this command.");
    80. }
    81. return true;
    82. }
    83. }
    84.  


    BTW sorry for any misspelling or bad English.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Ezrab_ Do a length check for args.length == 4
     
  3. Offline

    Ezrab_

    Thanks for the quick response! Could you be more detailed though? :)
     
  4. Offline

    timtower Administrator Administrator Moderator

    Set a variable to 1
    Check if the length == 4, if so: parse the number and change the value of the variable.
     
  5. Offline

    Ezrab_

    So where would I put the if (args.lenght == 4) { ?
     
  6. Offline

    KarimAKL

    I think you would be able to do it like this:
    Code:
    if (cmd.getName().equalsIgnoreCase(kitpvp)) {
      if (args.length == 0) {
        //code
      else if (args.length == 4) {
        //code if arg 4 is set
    
     
  7. To clarify what @timtower said try this out.

    Code:
    int quantity = 1;
    if (args.length == 4){
        quantity = args[3];
    }
    The complete command would be something similar to this.

    Code:
    else if (args[0].equalsIgnoreCase("add")) {
      Integer q = 1; //Setting quantity to default at 1
    
      if (args.length == 3) {
        q = Integer.parseInt(args[3]); // we set q equal to args[3] since it was specified
      }
    
      if (args.length == 1) {
        p.sendMessage(
          Main.msg + ChatColor.RESET + ChatColor.RED + "You have to specify more arguments.");
        return true;
      }
      if (Main.plugin.getConfig().contains("kits." + args[1])) {
        if (args.length == 2) {
          p.sendMessage(Main.msg + ChatColor.RESET + ChatColor.RED + "Specify a material please.");
          return true;
        }
        if (Material.getMaterial(args[2].toUpperCase()) != null) {
          ItemStack is = new ItemStack(Material.valueOf(args[2].toUpperCase()), q); //Replaced args[3] with q
          p.getInventory().addItem(is);
        } else {
          p.sendMessage(ChatColor.RED + "DIDN'T WORK!");
        }
      } else {
        p.sendMessage(Main.msg + ChatColor.RESET + ChatColor.RED + args[1] + " is not a valid kit.");
      }
    
     
    Last edited: Mar 16, 2018
    Ezrab_ likes this.
  8. Offline

    Ezrab_

    Solved it with this:
    Code:java
    1.  
    2.  
    3. if (Main.plugin.getConfig().contains("kits." + args[1])) {
    4. if (args.length == 2) {
    5. p.sendMessage(Main.msg + ChatColor.RESET + ChatColor.RED + "Specify a material please.");
    6. return true;
    7. }
    8. if (Material.getMaterial(args[2].toUpperCase()) != null) {
    9.  
    10. int x = 1;
    11. if (args.length == 4) {
    12. x = Integer.parseInt(args[3]);
    13. }
    14. ItemStack is = new ItemStack(Material.valueOf(args[2].toUpperCase()), x);
    15. }
    16.  
     
Thread Status:
Not open for further replies.

Share This Page