Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement API for new camera features #5832

Draft
wants to merge 7 commits into
base: minor-next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/data/bedrock/CameraEaseTypeIdMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\data\bedrock;

use pocketmine\player\camera\CameraEaseType;
use pocketmine\utils\SingletonTrait;
use function array_key_exists;

final class CameraEaseTypeIdMap{
use SingletonTrait;

/**
* @var CameraEaseType[]
* @phpstan-var array<string, CameraEaseType>
*/
private array $idToEnum = [];
/**
* @var string[]
* @phpstan-var array<int, string>
*/
private array $enumToId = [];

public function __construct(){
$this->register("linear", CameraEaseType::LINEAR());
$this->register("spring", CameraEaseType::SPRING());
$this->register("in_quad", CameraEaseType::IN_QUAD());
$this->register("out_quad", CameraEaseType::OUT_QUAD());
$this->register("in_out_quad", CameraEaseType::IN_OUT_QUAD());
$this->register("in_cubic", CameraEaseType::IN_CUBIC());
$this->register("out_cubic", CameraEaseType::OUT_CUBIC());
$this->register("in_out_cubic", CameraEaseType::IN_OUT_CUBIC());
$this->register("in_quart", CameraEaseType::IN_QUART());
$this->register("out_quart", CameraEaseType::OUT_QUART());
$this->register("in_out_quart", CameraEaseType::IN_OUT_QUART());
$this->register("in_quint", CameraEaseType::IN_QUINT());
$this->register("out_quint", CameraEaseType::OUT_QUINT());
$this->register("in_out_quint", CameraEaseType::IN_OUT_QUINT());
$this->register("in_sine", CameraEaseType::IN_SINE());
$this->register("out_sine", CameraEaseType::OUT_SINE());
$this->register("in_out_sine", CameraEaseType::IN_OUT_SINE());
$this->register("in_expo", CameraEaseType::IN_EXPO());
$this->register("out_expo", CameraEaseType::OUT_EXPO());
$this->register("in_out_expo", CameraEaseType::IN_OUT_EXPO());
$this->register("in_circ", CameraEaseType::IN_CIRC());
$this->register("out_circ", CameraEaseType::OUT_CIRC());
$this->register("in_out_circ", CameraEaseType::IN_OUT_CIRC());
$this->register("in_bounce", CameraEaseType::IN_BOUNCE());
$this->register("out_bounce", CameraEaseType::OUT_BOUNCE());
$this->register("in_out_bounce", CameraEaseType::IN_OUT_BOUNCE());
$this->register("in_back", CameraEaseType::IN_BACK());
$this->register("out_back", CameraEaseType::OUT_BACK());
$this->register("in_out_back", CameraEaseType::IN_OUT_BACK());
$this->register("in_elastic", CameraEaseType::IN_ELASTIC());
$this->register("out_elastic", CameraEaseType::OUT_ELASTIC());
$this->register("in_out_elastic", CameraEaseType::IN_OUT_ELASTIC());
}

public function register(string $stringId, CameraEaseType $type) : void{
$this->idToEnum[$stringId] = $type;
$this->enumToId[$type->id()] = $stringId;
}

public function fromId(string $id) : ?CameraEaseType{
return $this->idToEnum[$id] ?? null;
}

public function toId(CameraEaseType $type) : string{
if(!array_key_exists($type->id(), $this->enumToId)){
throw new \InvalidArgumentException("Missing mapping for camere ease type " . $type->name());
}
return $this->enumToId[$type->id()];
}
}
26 changes: 26 additions & 0 deletions src/network/mcpe/NetworkSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use pocketmine\lang\Translatable;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\network\mcpe\cache\ChunkCache;
use pocketmine\network\mcpe\compression\CompressBatchPromise;
Expand All @@ -52,6 +53,8 @@
use pocketmine\network\mcpe\handler\SessionStartPacketHandler;
use pocketmine\network\mcpe\handler\SpawnResponsePacketHandler;
use pocketmine\network\mcpe\protocol\AvailableCommandsPacket;
use pocketmine\network\mcpe\protocol\CameraInstructionPacket;
use pocketmine\network\mcpe\protocol\CameraPresetsPacket;
use pocketmine\network\mcpe\protocol\ChunkRadiusUpdatedPacket;
use pocketmine\network\mcpe\protocol\ClientboundPacket;
use pocketmine\network\mcpe\protocol\DisconnectPacket;
Expand Down Expand Up @@ -81,6 +84,7 @@
use pocketmine\network\mcpe\protocol\types\AbilitiesData;
use pocketmine\network\mcpe\protocol\types\AbilitiesLayer;
use pocketmine\network\mcpe\protocol\types\BlockPosition;
use pocketmine\network\mcpe\protocol\types\CacheableNbt;
use pocketmine\network\mcpe\protocol\types\command\CommandData;
use pocketmine\network\mcpe\protocol\types\command\CommandEnum;
use pocketmine\network\mcpe\protocol\types\command\CommandParameter;
Expand All @@ -94,6 +98,8 @@
use pocketmine\network\PacketHandlingException;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use pocketmine\player\camera\CameraPresetFactory;
use pocketmine\player\camera\instruction\CameraInstruction;
use pocketmine\player\GameMode;
use pocketmine\player\Player;
use pocketmine\player\PlayerInfo;
Expand Down Expand Up @@ -959,6 +965,17 @@ public function syncAdventureSettings() : void{
));
}

public function sendCameraPresets() : void {
$presetsTag = new ListTag();
foreach (CameraPresetFactory::getInstance()->getAll() as $preset) {
$presetsTag->push($preset->toCompoundTag());
}

$this->sendDataPacket(CameraPresetsPacket::create(
new CacheableNbt(CompoundTag::create()->setTag("presets", $presetsTag))
));
}

public function syncAvailableCommands() : void{
$commandData = [];
foreach($this->server->getCommandMap()->getCommands() as $name => $command){
Expand Down Expand Up @@ -1152,6 +1169,15 @@ public function onToastNotification(string $title, string $body) : void{
$this->sendDataPacket(ToastRequestPacket::create($title, $body));
}

public function onCameraInstruction(CameraInstruction ...$instructions) : void{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sendCameraInstructions() would more fit according to the parameter.

$instructionsData = CompoundTag::create();
foreach ($instructions as $instruction) {
$instruction->writeInstructionData($instructionsData);
}

$this->sendDataPacket(CameraInstructionPacket::create(new CacheableNbt($instructionsData)));
}

public function onOpenSignEditor(Vector3 $signPosition, bool $frontSide) : void{
$this->sendDataPacket(OpenSignPacket::create(BlockPosition::fromVector3($signPosition), $frontSide));
}
Expand Down
5 changes: 4 additions & 1 deletion src/network/mcpe/handler/PreSpawnPacketHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function setUp() : void{
$levelSettings->gameRules = [
"naturalregeneration" => new BoolGameRule(false, false) //Hack for client side regeneration
];
$levelSettings->experiments = new Experiments([], false);
$levelSettings->experiments = new Experiments(["cameras" => true], true);

$this->session->sendDataPacket(StartGamePacket::create(
$this->player->getId(),
Expand Down Expand Up @@ -129,6 +129,9 @@ public function setUp() : void{
$this->session->syncAbilities($this->player);
$this->session->syncAdventureSettings();

$this->session->getLogger()->debug("Sending camera presets");
$this->session->sendCameraPresets();

$this->session->getLogger()->debug("Sending effects");
foreach($this->player->getEffects()->all() as $effect){
$this->session->getEntityEventBroadcaster()->onEntityEffectAdded([$this->session], $this->player, $effect, false);
Expand Down
2 changes: 1 addition & 1 deletion src/network/mcpe/handler/ResourcePacksPacketHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function handleResourcePackClientResponse(ResourcePackClientResponsePacke
//we don't force here, because it doesn't have user-facing effects
//but it does have an annoying side-effect when true: it makes
//the client remove its own non-server-supplied resource packs.
$this->session->sendDataPacket(ResourcePackStackPacket::create($stack, [], false, ProtocolInfo::MINECRAFT_VERSION_NETWORK, new Experiments([], false)));
$this->session->sendDataPacket(ResourcePackStackPacket::create($stack, [], false, ProtocolInfo::MINECRAFT_VERSION_NETWORK, new Experiments(["cameras" => true], true)));
$this->session->getLogger()->debug("Applying resource pack stack");
break;
case ResourcePackClientResponsePacket::STATUS_COMPLETED:
Expand Down
5 changes: 5 additions & 0 deletions src/player/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
use pocketmine\permission\DefaultPermissions;
use pocketmine\permission\PermissibleBase;
use pocketmine\permission\PermissibleDelegateTrait;
use pocketmine\player\camera\instruction\CameraInstruction;
use pocketmine\player\chat\StandardChatFormatter;
use pocketmine\Server;
use pocketmine\timings\Timings;
Expand Down Expand Up @@ -2095,6 +2096,10 @@ public function sendToastNotification(string $title, string $body) : void{
$this->getNetworkSession()->onToastNotification($title, $body);
}

public function sendCameraInstruction(CameraInstruction ...$instructions) : void{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sendCameraInstructions() would more fit according to the parameter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it this way because most of the time only a single instruction is sent

$this->getNetworkSession()->onCameraInstruction(...$instructions);
}

/**
* Sends a Form to the player, or queue to send it if a form is already open.
*
Expand Down
106 changes: 106 additions & 0 deletions src/player/camera/CameraEaseType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\player\camera;

use pocketmine\utils\EnumTrait;

/**
* This doc-block is generated automatically, do not modify it manually.
* This must be regenerated whenever registry members are added, removed or changed.
* @see build/generate-registry-annotations.php
* @generate-registry-docblock
*
* @method static CameraEaseType LINEAR()
* @method static CameraEaseType SPRING()
* @method static CameraEaseType IN_QUAD()
* @method static CameraEaseType OUT_QUAD()
* @method static CameraEaseType IN_OUT_QUAD()
* @method static CameraEaseType IN_CUBIC()
* @method static CameraEaseType OUT_CUBIC()
* @method static CameraEaseType IN_OUT_CUBIC()
* @method static CameraEaseType IN_QUART()
* @method static CameraEaseType OUT_QUART()
* @method static CameraEaseType IN_OUT_QUART()
* @method static CameraEaseType IN_QUINT()
* @method static CameraEaseType OUT_QUINT()
* @method static CameraEaseType IN_OUT_QUINT()
* @method static CameraEaseType IN_SINE()
* @method static CameraEaseType OUT_SINE()
* @method static CameraEaseType IN_OUT_SINE()
* @method static CameraEaseType IN_EXPO()
* @method static CameraEaseType OUT_EXPO()
* @method static CameraEaseType IN_OUT_EXPO()
* @method static CameraEaseType IN_CIRC()
* @method static CameraEaseType OUT_CIRC()
* @method static CameraEaseType IN_OUT_CIRC()
* @method static CameraEaseType IN_BOUNCE()
* @method static CameraEaseType OUT_BOUNCE()
* @method static CameraEaseType IN_OUT_BOUNCE()
* @method static CameraEaseType IN_BACK()
* @method static CameraEaseType OUT_BACK()
* @method static CameraEaseType IN_OUT_BACK()
* @method static CameraEaseType IN_ELASTIC()
* @method static CameraEaseType OUT_ELASTIC()
* @method static CameraEaseType IN_OUT_ELASTIC()
*/
final class CameraEaseType{
use EnumTrait;

protected static function setup() : void{
self::registerAll(
new self("linear"),
new self("spring"),
new self("in_quad"),
new self("out_quad"),
new self("in_out_quad"),
new self("in_cubic"),
new self("out_cubic"),
new self("in_out_cubic"),
new self("in_quart"),
new self("out_quart"),
new self("in_out_quart"),
new self("in_quint"),
new self("out_quint"),
new self("in_out_quint"),
new self("in_sine"),
new self("out_sine"),
new self("in_out_sine"),
new self("in_expo"),
new self("out_expo"),
new self("in_out_expo"),
new self("in_circ"),
new self("out_circ"),
new self("in_out_circ"),
new self("in_bounce"),
new self("out_bounce"),
new self("in_out_bounce"),
new self("in_back"),
new self("out_back"),
new self("in_out_back"),
new self("in_elastic"),
new self("out_elastic"),
new self("in_out_elastic")
);
}
}
80 changes: 80 additions & 0 deletions src/player/camera/CameraPreset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\player\camera;

use pocketmine\nbt\tag\CompoundTag;
use pocketmine\player\camera\element\CameraState;

final class CameraPreset{

private const TAG_IDENTIFIER = "identifier"; //TAG_String
private const TAG_INHERIT_FROM = "inherit_from"; //TAG_String

private const TAG_POSITION_X = "pos_x"; //TAG_Float
private const TAG_POSITION_Y = "pos_y"; //TAG_Float
private const TAG_POSITION_Z = "pos_z"; //TAG_Float

private const TAG_ROTATION_YAW = "rot_x"; //TAG_Float
private const TAG_ROTATION_PITCH = "rot_y"; //TAG_Float

public function __construct(
private string $identifier,
private string $inheritFrom = "",
private ?CameraState $state = null
){
}

public function getIdentifier() : string{
return $this->identifier;
}

public function getInheritFrom() : string{
return $this->inheritFrom;
}

public function getCameraState() : ?CameraState{
return $this->state;
}

public function toCompoundTag() : CompoundTag{
$tag = CompoundTag::create()
->setString(self::TAG_IDENTIFIER, $this->identifier)
->setString(self::TAG_INHERIT_FROM, $this->inheritFrom);

if ($this->state !== null) {
if (($position = $this->state->getPosition()) !== null) {
$tag->setFloat(self::TAG_POSITION_X, $position->x)
->setFloat(self::TAG_POSITION_Y, $position->y)
->setFloat(self::TAG_POSITION_Z, $position->z);
}
if (($yaw = $this->state->getYaw()) !== null && ($pitch = $this->state->getPitch()) !== null) {
$tag->setFloat(self::TAG_ROTATION_YAW, $yaw)
->setFloat(self::TAG_ROTATION_PITCH, $pitch);
}
}

return $tag;
}

}
Loading