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

Solved BlockPlaceEvent

Discussion in 'Development' started by KHAV, Jul 20, 2018.

  1. KHAV

    KHAV Baby Zombie

    Messages:
    152
    GitHub:
    xkhhv
    I tried to make if under the block who player putted a specific block id cancel it, But it doesn't work.

    This is my code:
    PHP:
    public function onPlace(BlockPlaceEvent $event){
        
    $player $event->getPlayer();
        
    $block $event->getBlock();
                if (
    $block->getId() == 159 && $block->getDamage() == 14 && $block->getY() > 70) {
                    
    $player->sendMessage("§cYou can't put blocks here.");
                    
    $event->setCancelled(true);
                }
    }
    But it doesn't work, I think there is a problem that i think is the $block is what you putted not what under it, Any help?
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Assuming your Event Handler is working correctly, either:
    - The block's id is not 159
    - The block's damage is not 14
    - The block's y coordinate is less than 70.

    I don't know what block you placed, what damage it had, or where you placed it, so I can't narrow it down any further.

    Most times, people's code does work, it just doesn't function as they intended, because of simple logic mistakes.
     
    Muqsit likes this.
  3. KHAV

    KHAV Baby Zombie

    Messages:
    152
    GitHub:
    xkhhv
    Thank you I replaced $block = $event->getBlock(); with $block = $event->getBlockAgainst(); and it works!, Solved.
     
  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.