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

Clear inventory if in a particular level

Discussion in 'Development' started by rektpixel, Aug 30, 2017.

  1. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    How do I clear a players inventory if they are in level "hub2" ?
     
    armagadon159753 likes this.
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Have you made any attempts?
     
    jasonwynn10 likes this.
  3. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    While attempts should be made, I'll try to help anyways (not with full code though).
    So you want to clear the inventory of all player if they are in hub2
    Now this needs to be defined more cleanly. You always need to know exactly what you want.
    I am assuming you want to clear the inventory of every player when they enter the "hub2" level.
    So, now you should start looking for something that can help your plugin to see when there is a player entering. I guess you already heard of events, so let's search for one that helps us here.
    https://github.com/pmmp/PocketMine-MP/tree/master/src/pocketmine/event
    events are located here. Now as our wanted event has something to do with a player, why not look into the "player" folder.
    https://github.com/pmmp/PocketMine-MP/tree/master/src/pocketmine/event/player
    hmmm, nothing says level or world here. you could just randomly look into other folders or perform a repo search for something like "level event". But as you might also know Players are entities, so let's look into there.
    https://github.com/pmmp/PocketMine-MP/tree/master/src/pocketmine/event/entity
    nice, there's a file called "EntityLevelChangeEvent.php", that sounds as it might help.
    https://github.com/pmmp/PocketMine-...tmine/event/entity/EntityLevelChangeEvent.php
    cool, it tells us the target level, so that's probably the level the player wants to change to https://github.com/pmmp/PocketMine-...e/event/entity/EntityLevelChangeEvent.php#L46. Just calling Event->getTarget->getName() should tell us where the Entity/Player is headed. But what is that? There's no getPlayer/getEntity function here, but we need the player instance to perform a inventory clear... Also we need to check if it is actually a player who's changing levels... That's where we need to look if the class extends any other class https://github.com/pmmp/PocketMine-...e/event/entity/EntityLevelChangeEvent.php#L30 and it sure does! let's jump into EntityEvent then.
    https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/event/entity/EntityEvent.php#L36 so here it is, our getEntity function. Now with that we can check if it is a player, and then perform our inv clear!
     
  4. R4EVAELRM

    R4EVAELRM Witch

    Messages:
    73
    Better to teach how to do it rather than tell them how to do it. Nice!
     
    sharletsings123 and jasonwynn10 like this.
  5. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    [//php]
    if($player->getLevel()->getName() === "hub2"){
    $player->getInventory()->clearAll()
     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    You might just want PerWorldInventory.
     
  7. KYUMA

    KYUMA Silverfish

    Messages:
    16
    GitHub:
    Ky75
    PHP:
    if($player->getLevel()->getName() === "hub2"){
    $player->getInventory()->clearAll(); //use pocketmine\inventory\InventoryBase;
    }
    =)
     
    armagadon159753 likes this.
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    use pocketmine\inventory\InventoryBase
    =)
     
    KYUMA likes this.
  9. KYUMA

    KYUMA Silverfish

    Messages:
    16
    GitHub:
    Ky75
    Thanks?
     
  10. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    for what?
     
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That class doesn't exist, and I know which class he actually meant but it's still not needed.
     
  12. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Then say that instead of just reposting the mistake ;)

    To clear this up for anyone else reading that may be confused:

    You don't need to 'use' the BaseInventory class for the code above to work properly.
    It's BaseInventory, not InventoryBase @KYUMA :D
     
    sharletsings123 and jasonwynn10 like this.
  13. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    Dont fogot to change level name in Level.dat
     
  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.