|
| 1 | +<?php |
| 2 | +namespace aliuly\trampoline; |
| 3 | + |
| 4 | +use pocketmine\plugin\PluginBase; |
| 5 | +use pocketmine\event\Listener; |
| 6 | +use pocketmine\event\player\PlayerMoveEvent; |
| 7 | +use pocketmine\utils\Config; |
| 8 | +use pocketmine\block\Block; |
| 9 | +use pocketmine\math\Vector3; |
| 10 | + |
| 11 | +use pocketmine\Player; |
| 12 | + |
| 13 | + |
| 14 | +use pocketmine\Server; |
| 15 | +use pocketmine\utils\TextFormat; |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +use pocketmine\item\Item; |
| 20 | +use pocketmine\network\protocol\SetHealthPacket; |
| 21 | + |
| 22 | +use pocketmine\event\entity\EntityDamageEvent; |
| 23 | +use pocketmine\scheduler\CallbackTask; |
| 24 | + |
| 25 | + |
| 26 | +class Main extends PluginBase implements Listener { |
| 27 | + protected $blocks; |
| 28 | + |
| 29 | + public function onEnable(){ |
| 30 | + $defaults = [ |
| 31 | + "blocks" => [ Block::SPONGE ], |
| 32 | + ]; |
| 33 | + if (!is_dir($this->getDataFolder())) mkdir($this->getDataFolder()); |
| 34 | + $cfg=(new Config($this->getDataFolder()."config.yml", |
| 35 | + Config::YAML,$defaults))->getAll(); |
| 36 | + $this->blocks = []; |
| 37 | + if (isset($cfg["blocks"]) && is_array($cfg["blocks"])) { |
| 38 | + foreach ($cfg["blocks"] as $id) { |
| 39 | + if ($id == Block::AIR) continue; |
| 40 | + $this->blocks[$id] = $id; |
| 41 | + } |
| 42 | + } |
| 43 | + if (count($this->blocks)) { |
| 44 | + $this->getServer()->getPluginManager()->registerEvents($this, $this); |
| 45 | + $this->getLogger()->info(TextFormat::GREEN."Trampoline block ids:". |
| 46 | + count($this->blocks)); |
| 47 | + } else { |
| 48 | + $this->getLogger()->info(TextFormat::RED."No blocks configured"); |
| 49 | + |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public function onFall(EntityDamageEvent $ev) { |
| 54 | + $cause = $ev->getCause(); |
| 55 | + if ($cause !== EntityDamageEvent::CAUSE_FALL) return; |
| 56 | + $et = $ev->getEntity(); |
| 57 | + $id = $et->getLevel()->getBlockIdAt($et->getX(),$et->getY()-1,$et->getZ()); |
| 58 | + if (isset($this->blocks[$id])) { |
| 59 | + // Soft landing! |
| 60 | + $ev->setCancelled(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public function onMove(PlayerMoveEvent $ev) { |
| 65 | + $from = $ev->getFrom(); |
| 66 | + $to = $ev->getTo(); |
| 67 | + $dir = ["dx"=>$to->getX()-$from->getX(), |
| 68 | + "dy"=>$to->getY()-$from->getY(), |
| 69 | + "dz"=>$to->getZ()-$from->getZ()]; |
| 70 | + if (!$dir["dy"]) return; |
| 71 | + $id = $to->getLevel()->getBlockIdAt($to->getX(),$to->getY()-1,$to->getZ()); |
| 72 | + if (isset($this->blocks[$id])) { |
| 73 | + $ev->getPlayer()->setMotion(new Vector3($dir["dx"],-$dir["dy"]*1.1,$dir["dz"])); |
| 74 | + |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments