Solved Issue with MaxStackSize in Custom Inventory

Discussion in 'Plugin Development' started by Demyxa, May 9, 2022.

  1. Offline

    Demyxa

    Hullo there!

    Simple recap:
    I have a bunch of custom items, which can be duplicate (It's a card game plugin so of course you can have the same card twice.)

    A player's deck is a custom inventory which I serialize and de-serialize when needed.
    The actual Bukkit.Inventory is set to have a MaxStackSize of "1", as a player's deck is supposed to be limited to 18 slots. So, 1 Card per slot.

    This all works well enough, until you try to have the same card inside your deck twice.
    When saving the deck, the card DOES show up twice in the .yml file, however when opening the deck and desrialising the items, I think what's happening is that the game tries to stack the 2 identical cards ontop of eachother which it obviously cannot do, as the MaxStackSize of the Inventory is "1". You'd think it would take the next best empty slot but it doesn't. Instead, the 2nd card is simply not added to the inventory.

    I've looked at some solutions but none of them seem possible.

    Code:
    Inventory deckInv = Bukkit.createInventory(null, 18, "Deck von " + player.getDisplayName());
                        ArrayList<Map<String, Object>> deck =  GetterSetter.getDeck(player.getName());
                       
                        for (int i = 0; i < deck.size(); i++) {
                           
                            ItemStack card = ItemSerialiser.MapToItem(deck.get(i));
                           
                           
                            deckInv.addItem(card);
                        }
                       
                        player.openInventory(deckInv);
    This is the code I use to open the deck. (It would all be so simple if I could do "deckInv.addItem(card, index)" but noooo ;-;)
     
  2. Offline

    CraftCreeper6

    @Demyxa
    Try deckInv.setItem(slot, itemStack) instead.
     
    Demyxa likes this.
  3. Offline

    Demyxa

    Here Sir, my Bukkit License.
    I don't think I'm allowed to have it anymore. (Thank you Sir, thank you. Somehow I completely overlooked that in the available methods.)

    [​IMG]
     
  4. Offline

    CraftCreeper6

    @Demyxa
    I'll just hand that right back with "provisional" written on it ;)
     

Share This Page