Best way to check if item is a tool?

Discussion in 'Plugin Development' started by Specops343, Sep 24, 2011.

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

    Specops343

    What is the best way i can check that an item is a tool? I cannot use <= data values because I need to use those with the plugin also. Any suggestions?
     
  2. Offline

    ItsHarry

    Hmm.. I don't know of any other ways to do it then using a switch
     
  3. Offline

    Nitnelave

    You can make a list containing all of the tools item ID, and check if the list.contains(item.getTypeId()). Though I think it woukd be simpler to check for data values, and if there are some, check against a list of items (id>127 I believe) that have data but are not tools (like dyes).
     
  4. Offline

    Specops343

    Yeah, thats what I started doing, and wished there was a better way :( Anyways, thanks guys, ill guess ill just continue down that path.

    @ItsHarry
    @Nitnelave

    I ended up doing that, but for some reason, only the last 5 numbers of the list are erroring in eclipse, and giving me unresolved compiling errors in-game :
    Code:
    if (event.getItem().getType().getMaxDurability() < 30 && event.getItem().getTypeId() != 256,257,258,259,267,268,269,270,271,272,273,274,275,276,277,278,279,283,284,285,286,290,291,292,293,294,298,299,300,301,302,303,304,305,306,307,308,309,310,311312,313,314,315,316,318) {
                        event.getPlayer().sendMessage("[" + ChatColor.YELLOW + "Signshop" + ChatColor.WHITE +"]" + ChatColor.RED + "Equip the item you wish to sell.");
                        return;
                    }
                    if (event.getItem().getType() == Material.AIR || event.getItem().getDurability() != (short) 0) {
                        event.getPlayer().sendMessage("[" + ChatColor.YELLOW + "Signshop" + ChatColor.WHITE +"]" +ChatColor.RED + "Your items must be undamaged to be able to be sold.");
                        return;
                        //if (event.getItem().getTypeId() != 256,257,258,259,267,268,269,270,271,272,273,274,275,276,277,278,279,283,284,285,286,290,291,292,293,294,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,318)
                }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  5. Offline

    ItsHarry

    311312 needs to be 311, 312
    (first line)
     
  6. Offline

    Specops343

    Hmm, corrected that, but it still wants me to delete 312-317. Says there is a syntax error, not exactly sure how that is possible.
     
  7. Offline

    ItsHarry

    Hmmmm.. Source code + stack trace please?
     
  8. Offline

    Specops343

  9. Offline

    ItsHarry

    Can you even use commas in an if statement? O_O.
    Maybe you should just use a switch
     
  10. Offline

    Specops343

    Alright, thanks, I'll give that a go.
     
  11. Offline

    Nitnelave

    You should create a final list new List(Arrays.asList(1, 2, 3, ...))
    and then check if list.contains(item.getTypeId())
     
  12. Offline

    Specops343

    Any idea what the highest durability an item has? I mean, do blocks like dirt/stone whatever have a durability?
     
  13. Offline

    DrAgonmoray

    I think blocks like dirt have a durability/data/damage/whatever of 0.
     
  14. Offline

    Afforess

    Easy way with Spout.

    Item item = MaterialData.getItem(id, data);
    if (item instanceof Tool) {
    //do stuff
    }
     
    r3Fuze likes this.
  15. Offline

    Nitnelave

    I think the highest it can get is 15 (wool or dye) for a non-tool.
     
  16. Offline

    Specops343

    Code:
                    }
                    if (event.getItem().getType().getMaxDurability() < 30) {
                    //if (event.getItem().getType().getMaxDurability() < 30 && event.getItem().getTypeId() != 256,257,258,259,267,268,269,270,271,272,273,274,275,276,277,278,279,283,284,285,286,290,291,292,293,294,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317) {
                        event.getPlayer().sendMessage("[" + ChatColor.YELLOW + "Signshop" + ChatColor.WHITE +"]" + ChatColor.RED + "Equip the item you wish to sell.");
                        return;
                    }
                    if (event.getItem().getType() == Material.AIR || event.getItem().getDurability() != (short) 0) {
                        event.getPlayer().sendMessage("[" + ChatColor.YELLOW + "Signshop" + ChatColor.WHITE +"]" +ChatColor.RED + "Your items must be undamaged to be able to be sold.");
                        return;
    Any ideas on why I might be getting an NPE on if(event.getItem().getType().getMaxDurability() < 30) ?
     
  17. Offline

    Nitnelave

    Still trying to recognize a tool? Check if it has durability (unless you want to differentiate unbroken tools too) and then check against a list with dyes, and the other few items with durability that are not tools.
     
Thread Status:
Not open for further replies.

Share This Page