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

SetKeepInventory

Discussion in 'Development' started by sdsd16, Jan 25, 2019.

  1. sdsd16

    sdsd16 Spider Jockey

    Messages:
    41
    GitHub:
    Danding1314
    How to set some world no keepinventory some world have keepinventory
    for exmaple pvp have keepinventory spawn no keepinventory
     
  2. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Use PlayerDeathEvent, check if player is in pvp world then set drops to an empty array and finally store the inventory items into a global array like this:
    PHP:
    $player $event->getPlayer();
    if(
    $player->getLevel()->getName() == "pvp") {
        
    $event->setDrops([]);
        
    $this->contents[$player->getName()] = $player->getInventory()->getContents();
    }
    // Array format would be like this
    $contents = [
        
    "playerName" => $content
    ];
    Now you're going to use PlayerRespawnEvent and you're going to check if the player name is in the array, then you want to add those contents into the player inventory. Finally you're going to unset the player array.
    PHP:
    $player $event->getPlayer();
    if(isset(
    $this->contents[$username $player->getName()])) {
        
    $player->getInventory()->setContents($this->contents[$username]);
        unset(
    $this->contents[$username]);
    }
     
    sdsd16 likes this.
  3. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    There's an even simpler method
    PHP:
    public function PlayerDeathEvent(PlayerDeathEvent $event){
        if(
    $event->getPlayer()->getLevel()->getName() === "pvp")
            
    $event->setKeepInventory(true);
    }
     
  4. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Didn't know that exists, lol
     
  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.