1.16.5 Can't detect next empty slot

Discussion in 'Spigot Plugin Development' started by luan2809, Jul 7, 2021.

  1. Hi, I'm trying to find the next empty slot to put my item in it but it seem doesn't works, i tried inv.fristEmpty() too but it didn't work either.
    Here's the code.

    Attached Files:

  2. SteelPhoenix


    Inventory#firstEmpty() works, and has worked as intended for the past years, so you must have messed up somewhere.
  3. I think so but I has no idea where did I mess up.
    Here's my full Inventory code
  4. Where is the confusion? At the end of your init method, you loop over all slots until you find an empty one. You need to get rid of the for loop and break statement, but just that. Don't remove the if statement and inv#setItem inside the loop.
    Now instead of incrementing 'i' over a for loop, you can set 'i' to be the first empty slot returned by inv#firstEmpty(). If there is no empty slot available, it returns -1, so make sure that you check this before calling #setItem as well, otherwise you'll likely receive an IndexOutOfBoundsException.

    Basically this:
    Code (Java):
    for (int i = 0; i < 45; i++) {
        if (inv.getItem(i) == null) {
            inv.setItem(i, Items.teleport_point);
    ..should look something more like this:
    Code (Text):
    int i = get first empty slot
    if (i is not -1 && inv item in slot i is not null) {
        set item of inv slot i to items.teleport_point
  5. Thanks for your answer but it doesn't work, I tried getItem == null too but it still not work, here's my code.
    #5 luan2809, Jul 8, 2021
    Last edited: Jul 8, 2021
  6. No, it doesn't work for me too.
  7. Are there any errors on your console?
  8. There's no errors on my console, it just when i add new item to a empty slot, it replace the frist item in that inventory.
  9. You didn't remove the for loop like I suggested. We also need a bit more information besides "it doesn't work" - does it throw errors? Unexpected behavior? Nothing whatsoever?
  10. do you mean that?: the item you want to put appears when you add some other item to an empty slot on inventory
  11. You can watch this video, sorry if I records on my laptop, it will be very laggy. I will explain the video, "/tpp" to open Inventory and "/setp <name>" to add new item to the inventory. Frist I type "/setp a" to add an item name "a" to it and then "setp b" which should add an new item in next empty slot but now it replace the "a" and I want to fix that.
  12. Maybe you are creating a new instance of inventory everytime you set an item to the menu.
  13. Would it not be much easier to use the Inventory#addItem method?

    Code (Java):
    for (int i = 0; i < 45; i++) {
  14. Yes, that went right over my head.. but also, we still don't need the for loop :(
  15. Well, I did it before and there's no error on console but in Minecraft when I use "/tpp" and "/setp" it says "An internal error occurred while attempting to perform this command". And how can you add that sad icon, i can't do that.
  17. This is where my open Inventory code, I put it in another file. I tried to use HashMap but then I realized I always create new inv, are there anyways to fix it :unsure:

    Attached Files: