Solved Checking if a tool is damaged or has lost durability

Discussion in 'Plugin Development' started by Epicballzy, Jul 22, 2014.

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

    Epicballzy

    Hey guys, I need to check if an item (Tool) in the players inventory has loss of durability. I need this for a shop. My shop works fine, but I would like to add something where if the tool they're trying to sell has lost some durability, they will receive a message saying "You can't sell {ITEM}. It has lost it's durability" or something along the line.

    So how would you make a check to see if the tool that they're selling, has lost some durability or is damaged?
     
  2. Offline

    CorrieKay

    Minecraft counts durability from 0 upwards. So, if you have an item stack they're trying to sell, check if the durability is greater than 0. If so, its been used. If not, then its brand new.

    You can get the durability by checking itemStack.getDurability()
     
  3. Offline

    mythbusterma

    Get the ItemStack you're looking for and call getDurability()
     
  4. Offline

    Epicballzy

    CorrieKay mythbusterma Thanks. But one more question. How would I get the item (Item the player is selling)
    from a line off a sign?

    This is what I use to check if the player has the item, and remove the item:
    Code:java
    1. if(player.getInventory().contains(Material.getMaterial(sign.getLines()[2].toUpperCase()))) {
    2. player.getInventory().removeItem(new ItemStack[] {new ItemStack(Material.getMaterial(sign.getLines()[2].toUpperCase()), 1) });
    3. CurrencyUpdates.sell(player, Integer.parseInt(sign.getLine(3)));
    4. player.updateInventory();
    5. MavenCurrency.saveFile();
    6. }
     
  5. Offline

    mythbusterma

    Epicballzy

    Ask for the durability of that same ItemStack that you are removing.
     
  6. Offline

    JRL1004

    CorrieKay mythbusterma Would that work if the player is trying to sell something such as colored wool? From what I understand, many blocks' durabilities are their meta value. You also can't fix this using getType().isBlock() or something like that as it still leaves charcoal broken. I think it would make more sense to check if the ItemStack's MaterialData is instanceof Repairable before doing a durability check (that's assuming the MaterialData would store if it's repairable and it's not stored another way).
     
  7. Offline

    mythbusterma

    Durability != Block Data.
     
  8. Offline

    CorrieKay

    Its likely going to be a much better option to require the player to hold the item before doing anything. This allows you to easily get the item stack, and allow the player to make the choice which item theyre trying to sell (dont want to accidentally remove their just-crafted and enchanted diamond pickaxe instead of the non-enchanted one!).
     
  9. Offline

    JRL1004

    mythbusterma Both ItemStacks and Blocks have MaterialData, not just one of them.
     
  10. Offline

    mythbusterma

    JRL1004

    Sorry, MaterialData, doesn't change anything though.
     
  11. Offline

    Epicballzy

Thread Status:
Not open for further replies.

Share This Page