Getting Size of Inventory

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

Thread Status:
Not open for further replies.
  1. I am not a computer where I can test this, and I always forget when I need it. When making a for-loop to iterate though an inventory, you can use:
    Code (Text):
    for (int i = 0; i < p.getInventory().getSize(); i++)
    However, does spigot start its size count at 0 or 1, because if they start it at 1 i'd have to do 'getSize() - 1', but I can never remember which one it is for inventory size. I always remember that for some reason Spigot starts at 0 and then I have to change my code, but everything else starts at 1 for size so I always forget.
     
  2. It should start at 0. You could print out the size to see where it starts.
     
  3. I would, but I'm not at a computer that I can test at the moment.


    You'd think size would be multiples of 9 because each row has 9 slots, but I always seem to remember that getSize() starts at 0 and goes to 8, instead of 1 to 9.
     
  4. This is correct. Java is built around Zero-based indexing.
     
    • Agree Agree x 1
  5. Don't all things relating to size start at 1 though, it's only when you iterate over it that you start at 0. If it starts at 0, then getSize() should really be named, getLastSlotNumber() or something like that.

    Hypothetically, if you had an inventory with a single slot, you wouldn't say its size is 0, you'd say its size is 1.
     
  6. Yes, that's correct. In that case you're not getting an index, though, so what @Cewlt said is correct.
     
  7. Ok, so just some awkward naming. So for anyone else wondering, getSize() starts at 0 and goes to 8, even though there are 9 slots per row.
     
  8. The getSize() should start at 1 instead of 0, but I guess something got messed up or the method name is a bit off because (I believe) it should work like '.length()' does for command arguments where size is the amount of arguments, but args[0] will get you the first argument.
     
    #8 Hunky524, May 25, 2016
    Last edited: May 25, 2016
  9. The length variable starts at zero.
     
  10. It starts at 0, because slot 4 is the middle of the first line.
     
  11. if(args.length == 0){
    player.sendMessage("This has NO ARGUMENTS");
    }
    if(args.length == 1){
    player.sendMessage("This has ONE ARGUMENT, it starts at 1, not 0");
    }
     
  12. Doesn't the length start at 1? Because if you do '.length()' with a single argument it will return 1.
     
  13. This little difference between '.length()' starting at 1 and '.getSize()' starting at 0 is why I always forget where size starts at. I think I can remeber now though after this thread.
     
  14. It still starts at zero? I don't see your point. It's a literal size.
     
    • Agree Agree x 1
  15. The length does start at anything, but it does count from 1, but since you are not using less than or equal to, you should be fine if you start from 0. Because if the size is 54, it will stop at 53, which is the last index.
     
    • Agree Agree x 2
  16. sothatsit

    Patron

    Size and length will return the same sort of thing. The only difference is the name.

    Think about it this way:
    If the size started at 0, then for a 0 length array (or 0 sized inventory) it would have to return -1 as the length, now thats not very intuitive is it? It is also non intuitive for using the length of the array to display to the user, as how are they supposed to know that it starts at -1?

    This also allows easy looping between them just using less than rather than <=.

    By using less than the index will never be greater than or equal to the size, so your index will go in the range 0 -> (size - 1).

    99% of things use 0 based indexing, so you better get used to it, and anything which does not i find incredibly annyoing.

    If you want to know why we use 0 based indexing, heres a basic understanding:
    Arrays are kept linearly in memory, that means 1 array in memory is 1 big block. This is useful because then to figure out where a piece of the array is when we want to access it, we can move directly to the value using our index. To understand this more, ill explain that all your variables in your code have pointers, these pointers point (hence the name) to a piece of memory where the information for that variable is stored, so we can read and modify it. Now for arrays we don't want to store a pointer for every index in the array, that would be a massive memory waste (and its also very impractical), so instead we store the index for the start of the array, and then we can use pointer arithmetic to access the element we want. This basically means we add the index times the size of each element in the array in bytes to get the pointer to a specific entry in the array. This is why 0 based indexing is used, as otherwise we would have to have a pointer to the memory 1 element to the left of where it starts (which contains completely different information, nothing to do with the array) or we would have to subtract 1 from the index every time we want to use it. Maybe this makes you appreciate why indexes traditionally start at 0, and due to this tradition, and the un-needed subtraction, why this will not change in the foreseeable future.
     
    #16 sothatsit, May 25, 2016
    Last edited: May 25, 2016
  17. Choco

    Moderator

    Length vs. Index
    Keep that in mind. Indexes start at 0. Though if you're attempting to get the length or size of something, it's going to be its literal size

    EDIT: Okay that's twice I've been sniped by @sothatsit xD I give up
     
    • Funny Funny x 2
  18. The flip of that though is, if you have an inventory with a single slot, you can get the first item from it using 'inventory.getItem(0);', but if you did '.getSize()' on that same inventory, you would get 0 back, which also seems odd because that makes it seem like your inventory has a no size. It's just the wording of the method that throws me off; seems like it should be named 'getLastSlotNumber()' or something like that so it's clear we're using zero-based indexing and not getting the amount of slots.

    However, after this thread, I will always remember '.getSize()' starts at 0 not 1.
     
  19. You would get "1" not "0", so it would throw an exception. look back at my comment :p
     
  20. You said I should be in your first comment, but in your second you said it would throw an exception?
     
Thread Status:
Not open for further replies.