Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved add Item into saved inventory

Comments in 'Plugin Development' started by Creeperface, Jul 24, 2015.

  1. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    i save player inventory into array($slot => $item) but i need to add item to it.
    PHP:
    foreach($p->getInventory()->getContents() as $slot => $item){
        
    $this->savedInv[$slot] = $item;
    }
    i tried code from BaseInventory but it doesnt work it says undefinet offset 1 but player inventory has 36(0-35) slots right? or have you any good idea how to do it?

    PHP:
    public function addItem($inv$slots){ //$inv is saved inventory
        
    $itemSlots = [];
        foreach(
    $slots as $slot){
            if(
    $slot instanceof Item){
                if(
    $slot->getId() !== and $slot->getCount() > 0){
                    
    $itemSlots[] = clone $slot;
                }
            }
        }
        
    $emptySlots = [];
        for(
    $i 0$i 35; ++$i){
            
    $item $inv[$i];
            if(
    $item->getId() === Item::AIR || $item->getCount() <= 0){
                
    $emptySlots[] = $i;
            }
            foreach(
    $itemSlots as $index => $slot){
                if(
    $slot->equals($itemtrue)){
                    
    $amount min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), 64);
                    if(
    $amount 0){
                        
    $slot->setCount($slot->getCount() - $amount);
                        
    $item->setCount($item->getCount() + $amount);
                        
    $inv[$i] = $item;
                        if(
    $slot->getCount() <= 0){
                            unset(
    $itemSlots[$index]);
                        }
                    }
                }
            }
            if(
    count($itemSlots) === 0){
                break;
            }
        }
        if(
    count($itemSlots) > and count($emptySlots) > 0){
            foreach(
    $emptySlots as $slotIndex){
       
    //This loop only gets the first item, then goes to the next empty slot
                
    foreach($itemSlots as $index => $slot){
                    
    $amount min($slot->getMaxStackSize(), $slot->getCount(), 64);
                    
    $slot->setCount($slot->getCount() - $amount);
                    
    $item = clone $slot;
                    
    $item->setCount($amount);
                    
    $inv[$slotIndex] = $item;
                    if(
    $slot->getCount() <= 0){
                        unset(
    $itemSlots[$index]);
                    }
                    break;
                }
            }
        }
        }
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You want to let the Player have two inventories?
  3. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    yes for a while i want to make shop in inventory instead of signs it is better
  4. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    can you help me please? :)
  5. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
  6. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    I need to special shop for bedwars but i look at your code
    Can you send me line please?
    Last edited: Jul 25, 2015
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    jojoe77777 and Creeperface like this.
  8. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    :D and i know $inventory->getContents() return only slots with item not free slots :D
  9. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    hm i can check if saved inventor contains item but i can´t change anything :/ why?
    PHP:
    foreach($inv as $slot => $item){
                if(
    $item->getId() === $costItem->getId() && $item->getDamage() === $costItem->getDamage() && $item->getCount() >= $costItem->getCount()){ //works
                    
    $inv[$slot] = Item::get($costItem->getId(), $costItem->getDamage(), $item->getCount() - $costItem->getCount()); //doesn´t work
                    
    $p->sendMessage($this->plugin->plugin->getPrefix().TextFormat::GREEN."Koupil jsi {$buyItem->getName()}");
                    return;
                }
            }
  10. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    i clonned inventory it is best way :D
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I think you should just make a CustomInventory instance and put the contents into it and use it like a normal inventory, and store back the items when appropriate.
  12. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    it is better than clone PlayerInventory?
  13. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    and CustomInventory is Abstract class i can´t make instance
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Make a subclass.
    It is never a good idea to clone an Inventory. Say, for example, doing transactions on it will send it to the player.
    Creeperface likes this.
  15. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    but when i add item to cloned inventory so it doesnt add item to real player inventory right?
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It will still get sent to the Player because a PlayerInventory has reference to the same player. You don't understand cloning very well.
    Creeperface likes this.
  17. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    Hm when i clone some object it is absolutely same right? And so should i make class extending custominventory?
  18. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes and yes. But you are cloning a PlayerInventory that has a pointer to the same player object. A PlayerInventory sends its contents to its pointing player object when it has transactions.
    Creeperface likes this.
  19. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace

Share This Page

Advertisement