[Solved] How to find out if integer is an item id?

Discussion in 'Spigot Plugin Development' started by HotProtato, May 30, 2015.

  1. I've tried to create an itemstack and catch any exceptions and if it does throw an exception then it isn't an item id, is this correct? Is there a better method?
  2. Just found out it doesn't throw an exception, just great -.- instead it does nothing
  3. Nvm :) Throws a NullPointerException so the item is null if new ItemStack(this integer isn't valid) never mind guys!
  4. id are no longer used you should try other methods :)
  5. I just
    new Material(theId).name();
    } catch(NullPointerException e){
    //then it isn't a valid item id
  6. With that you heavily rely on something, which can change anytime, as it isn't specified in the API. For example in 1.8.7 it could throw a IllegalArgumentException or just return "AIR". You don't know.

    Better only use functions you can rely on: Material.getMaterial(id) == null when given invalid id.
    • Like Like x 1
  7. It is command based, generally it would be better that way, however what I am using it for is better with my method as the item is generated via command into a custom GUI, I don't think it would change if the event was cancelled all the time :p