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

im making a serveratings plugin here is what i have so far

Comments in 'Plugin Development' started by jerlegoman, Jan 12, 2014.

  1. jerlegoman
    Offline

    jerlegoman Active Member

    Joined:
    Dec 10, 2013
    Posts:
    120
    Minecraft User:
    jeremy647
    PHP:
    <?php

    /*
    __PocketMine Plugin__
    name=Server Rating
    description=tells console the player's rating about the server
    version=1.0
    author=jerlegoman
    class=rating
    apiversion=10,11
    */

    /*
    Descrption:
    Have you ever wondered what guests on your server think of your server? well now you can with the server rating plugin. this plugin uses 4 commands for different ratings. The plugin has rate bad, rate ok, rate good and rate great.
    To get the most out of this plugin use the Signconsole plugin and the EchoCommand plugin then all the person has to do is click on the sign and it sends the rating to the console and if you use the EchoCommand plugin you can see who sent the rating.
    ================
    Commands :
    /ratebad - gives bad rating
    /rateok - gives an ok rating
    /rategood - gives rating good
    /rategreat - gives rating great
    */

    /*
    Change logs

    1.0
    doesnt save ratings yet

    */

    class rating implements Plugin{
      private 
    $api;

      public function 
    __construct(ServerAPI $api$server false){
        
    $this->api $api;
      }

      public function 
    init(){
        
    $this->api->console->register("ratebad","send to console bad", array($this"serverbad"));
        
    $this->api->console->register("rateok","send to console ok", array($this"serverok"));
        
    $this->api->console->register("rategood","send to console good", array($this"servergood"));
        
    $this->api->console->register("rategreat","send to console great", array($this"servergreat"));
      }

      public function 
    serverbad($cmd$args){
        
    $this->api->console->run("tell console server is bad");
      }
     
      public function 
    serverok($cmd$args){
        
    $this->api->console->run("tell console server is ok");
      }
     
      public function 
    servergood($cmd$args){
              
    $this->api->console->run("tell console server is good");
      }

          public function 
    servergreat($cmd$args){
              
    $this->api->console->run("tell console server is great");
      }
     
      public function 
    __destruct(){
      }
    }
    ?>
    It works but maybe someone can help me make it better

    Attached Files:

  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    First of all don't do /tell console
    Do console() instead.

    Second, the best method to save is
    PHP:
    if(in_array($rater->username,explode("\n",file_get_contents($theFilePathWhereYouSaveThePlayers))))return "You already rated";
    file_put_contents($rater->username,FILE_APPEND);
    file_put_contents(file_get_contents($fileWhereYouSaveANumberThatIsTheSumOfRatings)+$rating);
    For $rating, it should range from -2 to 2, excluding 0 (which means I give up the right to vote)
  3. jerlegoman
    Offline

    jerlegoman Active Member

    Joined:
    Dec 10, 2013
    Posts:
    120
    Minecraft User:
    jeremy647
    is there a way to save it to a config
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    There is of course, but since you are saving strings and numbers only, it is higher efficiency to save it in plain text.
    I was in a hurry last night, and this is the better code:
    PHP:
    class Serveratings implements Plugin{
    private 
    $dir=ServerAPI::request()->api->plugin->configPath($this);
    private 
    $cmds=array("rategreat","rategood","rateng","ratepoor","raterubbish");//rateng is rate-no-good
    public function __construct(ServerAPI $api,$s=0){}
    public function 
    __destruct(){}
    public function 
    init(){
      foreach(
    $this->cmds as $cmd){
        
    ServerAPI::request()->api->console->register($cmd,"rate the server",array($this,"cmdHandler"));
      }
    }
    public function 
    cmdHandler($cmd,$arg,$rater){
      
    switchw($cmd){
        case 
    $this->cmds[0]:
          return 
    $this->rate(2,$rater);
        case 
    $this->cmds[1]:
          return 
    $this->rate(1,$rater);
        case 
    $this->cmds[2]:
          return 
    $this->rate(-1,$rater);
        case 
    $this->cmds[3]:
          return 
    $this->rate(-2,$rater);
        case 
    $this->cmds[4]:
          return 
    $this->rate(-3,$rater);
      }
    }
    private function 
    rate($score,$rater){
      
    $data=explode("\n",file_get_contents($this->dir."Raters.txt"));
      if(
    $data!==false and in_array($rater->username,$data))
        return 
    "You cannot rate twice.";
      
    file_put_contents($this->dir."Raters.txt","\n".$rater->username,FILE_APPEND);
      
    $rating=file_get_contents($this->dir."SumedRate.txt");
      
    $rate=($rating===false?0:(int)$rating)+score;
      
    file_put_contents($this->dir."Raters.txt",$rate);
    }
    Well basically I made the whole plugin for you.
    To give you a chance to make your own plugin, you can improve the file functions using this:
    http://hk2.php.net/manual/en/ref.filesystem.php
    note the fopen(), fwrite(), fread() and fclose() functions.

Share This Page

Advertisement