Detect when a player walk on a specific block

Discussion in 'Development' started by dadodasyra, Jul 12, 2020.

  1. dadodasyra

    dadodasyra Spider Jockey

    Messages:
    43
    GitHub:
    dadodasyra
    I would like to detect in a clean and optimized way when a player walks on a specific block
     
  2. Mohagames205

    Mohagames205 Spider Jockey

    Messages:
    25
    GitHub:
    mohagames205
    Check on which block the Player is standing on the PlayerMoveEvent
     
  3. Rim

    Rim Spider Jockey

    Messages:
    28
    GitHub:
    boomyourbang
    PlayerMoveEvent->getTo() should do the trick. Just don't kick the player during this.
     
  4. Primus

    Primus Zombie Pigman

    Messages:
    408
    Avoid using PlayerMoveEvent whenever you can. Task can accomplish this task as good. Make sure, You're doing the checks only when necessary (specific level, area and/or specific players etc.)
     
  5. <Denny>

    <Denny> Spider

    Messages:
    7
    You're completely wrong. You should prefer pocketmine events over tasks.
     
    GamakCZ likes this.
  6. Primus

    Primus Zombie Pigman

    Messages:
    408
    What's the source of this information?

    Well of course, this depends on per situation basis. I'm not sure if calling twice per tick is necessary in this situation.
     
    Last edited: Jul 15, 2020
    Muqsit likes this.
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,572
    GitHub:
    muqsit
    PlayerMoveEvent would give you the most precise results. A task, assuming it's delay > 1 tick is only going to skip checking the block below.

    F.e, if you sprint over a grass block in 1 second and the task has a delay of 20 ticks, the task won't catch it.

    Any way you do it, it's going to result in many unnecessary block check calls. At the least, the event method won't be doing block below calls when player isn't moving.

    Doing a mere block below check in PlayerMoveEvent is not heavy, unless ofcourse you're doing something more than just that. Several surrounding block checks happen anyway when mobs and dropped items move.
     
    Primus likes this.
  8. tungstenvm

    tungstenvm Spider Jockey

    Messages:
    40
    i will use
    Code:
    $player = $player #you get at everywhere;
    $block = $player->getLevel()->getBlock(new Vector3($player->x, $player->y - 0.5, $player->z));
    return $block->getId(); #block id
     
  9. dadodasyra

    dadodasyra Spider Jockey

    Messages:
    43
    GitHub:
    dadodasyra
    Wow, I was not expecting as much response, I already know the move event but it seems to be quite overloaded that's why I said to myself there may be a way to do it via colision with a block or whatever. If someone has more info I am a taker
     
  10. Primus

    Primus Zombie Pigman

    Messages:
    408
    Player's parent is Vector3
    PHP:
    $block $player->getLevel()->getBlock($player->subtract(0, -0.5));
     
    tungstenvm likes this.
  11. dadodasyra

    dadodasyra Spider Jockey

    Messages:
    43
    GitHub:
    dadodasyra
    Or $player->getPos() For a best clarity
     
  12. tungstenvm

    tungstenvm Spider Jockey

    Messages:
    40
    no, if you use this code, it will get the block that the leg is inside which is air ,not the block under
     
  13. dadodasyra

    dadodasyra Spider Jockey

    Messages:
    43
    GitHub:
    dadodasyra
    PHP:
    $player->getPos()->substract(0, -0.5);
    Are-you happy ?
     
  14. tungstenvm

    tungstenvm Spider Jockey

    Messages:
    40
    i have just noticed that there is no getPos() , it must be getPostion() or sth else

    and subtract() is already has a minus symbol on it ,and it was required 3 parameters not 2 https://github.com/pmmp/Math/blob/master/src/Vector3.php#L124-L126
    so it also must be subtract(0,0.5,0);
    PHP:
    $player->asVector3()->substract(0,0.5,0);
    Let me make this clear, i didnt want to mock you, you just was wrong and still wrong again , you was so bad
     
    Last edited: Jul 28, 2020
    Primus likes this.
  15. GodWeedZao

    GodWeedZao Zombie

    Messages:
    233
    GitHub:
    godweedzao
    Easy way:
    1. check under block of player onMovement
    2. getBlock($player->x, $player->y - 0.7. $player->z);
    i used 0.7 its better than 0.5, when player jump on movment it has bug with 0.5 :)
    3. if ($block->getId() == Block::GRASS_BLOCK) {
    //do something or RUN CODE
    }
    4. alway use this code to if block is air dont run code or cmd.
    if ($block->getId !== Block::AIR) {
    //now if isnt block under Air
    }
    5. Every thing is done for checking player under block
     

Share This Page

  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.