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

Run a command when standing on a block

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

  1. Remarkabless

    Remarkabless Slime

    Messages:
    83
    GitHub:
    Remakem
    How can I run a command when I lets say stand on a block. Lets say I fall off a spawn and hit a grass block how can I run a command when I land on a block or stand ontop of it? please thanks
     
  2. Yexeed

    Yexeed Slime

    Messages:
    76
    You can check player coordinates when he issuing the command
     
  3. 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 PlayerMoveEvent. Within the created function, add an if statement which checks if the block under the player's id matches the block you want to check for's id (also be sure to check the damage for meta-specific blocks like wool with color). If the ids and damages match, then use the Server::dispatchCommand() function to run a command. The first argument is the CommandSender, and the second is the command string. Make sure you access that function by getting the Server instance from your plugin's main class.

    Please show proof of a previous attempt before asking for code
     
    Remarkabless likes this.
  4. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    PHP:
    use pocketmine\event\player\PlayerMoveEvent// don’t forget to add both needed classes (and register the Listener)
    use pocketmine\command\ConsoleCommandSender;

    public function 
    onMove(PlayerMoveEvent $event) {
        
    $player $event->getPlayer();
        
    $block $player->getLevel()->getBlock($player->subtract(010)); // subtracts 1 from XYZ position of player and gets the block
        
    if($block->getId() === 2) { // if block underneath player is Grass/if player is walking on Grass
            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "<command>"); // command without slash (/)
        
    }
    }
     
    Remarkabless likes this.
  5. Remarkabless

    Remarkabless Slime

    Messages:
    83
    GitHub:
    Remakem
    Thanks also since I wanna use this to teleport somewhere with /warp from essentials how can I hide the “warping to etc” message? Is it possible?
     
  6. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    You can run the command as console (the message will still appear in console) or edit your EssentialsPE plugin and remove the message.
     
  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.