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

How to command on item?

Discussion in 'Development' started by manyDARK, Aug 23, 2018.

  1. manyDARK

    manyDARK Spider

    Messages:
    8
    GitHub:
    many1337
    Hey!
    I want to make a command on the item, but it's not good for something. (Maybe bind a command to item?)
    "Item Code":
    PHP:
            }elseif($item->getCustomName() == "§eTeleporter"){
                
                
    $name $sender->getName();
                
    $this->getServer()->dispatchCommand(new ConsoleCommandSender("teleport $name"));
                           
    Thanks for the help! Bye
     
  2. MisterTurtle

    MisterTurtle Creeper

    Messages:
    5
    }elseif($item->getCustomName() == "§eTeleporter"){

    $name = $sender->getName();
    $this->getServer()->dispatchCommand(new ConsoleCommandSender("teleport ". $name ." ".$x." ".$y." ".$z));

    But where did you wright this code? You need to wright in a PlayerInteractEvent function
     
  3. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    PHP:
            }elseif($item->getCustomName() == "§eTeleporter"){
                
    $name $sender->getName();
                
    $where = new \pocketmine\math\Vector3($x$y$z); // Replace x y z variables with your xyz coords.
                
    $sender->teleport($where);
                           
     
  4. DrakerZR

    DrakerZR Silverfish

    Messages:
    21
    GitHub:
    drakerzr
    You can use PlayerInteractEvent
    PHP:
    public function  onInteract(PlayerInteractEvent $ev){
        
    $player $ev->getPlayer();
        
    $item $player->getItem();
        if(
    $item->getCustomName() === "§aTeleporter" && ($ev->getAction() === PlayerInteractEvent::LEFT_CLICK_BLOCK || $ev->getAction() === PlayerInteractEvent::RIGHT_CLICK_BLOCK)) {
            
    $this->getServer()->getCommandMap()->dispatch(new ConsoleCommandSender(),"tp " $player->getName() . $x " " $y " " $z/*You have to define $x, $y & $z*/);
        }
    }
     
    Last edited: Sep 19, 2018
  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.