Solved Why do i get 4 messages on: for(ItemStack slot:player.getInventory().getContents()){

Discussion in 'Spigot Plugin Development' started by redstonebiten, May 11, 2016.

  1. question in title...
    Code:
    Code (Text):
                for(ItemStack slot:player.getInventory().getContents()){
                    if(slot !=null){
                        if(slot.getType() == Material.SKULL_ITEM){
                        }else{
    player.sendmessage("test");
                    }
                }
    And i get the message 4 times? Why?
     
  2. MiniDigger

    Supporter

    because you got 4 skulls in your inventory? ^^
    or the whole loop getts called 4 times, we will need for context then
     
  3. No? i got nothing in my inventory?

    it's sending the message if i DONT have skulls in my inventory
     
  4. And there is NOOOOO way it can be called 4 times?...
     
  5. If you're saying that you have an inventory full of skulls yet it still sends the message 4 times, I'm assuming that either the crafting table or armour contents are included in the Inventory#getContents()
     
  6. Non-skulls*
     
  7. Hmmm..
     
  8. Code (Text):
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
        Player player = (Player) sender;
        if(sender instanceof Player){
            if(player.hasPermission("MobHeads.sellall")){
                for(ItemStack slot:player.getInventory().getContents()){
                    if(slot !=null){
                        if(slot.getType() == Material.SKULL_ITEM){
                        }else{
                            player.sendMessage("Test");
                    }
                }
            }
        }
    }
    There is the code?..
     
  9. No i'm saying when i DONT have any skulls in my inventory AND when i have i get the message... it should only give me when i dont have... + it gives me it 4 times?
     
  10. MiniDigger

    Supporter

    oop misread that. That empty if case tho...
    add + slot.getType() to your test message to see what item there is. My guess would be its AIR, so you gotta ignore that case
     
  11. oh...wtf?

    https://gyazo.com/daedae7315cca89c1c317dff0f3dcc3f
     
  12. MiniDigger

    Supporter

    can't see that image, I am behind a company proxy. Go gotta embedd it into the img tags using the direct link so that I can see it (or just tell we what you are seeing)
     
  13. it does? o_O i don't see the problem in the code? xD
     
  14. It gives me the things i have in my inventory?
     
  15. MiniDigger

    Supporter

    what are you even trying to do? ^^
     
  16. In your hotbar, you have 4 items that are not skulls. In your code, you send a message whenever an item is not a skull. I don't see what the problem is, the code does exactly what it is supposed to do (as you are iterating over your inventory contents).

    What did you expect it would be doing?
     
  17. No i dont!?

    Code (Text):
    if(slot.getType().equals(Material.SKULL_ITEM)){
    //Do something but don't wanna show it..
    }else{
    player.sendmessage("You don't have any heads!");
     
  18. Did you want it to message you once if you have no skulls in your inventory?

    if so, you are sending the message after every checking every item. This is how your code is running:

    1) Looks at item 1,
    2) Checks if the item is a skull
    2a) if it is, do nothing
    2b) else, send the player a message.
    3) Looks at item 2,
    4) Checks if the item is a skull
    4a) if it is, do nothing
    4b) else, send the player a message

    etc. and repeats this for every item in your inventory
     
  19. @redstonebiten you made a mistake that a crap ton of people make.
    For EACH single item in the inv, you check if the type is a skull, and if it's not you print. You do this for EACH item, the else doesn't make it only do it if there are no skulls at all