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.

Main.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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\entity\Entity;
  20. use pocketmine\Player;
  21. use pocketmine\event\player\PlayerToggleSneakEvent;
  22. use pocketmine\event\player\PlayerMoveEvent;
  23. class Main extends PluginBase implements Listener {
  24. protected $elevator;
  25. protected $boost;
  26. protected $jump;
  27. public $cancelUpAfterDown = [];
  28. public function onEnable(){
  29. $this->getServer()->getPluginManager()->registerEvents($this, $this);
  30. // Loading from config
  31. $this->elevator = Item::fromString($this->getConfig()->get("ElevatorBlock"));
  32. if(!($this->elevator instanceof ItemBlock)) {
  33. $this->getLogger()->warning("Invalid block provided as elevator.");
  34. $this->elevator = Item::get(139);
  35. }
  36. $this->boost = Item::fromString($this->getConfig()->get("BoostBlock"));
  37. if(!($this->elevator instanceof ItemBlock)) {
  38. $this->getLogger()->warning("Invalid block provided as boost block.");
  39. $this->elevator = Item::get(79);
  40. }
  41. $this->jump = Item::fromString($this->getConfig()->get("JumpBlock"));
  42. if(!($this->elevator instanceof ItemBlock)) {
  43. $this->getLogger()->warning("Invalid block provided as jump block.");
  44. $this->elevator = Item::get(170);
  45. }
  46. $this->getServer()->getScheduler()->scheduleRepeatingTask(new tasks\FastingTask($this), 5);
  47. }
  48. public function onLoad(){
  49. $this->saveDefaultConfig();
  50. }
  51. /*
  52. Tests when a player sneaks
  53. @param $event \pocketmine\event\player\PlayerToggleSneakEvent
  54. @return void
  55. */
  56. public function onPlayerSneak(PlayerToggleSneakEvent $event) {
  57. $b = $event->getPlayer()->getLevel()->getBlock(new \pocketmine\math\Vector3(round($event->getPlayer()->x), floor($event->getPlayer()->y - 1), round($event->getPlayer()->z)));
  58. 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.
  59. $bl = $this->getBlockUnder($b);
  60. if(!is_null($bl) && !$this->hasFreeSpace($bl)) {
  61. while(!is_null($bl) && !$this->hasFreeSpace($bl)) {
  62. $bl = $this->getBlockUnder($bl);
  63. }
  64. }
  65. if(!is_null($bl)) { // A elevator under exists
  66. $event->getPlayer()->teleport(new \pocketmine\math\Vector3($bl->x, $bl->y + 1.5, $bl->z));
  67. $this->setCooldown($event->getPlayer());
  68. $event->getPlayer()->getLevel()->addSound(new \pocketmine\level\sound\EndermanTeleportSound($event->getPlayer()));
  69. }
  70. }
  71. }
  72. /*
  73. Check when a player jumps
  74. @param $event \pocketmine\event\player\PlayerMoveEvent
  75. @return void
  76. */
  77. public function onPlayerMove(PlayerMoveEvent $event) {
  78. $b = $event->getPlayer()->getLevel()->getBlock(new \pocketmine\math\Vector3(round($event->getFrom()->x), floor($event->getFrom()->y - 1), round($event->getFrom()->z)));
  79. 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.
  80. // $this->getLogger()->debug($event->getFrom() . "/" . $event->getTo());
  81. $bl = $this->getBlockAbove($b);
  82. if(!is_null($bl) && !$this->hasFreeSpace($bl)) {
  83. while(!is_null($bl) && !$this->hasFreeSpace($bl)) {
  84. $bl = $this->getBlockAbove($bl);
  85. }
  86. }
  87. if(!is_null($bl) && !isset($this->cancelUpAfterDown[$event->getPlayer()->getName()])) { // A elevator above exists
  88. // $this->getLogger()->debug("Up" . $bl->y);
  89. $event->setTo(new \pocketmine\level\Location($bl->x, $bl->y + 1.5, $bl->z));
  90. $this->setCooldown($event->getPlayer());
  91. $event->getPlayer()->getLevel()->addSound(new \pocketmine\level\sound\EndermanTeleportSound($event->getPlayer()));
  92. }
  93. }
  94. }
  95. /*
  96. _ ____ ___
  97. / \ | _ \ |_ _|
  98. / _ \ | |_) | | |
  99. / ___ \ | __/ | |
  100. /_/ \_\ |_| |___|
  101. */
  102. /*
  103. Get blocks under the block provided that have the same id and damage.
  104. @param $block pocketmine\block\Block
  105. @return pocketmine\block\Block|null
  106. */
  107. public function getBlockUnder(\pocketmine\block\Block $b) {
  108. for($i = $b->y - 2/* TP atleast under two blocks of the current one*/; $i > 0; $i--) {
  109. $block = $b->getLevel()->getBlock(new \pocketmine\math\Vector3($b->x, $i, $b->z));
  110. if($block->getId() == $b->getId() && $block->getDamage() == $block->getDamage()) return $block;
  111. }
  112. return null;
  113. }
  114. /*
  115. Get the first block above the block provided that have the same id and damage.
  116. @param $block pocketmine\block\Block
  117. @return pocketmine\block\Block|null
  118. */
  119. public function getBlockAbove(\pocketmine\block\Block $b) {
  120. for($i = $b->y + 2/* TP atleast above two blocks of the current one*/; $i < 128; $i++) {
  121. $block = $b->getLevel()->getBlock(new \pocketmine\math\Vector3($b->x, $i, $b->z));
  122. if($block->getId() == $b->getId() && $block->getDamage() == $block->getDamage()) return $block;
  123. }
  124. return null;
  125. }
  126. /*
  127. Check if the space above a the provided block has transparent block so the player won't die of suffocation'
  128. @param $block pocketmine\block\Block
  129. @return bool
  130. */
  131. public function hasFreeSpace(\pocketmine\block\Block $b) : bool {
  132. $b1 = $b->getLevel()->getBlock(new \pocketmine\math\Vector3($b->x, $b->y + 1, $b->z));
  133. $b2 = $b->getLevel()->getBlock(new \pocketmine\math\Vector3($b->x, $b->y + 2, $b->z));
  134. return $b1->isTransparent() && $b2->isTransparent();
  135. }
  136. /*
  137. Return a clone of the elevator block
  138. @return \pocketmine\block\Block
  139. */
  140. public function getElevatorBlock() : \pocketmine\block\Block {
  141. return clone $this->elevator->getBlock();
  142. }
  143. /*
  144. Return a clone of the boosting block
  145. @return \pocketmine\block\Block
  146. */
  147. public function getBoostBlock() : \pocketmine\block\Block {
  148. return clone $this->boost->getBlock();
  149. }
  150. /*
  151. Return a clone of the jumpig block block
  152. @return \pocketmine\block\Block
  153. */
  154. public function getJumpBlock() : \pocketmine\block\Block {
  155. return clone $this->jump->getBlock();
  156. }
  157. /*
  158. Sets a cooldown of using the up & down blocks to a player
  159. @param $player Player
  160. @return void
  161. */
  162. public function setCooldown(Player $player) {
  163. $this->cancelUpAfterDown[$player->getName()] = true;
  164. $this->getServer()->getScheduler()->scheduleDelayedTask(new tasks\UpCooldownTask($this, $player), 20);
  165. }
  166. }