Quick Question(Replace a Block)

Discussion in 'Help' started by ShushImSam, May 15, 2019.

  1. ShushImSam

    ShushImSam Creeper

    Messages:
    3
    Hello, so I've searched before but I couldn't find what I was looking for.
    so I'm making somewhat of grinding thing.
    What I do is
    PHP:
    $event->setCancelled(); 
    To simply cancel the event when he breaks it and he receive the block. Although id like that it completly remove the block and replace it so the Pickaxe can actually lose durability.

    Any idea on how to?
     
  2. HimbeersaftLP

    HimbeersaftLP Chicken

    Messages:
    1,589
    GitHub:
    HimbeersaftLP
  3. ShushImSam

    ShushImSam Creeper

    Messages:
    3
    It Indeed works, but it does not replace the block afterward
     
  4. HimbeersaftLP

    HimbeersaftLP Chicken

    Messages:
    1,589
    GitHub:
    HimbeersaftLP
    Just use setBlock to place it again?
     
  5. HimbeersaftLP

    HimbeersaftLP Chicken

    Messages:
    1,589
    GitHub:
    HimbeersaftLP
    Or even better, cancel the event, but give the drops manually by using getDrops
     
  6. ShushImSam

    ShushImSam Creeper

    Messages:
    3
    PHP:
        public function onBreak(BlockBreakEvent $event)
        {
            
    $block $event->getBlock();
            
    $player $event->getPlayer();
            
    $level $player->getLevel();
            if (
    $block->getId() == Block::STONE) {
                
    $event->setDrops([]);
                
    $pos $block->floor();
                
    $level->setBlock($pos , new Stone());
            }
    It doesnt set the block, do I need to get the pos of the broken block before?
     
  7. HimbeersaftLP

    HimbeersaftLP Chicken

    Messages:
    1,589
    GitHub:
    HimbeersaftLP
    By stealing drop logic from here.
    We can come up with this code that cancels the event (cuz replacing the block would still do blockupdates and stuff) and still gives drops:
    PHP:
    public function onBreak(BlockBreakEvent $event)
    {
        
    $block $event->getBlock();
        
    $player $event->getPlayer();
        
    $level $player->getLevel();
        
    $event->setCancelled();
        if (
    $block->getId() == Block::STONE)
        {
            
    $drops $event->getDrops();
            if(!empty(
    $drops)){
                
    $dropPos $block->add(0.50.50.5);
                foreach(
    $drops as $drop){
                    if(!
    $drop->isNull()){
                        
    $level->dropItem($dropPos$drop);
                    }
                }
            }
        }
    }
     

Share This Page

  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.