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

save player inventory like another object

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

  1. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    how can i save PlayerInventory object without holder only whole inventopry because i want to save player inventory and add another items. But when i make $inv = $p->getInventory() i can´t add items into saved inventory without add items to real player´s inventory. and then i can make
    PHP:
    $inv saved inventory;
    $p->getInventory(setContents($inv->getContents()));
  2. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    can i use clone $p->getInventory() ?
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Don't! That will result in memory leaks.
    Save it item by item. Convert each item between array and Item object (save ID, damage and count in array) and save it using a data serialization language like YAML or JSON.
  4. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    i know but it is harder get free slots or adding items to the saved inventory and i want to save inventory only for a while so memory will free in few seconds
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Btw, what AREmemory leaks in PM?
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Saving a reference to an object that should be garbaged. For example, referencing to a player object that should be garbaged after player quit.
    Legoboy0215 likes this.
  7. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    so how can i add item to saved inventory in array[$slot => $item]
  8. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    is it right?
    PHP:
    foreach($inv as $slot => $item){
        if(
    $item->getId() === 0){
            
    $item $CustomItem;
        }
    }
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    If you mean to save it into the harddisk, save it in a YAML or JSON file.
    PHP:
    function saveInventory(Inventory $inventory$file){
      
    $items = [];
      foreach(
    $inventory->getContents() as $slot => $item){
        
    $items[$slot] = [$item->getId(), $item->getDamage(), $item->getCount()];
      }
      
    file_put_contents($filejson_encode($items));
    }
    function 
    readInventory(Inventory $inv$file){
      
    $items json_decode(file_get_contents($file));
      
    $inv->setContents([]);
      foreach(
    $items as $k => $v)
        
    $inv->setItem($k$v);
    }
  10. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    no i want to save it only for few time. I want to make shop and i need to check if saved inventory contains specified amount of specified item and if yes so ass some Item to saved inventory.
    my not working code:
    PHP:
    public function clickItem(Player $pItem $buyItemItem $costItem$inv){ //$inv is saved inventory
            
    $buy false;
            foreach(
    $inv as $slot => $item){
                if(
    $buy === false){
                    if(
    $item->getId() === $costItem->getId() && $item->getDamage() === $costItem->getDamage() && $item->getCount() >= $costItem->getCount()){
                        
    $inv[$slot] = Item::get($costItem->getId(), $costItem->getDamage(), $item->getCount() - $costItem->getCount());
                        
    $buy true;
                        continue;
                    }
                }
                if(
    $item->getId() === Item::AIR or $item->getCount() <= 0){
                    
    $item $buyItem;
                    
    $p->sendMessage($this->plugin->plugin->getPrefix().TextFormat::GREEN."Koupil jsi {$buyItem->getName()})");
                }
            }
            
    $p->sendMessage($this->plugin->plugin->getPrefix().TextFormat::RED."Nemas dostatek surovin");
        }
    i think check if player have space in saved inventory doesn´t work
    Last edited: Jul 24, 2015
  11. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    Use code from LIAB (by aliuly)
  12. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    What there is not any code which i want .
  13. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    You can copy and edit functions loadInventory() and saveInventory()
  14. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    Read thread again ;)
  15. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    If you're doing BedWars Shop (I think so), I think that you can save player inventory, then he buys something an you load his inventory and add item in it. It's method I use in BedWars
  16. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    i need to add item to saved inventory
    Last edited by a moderator: Jul 24, 2015

Share This Page

Advertisement