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

Http/https request

Comments in 'Plugin Development' started by wasd-studios, Aug 29, 2014.

  1. wasd-studios
    Offline

    wasd-studios Active Member

    Joined:
    Apr 23, 2014
    Posts:
    21
    Minecraft User:
    Josh299
    Hey,
    I'm currently working on a custom plugin and I'm trying setup something so that when a http request is make, it gives the selected player a set block/item

    For example when
    Code:
    http://w-a-s-d.co.uk/qwertyuipp
    Is called from a web browser
    player - Dave gets 20 stone
    (But with the text after the / hashed and a single use URL generated )


    What would you recommend ? And what would you guys recommend as being the safest method of doing this ?

    Thanks in advance for your time :)
    Last edited: Aug 29, 2014
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    @Falk's HTTPServer can be referenced to do this.
    64FF00 and Dutok like this.
  3. wasd-studios
    Offline

    wasd-studios Active Member

    Joined:
    Apr 23, 2014
    Posts:
    21
    Minecraft User:
    Josh299
    Can the pocketmine api be referenced directly in a php file served to the web ? (On @Falk s http server )
    64FF00 and Dutok like this.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The web server is run at a separate thread.
    64FF00, Dutok and wasd-studios like this.
  5. wasd-studios
    Offline

    wasd-studios Active Member

    Joined:
    Apr 23, 2014
    Posts:
    21
    Minecraft User:
    Josh299
    Ahh okay sorry I over looked that that in the documentation :)

    So what would you recommend to be the simplest method to allow a php we page to make API requests . And if it is actually a viable goal

    Thanks again
  6. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    You will need to have a separate thread regardless.
    Last edited: Aug 29, 2014
    64FF00 likes this.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Well, you can queue the operation to be run in the main thread.
    For example, in the following example code, the server pings the website http://example.com/ping.php every 10 seconds. If the result starts with "Pong: ", the message "Ping replied: {result from the website}" will be broadcasted on the server.

    PHP:
    <?php

    namespace exampleplugin;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\Thread;

    class 
    ExamplePlugin extends PluginBase{
      private 
    $thread;
      public function 
    onEnable(){
        
    $this->thread = new PingThread($this);
        
    $this->thread->start();
        
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new PingWatcher($this->thread), 1);
      }
      public function 
    onDisable(){
        
    $this->thread->running false;
      }
    }

    class 
    PingThread extends Thread{
      public 
    $plugin;
      public 
    $running true;
      public 
    $pong null;
      public function 
    __construct(ExamplePlugin $plugin){
        
    $this->plugin $plugin;
      }
      public function 
    run(){
        while(
    $this->running){
          
    $contents Utils::getURL("http://example.com/ping.php");
          if(
    is_string($contents)){
            if(
    substr($contents06) === "Pong: "){
              
    $this->pong substr($contents6);
            }
          }
          
    $this->wait(1000000); // I think this is 1 second; too lazy to check
        
    }
      }
    }

    class 
    PingWatcher extends PluginTask{
      private 
    $thread;
      public function 
    __construct(PingThread $thread){
        
    $this->thread $thread;
      }
      public function 
    onRun($ticks){
        if(
    is_string($this->thread->pong)){
          
    $this->thread->plugin->getServer()->broadcastMessage("Ping replied: {$this->thread->pong}");
          
    $this->thraed->pong null;
        }
      }
    }
      
    This example may have some issues of not conforming to the new threads system; I'm not sure yet. But I think it solves your question.
    64FF00 and wasd-studios like this.
  8. wasd-studios
    Offline

    wasd-studios Active Member

    Joined:
    Apr 23, 2014
    Posts:
    21
    Minecraft User:
    Josh299
    Thank you ! That's exactly the kind of thing I was looking for
    I look forward to adding this to my plugin I'll update the post with my results :)
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The above is a very simple example. If you expect more than 1 request per check, you have to queue the requests in an array. However, you need to serialize the array in order to make it safely shared between threads. For example:

    PHP:
    // thread class
    public $queue;
    public 
    $tempQueue = [];
    public 
    $lock false// to avoid the array being processed by two threads at the same time
    public function __construct(){
      
    $this->queue serialize([]); // initialize the array
    }
    public function 
    run(){
      while(
    $this->running){
        if(
    $this->lock === false){
          
    $this->lock true;
          
    $array unserialize($this->queue);
          while(
    count($this->tempQueue) > 0){
            
    $array[] = array_shift($this->tempQueue);
          }
          
    $this->queue serialize($array);
          
    $this->lock false;
        }
        if(
    substr(...)){
          if(
    $this->lock === false){
            
    $this->lock true;
            
    $array unserialize($this->queue);
            
    $array[] = substr($contents6);
            
    $this->queue serialize($array);
            
    $this->lock false;
          }
          else{
            
    $this->tempQueue[] = substr($contents6);
          }
        }
        
    $this->wait(1000000);
      }
    }
    public function 
    check(){ // called in the main thread
      
    if($this->lock === true){
        return;
      }
      
    $this->lock true;
      
    $array unserialize($this->queue);
      
    $this->queue serialize([]);
      
    $this->lock false;
      foreach(
    $array as $a){
        
    // process $a
      
    }
    }
    64FF00 and wasd-studios like this.
  10. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    what does $this->wait(); do? more like a scheduling stuff?
  11. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    64FF00 likes this.
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Something like sleep().
    http://hk2.php.net/manual/en/threaded.wait.php
    This is basically same as usleep() except that the sleep can be interrupted by $thread->notify(), according to what I understand.

    Edit: @Falk why are you always a few minutes faster than me? :p
    64FF00 and LDX like this.
  13. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    so it does it affect much in the TPS?
    64FF00 likes this.
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It is in another thread. TPS counts how fast the main thread is.
  15. Humerus
    Offline

    Humerus Staff Member Plugin Reviewer

    Joined:
    Aug 23, 2013
    Posts:
    114
    Plugins:
    1
    Well, I would've taken this situation entirely differently. I would've just enabled RCON for the server and created a PHP script that would connect to the server to do anything I need. If there wasn't a command that could do what I wanted, then I would look into writing a standalone plugin.
    wasd-studios likes this.
  16. wasd-studios
    Offline

    wasd-studios Active Member

    Joined:
    Apr 23, 2014
    Posts:
    21
    Minecraft User:
    Josh299
    Good thinking @Humerus ! I didn't think of that maybe that could be a simpler idea for lots of commands for each player :)

    +1 ;)
  17. wasd-studios
    Offline

    wasd-studios Active Member

    Joined:
    Apr 23, 2014
    Posts:
    21
    Minecraft User:
    Josh299
    Or a combination of the two ? ......

Share This Page

Advertisement