Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Replace placing block

Comments in 'Plugin Development' started by udwarf, Sep 11, 2016.

  1. udwarf
    Offline

    udwarf Active Member

    Joined:
    Apr 17, 2016
    Posts:
    61
    Minecraft User:
    1Zaza
    Hi, guys!
    What is the best way to replace block that player trying to place?
    I'm trying to make so when some player places ANY block, the DIAMOND_BLOCK will be placed instead.

    I was trying to hook a BlockPlaceEvent, but i cant find how to change placed block from this event handler.
    The only way I see is to cancel this BlockPlaceEvent and place manually DIAMOND_BLOCK instead.
    But may be there is a better way?
  2. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    PHP:
    $lvl->setBlock($block,Block::get($id));
    Primus likes this.
  3. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    BlockPlaceEvent is the best way :)
    get the block in that event using:
    PHP:
    $block $event->getBlock();
    Than get level of the player that place that block
    PHP:
    $level $event->getPlayer()->getLevel();
    The block is already an instanceof Vector3, so:
    PHP:
    $level->setBlock($blockpocketmine\block\Block::get($diamond_block_id));
    There, simple and easy :)
    Primus likes this.
  4. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    If you set the block on BlockPlaceEvent then don't forget to cancel it or it will be replaced.
  5. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    YASS i forgot xD
  6. udwarf
    Offline

    udwarf Active Member

    Joined:
    Apr 17, 2016
    Posts:
    61
    Minecraft User:
    1Zaza
    Yes, it works, thank you!
    I thought that may be exist some kind of pre-event for this. Like PlayerPreLoginEvent or PlayerTextPreSendEvent
    But this way also ok

    ps: I would mark this thread as "solved" if I knew how to...
  7. MikkiMCPE
    Offline

    MikkiMCPE Active Member

    Joined:
    May 16, 2015
    Posts:
    125
    Minecraft User:
    MikkiMC
    Edit thread and add Solved tag. its more easier than write code.

Share This Page

Advertisement