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

How to add money when block is broken(with popup) and more

Discussion in 'Development' started by DarkkzMC, Nov 20, 2017.

  1. DarkkzMC

    DarkkzMC Spider

    Messages:
    9
    GitHub:
    BigDGucci
    I want to know how to break certain block and get a popup that says # has been added to your account. Also when block is broken turns to obsidian. Now after a while block turns back to it's original block.

    I still a noob at coding and I would like to know
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Register an Event Listener class which implements Listener. Create a function within the class with a single argument for BlockBreakEvent. Within the created function, add an if statement which checks if the block broken's id matches the block you want broken (also be sure to check the damage for meta-specific blocks like wool with color). If the ids and damages match, then use the Player::sendPopup() function to send a message to the player in the form of a popup. Make sure you access that function by getting the Player instance from BlockBreakEvent::getPlayer();

    Please show proof of a previous attempt before asking for code
     
    Last edited: Nov 20, 2017
    NickTehUnicorn likes this.
  3. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    In your main PHP file:
    PHP:
    use pocketmine\event\block\BlockBreakEvent// don’t forget to add the needed classes (and register the Listener)
    use pocketmine\command\ConsoleCommandSender;

    public function 
    onBreak(BlockBreakEvent $event) {
        
    $player $event->getPlayer();
        
    $block $event->getBlock();
        if(
    $block->getId() . ":" $block->getDamage() === "1:6") { // "1:6" = Polished Obsidian, <ID>:<meta>
            
    $this->getServer()->getScheduler()->scheduleDelayedTask(new BlockReplaceTask($this$block), 1200); // runs the ’BlockReplaceTask‘ after 1,200 ticks (60 seconds/1 minute)
            
    $block->getLevel()->setBlock($blockBlock::OBSIDIANtruetrue); // replaces the block by Obsidian
            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "givemoney " $player->getName() . " 10"); // gives the player 10
            
    $player->sendMessage("Added " $block->getName() . " (" $block->getId() . ":" $block->getDamage() . ") has been added."); // just use ’sendPopup()‘ or ’sendTip()‘ instead of ’sendMessage()‘ if you want
        
    }
    }
    Your file (’BlockReplaceTask‘ in the code above):

    PHP:
    public $block;

    public function 
    __construct(Main $mainBlock $block) {
        
    $this->main $main;
        
    $this->block $block;
        
    parent::__construct($main);
    }

    public function 
    onRun(int $tick) {
        
    $block $this->block;
        
    $level $block->getLevel();
        
    $level->setBlock($block->getX(), $block->getY(), $block->getZ(), $blocktruetrue); // replaces the Obsidian by old block’s version (1:6, Polished Obsidian)
    }
     
    Last edited: Nov 20, 2017
  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.