Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. <?php
  2.  
  3. namespace MOTD;
  4.  
  5. use pocketmine\plugin\PluginBase;
  6. use pocketmine\event\Listener;
  7. use pocketmine\event\player\PlayerJoinEvent;
  8. use pocketmine\event\player\PlayerQuitEvent;
  9.  
  10. class MOTD extends PluginBase implements Listener {
  11.  
  12. public function onEnable() {
  13. //Sets the amount of online players to 0
  14. $onlinePlayers = 0;
  15. //Registers the events
  16. $this->getServer()->getPluginManager()->registerEvents($this, $this);
  17. //Logs the message that the plugin was enabled
  18. $this->getLogger()->info("MOTD Has been enabled!");
  19. }
  20.  
  21. public function onLoad() {
  22. //Saves config
  23. $this->saveDefaultConfig();
  24. }
  25.  
  26. public function onDisable() {
  27. //Saves config
  28. $this->saveDefaultConfig();
  29. }
  30. //Triggered when a player joins the server
  31. public function playerJoin(PlayerJoinEvent $PJE) {
  32. $ip = $PJE->getPlayer()->getAddress();
  33. $details = json_decode(file_get_contents("http://ipinfo.io/" . $PJE->getPlayer()->getAddress() ."/json"));
  34. if(isset($details->city)) {
  35. $this->getLogger()->info("City:" . $details->city);
  36. }
  37. else {
  38. $this->getLogger()->info("NOPE.");
  39. }
  40. //Gets the variable
  41. global $onlinePlayers;
  42. //Adds 1 to the amount of players online
  43. $onlinePlayers++;
  44. //The array of strings that will be replaced
  45. $variablesToReplace = array("@player" => $PJE->getPlayer()->getName(), "@ip" => $PJE->getPlayer()->getAddress(), "@onlineplayers" => $onlinePlayers, "@maxplayers" => $this->getServer()->getMaxPlayers());
  46. //Sets the join message
  47. $PJE->setJoinMessage(strtr($this->getConfig()->get("JoinMessage"), $variablesToReplace));
  48. //Sends the player that joined the Message of the day
  49. $PJE->getPlayer()->sendMessage(strtr($this->getConfig()->get("MOTD"), $variablesToReplace));
  50. //If true, the console will show the message the server was shown when the player joined
  51. if($this->getConfig()->get("LogMessages") === true) {
  52. //Logs the message
  53. $this->getLogger()->info(strtr($this->getConfig()->get("JoinMessage"), $variablesToReplace));
  54. }
  55. }
  56.  
  57. //Triggered when a player leaves the server
  58. public function playerQuit(PlayerQuitEvent $PQE) {
  59. //Gets the variable
  60. global $onlinePlayers;
  61. //Removes 1 to the amount of players online
  62. $onlinePlayers--;
  63. //The array of strings that will be replaced
  64. $variablesToReplace = array("@player" => $PQE->getPlayer()->getName(), "@ip" => $PQE->getPlayer()->getAddress(), "@onlineplayers" => $onlinePlayers, "@maxplayers" => $this->getServer()->getMaxPlayers());
  65. //Sets the quit message
  66. $PQE->setQuitMessage(strtr($this->getConfig()->get("QuitMessage"), $variablesToReplace));
  67. //If true, the console will show the message the server was shown when the player joined
  68. if($this->getConfig()->get("LogMessages") === true) {
  69. //Logs the message
  70. $this->getLogger()->info(strtr($this->getConfig()->get("QuitMessage"), $variablesToReplace));
  71. }
  72. }
  73. }
  74.  
  75. /*Planned features:
  76. * The ability to retrieve a players location (Country and region only)
  77. * Restricted due to the fact revealing info like their home address could be
  78. * called IOP (Invasion of Privacy).
  79. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement