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

Solved Send message when player step on grass

Discussion in 'Development' started by KittyDev, Oct 16, 2017.

  1. KittyDev

    KittyDev Slime

    Messages:
    96
    GitHub:
    FreakingDev
    Can someone give some tips :D Thanks in advance
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    On PlayerMoveEvent check if block below player is Item::GRASS, then send player the message you want
     
  3. KittyDev

    KittyDev Slime

    Messages:
    96
    GitHub:
    FreakingDev
    Thanks. Can you put a example code :D
     
  4. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Can you please show proof of a previous attempt first?
     
  5. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    You can use the PlayerMoveEvent and check if the player is standing on grass:
    PHP:
    public function onMove(PlayerMoveEvent $event) {
        
    $player $event->getPlayer();
        
    $blockunderneath $this->getLevel()->getServer()->getBlock($player->subtract(010));
        if(
    $blockunderneath->getId() === 2) {
            
    $player->sendMessage("Message here");
        }
    }
     
    OnTheVerge likes this.
  6. KittyDev

    KittyDev Slime

    Messages:
    96
    GitHub:
    FreakingDev
    Thankyou so much :)
     
  7. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    It's Block::GRASS not Item::GRASS.
     
    jasonwynn10 likes this.
  8. KittyDev

    KittyDev Slime

    Messages:
    96
    GitHub:
    FreakingDev
    Is it possible? If player step on a grass with a custom name?
     
  9. KittyDev

    KittyDev Slime

    Messages:
    96
    GitHub:
    FreakingDev
    PHP:
    if($item->getId() == 369 && $item->getCustomName() == "TRAP"){
    $player->sendMessage("You step on traps!");
    }
     
  10. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Just write a code to set blocks as traps:
    PHP:
    public $trapblocks = [];

    public function 
    onInteract(PlayerInteractEvent $event) {
        
    $block $event->getBlock();
        
    $this->trapblocks[] = $block->getX() . ":" $block->getY() . ":" $block->getZ() . ":" $block->getLevel()->getName(); // saves the block's position to an array
        
    $sender->sendMessage("Set block as trap!");
    }

    public function 
    onMove(PlayerMoveEvent $event) {
        
    $player $event->getPlayer();
        
    $block $this->getLevel()->getServer()->getBlock($player->subtract(010)); // gets block player's standing on
        
    if(in_array($block->getX() . ":" $block->getY() . ":" $block->getZ() . ":" $block->getLevel()->getName(), $this->trapblocks)) {
            
    $player->sendMessage("You stepped on a trap!");
        }
    }
     
    Last edited: Oct 18, 2017
  11. KittyDev

    KittyDev Slime

    Messages:
    96
    GitHub:
    FreakingDev
    Thanks but im planning to give a player a specific block with a CustomName for traps. Is it possible to detect when player step on a block with a CustomName? If not i'll make it using the code you gave
     
  12. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I'm not sure if it's possible, but I think there's a way with NBT. I don't know anything about NBT (and don't even know what NBT is...), I cannot help you.
    I would just save the block to an array and check if the player is standing on a block marked as a trap.
     
  13. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    if($block->getName() === "customName") {
    //your code here
    }
     
  14. KittyDev

    KittyDev Slime

    Messages:
    96
    GitHub:
    FreakingDev
    Thanks it really helps :D
     
  15. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Wait, do blocks even have custom names? Maybe your item has a custom name, but once you place the block, the item name shouldn't exist anymore.
     
    WinterBuild7074 likes this.
  16. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Checking PMMP source verifies this, Block::getName() returns the actual name of the block or "Unknown" if it doesn't have a fallback name.
     
  17. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The fallbackName field is not used anywhere. In particular, Grass->getName() always returns "Grass". So unless you are working on custom block types, custom names won't help you at all.
     
    WinterBuild7074 likes this.
  18. KittyDev

    KittyDev Slime

    Messages:
    96
    GitHub:
    FreakingDev
    ok thanks for your concern. I think i should save it on config
     
  19. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    That was my point.
     
  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.