Last active
February 10, 2020 19:44
-
-
Save PJZ9n/6522b88c5e8586cdf62f24f16208f83c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| declare(strict_types=1); | |
| /** | |
| * @name DirectionTest | |
| * @version 1.0.0 | |
| * @main PJZ9n\DirectionTest\DirectionTest | |
| * @api 3.0.0 | |
| */ | |
| namespace PJZ9n\DirectionTest { | |
| use pocketmine\entity\Entity; | |
| use pocketmine\entity\Human; | |
| use pocketmine\event\entity\EntityDamageByEntityEvent; | |
| use pocketmine\event\entity\EntityDamageEvent; | |
| use pocketmine\event\Listener; | |
| use pocketmine\event\player\PlayerCommandPreprocessEvent; | |
| use pocketmine\event\player\PlayerMoveEvent; | |
| use pocketmine\math\Vector3; | |
| use pocketmine\nbt\tag\CompoundTag; | |
| use pocketmine\nbt\tag\ShortTag; | |
| use pocketmine\nbt\tag\StringTag; | |
| use pocketmine\Player; | |
| use pocketmine\plugin\PluginBase; | |
| use ReflectionClass; | |
| use ReflectionException; | |
| class DirectionTest extends PluginBase implements Listener | |
| { | |
| /** @var Entity */ | |
| private $entity; | |
| /** | |
| * @throws ReflectionException | |
| */ | |
| public function onEnable(): void | |
| { | |
| var_dump(Entity::registerEntity(MyHuman::class, true)); | |
| //for debug | |
| $class = new ReflectionClass(Entity::class); | |
| $property = $class->getProperty("knownEntities"); | |
| $property->setAccessible(true); | |
| var_dump(array_keys($property->getValue())); | |
| $this->getServer()->getPluginManager()->registerEvents($this, $this); | |
| } | |
| public function onPlayerCommandPreProcess(PlayerCommandPreprocessEvent $event): void | |
| { | |
| if ($event->getMessage() !== "/spawn") { | |
| return; | |
| } | |
| $level = $event->getPlayer()->getLevel(); | |
| $nbt = Entity::createBaseNBT($event->getPlayer(), null, 1, 1); | |
| $nbt->setTag(new ShortTag("Health", 20)); | |
| $nbt->setTag(new CompoundTag("Skin", [ | |
| new StringTag("Data", $event->getPlayer()->getSkin()->getSkinData()), | |
| new StringTag("Name", $event->getPlayer()->getSkin()->getSkinId()), | |
| ])); | |
| $this->entity = Entity::createEntity((new ReflectionClass(MyHuman::class))->getShortName(), $level, $nbt); | |
| $this->entity->setImmobile(true); | |
| $this->entity->spawnToAll(); | |
| $event->getPlayer()->sendMessage("スポーンしました!"); | |
| } | |
| public function onPlayerMove(PlayerMoveEvent $event): void | |
| { | |
| if (!$this->entity instanceof Entity) { | |
| return; | |
| } | |
| $player = $event->getPlayer(); | |
| $data = Utils::getDirection($this->entity, $player); | |
| $this->entity->pitch = $data["pitch"]; | |
| $this->entity->yaw = $data["yaw"]; | |
| } | |
| } | |
| class MyHuman extends Human | |
| { | |
| public function attack(EntityDamageEvent $source): void | |
| { | |
| //No Damage | |
| $source->call(); | |
| if ($source->isCancelled()) { | |
| return; | |
| } | |
| if (!$source instanceof EntityDamageByEntityEvent) { | |
| return; | |
| } | |
| $damager = $source->getDamager(); | |
| if (!$damager instanceof Player) { | |
| return; | |
| } | |
| $damager->sendMessage("Hello! by MyHuman"); | |
| } | |
| } | |
| class Utils | |
| { | |
| private function __construct() | |
| { | |
| //Utils Class | |
| } | |
| public static function getDirection(Vector3 $pos1, Vector3 $pos2): array | |
| { | |
| $diff = $pos2->subtract($pos1)->normalize(); | |
| $pitch = rad2deg(asin(-$diff->y)); | |
| //$yaw = rad2deg(acos($diff->z / cos($pitch))); | |
| //Thanks deceitya(さん)! | |
| $yaw = rad2deg(atan2($diff->z, $diff->x)) - 90; | |
| return [ | |
| "pitch" => $pitch, | |
| "yaw" => $yaw, | |
| ]; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment