Solved Inventory GUI. Converting a list of ItemStack to GUI

Discussion in 'Spigot Plugin Development' started by minion325, Jun 7, 2018.

Thread Status:
Not open for further replies.
  1. Basically im trying to create an infinite gui but its kinda custom.

    Im using this method to update the GUI when an item is added or removed
    Code (Java):
    public void update() {
            Iterator<ItemStack> iterator = thisgetItems().iterator();
            pages.clear();
            int pagesNeeded = (int) Math.ceil(this.getItems().size() / 45.0);
            int currentItem = 0;
            for (int i= 0; i < pagesNeeded; i++){
                Inventory page = createPage();
                pages.add(page);
                while (iterator.hasNext()){
                    ItemStack itemStack = iterator.next();
                    System.out.println(itemStack + "in loop");
                    page.setItem(currentItem++,itemStack);
                    if (currentItem == 45) {
                        currentItem = 0;
                        break;
                    }
                }
            }
            for(Inventory inventory:pages){
                for(ItemStack itemStack : inventory){
                    System.out.println(itemStack != null ? itemStack:"none");
                }
            }
        }
    The output i get on running this is:
    Code (Text):
    [23:42:51] [Server thread/INFO]: ItemStack{SKULL x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=§askorpionx, lore=[§bClick for more info, §bBounty set by: minion325]}}in loop
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: ItemStack{PAPER x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=§aPrevious Page, lore=[§bGo to the previous page]}}
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: ItemStack{BARRIER x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=§4Close}}
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: none
    [23:42:51] [Server thread/INFO]: ItemStack{PAPER x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=§aNext Page, lore=[§bGo to the next page]}}
    from this.. i can tell that the item is not being added to the page. My question here is why?

    some of the code may be bad that is because ive been trying to solve this for a few hours now and im just trying anything i can think of.. ill look to clean up the code later
     
  2. My guess would be that you are breaking out of the wrong loop
     
  3. I'm guessing you're using Inventory#getContents() somewhere. I believe the method returns empty inventory slots as null.
     
  4. Uh, #getContents() isn't being used, it looks like the inventory is being looped over directly.

    And yes, empty inventory shoes should always be null. However, the issue is that they are still empty after the setup loops.
     
  5. It's not used explicitly in that snippet, but it might be part of a method he calls.
     
  6. Yes but I still don't see how that's relevant. We know they are empty because the code provided does not set every slot as OP wanted it to.

    Empty inventory slots should always be null.
     
  7. ? He asked why it wasn't working, so I stated why.
     
  8. The fact that they could be using #getContents() has nothing at all to do with their issue.
     
    • Optimistic Optimistic x 1
  9. Never mind what I said before. Either way, it's pretty much the same function and the empty inventory slots return null. He'd need to check if it's null, then set the ItemStack to air instead or skip the slot.
     
  10. I don't mind the slots being null..
    Look at the code everything you need is there. I tried to add a single item and yet it didn't work still
     
  11. I agree.. i have a problem setting items not getting them. Take a look at the output. The inventory does contain items(only the ones needed for the player to change pages etc not the items that I added)


    In the output it prints the itemstack to be added as a string followed by the words in loop. This does happen which means that the item is supposed to be added to the inventory right after. Im puzzled as to why it doesn't.
     
  12. I ran some tests, and this works fine:
    Code (Java):
        @EventHandler
        public void onSneak(PlayerToggleSneakEvent e){
            List<Inventory> pages = new ArrayList<>();
            Iterator<ItemStack> iterator = this.getItems().iterator();
            pages.clear();
            int pagesNeeded = (int) Math.ceil(this.getItems().size() / 45.0);
            int currentItem = 0;
            for (int i= 0; i < pagesNeeded; i++){
                Inventory page = createPage();
                pages.add(page);
                while (iterator.hasNext()){
                    ItemStack itemStack = iterator.next();
                    System.out.println(itemStack + " in loop");
                    page.setItem(currentItem++,itemStack);
                    if (currentItem == 45) {
                        currentItem = 0;
                        break;
                    }
                }
            }
            for(int i = 0; i < pages.size(); i++){
                for(int j = 0; j < pages.get(i).getSize(); j++){
                    ItemStack itemStack = pages.get(i).getItem(j);
                    System.out.println(i + " " + j + " - " + (itemStack != null ? itemStack : "none"));
                }
            }
        }

        private Inventory createPage() {
            Inventory i = Bukkit.createInventory(null, 54, "Test inventory");
            i.setItem(45, new ItemStack(Material.PAPER));
            i.setItem(49, new ItemStack(Material.BARRIER));
            i.setItem(53, new ItemStack(Material.PAPER));
            return i;
        }

        private List<ItemStack> getItems() {
            List<ItemStack> l = new ArrayList<>();
            l.add(new ItemStack(Material.DRAGON_EGG));
            return l;
        }
    Prints everything exactly as expected. I did not change anything about the code. However, when I first started testing it I used Material.SKULL in getItems(), and I had the same issue you did. After changing to Material.DRAGON_EGG, it worked perfectly.

    Oh, I just realized - Material.SKULL is the block form of the item. You need Material.SKULL_ITEM for it to be added properly.
     
    • Useful Useful x 1
  13. Setting the item stack to air will just make it null again. Empty inventory slots are always null. (Or should be, the API isn't entirely consistent, and that will be fixed in 1.13 IIRC). The issue was not that they were null. The issue was that they were empty. (In this case because they tried to set a block item in an inventory.)
     
    • Useful Useful x 1
  14. Tysm. Its solved now ty. Will mark this as solved and give you a rating :)
     
    • Like Like x 1
Thread Status:
Not open for further replies.