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

Solved How to get the block that player's crosshair is aiming

Discussion in 'Development' started by minijaham, Aug 7, 2020.

  1. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Hey! I am trying to make a "FireBall" plugin which breaks the block that is aimed at by a player's crosshair.
    So I was wondering on how to get the block that player's crosshair is aiming, since I couldn't find anything related to this based on my search on forums and PocketMineMP github.
     
  2. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    explain more wdym by croshair? im not pro at mc gameplay so idk what is that :p
     
  3. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    It's the cross symbol thingy you see when you turn on "split control"
    But you can see this as default when playing on win10 version
     
  4. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    sorry, idk, wait to anwser other people :(
     
  5. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    It's this thing
    upload_2020-8-8_11-13-10.png
     
  6. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    oh, cool now iknow Lol
    so what do you want to do?

    when you shoot fire ball\ snow ball with crsr (crosshair) do stuff?
     
  7. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    for example you shoot a fire ball and when hit to block, break that block yes?
     
  8. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    or when hit to block do some thing?
     
  9. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    use this function:
    PHP:
    public function onShoot(ProjectileHitBlockEvent $event) { //use custom name to this func
    $block $event->getBlockHit(); //to get block hit
    $snow $event->getEntity(); //to get entity => Snow/Fireball/Arrow
    if ($snow instanceof Snowball) { //to check if entity is snow bal and it isnt fire ball or arrow
         
    if ($block->getId() === Block::SNOW) { //check if Block hit is Snow
             //set it to AIR with $level->setBlock($block->x, $block->y, $block->z, Block::AIR);
            
    }
        }
    }
    //and DONE
    Solved?
     
  10. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    also dont forgt to define $level
    it should be $snow->getLevel();
    and its DONE
     
  11. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    You can get the block the player is looking at with $player->getTargetBlock(int $max_distance). In order to destroy it, use $player->getLevel()->setBlock($target_block, BlockFactory::get(BlockIds::AIR)).
     
    GodWeedZao likes this.
  12. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Thank you both for your explanations! I was looking for the $player->getTargetBlock :)
    Thank you so much!
     
    Diduhless likes this.
  13. GodWeedZao

    GodWeedZao Zombie Pigman

    Messages:
    401
    GitHub:
    godweedzao
    oh, now i undrstand :D
     
  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.