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

Run Command On Interact

Discussion in 'Development' started by CocoonBuilder, Oct 18, 2018.

  1. CocoonBuilder

    CocoonBuilder Spider Jockey

    Messages:
    43
    GitHub:
    chimneychuckles
    I know how to run command on PlayerInteractEvent but how do i make it run command such as givemoney {player} 10000

    Any help are highly appreciated!
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    If you know how to run command on PlayerInteractEvent, can you show us that.
     
  3. CocoonBuilder

    CocoonBuilder Spider Jockey

    Messages:
    43
    GitHub:
    chimneychuckles
    just a simple command without {player} like /time set day

    edit:

    public function onInteract(PlayerInteractEvent $event){

    $p = $event->getPlayer();
    $item = $event->getItem();
    if($item->getId() === 339) {
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "time set day");
    $p->getInventory()->removeItem(Item::get(339));

    There!

    I just dont know how to use with {player} :/
     
    Last edited: Oct 18, 2018
    Muqsit likes this.
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Oh, you're almost there.
    Suppose this is your command:
    PHP:
    $cmd "setgroup {player} Admin";
    And you'd like to replace {player} with the player's name... You can use str_replace here.
    PHP:
    $player $p->getName();
    $cmd str_replace("{player}"$player$cmd);
    Or a one liner...
    PHP:
    $cmd str_replace("{player}"$p->getName(), "setgroup {player} Admin");
    P.S. You might want to read this: Players with space in their username!
     
  5. CocoonBuilder

    CocoonBuilder Spider Jockey

    Messages:
    43
    GitHub:
    chimneychuckles
    Oof thx a lot, but if i want to have more than one command and each command use for different items?
     
  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.