From ce4e2700f8fd992231c3a5eee5ca38074d976e74 Mon Sep 17 00:00:00 2001 From: MakStashkevich Date: Sat, 21 Dec 2019 19:55:20 +0300 Subject: [PATCH] add new functions --- client/Bot.php | 243 +++++++++++++++++++++++++++++++- client/PocketEditionClient.php | 24 +++- client/entity/EntityHelpers.php | 143 +++++++++++++++++++ console/Console.php | 4 - 4 files changed, 401 insertions(+), 13 deletions(-) create mode 100644 client/entity/EntityHelpers.php diff --git a/client/Bot.php b/client/Bot.php index 49fbca5..e28f360 100644 --- a/client/Bot.php +++ b/client/Bot.php @@ -2,6 +2,7 @@ namespace client; +use client\entity\EntityHelpers; use client\level\Level; use pocketmine\entity\Attribute; use pocketmine\math\Vector3; @@ -85,6 +86,15 @@ class Bot public $flags = 0; public $userPermission = 0; + /** @var array */ + public $metadata = []; + + /** @var int */ + public $gamemode = 0; + + /** @var array */ + public $playersOnline = []; + /** * Bot constructor. * @param string $username @@ -101,6 +111,46 @@ function __construct(string $username = null, string $password = null, Address $ $this->level = new Level(); } + /** + * @param array $players + */ + function addPlayersOnline(array $players = []) + { + array_merge($this->playersOnline, $players); + } + + /** + * @param array $players + */ + function removePlayersOnline(array $players = []) + { + //todo + } + + /** + * @return array + */ + function getPlayersOnline(): array + { + return $this->playersOnline; + } + + /** + * @param int $mode + */ + function setGamemode(int $mode) + { + $this->gamemode = $mode; + } + + /** + * @return int + */ + function getGamemode(): int + { + return $this->gamemode; + } + /** * @param float $x * @param float $y @@ -517,10 +567,199 @@ function setUserPermission(int $permission = 0) } /** - * @return bool + * @return int */ - function getUserPermission(): bool + function getUserPermission(): int { return $this->userPermission; } + + /** + * @return bool + */ + function isOp(): bool + { + return $this->getUserPermission() === 1; + } + + /** + * @param array $metadata + */ + function addMetadata(array $metadata = []) + { + if ($this->metadata === []) $this->setMetadata($metadata); + else $this->metadata = array_merge($this->metadata, $metadata); + } + + /** + * @param int $id + */ + function removeMetadata(int $id) + { + unset($this->metadata[$id]); + } + + /** + * @param array $metadata + */ + function setMetadata(array $metadata = []) + { + $this->metadata = $metadata; + } + + /** + * @return array + */ + function getMetadata(): array + { + return $this->metadata; + } + + /** + * @param int $id + * @return bool + */ + function hasMetadata(int $id): bool + { + return isset($this->metadata[$id]); + } + + /** + * @return array + */ + function getMetadataFlags(): array + { + return $this->metadata[EntityHelpers::DATA_FLAGS] ?? []; + } + + /** + * @param $id + * @return mixed|null + */ + function getDataProperty(int $id) + { + $data = $this->getMetadataFlags(); + return isset($data[$id]) ? $data[$id][1] : null; + } + + /** + * @param $id + * @return mixed|null + */ + function getDataPropertyType(int $id) + { + $data = $this->getMetadataFlags(); + return isset($data[$id]) ? $data[$id][0] : null; + } + + /** + * @param $propertyId + * @param $id + * @return bool + */ + function getDataFlag(int $propertyId, int $id): bool + { + return (((int)$this->getDataProperty($propertyId)) & (1 << $id)) > 0; + } + + /** + * @return bool + */ + function isImmobile(): bool + { + return $this->getDataFlag(EntityHelpers::DATA_FLAGS, EntityHelpers::DATA_FLAG_IMMOBILE); + } + + /** + * @return bool + */ + function isGliding(): bool + { + return $this->getDataFlag(EntityHelpers::DATA_FLAGS, EntityHelpers::DATA_FLAG_GLIDING); + } + + /** + * @return bool + */ + function isSprinting(): bool + { + return $this->getDataFlag(EntityHelpers::DATA_FLAGS, EntityHelpers::DATA_FLAG_SPRINTING); + } + + /** + * @return bool + */ + function isSneaking(): bool + { + return $this->getDataFlag(EntityHelpers::DATA_FLAGS, EntityHelpers::DATA_FLAG_SNEAKING); + } + + /** + * @return float + */ + function getScale(): float + { + return (float)$this->getDataProperty(EntityHelpers::DATA_SCALE); + } + + /** + * @return string + */ + function getNameTag(): string + { + return $this->getDataProperty(EntityHelpers::DATA_NAMETAG); + } + + /** + * @return bool + */ + function isNameTagVisible(): bool + { + return $this->getDataFlag(EntityHelpers::DATA_FLAGS, EntityHelpers::DATA_FLAG_CAN_SHOW_NAMETAG); + } + + /** + * @return bool + */ + function isNameTagAlwaysVisible(): bool + { + return $this->getDataFlag(EntityHelpers::DATA_FLAGS, EntityHelpers::DATA_FLAG_ALWAYS_SHOW_NAMETAG); + } + + /** + * Returns whether the entity is able to climb blocks such as ladders or vines. + * @return bool + */ + function canClimb(): bool + { + return $this->getDataFlag(EntityHelpers::DATA_FLAGS, EntityHelpers::DATA_FLAG_CAN_CLIMB); + } + + /** + * Returns whether this entity is climbing a block. By default this is only true if the entity is climbing a ladder or vine or similar block. + * + * @return bool + */ + function canClimbWalls(): bool + { + return $this->getDataFlag(EntityHelpers::DATA_FLAGS, EntityHelpers::DATA_FLAG_WALLCLIMBING); + } + + /** + * Returns the entity ID of the owning entity, or null if the entity doesn't have an owner. + * @return int|string|null + */ + function getOwningEntityId() + { + return $this->getDataProperty(EntityHelpers::DATA_OWNER_EID); + } + + /** + * Returns the entity ID of the entity's target, or null if it doesn't have a target. + * @return int|string|null + */ + function getTargetEntityId() + { + return $this->getDataProperty(EntityHelpers::DATA_TARGET_EID); + } } \ No newline at end of file diff --git a/client/PocketEditionClient.php b/client/PocketEditionClient.php index e5f0c6a..e521a87 100644 --- a/client/PocketEditionClient.php +++ b/client/PocketEditionClient.php @@ -787,7 +787,11 @@ protected function handleDataPacket(DataPacket $packet): void $pk = new RequestChunkRadiusPacket(); $pk->radius = 8; $this->sendDataPacket($pk); - $this->player->setLocation($packet->x, $packet->y, $packet->z, $packet->yaw, $packet->pitch); + + $player = $this->getPlayer(); + $player->setId($packet->entityRuntimeId); + $player->setGamemode($packet->playerGamemode); + $player->setLocation($packet->x, $packet->y, $packet->z, $packet->yaw, $packet->pitch); } elseif ($packet instanceof UpdateAttributesPacket) { $player = $this->player; if ($player->getId() === $packet->entityRuntimeId) { @@ -812,12 +816,18 @@ protected function handleDataPacket(DataPacket $packet): void $player->setFlags((int)$packet->flags); $player->setUserPermission((int)$packet->userPermission); return; + } elseif ($packet instanceof SetEntityDataPacket) { + $id = $packet->entityRuntimeId; + $player = $this->getPlayer(); + if ($id === $player->getId()) { + $player->addMetadata((array)$packet->metadata); + } + return; } elseif ($packet instanceof PlayerListPacket) { - foreach ($packet->entries as $id => $e) { - if (isset($e[2]) && $e[2] === $this->player->getName()) { - $this->player->setId((int)$e[1]); - return; - } + if ($packet->type === PlayerListPacket::TYPE_ADD) { + $this->getPlayer()->addPlayersOnline($packet->entries); + } else { + $this->getPlayer()->removePlayersOnline($packet->entries); } return; } elseif ($packet instanceof FullChunkDataPacket) { @@ -873,7 +883,7 @@ protected function handleDataPacket(DataPacket $packet): void PlayerListPacket::NETWORK_ID, SetEntityDataPacket::NETWORK_ID, AddPlayerPacket::NETWORK_ID, RemoveEntityPacket::NETWORK_ID, MovePlayerPacket::NETWORK_ID, MoveEntityPacket::NETWORK_ID, LevelSoundEventPacket::NETWORK_ID, PlayerActionPacket::NETWORK_ID, InventoryActionPacket::NETWORK_ID, - EntityEventPacket::NETWORK_ID, SetEntityDataPacket::NETWORK_ID, AnimatePacket::NETWORK_ID, + EntityEventPacket::NETWORK_ID, AnimatePacket::NETWORK_ID, SetEntityMotionPacket::NETWORK_ID, LevelEventPacket::NETWORK_ID, UpdateBlockPacket::NETWORK_ID, MobArmorEquipmentPacket::NETWORK_ID, AddItemEntityPacket::NETWORK_ID, BlockEventPacket::NETWORK_ID, SetEntityLinkPacket::NETWORK_ID, MobEquipmentPacket::NETWORK_ID, AddEntityPacket::NETWORK_ID, diff --git a/client/entity/EntityHelpers.php b/client/entity/EntityHelpers.php new file mode 100644 index 0000000..cdd6869 --- /dev/null +++ b/client/entity/EntityHelpers.php @@ -0,0 +1,143 @@ +getPlayer()->getPosition()->add(0, (float)$args[1])); break; - case 'test': - var_dump($this->client->getPocketClient()->getPlayer()->getLevel()->getChests()); - break; - default: if ($this->chat) $this->client->chat($command); break;