Guest User

Untitled

a guest
May 26th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.94 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. __PocketMine Plugin__
  5. name=TrustPlayer
  6. description=You can't place or break anything if the admin don't accept you on the server.
  7. version=0.0.4
  8. apiversion=6,7
  9. author=InusualZ
  10. class=TrustPlayer
  11. */
  12.  
  13. class TrustPlayer implements Plugin{
  14.     private $api, $config, $sqli;
  15.  
  16.     public function __construct(ServerAPI $api, $server = false){
  17.         $this->api  = $api;
  18.     }
  19.      
  20.     public function init()
  21.     {
  22.  
  23.         //Adding Handler
  24.         $this->api->addhandler('player.block.touch', array($this, 'handler'), 15);
  25.  
  26.         //Adding Commands and Alias
  27.         $this->api->console->register('trust', '[TrustPlayer] Trust a player.', array($this, 'command'));
  28.         $this->api->console->register('detrust', '[TrustPlayer] Destrust a player.', array($this, 'command'));
  29.         $this->api->console->alias("tpl", "trust");
  30.         $this->api->console->alias("dpl","detrust");
  31.  
  32.         //Creating Configuration File
  33.         $this->config = new Config($this->api->plugin->configPath($this) . "config.yml", CONFIG_YAML, array(
  34.             "send-msg"      => true,
  35.             "msg-break"     => "[TrustPlayer] You can't break block, you are not a trust player.",
  36.             "msg-place"     => "[TrustPlayer] You can't place block, you are not a trust payer.",
  37.             "msg-pickup"    => "[TrustPlayer] You can't pickup anything, you are not a trust payer.",
  38.             "msg-default"   => "[TrustPlayer] You are not a trust player."
  39.         ));
  40.  
  41.         //Creating SQLite Database and Table
  42.         $this->sqli = new SQLite3($this->api->plugin->configPath($this) . 'trustplayer.db');
  43.         $this->sqli->exec("CREATE TABLE IF NOT EXISTS Players (id INTEGER PRIMARY KEY, username TEXT)");
  44.        
  45.     }
  46.  
  47.     public function __destruct()
  48.     {
  49.         //Closing file and connection when finish.
  50.         $this->sqli->close();
  51.     }
  52.  
  53.     public function handler(&$data, $event)
  54.     {
  55.         switch ($event)
  56.         {
  57.             case "player.block.touch":
  58.  
  59.                 if (!$this->usernameExist($data['player']))//Checking if username exist on the database.
  60.                 {
  61.                     if ($this->config->get("send-msg"))// If send-msg variable is true send the message.
  62.                     {
  63.                         switch ($data["type"])
  64.                         {
  65.                             case "place":
  66.                                 $this->api->player->get($data["player"])->eventHandler($this->config->get("msg-place"), "server.chat");
  67.                                  break;
  68.                             case "break":
  69.                                 $this->api->player->get($data["player"])->eventHandler($this->config->get("msg-break"), "server.chat");
  70.                                 break;
  71.                             case "pickup":
  72.                                 $this->api->player->get($data["player"])->eventHandler($this->config->get("msg-pickup"), "server.chat");
  73.                                 break;
  74.                             case "active":
  75.                                 $this->api->player->get($data["player"])->eventHandler($this->config->get("msg-default"), "server.chat");
  76.                                 break;
  77.                             }
  78.                     }                        
  79.                     return false;
  80.                 }
  81.                 break;
  82.         }
  83.     }        
  84.  
  85.     public function command($cmd, $params)
  86.     {
  87.         switch ($cmd)
  88.         {
  89.             case "trust":
  90.                 $username = array_shift($params);
  91.                 if(empty($username) || $username == NULL)
  92.                 {
  93.                     console('[INFO] Usage: /trust <player>');
  94.                 }
  95.                 else
  96.                 {
  97.                     if($this->usernameExist($username))
  98.                     {
  99.                         console('[TrustPlayer] Username already exist.');
  100.                     }
  101.                     else
  102.                     {
  103.                         $this->trust($username);
  104.                     }
  105.                    
  106.                 }
  107.             break;
  108.            
  109.             case "detrust":
  110.                 $username = array_shift($params);
  111.                 if(empty($username) || $username == NULL)
  112.                 {
  113.                     console('[INFO] Usage: /detrust <player>');
  114.                 }
  115.                 else
  116.                 {
  117.                     if($this->usernameExist($username))
  118.                     {
  119.                         $this->detrust($username);
  120.                         console('[TrustPlayer] The username has been deleted.');
  121.                     }
  122.                     else
  123.                     {
  124.                         console("[TrustPlayer] Username don't exist");
  125.                     }
  126.                 }
  127.             break;
  128.         }
  129.     }
  130.  
  131.     public function trust($username)
  132.     {
  133.         console("[TrustPlayer] Inserting username on database...");
  134.         return $this->sqli->query("INSERT OR REPLACE INTO  Players (username) VALUES ('$username')");
  135.     }
  136.  
  137.     public function detrust($username)
  138.     {
  139.         if($this->usernameExist($username))//Checking if username exists on the database
  140.         {  
  141.             console("[TrustPlayer] Detrusting player...");
  142.             return $this->sqli->query("DELETE FROM Players WHERE username = '$username'");
  143.         }
  144.         else
  145.         {
  146.             console("[TrustPlayer] Username don't exist on the database");
  147.             return false;
  148.         }
  149.     }
  150.  
  151.     public function usernameExist($name)
  152.     {
  153.         $result = $this->sqli->query("SELECT count(username) AS count FROM Players WHERE username = '$name'");
  154.         console("[TrustPlayer] Checking if username exists");
  155.         if($result !== NULL OR $result !== FALSE OR $result !== TRUE)
  156.         {
  157.             $result = $result->fetchArray(SQLITE3_ASSOC);
  158.             return $result['count'];
  159.         }
  160.         else
  161.         {
  162.             return false;
  163.         }
  164.  
  165.     }
  166.  
  167.  
  168. }
  169. ?>
Advertisement
Add Comment
Please, Sign In to add comment