1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Inventory Plugin Help

Discussion in 'Development' started by Emis02, Jun 19, 2019.

  1. Emis02

    Emis02 Spider Jockey

    Messages:
    34
    GitHub:
    RubyTemple
    Hi.
    How may I save an inventory for a world and then clear it, and if then the player back to that world, load it?
    Example: If player was in world "factions" and he run hub, save his inventory and clear it and then he go to the world "factions" it loads the factions' inventory.
     
  2. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    PHP:
    <?php

    public $invs = [];

    public function 
    saveInv($player) : bool{
        
    $this->invs[$player->getName()] = $player->getInventory()->getContents();
        return 
    true;
    }
    public function 
    getInv($player){
        if(isset(
    $this->invs[$player->getName()])){
        return 
    $this->invs[$player->getName()];
        }
        return 
    null;
    }
    public 
    $world = [];

    public function 
    onMove(PlayerMoveEvent $e){
    $p $e->getPlayer();
    $m $p->getLevel()->getName();
    //If use more worlds use: if(in_array($m, array("world1", "world2", "world3"))){
    if($m == "world-for-clear-items"){
    if(
    $m != $this->world[$p->getName()]){
        
    $this->saveInv($p);
        
    $p->getInventory()->clearAll();
        
    $this->world[$p->getName()] = $m;
        
    $p->sendMessage("Your Inventory was saved!");
        return;
    }
    }

    if(
    $m == "world-for-give-items"){
    if(
    $m != $this->world[$p->getName()]){
    $p->getInventory()->setContents($this->getInv($p));
    $this->world[$p->getName()] = $m;
    $p->sendMessage("Your Inventory was recovered");
    unset(
    $this->invs[$p->getName()]);
    return;
    }
    }
    }
    public function 
    onQuit(PlayerQuitEvent $e){
    if(
    $this->getInv($e->getPlayer()) != null){
    $e->getPlayer()->getInventory()->setContents($this->getInv($p));
    unset(
    $this->invs[$e->getPlayer()->getName()]);
    }
    }

    public function 
    onJoin(PlayerJoinEvent $e){
    $this->world[$e->getPlayer()->getName()] = 0;
    }
     
  3. Emis02

    Emis02 Spider Jockey

    Messages:
    34
    GitHub:
    RubyTemple
  4. Emis02

    Emis02 Spider Jockey

    Messages:
    34
    GitHub:
    RubyTemple
    yes but as I restart the server it lose the saved inventory, because I don't see anywhere the saving on file
     
  5. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    when the player leaves the server his inventory comes back to him then he is already saved in the player's own inventory, when the player comes and I had in a world that is not to have inventory the code will start and it will remove its inventory and it will be saved in cache
     
  6. Emis02

    Emis02 Spider Jockey

    Messages:
    34
    GitHub:
    RubyTemple
    [QUOTE = "HeyDeniis_, post: 64439, membro: 3302"] quando il giocatore lascia il server, il suo inventario ritorna a lui, quindi viene già salvato nell'inventario del giocatore, quando il giocatore arriva e io ho avuto in un mondo che è per non avere scorte il codice inizierà e rimuoverà il suo inventario e verrà salvato nella cache [/ QUOTE]
    ok i try this code thanks
     
  7. Emis02

    Emis02 Spider Jockey

    Messages:
    34
    GitHub:
    RubyTemple
    PHP:
    public function onMove(PlayerMoveEvent $e)
        {
            
    $p $e->getPlayer();
            
    $m $p->getLevel()->getName();
            
    //If use more worlds use: if(in_array($m, array("world1", "world2", "world3"))){
            
    if (in_array($m, array("hub""FFA""newpvp"))) {
                if (
    $m != $this->world[$p->getName()]) {
                    
    $this->saveInv($p);
                    
    $p->getInventory()->clearAll();
                    
    $this->world[$p->getName()] = $m;
                    
    $p->sendMessage("Your Inventory was saved!");
                    return;
                }
            }
            if (
    $m == "fazioninew") {
                if (
    $m != $this->world[$p->getName()]) {
                    
    $p->getInventory()->setContents($this->getInv($p));
                    
    $this->world[$p->getName()] = $m;
                    
    $p->sendMessage("Your Inventory was recovered");
                    unset(
    $this->invs[$p->getName()]);
                    return;
                }
            }
        }

    according to what you say if I put in this way in the "fazioninew" world I should have a different inventor from the other worlds. Is true?
     
  8. Emis02

    Emis02 Spider Jockey

    Messages:
    34
    GitHub:
    RubyTemple
    not work :C
     
  9. Swourire

    Swourire Spider Jockey

    Messages:
    49
    GitHub:
    swourire
  10. Emis02

    Emis02 Spider Jockey

    Messages:
    34
    GitHub:
    RubyTemple
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.