Solved Replacing Material.AIR

Discussion in 'Spigot Plugin Development' started by BiastGaming, May 25, 2016.

Thread Status:
Not open for further replies.
  1. How would I look through an inventory and replace Material.AIR with a specified item
     
  2. Use a for-loop to go through the inventory and check the material of each item. If you find an item that has Material.AIR, replace it with what you want.
     
  3. or null, the inventory is stored with a lot of null, not air.
     
  4. In that case make sure you use '.equals()' not '=='. The double equals can only be used to compare primitive values not whole objects.
     
  5. if you're looking to replace it
    if(Material == null || Material == Material#AIR) // do stuff
     
  6. Yea, you can use the double equals for the material because material is just an ENUM with a integer value linked to it, hence the reason you can do the double equals. I mean if you wanted to do your null method, then you are checking the entire ItemStack, not just the material which means you would have to do '.equals()'. I don't believe doing 'Material == null' works either.
     
  7. I want it to be in an inventory so I guess I meant null. I want to change all null items to be a glass pane. This is my current code but I would need the inventory number which is null to set the item.
    Code (Text):
            for (ItemStack item : tags.getContents()) {
                if (item.equals(null) {
                 
                    tags.setItem(arg0, arg1);
     
    #7 BiastGaming, May 25, 2016
    Last edited: May 25, 2016
  8. As I said before, because ItemStacks are not primitive data types, you have to use '.equals' instead of '=='. If you do that, your code should work :)
     
  9. you can use both and the inventory is not just full with null, can also contains Material.AIR
     
  10. Code updated but how do I get the position of the null item so i can replace it
     
  11. Doing a for-loop with an increasing variable can allow you to do that:
    Code (Text):
    for (int i = 0; i < inventory.getSize() - 1; i++) {
        if (inventory.getItem(i).equals(null) {
            //Do stuff for null item. Replace with glass
        }
    }
     
  12. that is so false... then you're doing null#eqauls(null) so whenever you null check you must use '==' or '!=' since null is a base thing.
    You're right with the code except do this:
    Code (Java):
    for (int i = 0; i < inventory.getSize(); i++) {
        if (inventory.getItem(i) == null || inventory.getItem(i).getType() == Material.AIR) {
            //Do stuff for null item. Replace with glass
        }
    }
    First issue is the less than sign, whenever it is less than the size it will work, it will not go to 54 if the size is 54, it will stop at 53 which is what you want and you need == or != for null types. Next you'll want to check if it is air, JIC. That code should fully work.
    I know, that's why I have my code checking for both cases
     
Thread Status:
Not open for further replies.