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

Help With SAM Auth Plugin

Comments in 'Plugin Development' started by EdwardAllington, Dec 27, 2015.

  1. EdwardAllington
    Offline

    EdwardAllington Active Member

    Joined:
    Jan 29, 2015
    Posts:
    78
    Minecraft User:
    Edwardthedog2
    Hello everyone. I am currently working on a plugin called "Clans" (as some of you already know), but I'm getting some help on it, and am waiting for some people to get back to me. In the mean time, I've decided that I'm going to make an auth plugin that makes you register at the servers website before playing. The plugin will be called "SAM". In case you're wondering SAM stands for "Simple Authorization Mechanism". Anyway I need help getting some code to connect SAM with the Pocketmine server and the website. Could anyone please help?
  2. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    The way I connect the code with the website is sort of complex.

    Create a scheduled repeating task that will connect to the specified domain and attempt to find a file (I use something like connect.txt). If the server doesn't find the file, then do nothing. It will repeatedly check that file in the domain.
    Then, when a player registers on the website, make the file on the domain side (connect.txt), and set it to a value. The PocketMine server should detect this file, with the value, and react. You should then create a file (perhaps something.json) that the PocketMine server will check for on the domain.
    Once the PocketMine server finds this file (something.json), it should get the contents of the JSON file and get the encoded login information you put inside it.
    Finally, delete the connect.txt and the something.json files.

    It's sort of complex, but that's one solution I use to connect the PocketMine server to a website.
    AndrewBit likes this.
  3. EdwardAllington
    Offline

    EdwardAllington Active Member

    Joined:
    Jan 29, 2015
    Posts:
    78
    Minecraft User:
    Edwardthedog2
    Okay. Could you maybe give me an example please?
  4. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    I quickly wrote this, so it may be buggy, but here is a basic layout:
    applqpak likes this.
  5. EdwardAllington
    Offline

    EdwardAllington Active Member

    Joined:
    Jan 29, 2015
    Posts:
    78
    Minecraft User:
    Edwardthedog2
    Okay, thank you. So what about this for the config.yml?
    PHP:
    #SAM plugin default configuration
    ---
    #Keep logged in by IP
    IPLogintrue
    #Force single authentication
    force-single-authtrue
    #Allow not authenticated users to move
    allow-movetrue
    #Block chat for not authenticated users
    block-chattrue
    #Block commands for not authenticated users
    block-commandstrue
    #Block all events
    block-all-eventstrue
    #Login/Register timeout
    timeout120
    #Minimum password length
    minPasswordLength6
    #Maximum password length
    maxPasswordLength16
    #Login settings
    register:
    #Enable register for server
    enabledtrue
    #Website domain name
    domainwebsite.com
    login
    :
      
    #Enable login on your server
      
    enabledtrue\
     
  6. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    Looks good to me.
  7. EdwardAllington
    Offline

    EdwardAllington Active Member

    Joined:
    Jan 29, 2015
    Posts:
    78
    Minecraft User:
    Edwardthedog2
    Okay, cool. So how's this for the plugin.yml?
    PHP:
    nameSAM
    main
    SAM\SAM
    version
    "1.0.0"
    api: [1.11.0]
    authorEdwardthedog2
    description
    Pocketmine auth plugin for signing up at the servers website.

    commands:
    login:
      
    descriptionLogs in to an existing account.
       
    permissionsam.login
    Tim // robske Büba likes this.
  8. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    If you're using latest PocketMine, api is 1.13.0 or 2.0.0.
    Also, don't use "" for version. Just type version number in.
  9. EdwardAllington
    Offline

    EdwardAllington Active Member

    Joined:
    Jan 29, 2015
    Posts:
    78
    Minecraft User:
    Edwardthedog2
    Okay, so I fixed that. Also, when someone logs in for the first time I want this message to appear in chat: "This server uses SAM by Edwardthedog2. You must register at http://serverssite.com to play." How do I do that?
  10. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    Add the following into your code:
    - implement Listener to your main class.
    - make sure to use pocketmine\event\Listener and pocketmine\event\player\PlayerJoinEvent.
    - on PlayerJoinEvent, detect if a player file (playername.txt?) is in your DataFolder. (is_file())
    - on registration, in that function I provided, make a text file (playername.txt?) in the DataFolder. (fopen())
    - on PlayerJoinEvent, if the file (playername.txt?) is found, let player in, else, kick the player with a message. (kick())
  11. EdwardAllington
    Offline

    EdwardAllington Active Member

    Joined:
    Jan 29, 2015
    Posts:
    78
    Minecraft User:
    Edwardthedog2
    Okay, I think I can do that. Also, I'm having trouble making the login command. I've got the following so far.
    PHP:
    <?php
    namespace SAM;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\ComamndSender;

    class 
    LoginCommand

      
    public function onCommand(CommandSender $senderCommand $command$label, array $args){
        
    $commandName $command->getName();
        if(
    $commandName === "login"){
          return 
    true;
        }
        return 
    false;
      
  12. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    You're missing the last bracket } for onCommand().
    Also missing brackets for main class.
    applqpak likes this.
  13. EdwardAllington
    Offline

    EdwardAllington Active Member

    Joined:
    Jan 29, 2015
    Posts:
    78
    Minecraft User:
    Edwardthedog2
    Oh okay, I see, thank you for pointing that out. But now I need a lot of help. Whenever I test the plugin, on my Pocketmine server I have specifically for testing plugins, the server always crashes.
  14. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Crash Dumps? Server Logs? Anything that actually describes what's happening backend?
  15. EdwardAllington
    Offline

    EdwardAllington Active Member

    Joined:
    Jan 29, 2015
    Posts:
    78
    Minecraft User:
    Edwardthedog2
    Yes, the crash dump says "Error: Class SAM\SAM not found", but I'm not sure how to fix it.
  16. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Show me your code with the plugin.yml
  17. EdwardAllington
    Offline

    EdwardAllington Active Member

    Joined:
    Jan 29, 2015
    Posts:
    78
    Minecraft User:
    Edwardthedog2
    Okay then.
    plugin.yml
    PHP:
    nameSAM
    main
    SAM\SAM
    version
    1.0.0
    api
    : [1.13.0]
    authorEdwardthedog2
    description
    Pocketmine auth plugin for signing up at the servers website.

    commands:
    login:
      
    description"This is a test command"
      
    usage"/login <password>"
      
    permissionsam.login
    Web.php
    PHP:
    <?php
    // this is the plugin file.
    namespace sam;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\scheduler\PluginTask;
    class 
    sam extends PluginBase {
      public function 
    onEnable(){
        
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new Task($this), 20);
      }
      public function 
    action($array){
        
    $username $array["username"]; // username
        
    $password $array["password"]; // password
        // whatever script below.
      
    }
    }
    class 
    Task extends PluginTask {
      public 
    $plugin;
      public function 
    __construct($plugin){
        
    $this->plugin $plugin;
        
    $this->start false;
        
    parent::__construct($plugin);
      }
      public function 
    onRun($ticks){
        if (
    $this->start){
          
    $requesturl "yourdomain.com/the/core/directory/to/your/file/example.txt";
          
    $file_headers = @get_headers($requesturl);
            if(
    $file_headers[0] == 'HTTP/1.1 404 Not Found'){
               
    $file_exists false;
            }
            else {
               
    $file_exists true;
            }
          if (
    $file_exists === true){
            
    $data json_decode(file_get_contents("yourdomain.com/the/core/directory/to/your/file/data.json"), true);
            if (isset(
    $data["username"])){
              
    $this->plugin->action($data);
            }
          }
        }
        else{
          
    $this->start true;
        }
      }
    }
    ?>
    <?php
    // this is what can go onto the webserver.
    // if you are putting this directly, please pass information as post.
    $username $_POST["username"];
    $password $_POST["password"];
    if (
    is_file("example.txt")){
      
    unlink("example.txt");
    }
    $data fopen("example.txt""w+");
    fwrite($data"open"); // whatever value works ;)
    fclose($data);
    $login fopen("data.json""w+");
    fwrite($loginjson_encode(array("username" => $username"pswd" => $password)));
    fclose($login);
    // make sure to delete those files after.
    ?>
    LoginCommand.php
    PHP:
    <?php
    namespace SAM;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\ComamndSender;

    class 
    LoginCommand

      
    public function onCommand(CommandSender $senderCommand $command$label, array $args){
        
    $commandName $command->getName();
        if(
    $commandName === "login"){
          return 
    true;
        }
        return 
    false;
      }
    And SAM.php
    PHP:
    <?php
    namespace SAM;
    use 
    pocketmine\plugin\PluginBase;
    class 
    MyPlugin extends PluginBase{
    }
  18. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    class name for SAM.php should be SAM and your LoginCommand.php is missing brackets for the class
    applqpak likes this.
  19. EdwardAllington
    Offline

    EdwardAllington Active Member

    Joined:
    Jan 29, 2015
    Posts:
    78
    Minecraft User:
    Edwardthedog2
    Okay, I fixed the SAM.php, but where would the brackets go in LoginCommand.php?
  20. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    PHP:
    <?php
    namespace SAM;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\ComamndSender;

    class 
    LoginCommand{//here
    public function onCommand(CommandSender $senderCommand $command$label, array $args){$commandName $command->getName();
     if(
    $commandName === "login"){
     return 
    true;
     }
     return 
    false;
     }
    }
    //and here
    applqpak likes this.

Share This Page

Advertisement