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

Solved how to get block position and execute code when player does BreakBlockEvent

Discussion in 'Development' started by brokiem, Sep 7, 2020.

  1. brokiem

    brokiem Silverfish

    Messages:
    18
    GitHub:
    brokiem
    I want to make a one block survival plugin, where if a player destroys a block at certain coordinates
    , a random block will appear where the block was broken.

    My question: how to get player break block at certain cordinates and setblock where the block was broken (like one block survival)

    sorry with my english, I'm not really able to speak English perfectly
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
    1. Specify the location of the block
    PHP:
    private $theBlock = new Vector3(xyz);
    2. Make pre-defined list of "random" blocks. You don't want to replace it with lava for instance.
    PHP:
    const BLOCK_LIST = [
        
    BlockIds::STONE,
        
    BlockIds::DIRT,
        
    BlockIds::DIAMOND_BLOCK
        
    // etc. ...
    ]
    3. Listen for block break event (Remember to register the listener, search it up. PluginManager::registerEvents())
    PHP:
    public function onBlockBreak(BlockBreakEvent $event) {
        
    // Check if the block broken was the "special" one
        
    if($event->getBlock()->asVector3()->equals($this->theBlock)) {
             
    // Cancel the event
             
    $event->setCancelled(true);
             
    // 3.1 Break the block
             // 3.2 Give player the drops
             // 3.3 Replace the block, with random one from the list
        
    }
    }
    Not much of a code to see, but I hope this points you in the right direction.

    You might face a problem if the server gets too laggy. Make sure you prevent them from falling through the block.
     
    Last edited: Sep 8, 2020
    brokiem and Emirhan Akpınar like this.
  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.