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

Thoughts and suggestions on my first plugin?

Discussion in 'Development' started by Redux, Jan 10, 2017.

  1. Redux

    Redux Spider Jockey

    Messages:
    49
    GitHub:
    reduxredstone
    I was wondering if I could get some opinions and suggestions on my first pmmp plugin.
    Please keep in mind that this is my first plugin and I'm still fairly new to pmmp and MCPE/Win10 plugin development (though I have been working with php for quite some time now), so I'm still unaware of some of the best practices for development.

    As for the plugin itself, I decided to make a simple server-help "AI" using the MonkeyLearn API.

    Code:
    name: HelperAI
    main: Redux\Main
    version: 1.0.0
    api: 3.0.0-ALPHA2
    
    commands:
     ask:
      description: "Ask the AI a question"
      usage: "/ask <question>"
    
    Code:
    <?php
    
    namespace Redux;
    
    use Redux\Config;
    use Redux\Monkey;
    use Redux\Commands;
    
    use pocketmine\plugin\PluginBase;
    use pocketmine\event\Listener;
    
    
    class Main extends PluginBase implements Listener {
    
        public $config;
        public $Monkey;
    
        public function onLoad() {
            $this->getLogger()->info("HelperAI by Redux now loaded.");
        }
    
        public function onEnable() {
              $this->getLogger()->info("HelperAI by Redux now enabled.");
    
              $this->config = new Config();
              $this->config = $this->config->load();
    
              if ($this->config["Sandbox"]===true) {
              $this->getLogger()->info("§6§lLoading HelperAI in sandbox mode.");
              $this->getLogger()->info(print_r($this->config, true));
               }
           
              $this->Monkey = new Monkey($this->config["MonkeyKey"], $this->config["MonkeyModuleID"]);
    
              $this->getCommand("ask")->setExecutor(new Commands($this));
        }
    
        public function onDisable() {
            $this->getLogger()->info("HelperAI by Redux now disabled.");
        }
    }
    
    Code:
    <?php
    
    namespace Redux;
    
    class Config {
       
        public function load() {
    
            if (!file_exists("./plugins/HelperAI") && !is_dir("./plugins/HelperAI")) {
               mkdir("./plugins/HelperAI");
               $defaultAIKey         = "MONKEYLEARN KEY HERE";
               $defaultModuleID      = "MONKEYLEARN MODULE ID HERE";
               $defaultMessageTop    = "§a==========§6§lHelperAI§r§a==========§r";
               $defaultMessageBottom = "§a============================§r";
               $defaultConfig        = "MonkeyKey: $defaultAIKey\nMonkeyModuleID: $defaultModuleID\nTop Text: $defaultMessageTop\nBottom Text: $defaultMessageBottom\nSandbox: true\n\nResponses:\n default: §cI am not yet configured to properly answer that type of question.§r";
    
                $yaml = fopen("./plugins/HelperAI/config.yml", "wb");
                fwrite($yaml, $defaultConfig);
                fclose($yaml);
            }
    
            $config = file_get_contents("./plugins/HelperAI/config.yml");
            $config = yaml_parse($config);
    
            return $config;
        }
    }
    
    Code:
    <?php
    
    namespace Redux;
    
    use Redux\Config;
    use Redux\Monkey;
    use Redux\Commands;
    
    use pocketmine\plugin\PluginBase;
    use pocketmine\command\Command;
    use pocketmine\command\CommandSender;
    use pocketmine\command\CommandExecutor;
    
    
    class Commands implements CommandExecutor {
    
        private $server;
    
        public function __construct($server) {
            $this->server = $server;
        }
    
        public function onCommand(CommandSender $sender, Command $command, $label, array $args) {
            switch($command->getName()) {
                case "ask":
                    if (count($args) == 0) {
                        $sender->sendMessage("§4§l[ERROR]§r§c You must ask a question!§r");
                        return false;
                    }
    
                    $sender->sendMessage("§aAsking your question. Please wait...");
    
                    $question = implode(" ", $args);
                    $reply = json_decode($this->server->Monkey->ask($question, $this->server->config["Sandbox"]), true);
    
                    if (isset($this->server->config["Responses"][$reply["result"][0][0]["label"]])) {
                        $answer = $this->server->config["Responses"][$reply["result"][0][0]["label"]];
                    } else {
                        $answer = $this->server->config["Responses"]["default"];
                    }
                   
                    $sender->sendMessage($this->server->config["Top Text"]."\n".$answer."\n".$this->server->config["Bottom Text"]);
                    return true;
            }
        }
    }
    
    Code:
    <?php
    
    namespace Redux;
    
    class Monkey {
    
        private $key;
        private $module_id;
        private $monkey;
       
        public function __construct($key, $module_id) {
            $this->key = $key;
            $this->module_id = $module_id;
        }
    
        public function ask($question, $sandbox=false) {
            $payload = ["text_list" => $question];
            if ($sandbox===true) {
                $url = "https://api.monkeylearn.com/v2/classifiers/$this->module_id/classify/?sandbox=1";
            } else {
                $url = "https://api.monkeylearn.com/v2/classifiers/$this->module_id/classify/";
            }
            $curl = curl_init();
    
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Token '.$this->key));
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    
            $response = curl_exec($curl);
            curl_close($curl);
            return $response;
        }
    }
    
    To use the plugin, you first need to make a monkey on http://monkeylearn.com/. The `Responses` section of the config relate to the children of the Root in your monkey.
    An example monkey setup looks like this:
    [​IMG]
    Each child represents a different response. An example config.yml with responses would be:
    [​IMG]
    The `MonkeyKey` is your MonkeyLearn authentication token. The `MonkeyModuleID` is your MonkeyLearn ID of your Module/Monkey. `Top Text` is the text shown above the response, and `Bottom Text` is the text shown below the response. `Sandbox` determines if the Monkey is still in Sandbox mode or Live API mode.

    To ask the AI a question, you would use `/ask QUESTION`

    The full source code plus a compiled phar can be found here https://github.com/ReduxRedstone/MCPE-HelperAI

    Is there anything I should change or improve on this? This is my very first plugin, so I know there HAS to be room for improvement :)
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    Nice work!
    FYI there a class called config(no, i not against people using their own config but i just want to point out it exist(incase you dont know)),
    i dont really know how to pratically use it not really get about the setup/what it actually does
    overall good just note that do not use this on big servers as it does not seem to be able to handle over rate limit Or a internal rate limiter
     
  3. Redux

    Redux Spider Jockey

    Messages:
    49
    GitHub:
    reduxredstone
    Actually, I didn't know that Config already existed :) that would have saved me some time xD
    I also completely forgot about API call limit :eek: my bad. I'll add that in as soon as I get home to make changes.
     
  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.