[Util] Quick/easy/slightly dirty way of checking if an ItemStack is a Tool

Discussion in 'Resources' started by MakerofRobots, Aug 2, 2013.

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

    MakerofRobots

    Made this today while working on my plugin, thought I'd share.

    EDIT: use Enchantment.DURABILITY.canEnchantItem(item here)

    desht timtower Fixed it guys, thanks for telling me off ;)

    Its setup right now to get a list of all tools in a players inventory.
    Code:java
    1. List<ItemStack> tools = new ArrayList<ItemStack>();
    2. for (ItemStack i : player.getInventory().getContents()) {
    3. if (i!=null) {
    4. boolean enchants = true;
    5. try {
    6. Map<Enchantment, Integer> enchList = i.getEnchantments();
    7. i.addEnchantment(Enchantment.DURABILITY, 1);
    8. i.removeEnchantment(Enchantment.DURABILITY);
    9. i.addEnchantments(enchList);
    10. } catch (Exception ex) {
    11. enchants = false;
    12. }
    13. if (enchants) tools.add(i);
    14. }
    15. }
     
    timtower and ampayne2 like this.
  2. Offline

    timtower Administrator Administrator Moderator

    MakerofRobots How about this:
    Code:java
    1. List<ItemStack> tools = new ArrayList<ItemStack>();
    2. for (ItemStack i : player.getInventory().getContents()) {
    3. if (i!=null) {
    4. boolean enchants = true;
    5. if(Enchantment.DURABILITY.canEnchantItem(i)){
    6. tools.add(i);
    7. }
    8. }
    9. }
    10. }
     
  3. Offline

    desht

    Using exceptions for control flow is horrendously bad programming practice.
     
  4. Offline

    MakerofRobots

    I didn't realize that existed. Thanks so much.
     
  5. Offline

    Ultimate_n00b

    So it is bad to use exceptions for checking integers?
     

  6. Like Ultimate_n00b said, what about if you want to use it for like isInteger.
    Code:
    public boolean isInteger(String aString) {
         try {
              Integer.parseInt(aString);
              return true;
         } catch (Exception ex) {
              return false;
         }
    }
    
    What's another way to do this?
     
  7. Offline

    chasechocolate

    KingFaris11 that's fine, however, you could also catch a NumberFormatException.
     
    Ultimate_n00b likes this.
  8. Offline

    desht

    It depends. If you're reading the odd integer from player-typed input, then catching an exception is OK. On the other hand, if you're reading thousands of lines from a file, and attempting to parse integers from them where there may be a lot of non-integers, it will destroy your performance. In that case, test with a regex first, then use Integer.parseInt() without the try/catch.

    KingFaris11 I would do this:

    PHP:
    public boolean isInteger(String s) {
      return 
    s.matches("^\\d+$");
    }
     
    Ultimate_n00b likes this.

  9. Oh okay thanks =P

    I have a habit of using Except instead of BlahblahException. It's easier for me as most of the exceptions I handle by ignoring it and/or printing a custom message (Not ex.printStackTrace()).
     
  10. Offline

    Ultimate_n00b

    Be right back, I gotta go upload a few jars to bukkitdev.
     
    Garris0n likes this.
  11. Offline

    EhudBlum

    I just use this:
    Code:
    Material.{blabla}.getMaxDurability() > 0
     
  12. Offline

    Ultimate_n00b

    But, it works with swords too.
     
Thread Status:
Not open for further replies.

Share This Page