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

Limit block placing

Discussion in 'Development' started by DiamondGamer30, Nov 25, 2017.

  1. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    How can I limit the amount of blocks placed with BlockPlaceEvent?
     
  2. Kenn Fatt

    Kenn Fatt Slime

    Messages:
    82
    GitHub:
    kennfatt
    please explain what do you want.
    Limit each players or world?
     
  3. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    I want to limit the amount of tnt that can be placed near each other in an amount of block radius to prevent lag.
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Simple and short answer: Store all TNT coordinates, then when a TNT is placed, search your stored coordinates to see if there were TNTs nearby. Make sure you delete them with BlockBreakEvent.
    Long and optimized answer: Index the coordinates by clusters, probably in different files. When a player places a TNT, check for all TNTs in the nearby clusters.
    The best size of a "cluster" is a cube of r*r*r blocks (or r*256*r blocks if r is not very small), where r is comparable to your "nearby" definition.
    For example, if TNTs have to be at least 10 blocks away from each other, each cluster can be 8*8*8 lage or 16*16*16 large. When a TNT is placed, divide ach coordinate by 16 and take the floor value (i.e. bitshift the integer 4 times to the right), and store it in a list file of such coordinates. When you check for nearby blocks, you only need to load the nearby files (don't just load the exact file, because a TNT block can be in the adjacent cluster).
     
  5. KittyDev

    KittyDev Slime

    Messages:
    96
    GitHub:
    FreakingDev
    You keep posting but you didn't make the thread SOLVED when its done.
     
    OnTheVerge likes this.
  6. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    To store blocks placed save number of placed blocks to array
    Example:
    PHP:
    /** @var array $break */
    public $break = array();

    public function 
    onPlace(BlockPlaceEvent $e){
        
    $p $e->getPlayer();
        if(!isset(
    $this->break[strtolower($p->getName())])){
            
    $this->break[strtolower($p->getName())] = 0;
            return 
    false;
       }
       
    $this->break[strtolower($p->getName())]++;
       if(
    $this->break[strtolower($p->getName())]) >= 20){
             
    $e->setCancelled();
             
    $p->sendMessage("You reached limit!");
       }
    }
     
  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.