PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/WorldProtect/src/aliuly/worldprotect/WpBordersMgr.php

https://gitlab.com/Skull3x/pocketmine-plugins
PHP | 142 lines | 105 code | 13 blank | 24 comment | 41 complexity | e8a5c09d1dd1b9046034591896256313 MD5 | raw file
  1. <?php
  2. //= cmd:border,Sub_Commands
  3. //: defines a border for a world
  4. //> usage: /wp _[world]_ **border** _[range|none|x1 z1 x2 z2]_
  5. //:
  6. //: Defines a border for an otherwise infinite world. Usage:
  7. //> - /wp _[world]_ **border**
  8. //: - will show the current borders for _[world]_.
  9. //> - /wp _[world]_ **border** _x1 z1 x2 z2_
  10. //: - define the border as the region defined by _x1,z1_ and _x2,z2_.
  11. //> - /wp _[world]_ **border** _range_
  12. //: - define the border as being _range_ blocks in `x` and `z` axis away
  13. //: from the spawn point.
  14. //> - /wp _[world]_ **border** **none**
  15. //: - Remove borders
  16. //:
  17. //= features
  18. //: * World borders
  19. //= docs
  20. //: It is possible to create limits in your limitless worlds.
  21. //: So players are not able to go beyond a preset border. This is
  22. //: useful if you want to avoid overloading the server by
  23. //: generating new Terrain.
  24. //:
  25. namespace aliuly\worldprotect;
  26. use pocketmine\plugin\PluginBase as Plugin;
  27. use pocketmine\event\Listener;
  28. use pocketmine\command\CommandSender;
  29. use pocketmine\command\Command;
  30. use pocketmine\Player;
  31. use aliuly\worldprotect\common\mc;
  32. use pocketmine\event\player\PlayerMoveEvent;
  33. use pocketmine\event\entity\EntityTeleportEvent;
  34. class WpBordersMgr extends BaseWp implements Listener {
  35. public function __construct(Plugin $plugin) {
  36. parent::__construct($plugin);
  37. $this->owner->getServer()->getPluginManager()->registerEvents($this, $this->owner);
  38. $this->enableSCmd("border",["usage" => mc::_("[range|none|x1 z1 x2 z2]"),
  39. "help" => mc::_("Creates a border defined\n\tby x1,z1 to x2,z2\n\tUse [none] to remove\n\tIf [range] is specified the border is\n\t-range,-range to range,range\n\taround the spawn point"),
  40. "permission" => "wp.cmd.border"]);
  41. }
  42. public function onSCommand(CommandSender $c,Command $cc,$scmd,$world,array $args) {
  43. if ($scmd != "border") return false;
  44. if (count($args) == 0) {
  45. $limits = $this->owner->getCfg($world,"border",null);
  46. if ($limits == null) {
  47. $c->sendMessage(mc::_("[WP] %1% has no borders",$world));
  48. } else {
  49. list($x1,$z1,$x2,$z2) = $limits;
  50. $c->sendMessage(mc::_("[WP] Border for %1% is (%2%,%3%)-(%4%,%5%)",
  51. $world,$x1,$z1,$x2,$z2));
  52. }
  53. return true;
  54. }
  55. if (count($args) == 1) {
  56. $range = intval($args[0]);
  57. if ($range == 0) {
  58. $this->owner->unsetCfg($world,"border");
  59. $this->owner->getServer()->broadcastMessage(mc::_("[WP] Border for %1% removed",$world));
  60. return true;
  61. }
  62. if (!$this->owner->getServer()->isLevelLoaded($world)) {
  63. if (!$this->owner->getServer()->loadLevel($world)) {
  64. $c->sendMessage(mc::_("Error loading level %1%",$world));
  65. return true;
  66. }
  67. $unload = true;
  68. } else
  69. $unload = false;
  70. $l = $this->owner->getServer()->getLevelByName($world);
  71. if (!$l) {
  72. $c->sendMessage(mc::_("Unable to find level %1%",$world));
  73. return true;
  74. }
  75. $pos = $l->getSpawnLocation();
  76. if ($unload) $this->owner->getServer()->unloadLevel($l);
  77. $args = [ $pos->getX() - $range, $pos->getZ() - $range,
  78. $pos->getX() + $range, $pos->getZ() + $range ];
  79. }
  80. if (count($args) == 4) {
  81. list($x1,$z1,$x2,$z2) = $args;
  82. if (!is_numeric($x1) || !is_numeric($z1)
  83. || !is_numeric($x2) || !is_numeric($z2)) {
  84. $c->sendMessage(mc::_("[WP] Invalid border specification"));
  85. return false;
  86. }
  87. if ($x1 > $x2) list($x1,$x2) = [$x2,$x1];
  88. if ($z1 > $z2) list($z1,$z2) = [$z2,$z1];
  89. $this->owner->setCfg($world,"border",[$x1,$z1,$x2,$z2]);
  90. $this->owner->getServer()->broadcastMessage(mc::_("[WP] Border for %1% set to (%2%,%3%)-(%4%,%5%)", $world, $x1,$z1, $x2, $z2));
  91. return true;
  92. }
  93. return false;
  94. }
  95. private function checkMove($world,$x,$z) {
  96. if (!isset($this->wcfg[$world])) return true;
  97. list($x1,$z1,$x2,$z2) = $this->wcfg[$world];
  98. if ($x1 < $x && $x < $x2 && $z1 < $z && $z < $z2) return true;
  99. return false;
  100. }
  101. public function onPlayerMove(PlayerMoveEvent $ev) {
  102. if ($ev->isCancelled()) return;
  103. $pl = $ev->getPlayer();
  104. $pos = $ev->getTo();
  105. if ($this->checkMove($pl->getLevel()->getName(),
  106. $pos->getX(),$pos->getZ())) return;
  107. $this->owner->msg($pl,mc::_("You have reached the end of the world"));
  108. $ev->setCancelled();
  109. }
  110. public function onTeleport(EntityTeleportEvent $ev){
  111. if ($ev->isCancelled()) return;
  112. $pl = $ev->getEntity();
  113. if (!($pl instanceof Player)) return;
  114. $to = clone $ev->getTo();
  115. if (!$to) return;// This should never happen!
  116. if ($to->getLevel()) {
  117. $world = $to->getLevel()->getName();
  118. } else {
  119. $from = $ev->getFrom();
  120. if (!$from) return; // OK, this would be weird...
  121. if (!$from->getLevel()) return; // Can't determine the level at all!
  122. $world = $from->getLevel()->getName();
  123. }
  124. //echo __METHOD__.",".__LINE__."world=$world\n"; //##DEBUG
  125. if ($this->checkMove($world,$to->getX(),$to->getZ())) return;
  126. $this->owner->msg($pl,mc::_("You are teleporting outside the world"));
  127. $ev->setCancelled();
  128. }
  129. }