Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 22.35 KB | None | 0 0
  1. <?php
  2. /**
  3.  * author: advocaite aka serverkart_rod
  4.  */
  5.  
  6. namespace essentialsTP;
  7.  
  8. use pocketmine\command\Command;
  9. use pocketmine\command\CommandExecutor;
  10. use pocketmine\command\CommandSender;
  11. use pocketmine\Server;
  12. use pocketmine\event\player\PlayerDeathEvent;
  13. use pocketmine\event\player\PlayerJoinEvent;
  14. use pocketmine\event\player\PlayerRespawnEvent;
  15. use pocketmine\event\Listener;
  16. use pocketmine\level\Position;
  17. use pocketmine\level\Level;
  18. use pocketmine\math\Vector3;
  19. use pocketmine\Player;
  20. use pocketmine\permission\Permissible;
  21. use pocketmine\IPlayer;
  22. use pocketmine\plugin\PluginBase;
  23. use pocketmine\utils\TextFormat;
  24. use pocketmine\utils\Config;
  25.  
  26.  
  27.  
  28. class essentialsTP extends PluginBase  implements CommandExecutor, Listener {
  29.     private $db2;
  30.     public $username;
  31.     public $world;
  32.     public $home_loc;
  33.     public $warp_loc;
  34.     public $params;
  35.     public $death_loc;
  36.     public $config;
  37.     public $player_cords;
  38.     public $tp_sender;
  39.     public $tp_reciver;
  40.     public $result;
  41.     public $prepare;
  42.  
  43.     public function fetchall(){
  44.         $row = array();
  45.  
  46.         $i = 0;
  47.  
  48.         while($res = $this->result->fetchArray(SQLITE3_ASSOC)){
  49.  
  50.             $row[$i] = $res;
  51.             $i++;
  52.  
  53.         }
  54.         return $row;
  55.     }
  56.  
  57.     public function onLoad(){
  58.  
  59.     }
  60.  
  61.     public function update_cooldown($name, $time, $type){
  62.         $this->prepare = $this->db2->prepare("SELECT home,warp,spawn,player FROM cooldowns WHERE player =:name");
  63.         $this->prepare->bindValue(":name", $name, SQLITE3_TEXT);
  64.         $this->result = $this->prepare->execute();
  65.         $sql = $this->fetchall();
  66.         if (count($sql) > 0){
  67.  
  68.             switch ($type){
  69.                 case 'home':
  70.                      $this->prepare = $this->db2->prepare("UPDATE cooldowns SET home = :time WHERE player = :name");
  71.                     $this->prepare->bindValue(":name", $name, SQLITE3_TEXT);
  72.                     $this->prepare->bindValue(":time", time(), SQLITE3_INTEGER);
  73.                     $this->result = $this->prepare->execute();
  74.                     return true;
  75.                     break;
  76.                 default:
  77.                     return false;
  78.             }
  79.         }
  80.         else
  81.         {
  82.  
  83.             switch ($type){
  84.                 case 'home':
  85.                     $this->prepare = $this->db2->prepare("INSERT INTO cooldowns (home, warp, spawn, player) VALUES (:time, 0, 0, :name)");
  86.                     $this->prepare->bindValue(":time", time(), SQLITE3_INTEGER);
  87.                     $this->prepare->bindValue(":name", $name, SQLITE3_TEXT);
  88.  
  89.                     $this->result = $this->prepare->execute();
  90.                     return true;
  91.                     break;
  92.                 default:
  93.                     return false;
  94.             }
  95.         }
  96.     }
  97.  
  98.     public function onPlayerDeath(PlayerDeathEvent $event)
  99.     {
  100.         $player = $event->getEntity();
  101.         $this->death_loc[$player->getName()] = new Position(
  102.             round($player->getX()),
  103.             round($player->getY()),
  104.             round($player->getZ()),
  105.             $player->getLevel()
  106.         );
  107.     }
  108.     public function onPlayerRespawn(PlayerRespawnEvent $event){
  109.         $player = $event->getPlayer();
  110.         if ( isset($this->death_loc[$player->getName()]) ){
  111.             $this->username = $player->getName();
  112.             $this->prepare = $this->db2->prepare("SELECT player,x,y,z,title,world FROM homes WHERE player =:name AND title =:bed");
  113.             $this->prepare->bindValue(":name", $this->username, SQLITE3_TEXT);
  114.             $this->prepare->bindValue(":bed", 'bed', SQLITE3_TEXT);
  115.             $this->result = $this->prepare->execute();
  116.             $sql = $this->fetchall();
  117.             if (count($sql) > 0){
  118.                 $sql = $sql[0];
  119.                 foreach($player->getServer()->getLevels() as $aval_world => $curr_world)
  120.                 {
  121.                     if ($sql['world'] == $curr_world->getName())
  122.                     {
  123.                         $pos = new Position((int) $sql['x'], (int) $sql['y'], (int) $sql['z'], $curr_world);
  124.  
  125.                         $player->teleport($pos);
  126.                         $this->update_cooldown($this->username, time(), 'home');
  127.                         $player->sendMessage("[✔] Телепортация домой прошла успешно .");
  128.                         return true;
  129.                     }
  130.                 }
  131.             }
  132.             else
  133.             {
  134.                     $this->world = $player->getLevel()->getName();
  135.                     $this->prepare = $this->db2->prepare("SELECT x,y,z,world FROM spawns WHERE world = :world");
  136.                     $this->prepare->bindValue(":world", $this->world, SQLITE3_TEXT);
  137.                     $this->result = $this->prepare->execute();
  138.                     $sql          = $this->fetchall();
  139.                     if( count($sql) > 0 ) {
  140.                         $sql = $sql[0];
  141.                         foreach($player->getServer()->getLevels() as $aval_world => $curr_world)
  142.                         {
  143.                             if ($sql['world'] == $curr_world->getName())
  144.                             {
  145.                                 $pos = new Position((int) $sql['x'], (int) $sql['y'], (int) $sql['z'], $curr_world);
  146.                                 $player->teleport($pos);
  147.                                 $this->update_cooldown($player->getName(), time(), 'spawn');
  148.                                 $player->sendMessage("[✔] Телепортация на точку возрождения прошла успешно .");
  149.                                 return true;
  150.  
  151.                             }
  152.                         }
  153.                     }
  154.                     else
  155.                     {
  156.                         $player->teleport($player->getLevel()->getSpawnLocation());
  157.                         $this->update_cooldown($player->getName(), time(), 'spawn');
  158.                         $player->sendMessage("[✔] Телепортация на точку возрождения прошла успешно .");
  159.                         return true;
  160.                     }
  161.             }
  162.         }
  163.  
  164.     }
  165.  
  166.     public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
  167.         switch($cmd->getName()){
  168.             case 'home':
  169.                 if ($sender instanceof Player)
  170.                 {
  171.                     $this->username = $sender->getName();
  172.                     if (count($args) == 0)
  173.                     {
  174.                         $this->prepare = $this->db2->prepare("SELECT player,x,y,z,title,world FROM homes WHERE player =:name");
  175.                         $this->prepare->bindValue(":name", $this->username, SQLITE3_TEXT);
  176.                         $this->result = $this->prepare->execute();
  177.                         $sql = $this->fetchall();
  178.                         $home_list = null;
  179.                         foreach ($sql as $ptu)
  180.                         {
  181.                             $home_list .= '['.TextFormat::GOLD.$ptu['title'].TextFormat::WHITE.'] ';
  182.                         }
  183.                         if($home_list != null){
  184.                             $sender->sendMessage("§7> §fВаши точки дома : ".$home_list);
  185.                             return true;
  186.                         }
  187.                         else
  188.                         {
  189.                             $sender->sendMessage("§7> §fУ Вас нет точек дома на данный момент .");
  190.                             return true;
  191.                         }
  192.  
  193.                     }else{
  194.                         $this->prepare = $this->db2->prepare("SELECT home,warp,spawn,player FROM cooldowns WHERE player =:name AND home < :time");
  195.                         $this->prepare->bindValue(":name", $this->username, SQLITE3_TEXT);
  196.                         $this->prepare->bindValue(":time", (time() - $this->config->get("tp-home-cooldown")), SQLITE3_INTEGER);
  197.                         $this->result = $this->prepare->execute();
  198.                         $cool_sql = $this->fetchall();
  199.                             $this->home_loc = $args[0];
  200.                             $this->prepare = $this->db2->prepare("SELECT player,title,x,y,z,world FROM homes WHERE player = :name AND title = :title");
  201.                             $this->prepare->bindValue(":name", $this->username, SQLITE3_TEXT);
  202.                             $this->prepare->bindValue(":title", $this->home_loc, SQLITE3_TEXT);
  203.                             $this->result = $this->prepare->execute();
  204.                             $sql = $this->fetchall();
  205.                             if( count($sql) > 0 ) {
  206.                                 $sql = $sql[0];
  207.                                 foreach($this->getServer()->getLevels() as $aval_world => $curr_world)
  208.                                 {
  209.                                     if ($sql['world'] == $curr_world->getName())
  210.                                     {
  211.                                         $pos = new Position((int) $sql['x'], (int) $sql['y'], (int) $sql['z'], $curr_world);
  212.  
  213.                                         $sender->teleport($pos);
  214.                                         $this->update_cooldown($this->username, time(), 'home');
  215.                                         $sender->sendMessage("§7> §fТелепортация домой прошла успешно .");
  216.                                         return true;
  217.                                     }
  218.                                 }
  219.                             }
  220.                             else
  221.                             {
  222.                                 $sender->sendMessage(TextFormat::RED."§7> §fУ Вас нет точки дома с таким именем .");
  223.                                 return true;
  224.                             }
  225.                     }
  226.                 }
  227.                 else
  228.                 {
  229.                     $sender->sendMessage(TextFormat::RED."This command can only be used in the game.");
  230.                     return true;
  231.                 }
  232.                 break;
  233.             case 'sethome':
  234.                 if ($sender instanceof Player)
  235.                 {
  236.                     if((count($args) != 0) && (count($args) < 2))
  237.                     {
  238.                         $this->player_cords = array('x' => (int) $sender->getX(),'y' => (int) $sender->getY(),'z' => (int) $sender->getZ());
  239.                         $this->username = $sender->getName();
  240.                         $this->world = $sender->getLevel()->getName();
  241.                         $this->home_loc = $args[0];
  242.                         $this->prepare = $this->db2->prepare("SELECT player,title,x,y,z,world FROM homes WHERE player = :name AND title = :title");
  243.                         $this->prepare->bindValue(":name", $this->username, SQLITE3_TEXT);
  244.                         $this->prepare->bindValue(":title", $this->home_loc, SQLITE3_TEXT);
  245.                         $this->result = $this->prepare->execute();
  246.                         $sql          = $this->fetchall();
  247.                         if( count($sql) > 0 )
  248.                         {
  249.                             $this->prepare = $this->db2->prepare("UPDATE homes SET world = :world, title = :title, x = :x, y = :y, z = :z WHERE player = :name AND title = :title");
  250.                             $this->prepare->bindValue(":name", $this->username, SQLITE3_TEXT);
  251.                             $this->prepare->bindValue(":title", $this->home_loc, SQLITE3_TEXT);
  252.                             $this->prepare->bindValue(":world", $this->world, SQLITE3_TEXT);
  253.                             $this->prepare->bindValue(":x", $this->player_cords['x'], SQLITE3_TEXT);
  254.                             $this->prepare->bindValue(":y", $this->player_cords['y'], SQLITE3_TEXT);
  255.                             $this->prepare->bindValue(":z", $this->player_cords['z'], SQLITE3_TEXT);
  256.                             $this->result = $this->prepare->execute();
  257.  
  258.                         }
  259.                         else
  260.                         {
  261.                             $this->prepare = $this->db2->prepare("INSERT INTO homes (player, title, world, x, y, z) VALUES (:name, :title, :world, :x, :y, :z)");
  262.                             $this->prepare->bindValue(":name", $this->username, SQLITE3_TEXT);
  263.                             $this->prepare->bindValue(":title", $this->home_loc, SQLITE3_TEXT);
  264.                             $this->prepare->bindValue(":world", $this->world, SQLITE3_TEXT);
  265.                             $this->prepare->bindValue(":x", $this->player_cords['x'], SQLITE3_TEXT);
  266.                             $this->prepare->bindValue(":y", $this->player_cords['y'], SQLITE3_TEXT);
  267.                             $this->prepare->bindValue(":z", $this->player_cords['z'], SQLITE3_TEXT);
  268.                             $this->result = $this->prepare->execute();
  269.  
  270.                         }
  271.                         $sender->sendMessage("§7> §fТочка дома успешно установлена .");
  272.                         return true;
  273.                     }
  274.                     else
  275.                     {
  276.                         $sender->sendMessage("§7> §fИспользуйте команду для создание точки дома: §b/sethome название");
  277.                         return false;
  278.                     }
  279.                 }
  280.                 else
  281.                 {
  282.                     $sender->sendMessage(TextFormat::RED."This command can only be used in the game.");
  283.                     return true;
  284.                 }
  285.                 break;
  286.             case 'delhome':
  287.                 if ($sender instanceof Player)
  288.                 {
  289.                     if((count($args) != 0) && (count($args) < 2))
  290.                     {
  291.                         $this->username = $sender->getName();
  292.                         $this->home_loc = $args[0];
  293.                         $this->prepare = $this->db2->prepare("SELECT * FROM homes WHERE player = :name AND title = :title");
  294.                         $this->prepare->bindValue(":name", $this->username, SQLITE3_TEXT);
  295.                         $this->prepare->bindValue(":title", $this->home_loc, SQLITE3_TEXT);
  296.                         $this->result = $this->prepare->execute();
  297.                         $sql          = $this->fetchall();
  298.                         if( count($sql) > 0 )
  299.                         {
  300.                             $this->prepare = $this->db2->prepare("DELETE FROM homes WHERE player = :name AND title = :title");
  301.                             $this->prepare->bindValue(":name", $this->username, SQLITE3_TEXT);
  302.                             $this->prepare->bindValue(":title", $this->home_loc, SQLITE3_TEXT);
  303.                             $this->result = $this->prepare->execute();
  304.                             $sender->sendMessage("§7> §fТочка дома §e". $this->home_loc ." §fуспешно удалена .");
  305.                             return true;
  306.                         }
  307.                         else
  308.                         {
  309.                             $sender->sendMessage("§7> §fТочки дома с таким названием не существует .");
  310.                             return true;
  311.                         }
  312.                     }
  313.                     else
  314.                     {
  315.                         $sender->sendMessage("§7> §fИспользуйте команду для удаление точки дома: §b/delhome название");
  316.                         return false;
  317.                     }
  318.                 }
  319.                 else
  320.                 {
  321.                     $sender->sendMessage(TextFormat::RED."This command can only be used in the game.");
  322.                     return true;
  323.                 }
  324.                 break;
  325.             default:
  326.                 return false;
  327.             }
  328.         }
  329.  
  330.     public function create_db(){
  331.         $this->prepare = $this->db2->prepare("SELECT * FROM sqlite_master WHERE type='table' AND name='homes'");
  332.         $this->result = $this->prepare->execute();
  333.         $sql = $this->fetchall();
  334.         $count = count($sql);
  335.         if ($count == 0){
  336.             $this->prepare = $this->db2->prepare("CREATE TABLE homes (
  337.                      id INTEGER PRIMARY KEY,
  338.                      player TEXT,
  339.                      x TEXT,
  340.                      y TEXT,
  341.                      z TEXT,
  342.                      title TEXT,
  343.                      world TEXT)");
  344.             $this->result = $this->prepare->execute();
  345.             $this->getLogger()->info(TextFormat::AQUA."essentialsTP+ Homes database created!");
  346.         }
  347.         $this->prepare = $this->db2->prepare("SELECT * FROM sqlite_master WHERE type='table' AND name='tp_requests'");
  348.         $this->result = $this->prepare->execute();
  349.         $sql2 = $this->fetchall();
  350.         $count2 = count($sql2);
  351.         if ($count2 == 0){
  352.             $this->prepare = $this->db2->prepare("CREATE TABLE tp_requests (
  353.                      id INTEGER PRIMARY KEY,
  354.                      player TEXT,
  355.                      player_from TEXT,
  356.                      type TEXT,
  357.                      time TEXT,
  358.                      status TEXT)");
  359.             $this->result = $this->prepare->execute();
  360.             $this->getLogger()->info(TextFormat::AQUA."essentialsTP+ request database created!");
  361.         }
  362.         $this->prepare = $this->db2->prepare("SELECT * FROM sqlite_master WHERE type='table' AND name='warps'");
  363.         $this->result = $this->prepare->execute();
  364.         $sql3 = $this->fetchall();
  365.         $count3 = count($sql3);
  366.         if($count3 == 0){
  367.             $this->prepare = $this->db2->prepare("CREATE TABLE warps (
  368.                      id INTEGER PRIMARY KEY,
  369.                      x TEXT,
  370.                      y TEXT,
  371.                      z TEXT,
  372.                      world TEXT,
  373.                      title TEXT)");
  374.             $this->result = $this->prepare->execute();
  375.             $this->getLogger()->info(TextFormat::AQUA."essentialsTP+ warps database created!");
  376.         }
  377.         $this->prepare = $this->db2->prepare("SELECT * FROM sqlite_master WHERE type='table' AND name='spawns'");
  378.         $this->result = $this->prepare->execute();
  379.         $sql4 = $this->fetchall();
  380.         $count4 = count($sql4);
  381.         if($count4 == 0){
  382.             $this->prepare = $this->db2->prepare("CREATE TABLE spawns (
  383.                      id INTEGER PRIMARY KEY,
  384.                      x TEXT,
  385.                      y TEXT,
  386.                      z TEXT,
  387.                      world TEXT
  388.                      )");
  389.             $this->result = $this->prepare->execute();
  390.             $this->getLogger()->info(TextFormat::AQUA."essentialsTP+ Spawns database created!");
  391.         }
  392.         $this->prepare = $this->db2->prepare("SELECT * FROM sqlite_master WHERE type='table' AND name='cooldowns'");
  393.         $this->result = $this->prepare->execute();
  394.         $sql5 = $this->fetchall();
  395.         $count5 = count($sql5);
  396.         if($count5 == 0){
  397.             $this->prepare = $this->db2->prepare("CREATE TABLE cooldowns (
  398.                      id INTEGER PRIMARY KEY,
  399.                      home INTEGER,
  400.                      warp INTEGER,
  401.                      spawn INTEGER,
  402.                      player TEXT
  403.                      )");
  404.             $this->result = $this->prepare->execute();
  405.             $this->getLogger()->info(TextFormat::AQUA."essentialsTP+ cooldown database created!");
  406.         }
  407.  
  408.     }
  409.  
  410.     public function check_config(){
  411.  
  412.         $this->config = new Config($this->getDataFolder()."config.yml", Config::YAML, array());
  413.         $this->config->set('plugin-name',"essentalsTp+");
  414.         $this->config->save();
  415.  
  416.         if(!$this->config->get("sqlite-dbname"))
  417.         {
  418.             $this->config->set("sqlite-dbname", "essentials_tp");
  419.             $this->config->save();
  420.         }
  421.  
  422.         if($this->config->get("tpa-here-cooldown") == false)
  423.         {
  424.             $this->config->set("tpa-here-cooldown", "30");
  425.             $this->config->save();
  426.         }
  427.         if($this->config->get("tp-home-cooldown") == false)
  428.         {
  429.             $this->config->set("tp-home-cooldown", "5");
  430.             $this->config->save();
  431.         }
  432.         if($this->config->get("tp-warp-cooldown") == false)
  433.         {
  434.             $this->config->set("tp-warp-cooldown", "5");
  435.             $this->config->save();
  436.         }
  437.         if($this->config->get("tp-spawn-cooldown") == false)
  438.         {
  439.             $this->config->set("tp-spawn-cooldown", "5");
  440.             $this->config->save();
  441.         }
  442.         if($this->config->get("MOTD") == false)
  443.         {
  444.             $this->config->set("MOTD", "EssintialsTP+ Welcomes you please change this motd in config");
  445.             $this->config->save();
  446.         }
  447.         if($this->config->get("wild-MaxX") == false)
  448.         {
  449.             $this->config->set("wild-MaxX", "300");
  450.             $this->config->save();
  451.         }
  452.         if($this->config->get("wild-MaxY") == false)
  453.         {
  454.             $this->config->set("wild-MaxY", "300");
  455.             $this->config->save();
  456.         }
  457.  
  458.  
  459.     }
  460.  
  461.     public function onEnable(){
  462.  
  463.         $this->getLogger()->info(TextFormat::GREEN."essentialsTP+ loading...");
  464.         @mkdir($this->getDataFolder());
  465.         $this->check_config();
  466.         try{
  467.             if(!file_exists($this->getDataFolder().$this->config->get("sqlite-dbname").'.db')){
  468.                 $this->db2 = new \SQLite3($this->getDataFolder().$this->config->get("sqlite-dbname").'.db', SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
  469.             }else{
  470.                 $this->db2 = new \SQLite3($this->getDataFolder().$this->config->get("sqlite-dbname").'.db', SQLITE3_OPEN_READWRITE);
  471.             }
  472.         }
  473.         catch (Exception $e)
  474.         {
  475.             echo $e->getMessage();
  476.             die();
  477.         }
  478.         $this->create_db();
  479.         $this->getLogger()->info(TextFormat::GREEN."[INFO] loading [".TextFormat::GOLD."config.yml".TextFormat::GREEN."]....");
  480.         $this->tpa_cooldown = time() - $this->config->get("tpa-here-cooldown");
  481.         $this->getLogger()->info(TextFormat::GREEN."[INFO] loading [".TextFormat::GOLD."config.yml".TextFormat::GREEN."] DONE");
  482.         $this->getLogger()->info(TextFormat::GREEN."essentialsTP+ loaded!");
  483.         $this->getServer()->getPluginManager()->registerEvents($this, $this);
  484.  
  485.     }
  486.  
  487.     public function onDisable(){
  488.         $this->prepare->close();
  489.         $this->getLogger()->info("essentialsTP+ Disabled");
  490.     }
  491.  
  492.  
  493.  
  494. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement