Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
add new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MakStashkevich committed Dec 21, 2019
1 parent dd210df commit ce4e270
Show file tree
Hide file tree
Showing 4 changed files with 401 additions and 13 deletions.
243 changes: 241 additions & 2 deletions client/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace client;

use client\entity\EntityHelpers;
use client\level\Level;
use pocketmine\entity\Attribute;
use pocketmine\math\Vector3;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
24 changes: 17 additions & 7 deletions client/PocketEditionClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit ce4e270

Please sign in to comment.