Solved Check for different wood/spawnegg/slab types in inventory?

Discussion in 'Plugin Development' started by UltiByte, Sep 3, 2013.

Thread Status:
Not open for further replies.
  1. Hello!

    I'm currently developing a plugin where I need to check whether the type of item in the player's inventory (such as wood type, wool colour, slab type, dye colour, spawn egg type etc.) matches a particular one. So there'd be a bit which would check if they had a creeper egg in their inventory for example.

    My problem is I can't seem to make the plugin check for the type of wood, egg, whatever, it is. I want it to do a check to see if the type of it they have in their inventory matches the one it's checking for.

    I have tried various different things with no success, scoured these forums, and asked 2 of my friends who are also plugin developers, neither of whom knew how to do this.

    This is one method I tried for checking for the item type. Before the parameters are passed into this, I also tried to get its data in another method by using getData() , which I'm still not sure about, But ideally I just want to do: if (player.getInventory().contains(Material.WOOD//then something here to check if it is of a particular type, such as birch))
    (Though if this is not possible I'm happy to use a different way which you might suggest.)
    The literalName in the following code is the name Bukkit uses for the material, like "FURNACE" for example, and this is passed in correctly by an external method as well.
    Code:java
    1. private boolean sellMethod(Player player, String literalName) {
    2. for (Integer key : player.getInventory().all(Material.matchMaterial(literalName)Specifying the particular type here).keySet()) {
    3. // It should remove one of this particular item from their inventory here.
    4. }
    5. }


    If you can help with either the checking for the particular type of item or the removing it part, or both, that would be greatly appreciated.
     
  2. Offline

    Tarestudio

    UltiByte
    Wool is always Material.WOOL, the Type is the same for every color. What you are looking for ist the .getData() that have to match as well.
     
  3. Thanks. Could you, or anyone else, give me an example? How, for example, would I check if the player's inventory contained birch wood planks?
     
  4. Offline

    jayfella

    Code:java
    1.  
    2. if (itemstack.getData() instanceof Tree)
    3. {
    4. Tree tree = (Tree)itemstack.getData();
    5. TreeSpecies species = tree.getSpecies();
    6. }
    7.  
     
  5. Offline

    Tarestudio

    UltiByte
    You would loop through all slots of the inventory, check the Type for being wood, and then check .getData().getData() or .getDurability() for being 2
    .getData().getData() gives you a byte, its how the data of an item is stored
    .getDurability() gives you a short. It is the corresponding value to the first method.
     
    UltiByte likes this.
  6. Awesome! Thank you! That worked perfectly. I now have another dilemma which I'll elaborate on at the end of this post. :p

    This is what I did. It's linked in with a lot of other stuff, but here's the most relevant bit of code I made which used what you suggested. This works perfectly from what I can observe:
    Code:java
    1. private boolean checkIfCorrectData(int dataValue, Player player, String literalName, Material material) {
    2. boolean theyHaveIt = false;
    3. for (Integer key : player.getInventory().all(Material.matchMaterial(literalName)).keySet()) {
    4. if (player.getInventory().contains(Material.matchMaterial(literalName)) && player.getInventory().getItem(key).getDurability() == dataValue) {
    5. theyHaveIt = true;
    6. }
    7. }
    8. if (theyHaveIt == true) {
    9. return true;
    10. }
    11. return false;
    12. }

    My problem now is checking how much of this item they have in their inventory. So, say they had 6 birch wood for example. (This applies however for all items which have different variants such as slabs, eggs or whatever). Right now, my plugin can tell that they have birch wood, but it cannot also tell how much birch wood they have. How can I achieve this? Right now I'm using this:
    Code:java
    1. public static int getAmount(Player player, int id) {
    2. PlayerInventory inventory = player.getInventory();
    3. ItemStack[] items = inventory.getContents();
    4. int has = 0;
    5. for (ItemStack item : items) {
    6. if ((item != null) && (item.getTypeId() == id) && (item.getAmount() > 0)) {
    7. has += item.getAmount();
    8. }
    9. }
    10. return has;
    11. }


    Any ideas?

    Thanks again :p

    I solved the second question myself =D

    Code:java
    1. public static int getAmountExtData(Player player, int id, int dataValue) {
    2. PlayerInventory inventory = player.getInventory();
    3. ItemStack[] items = inventory.getContents();
    4. int has = 0;
    5. for (ItemStack item : items) {
    6. if ((item != null) && (item.getTypeId() == id) && (item.getDurability() == dataValue) && (item.getAmount() > 0)) {
    7. has += item.getAmount();
    8. }
    9. }
    10. return has;
    11. }


    Thank you all who contributed to this thread for your help :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page