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

Make the placed blocks cannot be broken.

Discussion in 'Plugin Help' started by IvanCraft623, May 26, 2021.

  1. IvanCraft623

    IvanCraft623 Baby Zombie

    Messages:
    105
    GitHub:
    IvanCraft623
    How can I make the blocks already placed on a map unbreakable?
    But if they can break blocks that the players have placed.

    I saw Boi's BedWars code, apparently it uses a variable called $placedBlocks, but nowhere did I find what defines it.
     
  2. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Store the placed blocks in an array and you can have a check for them on break.
    Or... query if a broken block is breakable by the player?
     
    wolfdale, Primus and Mcbeany like this.
  3. Axon

    Axon Zombie

    Messages:
    276
    @minijaham idea is great.
    If your players can only get certain blocks like wool. Then something like this would work. [I took some idea with the array from minijaham, thanks)
    PHP:
    public function onBlockBreak(\pocketmine\event\block\BlockBreakEvent $event){
        
    $level $event->getPlayer()->getLevel()->getName(); // Gets the player level
        
    if($level != "SkyWars"){return;} // Returns the function if the player isn't in SkyWars
        
    $allowedBlocks = [35121]; // Allow Wool and endstone to be broken
        
    if (!in_array($event->getBlock()->getID(), $allowedBlocks)){
            
    $event->setCanceled(); // Cancel the event if the block broken isn't wool
        
    }
    }
    It basically checks if it's an allowed block to break, if it's allowed it won't cancel the event.
    I think this is better since there may be more blocks that aren't allowed to be broken.
     
    Last edited: May 26, 2021
    Agent and minijaham like this.
  4. IvanCraft623

    IvanCraft623 Baby Zombie

    Messages:
    105
    GitHub:
    IvanCraft623
    Maybe it would be to save all the blocks positions as Vector3 in an array and when breaking a block check if the block position as Vector3 is in that array.
    But, I have not found a function that returns all blocks in a chunk and I also believe that all chunks must be loaded to get their blocks.
     
    Last edited: May 26, 2021
  5. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Huh? Why would you even do that? Then why don't you just make them unbreakable, not having to check for the block every single time?
     
    Axon likes this.
  6. IvanCraft623

    IvanCraft623 Baby Zombie

    Messages:
    105
    GitHub:
    IvanCraft623
    I'm doing a BedWars, in this minigame the blocks that come by default on the map cannot be broken, all the others can, that's what I want to achieve...
     
  7. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    We've literally spoon-fed you. If you're still not sure how to do it, then just give up. There are plenty of websites where you can learn PHP from.
     
    Last edited: May 27, 2021
  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.