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

ClearInventory

Discussion in 'Development' started by BruhLol, Nov 21, 2017.

  1. BruhLol

    BruhLol Baby Zombie

    Messages:
    122
    I noticed essentialspe only clears online player's inventory, is their a way to clear an offline player's inventory? can we use getOfflinePlayer or getPlayerExact for this?
     
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    PHP:
    /** @license LGPLv3  @author PocketMine Team & robske_110 */

    use pocketmine\nbt\NBT;
    use 
    pocketmine\nbt\tag\ListTag;

    /** @var $server pocketmine\Server */

    /** @var string */
    $playerName "yourplayername";

    if(
    file_exists($server->getDataPath()."players/".$playerName.".dat")){
       
    $namedtag $server->getOfflinePlayerData($playerName);
       
    $namedtag->setTag(new ListTag("Inventory", [], NBT::TAG_Compound));
     
       
    $nbt = new NBT(NBT::BIG_ENDIAN);
       
    $nbt->setData($namedtag);
     
       
    file_put_contents($server->getDataPath()."players/".$playerName.".dat"$nbt->writeCompressed());
    }
    that should do it
     
    jasonwynn10 likes this.
  3. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    I found an easier way to write the data:
    PHP:
    /** @license LGPLv3  @author PocketMine Team & robske_110 */

    use pocketmine\nbt\NBT;
    use 
    pocketmine\nbt\tag\ListTag;

    /** @var $server pocketmine\Server */

    /** @var string */
    $playerName "yourplayername";

    if(
    file_exists($server->getDataPath()."players/".$playerName.".dat")){
       
    $namedtag $server->getOfflinePlayerData($playerName);
       
       
    $namedtag->setTag(new ListTag("Inventory", [], NBT::TAG_Compound));
       
       
    $server->saveOfflinePlayerData($playerName$namedtagtrue); //last arg is whether to save async or not.
    }
     
  4. BruhLol

    BruhLol Baby Zombie

    Messages:
    122
    thanks, ill check if it works.
    EDIT: WORKED!
     
    Last edited: Nov 21, 2017
  5. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    You can use
    PHP:
    /** @var \pocketmine\OfflinePlayer $player */
    if($player->hasPlayedBefore()){
     
    }
    Instead of
    It save your time :D
     
    jasonwynn10 likes this.
  6. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    No, it doesn’t save time at all. Constructing an offline player is way more expensive than the things i have done above.
     
    BruhLol likes this.
  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.