Solved Problem with multipage Inventorys

Discussion in 'Spigot Plugin Development' started by David_Mayr, Feb 2, 2020.

  1. I have my own Inventory Class and own ItemStack class! They are called InventoryGUI and GUIItem!
    They both have an inventory and itemstack instance in them but the diference is that i can add a click callback to my items and the inventoryGUI manages these callbacks!

    So now i created an InfinityInventory class to make multipage inventorys!
    For the constructor it takes an GuiBuilder(normaly used to create inventoryGUIs but used here as our template, a list of GuiItems to fill the multipage inventory and an integer of the items per inv!

    My Init method:
    Code (Text):

    private void init() {
        this.inventoryGuis = new ArrayList<>();

        inventoryGuis.add(guiBuilder.build());
        int temp = stacksPerInv;

        for(int i = 0;i < itemStacks.size(); i++){
            if(i >= temp){
                inventoryGuis.add(guiBuilder.build());
                inventoryGuis.get(inventoryGuis.size()-1).addItem(itemStacks.get(i));
                temp = stacksPerInv * inventoryGuis.size();
            }else{
                inventoryGuis.get(inventoryGuis.size()-1).addItem(itemStacks.get(i));
            }
        }
    }

     
    additem:
    Code (Text):

    public void addItem(GuiItem item) {
        for (int i = 0; i < this.items.length; ) {
            if (this.items[i] != null) {
                i++;
                continue;
            }
            addItem(i, item);
            return;
        }
    }

     
    additem with integer:
    Code (Text):

    public void addItem(int i, GuiItem item) {
            this.items[i] = item;
            this.inventory.setItem(i, item.getItemStack().clone());
            item.setInventory(this);
    }

     
    i have set the items per inv to 5
    The result:
    http://prntscr.com/qweizy (The Left navigation is not filled so it looks a bit strange...)

    It creates 2 Pages(i need to fix the code so it makes 3 but thats not important now!)
    But all of the 2 pages have 12 Items...

    What is the problem?
     
  2. Your addItem is a method if the InventoryGUI or the InfiniteInventoy? Maybe you are adding the items to every inventory with that method. Can't you show more code?
     
  3. the addItem method is in the InventoryGUI! and in the init method it should only add it to one? Even if the inventory titles are changed the problem still exists!

    Here the initzialisation of the InventoryGui!
    https://pastebin.com/NVMXjyUq

    The inventory parameter is becouse of these two constructors!
    Code (Text):

    public GuiBuilderImpl(int size, MaschinenServerCore plugin) {
        this.plugin = plugin;
        this.items = new GuiItem[size];
    }

    public GuiBuilderImpl(Inventory inventory, MaschinenServerCore plugin) {
        this.plugin = plugin;
        this.tmp = inventory;
        this.items = new GuiItem[inventory.getSize()];
    }
     
    but i only used the one with the size so it created a new inventory!
     
  4. One recommendation, change
    Code (Java):
    if (inventory == null) {
                this.inventory = Bukkit.createInventory(null, this.items.length, this.name);
            } else {
                this.inventory = inventory;
            }
    for
    Code (Java):
    this.inventory = inventory == null ? Bukkit.createInventory(null, this.items.length, this.name) : inventory;
    Do you put the items on the inventories when you create the InfiniteInventoy (the constructor) or latter?
     
  5. Im quite confused on what you mean? so what is the problem? - is there any errors?
     
  6. There are no errors but it shouldnt put all the items in one inventory!

    the init gets called in the constructor

    Code (Text):

    public InfinityGUIImpl(GuiBuilder guiBuilder, List<GuiItem> itemStacks, int stacksPerInv, MaschinenServer maschinenServer) {
        this.guiBuilder = guiBuilder;
        this.itemStacks = itemStacks;
        this.stacksPerInv = stacksPerInv;
        this.maschinenServer = maschinenServer;

        init();
    }

    private void init() {
        this.inventoryGuis = new ArrayList<>();

        inventoryGuis.add(guiBuilder.build());
        int temp = stacksPerInv;

        for(int i = 0;i < itemStacks.size(); i++){
            if(i >= temp){
                inventoryGuis.add(guiBuilder.build());
                inventoryGuis.get(inventoryGuis.size()-1).addItem(itemStacks.get(i));
                temp = stacksPerInv * inventoryGuis.size();
            }else{
                inventoryGuis.get(inventoryGuis.size()-1).addItem(itemStacks.get(i));
            }
        }
    }
     
     
  7. bump

    If you have any questions about the code just ask...
     
  8. show us guiBuilder.build() please, that's the only point where I suspect something could be wrong. But then you said it generates 2 inventories, right?
     
  9. right! But both are fully filled!

    Code (Text):

    public InventoryGui build() {
        InventoryGui gui = new InventoryGuiImpl(this.plugin, this.items, this.name, this.tmp, this.closeEvent);
        return gui;
    }
     
    I looked now for 10 Hours at my code and i cant find the issue...
     
  10. so.. whats wrong with it? theres 12 items.. thats cool.. but why is that not what you want? whats the actual issue?

    /e nvm i read
    your code is really confusing to follow along. why cant you have one class for a multipage inventory, and have a multiarray of itemstack contents. #getContents, #setContents, ItemStack[][] pages,
    pages[page#][inventoryIndex] = item;
    on next page button -> inventory#setContents(pages[currentPage + 1])
     
  11. there should only be 5 in each inventory...
    The thing with the InventoryGUIs and GuiItems is that they have custom callbacks for click etc... so i dont need to make additional listeners...
     
  12. if more items than desired are being added to the inventory, see where youre adding to the inventory, and start printing everything you add to it. see if you have more items than you expect
     
  13. I FIXED IT!!!! The items werent cloned when doing build in the GuiBuilder so they were linked to all inventorys....