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
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
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(0, 1, 0)); // 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 (/) }}
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?
You can run the command as console (the message will still appear in console) or edit your EssentialsPE plugin and remove the message.