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

Sessions

Comments in 'Plugin Development' started by Angelo Vidrio, Jan 24, 2015.

  1. Angelo Vidrio
    Offline

    Angelo Vidrio Active Member Plugin Developer

    Joined:
    Jan 16, 2014
    Posts:
    123
    Plugins:
    5
    Minecraft User:
    LilCrispy45
    Can anyone teach me how to use sessions, I heard that its easy but I want to get a full understanding of it.
    xFlare likes this.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The whole principle is that normal elements of a plugin are run in a single flow, i.e. there won't be two parts of your plugin running st the same time (unless you start your own threads/AsyncTasks).

    In this example, it detects whether the player consecutively tap a grass block and then a bedrock block.

    First, create an array property in your class:
    PHP:
    private $lastClicked = [];
    This array will store whether the last block the player clicked is a grass block.
    Then, in a PlayerInteractEvent listener:
    PHP:
    public function onInteract(PlayerInteractEvent $e){
      
    $this->lastClicked[$e->getPlayer()->getId()] = $e->getBlock();
    }
    So it stores the last block the player clicked.

    To check if it is grass then bedrock, add this statement before the line that stores the block (otherwise the memory will be overwritten!):
    PHP:
    if(
        isset(
    $this->lastClicked[$e->getPlayer()->getId()] // to ensure that the player had touched a block, otherwise an ArrayOutOfBoundsException will be thrown!
        
    and $this->lastClicked[$e->getPlayer()->getId()]->getId() === Block::GRASS // check if the previous block is grass
        
    and $e->getBlock()->getId() === Block::BEDROCK // check if the block clicked now is bedrock
    ){
        
    // do something. Remember not to return, or the field won't be saved!
    }
    To optimize memory use, consider garbaging the memory when a player quits:
    PHP:
    public function onQuit(PlayerQuitEvent $e){
        if(isset(
    $this->lastClicked[$id $e->getPlayer()->getId()])) unset($this->lastClicked[$id]);
    }
    Angelo Vidrio and iksaku like this.
  3. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    That's the exact EssentialsPE sessions principle
  4. Angelo Vidrio
    Offline

    Angelo Vidrio Active Member Plugin Developer

    Joined:
    Jan 16, 2014
    Posts:
    123
    Plugins:
    5
    Minecraft User:
    LilCrispy45
    T
    Thanks
    Your awesome!:)

Share This Page

Advertisement