Item Command

Discussion in 'Plugin Development' started by nicholasntp, Oct 26, 2011.

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

    nicholasntp

    Hi, I need help. Lol. I am using this:

    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("i")) {
    3.  
    4. int idOfItems = Material.matchMaterial(args[0]).getId();
    5. int numberOfItems = Integer.parseInt(args[1]);
    6.  


    Now when I do that, if I specify a word for the number of items, I need to get an error. How would I get the error to say something like,

    Code:java
    1.  
    2. sender.sendMessage("That is not a valid amount.")
    3.  

    ???

    Also, if I specified an item that doesn't exist, I need to get an error. How would I get the error to say something like,

    Code:java
    1.  
    2. sender.sendMessage("That is not a valid item.")
    3.  
     
  2. Offline

    nickrak

    Code:java
    1. try
    2. {
    3. Integer.parseInt(args[0]);
    4. }
    5. {
    6. // Not a valid number
    7. }
    8.  
    9. if (Material.matchMaterial(args[1]) == null)
    10. {
    11. // Material is not a valid material
    12. }
     
  3. Offline

    nicholasntp

    Here is my code....
    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("i")) {
    3.  
    4. try
    5. {
    6. Integer.parseInt(args[0]);
    7. }
    8. {
    9. sender.sendMessage("Invalid amount.");
    10.  
    11. }
    12.  
    13. if (Material.matchMaterial(args[1]) == null)
    14. {
    15. sender.sendMessage("Invalid item.");
    16.  
    17. }
    18.  
    19. int idOfItems = Material.matchMaterial(args[0]).getId();
    20. int numberOfItems = Integer.parseInt(args[1]);
    21.  
    22. ItemStack ItemToAdd = new ItemStack(idOfItems,numberOfItems);
    23. PlayerInventory inventory = player.getInventory();
    24. inventory.addItem(ItemToAdd);
    25.  


    Sooo close. It just says invalid amount on everything. Even if its a real integer. But it still gives me the items. But if it is an invalid amount, it says it, it says its an invalid item (even though it isn't), then it says an internal error occurred. And the console gets an error.

    Anddd... if it is an invalid item, it says its an invalid amount, and gets a console error.
     
  4. Offline

    nickrak

    What is your test case?
     
  5. Offline

    enjikaka

  6. Offline

    nicholasntp

    ???

    Well, I'm not using a give command just yet. Im using a /i command. Im confused. Can someone help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  7. Offline

    nickrak

    what did you type to test it?

    ex. /i 10 stone
     
  8. Offline

    nicholasntp

    no... /i dirt 3

    EDIT: Oh, crap. Wrong order. Ill test with correct order.
     
  9. Offline

    enjikaka

  10. Offline

    nicholasntp

    Yeah, I've got most of the item command done. I just need to get a Material into a String.
    Anyone know how?
     
  11. Offline

    nickrak

    Material_obj.toString();
     
  12. Offline

    nicholasntp

    _obj ???
     
  13. Offline

    Windwaker

    ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

    ^ Proves your point better
     
  14. Offline

    nickrak

    (some object of type Material).toString();
     
  15. Also add a return true after each test to prohibit a NPE in the console
     
  16. Offline

    nicholasntp

    Yeah. Thanks. Except what if it fails both tests? EDIT: For example, if I said "/i ice-cream 3b2", meaning both args were not valid.

    Ok. Duhhh.

    It actually does! (Maybe you could add "???????????????????????????????????????????????????" To the end. That might be better.

    New code....

    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("i")) {
    3.  
    4. try
    5. {
    6. int numberOfItems = Integer.parseInt(args[1]);
    7. }
    8. {
    9. sender.sendMessage("§a[iTask] §cInvalid amount.");
    10. return true;
    11.  
    12. }
    13.  
    14. if (Material.matchMaterial(args[0]) == null)
    15. {
    16. sender.sendMessage("§a[iTask] §cInvalid item.");
    17. return true;
    18.  
    19. }
    20.  
    21. int idOfItems = Material.matchMaterial(args[0]).getId();
    22. int numberOfItems = Integer.parseInt(args[1]);
    23.  
    24.  
    25. ItemStack ItemToAdd = new ItemStack(idOfItems,numberOfItems);
    26. PlayerInventory inventory = player.getInventory();
    27. inventory.addItem(ItemToAdd);
    28.  
    29. Material itemGivenMat = Material.getMaterial(idOfItems);
    30. String itemGiven = itemGivenMat.toString();
    31. sender.sendMessage("§a[iTask] §cYou have been given §e" + args[1] + "§6 " + itemGiven + "§c.");
    32.  
    33. }
    34.  


    Except what if it fails both tests? For example, if I said "/i ice-cream 3b2", meaning both args were not valid.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  17. Offline

    tommytony

    Just because I can't resist your begging. This stuff is basic; try a bit harder. :)

    Code:
    boolean isSuccess = false;
    int numberOfItems = 1;
    String returnMsg = "";
    
    if (args.length >= 1) {
        Material toGive = Material.matchMaterial(args[0]);
        if (toGive == null)
        {
            returnMsg += ChatColor.PINK + "[iTask] " + ChatColor.WHITE + "Invalid item.");
            isSuccess = false;
        } else if (args.length > 1) {
            try
            {
                numberOfItems = Integer.parseInt(args[1]);
            }
            catch (NumberFormatException nfe)
            {
                returnMsg += ChatColor.PINK + "[iTask] " + ChatColor.WHITE + "Invalid amount.");
                isSuccess = false;
            }
        }
    }
    
    if (isSuccess) {
        returnMsg += "There you go";
        // give the amount of the item
    }
    
    if (!returnMsg.equals("")) {
        player.sendMessage(returnMsg);
    }
    
    return isSuccess;
     
    nicholasntp likes this.
  18. Offline

    nicholasntp

    Thank you so much. You are awesome tom. xD. Thats awesome.

    EDIT: Except.... How would I get the "toGive" Variable in the " if (isSuccess) "?

    *facepalm*
     
Thread Status:
Not open for further replies.

Share This Page