Checking if [string] is a Material Name

Discussion in 'Plugin Development' started by rtainc, May 18, 2013.

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

    rtainc

    I'm currently making a spawning command, which will spawn up Material.get(theNameSpecified), but I need to check if that item exists. What's the best (and least expensive) way to do so?
     
  2. Offline

    TheLunarFrog

    Code:java
    1. StringBuilder sb = new StringBuilder();
    2. for(int z = 0; z < args.length - 1; z++){ // Change z's initialization to 1 if you plan on making them enter a player name in the first spot, etc - already works to put item amount last
    3. sb.append(args[z]+" ");
    4. }
    5. Material mat = Material.matchMaterial(sb.toString().trim().replaceAll(" ", "_"));
    6. if(mat != null){
    7. int amount = Integer.parseInt(args[args.length-1]);
    8. //Give that item?
    9. }else{
    10. //Error?
    11. }


    Edit: so much broken formatting with this.
     
    rtainc likes this.
  3. Offline

    rtainc

    I remember trying this and it not working (oddly enough), but I'll try it again.
     
  4. Offline

    TheLunarFrog

    rtainc ^I edited my post, and then edited this one. I derped and italicized something on accident.

    Edit: apparently, I have a tendency to enter character combinations wysiwyg recognizes. Resulting in 10 different edits.

    Also, you could use a try/catch, catching IllegalArgumentException and use Material.valueOf(String), but it will require you to do a lot of format manipulation on your part.
     
  5. Offline

    rtainc

    It's working great. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page