Advertisement
Guest User

Untitled

a guest
Apr 25th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /*
  4. __PocketMine Plugin__
  5. name=SocialSign
  6. description=
  7. version=1.1
  8. author=Dinokiller
  9. class=SocialSign
  10. apiversion=12
  11. */
  12.  
  13. class SocialSign implements Plugin{
  14. private $api, $config, $tap;
  15. private $help = array(
  16. "Commands:\n",
  17. " /socialsign create - Creates a social sign.\n",
  18. " /ss - Alias for socialsign\n"
  19. );
  20.  
  21. public function __construct(ServerAPI $api, $server = false){
  22. $this->api = $api;
  23. }
  24.  
  25. public function init(){
  26. $this->config = new Config($this->api->plugin->configPath($this) . "config.yml", CONFIG_YAML, array(
  27. "op-only-create-signs" => true,
  28. "op-only-destroy-signs" => true,
  29. "sign-update-interval" => 10,
  30. "save-interval" => 5
  31. ));
  32. $this->config = $this->api->plugin->readYAML($this->api->plugin->configPath($this) . "config.yml");
  33. $this->api->addHandler("player.block.touch", array($this, "eventHandler"));
  34. $this->api->addHandler("player.block.break", array($this, "eventHandler"));
  35. $this->api->console->register("socialsign", "[function] [args]", array($this, "handleCommand"));
  36. $this->api->console->alias("ss", "socialsign");
  37. $this->api->schedule(20 * $this->config["sign-update-interval"], array($this, "updateSigns"), array(), false);
  38. }
  39.  
  40. public function eventHandler(&$data, $event){
  41. switch($event){
  42. case "player.block.break":
  43. $block = $data["target"];
  44. $player = $data["player"];
  45. foreach($this->config as &$sign){
  46. if(isset($sign["x"]) && isset($sign["y"]) && isset($sign["z"]) && isset($sign["level"])){
  47. if($sign["x"] == $block->x && $sign["y"] == $block->y && $sign["z"] == $block->z && $sign["level"] == $block->level->getName()){
  48. if(!$this->config["op-only-destroy-signs"] || $this->config["op-only-destroy-signs"] && $this->api->ban->isOp($player->username)){
  49. unset($sign);
  50. $player->sendChat("This social sign has been unregistered.\n");
  51. $this->api->plugin->writeYAML($this->api->plugin->configPath($this) . "signs.yml", $this->config);
  52. }else{
  53. $player->sendChat("You don't have permission to destroy social signs.\n");
  54. return false;
  55. }
  56. }
  57. }
  58. }
  59. break;
  60. case "player.block.touch":
  61. $player = $data["player"];
  62. $block = $data["target"];
  63. if(isset()isset($this->tap[$player->username]["type"]) && isset($this->tap[$player->username]["username"]) && $block->getID() == WALL_SIGN || $block->getID() == SIGN_POST){
  64. $pos = new Position($block->x, $block->y, $block->z, $block->level);
  65. $tile = $this->api->tile->get($pos);
  66. if(!$tile instanceof Tile){
  67. $tile = $this->api->tile->add($block->level, TILE_SIGN, $block->x, $block->y, $block->z);
  68. }
  69. foreach($this->config as $sign){
  70. if($sign["x"] == $block->x && $sign["y"] == $block->y && $sign["z"] == $block->z && $sign["level"] = $block->level->getName()){
  71. $player->sendChat("There's already a social sign here!\n");
  72. return;
  73. }
  74. }
  75. array_push($this->config, array(
  76. "type" => $this->tap[$player->username]["type"],
  77. "username" => $this->tap[$player->username]["username"],
  78. "x" => $block->x,
  79. "y" => $block->y,
  80. "z" => $block->z,
  81. "level" => $block->level->getName()
  82. ));
  83. $this->api->plugin->writeYAML($this->api->plugin->configPath($this) . "signs.yml", $this->config);
  84. unset($this->tap[$player->username]);
  85. $player->sendChat("Social sign created successfully!\n");
  86. }
  87. break;
  88. }
  89. }
  90.  
  91. public function handleCommand($cmd, $params, $issuer){
  92. $out = "";
  93. if($cmd == "socialsign"){
  94. if(count($params) == 0){
  95. $params[0] = "help";
  96. }
  97. switch($params[0]){
  98. case "create":
  99. if($issuer instanceof Player){
  100. if(!$this->config["op-only-create-signs"] || $this->config["op-only-create-signs"] && $this->api->ban->isOp($issuer->username)){
  101. if(isset($params[1]) && isset($params[2])){
  102. $params[1] = strtolower($params[1]);
  103. if($params[1] != "youtube" && $params[1] != "facebook" && $params[1] != "twitter"){
  104. $out .= "That social network doesn't exist or isn't supported.\n";
  105. }else{
  106. $this->tap[$issuer->username]["type"] = $params[1];
  107. $this->tap[$issuer->username]["username"] = $params[2];
  108. $out .= "Tap a sign to create a social sign!\n";
  109. }
  110. }else{
  111. $out .= "Usage: /socialsign create <socialnetwork> <username>\n";
  112. }
  113. }else{
  114. $out .= "You don't have permission to create social signs.\n";
  115. }
  116. }else{
  117. $out .= "[CMD] This can only be run in-game.\n";
  118. }
  119. break;
  120. case "help":
  121. foreach($this->help as $line){
  122. $out .= $line;
  123. }
  124. break;
  125. default:
  126. $out .= "Command doesn't exist! Use /help\n";
  127. }
  128. return $out;
  129. }
  130. }
  131.  
  132. public function updateSigns(){
  133. foreach($this->config as &$sign){
  134. if(isset($sign["type"]) && isset($sign["x"]) && isset($sign["y"]) && isset($sign["z"]) && isset($sign["level"]) && isset($sign["username"])){
  135. switch(strtolower($sign["type"])){
  136. case "youtube":
  137. $level = $this->api->level->get($sign["level"]);
  138. if(!$level instanceof Level){
  139. unset($tile);
  140. continue;
  141. }
  142. $pos = new Position($sign["x"], $sign["y"], $sign["z"], $level);
  143. $tile = $this->api->tile->get($pos);
  144. if(!$tile instanceof Tile){
  145. unset($tile);
  146. continue;
  147. }
  148. $tile->setText("[YouTube]", $sign["username"], "has " . $this->getYouTubeSubs($sign["username"]), "subscribers.");
  149. break;
  150. case "twitter":
  151. $level = $this->api->level->get($sign["level"]);
  152. if(!$level instanceof Level){
  153. unset($tile);
  154. continue;
  155. }
  156. $pos = new Position($sign["x"], $sign["y"], $sign["z"], $level);
  157. $tile = $this->api->tile->get($pos);
  158. if(!$tile instanceof Tile){
  159. unset($tile);
  160. continue;
  161. }
  162. $tile->setText("[Twitter]", $sign["username"], "has " . $this->getTwitterFollowers($sign["username"]), "followers.");
  163. break;
  164. case "facebook":
  165. $level = $this->api->level->get($sign["level"]);
  166. if(!$level instanceof Level){
  167. unset($tile);
  168. continue;
  169. }
  170. $pos = new Position($sign["x"], $sign["y"], $sign["z"], $level);
  171. $tile = $this->api->tile->get($pos);
  172. if(!$tile instanceof Tile){
  173. unset($tile);
  174. continue;
  175. }
  176. $tile->setText("[Facebook]", $sign["username"], "has " . $this->getFacebookLikes($sign["username"]), "likes.");
  177. break;
  178. case "twitch":
  179. $level = $this->api->level->get($sign["level"]);
  180. if(!$level instanceof Level){
  181. unset($tile);
  182. continue;
  183. }
  184. $pos = new Position($sign["x"], $sign["y"], $sign["z"], $level);
  185. $tile = $this->api->tile->get($pos);
  186. if(!$tile instanceof Tile){
  187. unset($tile);
  188. continue;
  189. }
  190. $tile->setText("[Twitch]", $sign["username"], "has " . $this->getTwitchFollowers($sign["username"]), "followers.");
  191. break;
  192. default:
  193. unset($tile);
  194. }
  195. }else{
  196. unset($sign);
  197. }
  198. }
  199. $this->api->schedule(20 * $this->config["sign-update-interval"], array($this, "updateSigns"), array(), false);
  200. }
  201.  
  202. public function getTwitchFollowers($username){
  203. $data = Utils::curl_get("https://api.twitch.tv/kraken/channels/" . strtolower($username) . "/follows.json");
  204. $data = json_decode($data, true);
  205. if(isset($data["error"]) || !isset($data["_total"])){ // If the user doesn't exist.
  206. return 0;
  207. }
  208. return $data["_total"];
  209. }
  210.  
  211. public function getYouTubeSubs($username){
  212. $data = file_get_contents("http://gdata.youtube.com/feeds/api/users/" . strtolower($username) ."?alt=json");
  213. if($data = "User not found"){
  214. return 0;
  215. }
  216. $data = json_decode($data, true);
  217. return $data["entry"]["yt$statistics"]["subscriberCount"];
  218. }
  219.  
  220. public function getTwitterFollowers($username){
  221. $data = Utils::curl_get("https://cdn.api.twitter.com/1/users/lookup.json?screen_name=" . strtolower($username));
  222. $data = json_decode($data, true);
  223. if(isset($data["errors"]) || !isset($data[0]["followers_count"])){ // If the user doesn't exist.
  224. return 0;
  225. }
  226. return $data[0]["followers_count"];
  227. }
  228.  
  229. public function getFacebookLikes($username){
  230. $data = file_get_contents("http://graph.facebook.com/" . strtolower($username));
  231. $data = json_decode($data, true);
  232. if(isset($data["error"]) || !isset($data["likes"])){ // If the user doesn't exist.
  233. return 0;
  234. }
  235. return $data["likes"];
  236. }
  237.  
  238. public function __destruct(){
  239.  
  240. }
  241.  
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement