Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Send command as a player

Comments in 'Plugin Development' started by Andrey Nazarchuk, Oct 2, 2015.

  1. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Hello,
    I searched the website, and did not find how to do this, so forgive me and post a link to a thread about this if there already is one that answers this question.

    I would like to know how to send a command as a player through the plugin code.

    Thanks! :D
  2. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    Make sure to grab all the required dependencies using use.

    And here's the code:
    PHP:
    $this->getPlugin()->getServer()->dispatchCommand($player$command);
    $player is the player.
    $command is the command $player will run.
    Andrey Nazarchuk likes this.
  3. PianoPlayer38
    Offline

    PianoPlayer38 New Member

    Joined:
    Sep 29, 2015
    Posts:
    3
    Minecraft User:
    Ment97
    PHP:
    $this->getServer()->dispatchCommand($player,"say hi");
  4. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    Make sure you define $player

    For instance
    PHP:
    $player $this->getServer()->getPlayer();
    To get the player and
    PHP:
    $name $player->getName();
    Then you could do somethig after executing the command like
    PHP:
    $this->getServer()->broadcastMessage($name "Executed" "$cmd");
    You would need to change $cmd to a string form of the command but this would send a message to everyone saying $name executed $cmd for example if my name is FlamingGenius and i did /help it would look like

    "FlamingGenius Executed /help"

    Like and follow if i helped ^_^
  5. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Thanks everyone :D
    HotFireyDeath likes this.
  6. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    1. $name = $player->getName(); isn't necessary in this specific case.
    2. Yes, you could do something after executing the command. *woah*
    3. Why did you reply directly at me?
    4. You don't need to make $cmd complex. You defined $cmd earlier as a string.
  7. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    No
    $this->getServer()->sendMessage("message");
    When you use sendMessage() it just sends to the person who used it not to everyone to send to everyone u would use broadcastMessage()
  8. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    Didnt mean to reply to you meant to reply to the question my bad :D
    HotFireyDeath likes this.
  9. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    When did he say he wanted to send it to everyone? :p
  10. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    I am just trying to be informitive :D
    HotFireyDeath likes this.
  11. zombie_Power
    Offline

    zombie_Power Active Member

    Joined:
    Sep 7, 2015
    Posts:
    239
    Minecraft User:
    Zombie_Power
    shouldn't the message be broadcasted to online players
    Code:
    $player = $this->getServer()->getOnlinePlayers();
  12. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    Its not needed when you do broadcastMessage() it sends to everyone on the server
    PEMapModder and HotFireyDeath like this.
  13. zombie_Power
    Offline

    zombie_Power Active Member

    Joined:
    Sep 7, 2015
    Posts:
    239
    Minecraft User:
    Zombie_Power
    isn't there a time needed for the message to be broadcasted, or will it broadcast once only?

    EDIT: unless there is a command
  14. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    This is off-topic for this thread if you need further info open your own thread
    HotFireyDeath likes this.
  15. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    broadcasts only once, the same way with how you'd use sendMessage();
  16. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    However you could have it broadcast multiple times over and over like this
    PHP:
    $x 5;
    while(
    $x == 5){
     
    $this->getServer()->broadcastMessage("message");
    }
    You should never do this though
  17. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    Actually, you defined a forever loop, which runs indefinitely over and over again.
    The way to broadcast it multiple times without crashing (does the same thing as a while loop) is to use scheduleRepeatingTask();
    which I just learned how to use today...
    Taha_The_Hacker and PEMapModder like this.
  18. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    Yea ik but im way to lazy to write that out :D i figure a while loop is a good example lol but he should know its not a good idea
    HotFireyDeath likes this.
  19. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    Just realized a much simpler way to do it USING the while loop
    PHP:
    $x 0;
    while(
    $x <= 3){
     
    //message code
     
    $x++;
    }
  20. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    I was trying to do a forever loop

Share This Page

Advertisement