Travel in MCPE faster and easier than you ever tought !
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.2 KiB

  1. <?php
  2. /*
  3. _____ _ _____ _
  4. | ___| __ _ ___ | |_ |_ _| _ __ __ _ __ __ ___ | |
  5. | |_ / _` | / __| | __| | | | '__| / _` | \ \ / / / _ \ | |
  6. | _| | (_| | \__ \ | |_ | | | | | (_| | \ V / | __/ | |
  7. |_| \__,_| |___/ \__| |_| |_| \__,_| \_/ \___| |_|
  8. Travel in MCPE faster and easier than you ever tought !
  9. @author Ad5001
  10. @link http://github.com/Ad5001/FastTravel
  11. */
  12. namespace Ad5001\FastTravel;
  13. use pocketmine\item\Item;
  14. use pocketmine\item\ItemBlock;
  15. use pocketmine\block\Block;
  16. use pocketmine\event\Listener;
  17. use pocketmine\plugin\PluginBase;
  18. use pocketmine\Server;
  19. use pocketmine\Player;
  20. use pocketmine\event\player\PlayerToggleSneakEvent;
  21. use pocketmine\event\player\PlayerMoveEvent;
  22. class Main extends PluginBase implements Listener {
  23. protected $elevator;
  24. protected $speed;
  25. public function onEnable(){
  26. $this->getServer()->getPluginManager()->registerEvents($this, $this);
  27. // Loading from config
  28. $this->elevator = Item::fromString($this->getConfig()->get("ElevatorBlock"));
  29. if(!($this->elevator instanceof ItemBlock)) {
  30. $this->getLogger()->warning("Invalid block provided as elevator.");
  31. $this->elevator = Item::get(139);
  32. }
  33. $this->speed = Item::fromString($this->getConfig()->get("SpeedBlock"));
  34. if(!($this->elevator instanceof ItemBlock)) {
  35. $this->getLogger()->warning("Invalid block provided as speed block.");
  36. $this->elevator = Item::get(79);
  37. }
  38. $this->getServer()->getScheduler()->scheduleRepeatingTask(new tasks\FastingTask($this), 5);
  39. }
  40. public function onLoad(){
  41. $this->saveDefaultConfig();
  42. }
  43. /*
  44. Tests when a player sneaks
  45. @param $event \pocketmine\event\player\PlayerToggleSneakEvent
  46. @return void
  47. */
  48. public function onPlayerSneak(PlayerToggleSneakEvent $event) {
  49. $b = $event->getPlayer()->getLevel()->getBlock(new \pocketmine\math\Vector3(round($player->x), round($player->y - 1), $player->z));
  50. if($b->getId() == $this->elevator->getId() && $b->getDamage() == $this->elevator->getDamage() && $event->isSneaking()) { // Checking if the player is sneaking on the block set as elevator.
  51. $bl = $this->getBlocksUnder($b);
  52. if(!is_null($bl) && !$this->hasFreeSpace($bl)) {
  53. while(!is_null($bl) && !$this->hasFreeSpace($bl)) {
  54. $bl = $this->getBlocksUnder($bl);
  55. }
  56. }
  57. if(!is_null($bl)) { // A elevator under exists
  58. $event->getPlayer()->teleport(new \pocketmine\math\Vector3($bl->x, $bl->y + 1, $bl->z));
  59. $event->getPlayer()->getLevel()->addSound(new \pocketmine\level\sound\EndermanTeleportSound($event->getPlayer()));
  60. }
  61. $event->setCancelled();
  62. }
  63. }
  64. /*
  65. Check when a player jumps
  66. @param $event \pocketmine\event\player\PlayerMoveEvent
  67. @return void
  68. */
  69. public function onPlayerMove(PlayerMoveEvent $event) {
  70. $b = $event->getPlayer()->getLevel()->getBlock(new \pocketmine\math\Vector3(round($player->x), round($player->y - 1), $player->z));
  71. if($b->getId() == $this->elevator->getId() && $b->getDamage() == $this->elevator->getDamage() && $event->getTo()->y > $event->getFrom()->y) { // Checking if the player is sneaking on the block set as elevator.
  72. $bl = $this->getBlocksAbove($b);
  73. if(!is_null($bl) && !$this->hasFreeSpace($bl)) {
  74. while(!is_null($bl) && !$this->hasFreeSpace($bl)) {
  75. $bl = $this->getBlocksAbove($bl);
  76. }
  77. }
  78. if(!is_null($bl)) { // A elevator above exists
  79. $event->setTo(new \pocketmine\math\Vector3($bl->x, $bl->y + 1, $bl->z));
  80. $event->getPlayer()->getLevel()->addSound(new \pocketmine\level\sound\EndermanTeleportSound($event->getPlayer()));
  81. }
  82. }
  83. }
  84. /*
  85. _ ____ ___
  86. / \ | _ \ |_ _|
  87. / _ \ | |_) | | |
  88. / ___ \ | __/ | |
  89. /_/ \_\ |_| |___|
  90. */
  91. /*
  92. Get blocks under the block provided that have the same id and damage.
  93. @param $block pocketmine\block\Block
  94. @return pocketmine\block\Block|null
  95. */
  96. public function getBlockUnder(\pocketmine\block\Block $b) {
  97. for($i = $b->y - 2/* TP atleast under two blocks of the current one*/; $i > 0; $i--) {
  98. $block = $b->getLevel()->getBlock(new \pocketmine\math\Vector3($b->x, $i, $b->z));
  99. if($block->geId() == $b->getId() && $block->getDamage() == $block->getDamage()) return $block;
  100. }
  101. return null;
  102. }
  103. /*
  104. Get the first block above the block provided that have the same id and damage.
  105. @param $block pocketmine\block\Block
  106. @return pocketmine\block\Block|null
  107. */
  108. public function getBlockAbove(\pocketmine\block\Block $b) {
  109. for($i = $b->y + 2/* TP atleast above two blocks of the current one*/; $i < 128; $i++) {
  110. $block = $b->getLevel()->getBlock(new \pocketmine\math\Vector3($b->x, $i, $b->z));
  111. if($block->geId() == $b->getId() && $block->getDamage() == $block->getDamage()) return $block;
  112. }
  113. return null;
  114. }
  115. /*
  116. Check if the space above a the provided block has transparent block so the player won't die of suffocation'
  117. @param $block pocketmine\block\Block
  118. @return bool
  119. */
  120. public function hasFreeSpace(\pocketmine\block\Block $b) : bool {
  121. $b1 = $b->getLevel()->getBlock(new \pocketmine\math\Vector3($b->x, $b->y + 1, $b->z));
  122. $b2 = $b->getLevel()->getBlock(new \pocketmine\math\Vector3($b->x, $b->y + 2, $b->z));
  123. return $b1->isTransparent() && $b2->isTransparent();
  124. }
  125. /*
  126. Return a clone of the elevator block
  127. @param
  128. @return \pocketmine\block\Block
  129. */
  130. public function getElevatorBlock() : \pocketmine\block\Block {
  131. return clone $this->elevator;
  132. }
  133. /*
  134. Return a clone of the speeding block
  135. @param
  136. @return \pocketmine\block\Block
  137. */
  138. public function getSpeed() : \pocketmine\block\Block {
  139. return clone $this->speed;
  140. }
  141. }