How am I to get a block's NBT tag in BlockBreakEvent?

Discussion in 'Development' started by NickteeChunky, Dec 24, 2019.

  1. NickteeChunky

    NickteeChunky Witch

    Messages:
    52
    GitHub:
    nickteechunky
    When I try to check if a block's tag is lb (lucky block), I run into this error.
    Code:
    Error: "Call to undefined method pocketmine\block\Sponge::getNamedTag()" (EXCEPTION) in "plugins/LuridCore/src/NickteeChunky/LCore/Core" at line 145
    Here is my code:

    PHP:
    public function onBreak(BlockBreakEvent $e){
            
    $p $e->getPlayer();
            
    $b $e->getBlock();
            
    $nbt $b->getNamedTag(); // Line 145
            
    if($b->getId() == 19){
                if(
    $nbt->hasTag("lb"StringTag::class)){
                    
    $p->sendMessage("Lucky block opened.");
                }
            }
        }
     
  2. Deniel

    Deniel Spider Jockey

    Messages:
    42
    GitHub:
    DenielWorld
    I believe blocks don't store them but tiles do, not sure though so don't quote me on that.
     
    OnTheVerge likes this.
  3. NickteeChunky

    NickteeChunky Witch

    Messages:
    52
    GitHub:
    nickteechunky
    Hmm, got any idea how I can do it in this event though?
     
  4. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    705
    GitHub:
    xXNiceYT
    Blocks don’t have nbt recommend saving the position or use tile to create NBT
     
    OnTheVerge and jasonwynn10 like this.
  5. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    705
    GitHub:
    xXNiceYT
     
  6. NickteeChunky

    NickteeChunky Witch

    Messages:
    52
    GitHub:
    nickteechunky
    How am I to use tiles?
    May you please show me an example?
     
  7. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    526
    GitHub:
    GamakCZ
    Just save the luckyblock's position and then check it. I don't think it is possible to create tile from sponge.
     
  8. NickteeChunky

    NickteeChunky Witch

    Messages:
    52
    GitHub:
    nickteechunky
    Okay, I've done that but how am I supposed to check each and every lucky block's position to find out it's value, if you save any lucky block's position, once you place another and haven't broken the previous one, the previous one's value changes. I'd really appreciate if you can help me further with this. Thanks in advance.
     
  9. NickteeChunky

    NickteeChunky Witch

    Messages:
    52
    GitHub:
    nickteechunky
  10. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    526
    GitHub:
    GamakCZ
    I normally haven't using sponge on my builds. So if I wanted luckyblock plugin, I just check the block id, if it's sponge.
     
  11. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    526
    GitHub:
    GamakCZ
    However if you wanted to save every lucky block when it's placed, you can try this code

    PHP:
    /** @var array $blocks */
    public $blocks;

    /**
    * @param BlockPlaceEvent $event
    */
    public function onPlace(BlockPlaceEvent $event) {
        
    $player $event->getPlayer();
        
    $block $event->getBlock();

        
    $this->blocks[] = "$block->x:$block->y:$block->z";
    }

    /**
    * @param BlockBreakEvent $event
    */
    public function onBreak(BlockBreakEvent $event) {
        
    $player $event->getPlayer();
        
    $block $event->getBlock();

        if(
    $block->getId() == Block::SPONGE && in_array("$block->x:$block->y:$block->z"$this->blocks)) {
            
    // your code

            
    unset($this->blocks[array_search("$block->x:$block->y:$block->z"$this->blocks) - 1]);
        }
    }
     
  12. NickteeChunky

    NickteeChunky Witch

    Messages:
    52
    GitHub:
    nickteechunky
    Sorry for going missing, I never received a notification regarding your reply and have been busy. Anyways, I've done as you've shown, and it stores the lucky block's position but what I'm trying to do here is store it's success rate, as in lucky block luck rate. How am I to check it from it's position now and get it's tag? @GamakCZ
     
  13. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,492
    GitHub:
    jasonwynn10
    It isn't possible to create a visible tile from a sponge, but a tile can still occupy the same position to hold NBT.

    A poggit virion was recently made to help handle tiles for block data storage which may help this case situation. What needs to be done is on block place, create tile with the same position. Then using the tile, store the namedTag.
    https://github.com/DenielWorld/EzTiles
     
    HimbeersaftLP likes this.

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.