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

plugin execute specific command

Discussion in 'Help' started by Winchester, Jul 31, 2018.

  1. Winchester

    Winchester Baby Zombie

    Messages:
    133

    I would like to change a command so that when executing the action instead of changing the game mode, the plugin executes a specific command, can anyone help me?



    if($next == 'creative'){
    $gameMode = Server::getGamemodeFromString($next);
    if($gameMode != -1){
    $player->setGamemode($gameMode);
    } else {
    if($this->main->cfg->get('debug') == true){
    $this->main->getLogger()->warning('Invalid game mode ('.$next.') set in config.yml');
    }
    }
    }
    if($next == 'fly'){
    $player->setAllowFlight(true);
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You would remove the part that sets the gamemode, and instead use
    PHP:
     $this->main->getServer()->dispatchCommand(new ConsoleCommandSender(), "command"); 
    If you want the command to be run as the player, replace "new ConsoleCommandSender()" with $player. Otherwise, it will be run through the console. You can replace "command" with any command you want, although it doesn't need a / like if you were running the command in-game. For example, if you want to give Steve a cookie, you could replace "command" with "give Steve cookie 1".
     
  3. AlexPads

    AlexPads Spider Jockey

    Messages:
    27
    GitHub:
    alexpads
    @corytortoise Does main have to be the name of the php file?
     
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Main could be any object that has a getServer method, but in this case it's most likely a PluginBase. If you use this in your main file (the one extending PluginBase), you can leave out the main-> part and use $this->getServer() directly.
    I'd recommend to learn the concept of object oriented programming.
     
    corytortoise likes this.
  5. AlexPads

    AlexPads Spider Jockey

    Messages:
    27
    GitHub:
    alexpads
    @HimbeersaftLP I was just asking because i already Knew the other way thats why i was confused why there was the main-> because i was trying to figure out why you would need that
     
  6. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    That's just the name OP used instead of plugin, which most people use.
     
  7. AlexPads

    AlexPads Spider Jockey

    Messages:
    27
    GitHub:
    alexpads
    ohhhhh ok
     
  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.