input problem.

Discussion in 'Plugin Development' started by BlockCat, May 23, 2011.

Thread Status:
Not open for further replies.
  1. hello.
    i am trying to take over an plugin.
    since i am very new in java, I don't know much.
    in the plugin you can only auction items by typing their ID.
    but i also want to auction items by typing their name.
    how would i do that?

    Code:
    int count = 0;
                        try
                        {
                            id = Items.validate(msg[2]);
                        }
    does something with this

    this stands in the Items.validate
    Code:
    public static int[] validate(String item)
        {
            int ret[] = {
                -1, 0
            };
            Material m = null;
    
            try
            {
    
                ret[0] = Integer.valueOf(item).intValue();
                m = Material.getMaterial(ret[0]);
            }
            catch(NumberFormatException e)
            {
                m = Material.matchMaterial(item);
                String val = "";
                for(Iterator iterator = iAuction.items.keySet().iterator(); iterator.hasNext();)
                {
                    String id = (String)iterator.next();
                    if(id.equalsIgnoreCase(item))
                    {
                        val = (String)iAuction.items.get(id);
    
                    }
                    else
                    if(((String)iAuction.items.get(id)).equalsIgnoreCase(item))
                        val = id;
                }
    
                if(val.contains(","))
                {
                    String split[] = val.split(",");
                    ret[0] = Integer.valueOf(split[0]).intValue();
                    ret[1] = Integer.valueOf(split[1]).intValue();
                } else
                {
                    ret[0] = Integer.valueOf(val).intValue();
                }
                if(ret[0] == -1)
                    return ret;
            }
            if(!checkID(ret[0]))
            {
                ret[0] = -1;
                return ret;
            } else
            {
                return ret;
            }
        }
     
  2. Offline

    Sammy

    I'm doing this by heart so bear with me please.
    Code:
    public static int[] validate(String itemName, int itemID))
    then do a if like this
    Code:
    if(itemName !=null){
    //make the material with the name
    }
    else{
    //make the material with the ID
    }
    
     
  3. then another question, how do i get the Material to integer?
     
  4. Offline

    Sammy

    Im not sure if this is right I dont have an IDE here but:
    Code:
    m = Material.getMaterial(ret[0]);
    using the m i think theres something like m.getValue() or something like that
     
  5. that gives an error: "Cannot convert Material to int".
     
  6. Offline

    Shamebot

    Material.getId()
     
  7. with this i get:
    The method getId() in the type Material is not applicable for the arguments (Material)

    but it had to be m.getId().
    but it still won't work.

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

    Shamebot

    Code:
    Material mat = Material.IRON_BLOCK;
    System.out.println(mat.getId()+"");
    This works
     
  9. So if i do this, it will look if it is an integer or a string?
     
  10. Offline

    Shamebot

    I'm not sure whether I understand you problem.
    If you want to get a Material you can use
    • Material.getMaterial(intid)
    • Material.getMaterial(Stringname)
    • Material.matchMaterial(Stringname)
    Your if will check whether the string isn't null, which doesn't mean it checks whether it's a number or not.
     
  11. i meant: if "itemName != null" returns true, does that mean it is an integer?
     
  12. Offline

    Shamebot

    Nope,
    Code:
    String itemName = "hallo";
    Sytem.out.println(itemName != null);
    will give you true.
    Code:
    try
    {
        int number = Integer.decode(aString);
    }
    catch(NumberFormatException ex)
    {
    
    }
    If you don't want to use exceptions I could post a snippet checking if a string is a number.
     
  13. that would be fine, thanks :)
    btw, you're dutch?
     
  14. Offline

    Shamebot

    No, why do you ask, do I have a dutch accent? Though accent may be an inappropriate word when applied to written text.
     
  15. no you said, hallo.
    hallo = hello in Holland.
    so you have an dutch accent :p
     
  16. Offline

    Shamebot

    Ahh, it's hallo in German too.
     
  17. and you are German?
    anyway, back to the subject.
     
  18. Offline

    Shamebot

    yes
    Are there any problems left?
     
  19. i now have it that it ONLY works with the item name.
    and not with the id anymore
     
  20. Offline

    Shamebot

    Code?
    However shouldn't be that difficult
    Code:
    Material getMaterial(String name)
    {
        Material mat = null;
        try
        {
            mat = Material.getMaterial(Integer.decode(name));
        }
        catch(NumberFormatException ex)
        {
            mat = Material.matchMaterial(name);
        }
        return mat;
    }
    
    if the return value of the method is null, there's no such material.
     
  21. Offline

    Lolmewn

    Most servers have a file in their main folder, called items.txt. If you just look for the item in there with the Properties, you will get an ID, which you can use ;)
     
  22. Offline

    Shamebot

    Why would he use something other than Material.getId() ?
     
  23. Offline

    Lolmewn

    because that eeh.. idk. I'm just sayin :p
     
    Shamebot likes this.
  24. i know, because the items.txt doesn't have those stupid: _
     
  25. when i do that, i get the error: cannot convert from material to int[].
    what could i do against that?
     
  26. Offline

    Shamebot

    What did you change?
     
  27. here is it:
    Code:
    public static int[] validate(String item)
        {
            int ret[] = {
                -1, 0
            };
            Material m = null;
    
            try
            {
    
                //if(item !=null){
                    //m = Material.getMaterial(item.toUpperCase());
                    //ret[0] = m.getId();
                    //}
                    //else{
                    //    ret[0] = Integer.valueOf(item);
                    //make the material with the ID
                    //}
                //below is what i changed
                {
    
                    try
                    {
                        m = Material.getMaterial(Integer.decode(item));
                    }
                    catch(NumberFormatException ex)
                    {
                        m = Material.matchMaterial(item);
                    }
                    return m;
                }
        
            }
            catch(NumberFormatException e)
            {
    
                 String val = "";
                for(Iterator iterator = iAuction.items.keySet().iterator(); iterator.hasNext();)
                {
                    String id = (String)iterator.next();
                    if(id.equalsIgnoreCase(item))
                    {
                        val = (String)iAuction.items.get(id);
    
                    }
                    else
                    if(((String)iAuction.items.get(id)).equalsIgnoreCase(item))
                        val = id;
                }
    
                if(val.contains(","))
                {
                    String split[] = val.split(",");
                    ret[0] = Integer.valueOf(split[0]).intValue();
                    ret[1] = Integer.valueOf(split[1]).intValue();
                } else
                {
                    ret[0] = Integer.valueOf(val).intValue();
                }
                if(ret[0] == -1)
                    return ret;
            }
            if(!checkID(ret[0]))
            {
                ret[0] = -1;
                return ret;
            } else
            {
                return ret;
            }
        }
    
     
  28. Offline

    Shamebot

    You want to get the item by number in the try.
    Code:
     m = Material.getMaterial(Integer.decode(item));
    Code:
                    else{
                        ret[0] = Integer.valueOf(item);
    
                    }
    How would you get the value if item is null?
    I don't really get what your code should do.
    Edit: posted before your second post
     
  29. i want to get the item by number and by name.
     
  30. Offline

    Shamebot

    Yeah but this Iterator and split string stuff.
     
Thread Status:
Not open for further replies.

Share This Page