From 02d7680fb5c88e67425a8b9ecc1d3e3129ac4cf8 Mon Sep 17 00:00:00 2001 From: Muqsit Date: Wed, 31 May 2023 10:34:47 +0000 Subject: [PATCH] ReflectionProperty::setAccessible() is no-op in PHP 8.1 --- plugin.yml | 4 ++-- .../fakeplayer/FakePlayerCommandExecutor.php | 1 - src/muqsit/fakeplayer/Loader.php | 2 -- ...ngeMovementInternalFakePlayerBehaviour.php | 23 +++++-------------- ...ateMovementInternalFakePlayerBehaviour.php | 22 +++++------------- .../network/FakePlayerNetworkSession.php | 3 --- 6 files changed, 14 insertions(+), 41 deletions(-) diff --git a/plugin.yml b/plugin.yml index 2529dd4..fd0aea6 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,7 +1,7 @@ name: FakePlayer main: muqsit\fakeplayer\Loader -api: 4.18.4 -version: 0.2.3 +api: 4.21.1 +version: 0.2.4 permissions: fakeplayer.command.fakeplayer: diff --git a/src/muqsit/fakeplayer/FakePlayerCommandExecutor.php b/src/muqsit/fakeplayer/FakePlayerCommandExecutor.php index 4fc2afb..f7a64ca 100644 --- a/src/muqsit/fakeplayer/FakePlayerCommandExecutor.php +++ b/src/muqsit/fakeplayer/FakePlayerCommandExecutor.php @@ -77,7 +77,6 @@ public function onCommand(CommandSender $sender, Command $command, string $label case "form": if(isset($args[2]) && isset($args[3])){ $_formIdCounter = new ReflectionProperty(Player::class, "formIdCounter"); - $_formIdCounter->setAccessible(true); $form_id = $_formIdCounter->getValue($player) - 1; $data = null; diff --git a/src/muqsit/fakeplayer/Loader.php b/src/muqsit/fakeplayer/Loader.php index 020e182..e4c8f2c 100644 --- a/src/muqsit/fakeplayer/Loader.php +++ b/src/muqsit/fakeplayer/Loader.php @@ -158,11 +158,9 @@ public function addPlayer(FakePlayerInfo $info) : Promise{ $network->getSessionManager()->add($session); $rp = new ReflectionProperty(NetworkSession::class, "info"); - $rp->setAccessible(true); $rp->setValue($session, new XboxLivePlayerInfo($info->xuid, $info->username, $info->uuid, $info->skin, "en_US" /* TODO: Make locale configurable? */, array_merge($info->extra_data, $this->default_extra_data))); $rp = new ReflectionMethod(NetworkSession::class, "onServerLoginSuccess"); - $rp->setAccessible(true); $rp->invoke($session); $packet = ResourcePackClientResponsePacket::create(ResourcePackClientResponsePacket::STATUS_COMPLETED, []); diff --git a/src/muqsit/fakeplayer/behaviour/internal/TryChangeMovementInternalFakePlayerBehaviour.php b/src/muqsit/fakeplayer/behaviour/internal/TryChangeMovementInternalFakePlayerBehaviour.php index 5c87a0c..253ba1f 100644 --- a/src/muqsit/fakeplayer/behaviour/internal/TryChangeMovementInternalFakePlayerBehaviour.php +++ b/src/muqsit/fakeplayer/behaviour/internal/TryChangeMovementInternalFakePlayerBehaviour.php @@ -27,34 +27,23 @@ public static function init(Loader $plugin) : void{ } private static function changeDrag(Player $player) : void{ + /** @see Player::$drag */ static $_drag = null; - if($_drag === null){ - /** @see Player::$drag */ - $_drag = new ReflectionProperty(Entity::class, "drag"); - $_drag->setAccessible(true); - } - + $_drag ??= new ReflectionProperty(Entity::class, "drag"); $_drag->setValue($player, $_drag->getValue($player) * 8); } private static function writeMovementToPlayer(Player $player, Vector3 $motion) : void{ + /** @see Player::$motion */ static $_motion = null; - if($_motion === null){ - /** @see Player::$motion */ - $_motion = new ReflectionProperty(Entity::class, "motion"); - $_motion->setAccessible(true); - } - + $_motion ??= new ReflectionProperty(Entity::class, "motion"); $_motion->setValue($player, $motion->asVector3()); } private static function tryChangeMovement(Player $player) : void{ + /** @see Human::tryChangeMovement() */ static $reflection_method = null; - if($reflection_method === null){ - /** @see Human::tryChangeMovement() */ - $reflection_method = new ReflectionMethod(Human::class, "tryChangeMovement"); - $reflection_method->setAccessible(true); - } + $reflection_method ??= new ReflectionMethod(Human::class, "tryChangeMovement"); $reflection_method->getClosure($player)(); } diff --git a/src/muqsit/fakeplayer/behaviour/internal/UpdateMovementInternalFakePlayerBehaviour.php b/src/muqsit/fakeplayer/behaviour/internal/UpdateMovementInternalFakePlayerBehaviour.php index b8dab44..b00d750 100644 --- a/src/muqsit/fakeplayer/behaviour/internal/UpdateMovementInternalFakePlayerBehaviour.php +++ b/src/muqsit/fakeplayer/behaviour/internal/UpdateMovementInternalFakePlayerBehaviour.php @@ -22,33 +22,23 @@ public static function init(Loader $plugin) : void{ } private static function readMovementFromPlayer(Player $player) : Vector3{ + /** @see Human::$motion */ static $_motion = null; - if($_motion === null){ - /** @see Human::$motion */ - $_motion = new ReflectionProperty(Human::class, "motion"); - $_motion->setAccessible(true); - } - + $_motion ??= new ReflectionProperty(Human::class, "motion"); return $_motion->getValue($player)->asVector3(); } private static function movePlayer(Player $player, Vector3 $dv) : void{ + /** @see Human::move() */ static $reflection_method = null; - if($reflection_method === null){ - /** @see Human::move() */ - $reflection_method = new ReflectionMethod(Human::class, "move"); - $reflection_method->setAccessible(true); - } + $reflection_method ??= new ReflectionMethod(Human::class, "move"); $reflection_method->getClosure($player)($dv->x, $dv->y, $dv->z); } private static function setPlayerLocation(Player $player, Location $location) : void{ + /** @see Human::$location */ static $reflection_property = null; - if($reflection_property === null){ - /** @see Human::$location */ - $reflection_property = new ReflectionProperty(Human::class, "location"); - $reflection_property->setAccessible(true); - } + $reflection_property ??= new ReflectionProperty(Human::class, "location"); $reflection_property->setValue($player, $location); } diff --git a/src/muqsit/fakeplayer/network/FakePlayerNetworkSession.php b/src/muqsit/fakeplayer/network/FakePlayerNetworkSession.php index df07f0d..78c33e7 100644 --- a/src/muqsit/fakeplayer/network/FakePlayerNetworkSession.php +++ b/src/muqsit/fakeplayer/network/FakePlayerNetworkSession.php @@ -95,7 +95,6 @@ public function unregisterSpecificPacketListener(string $packet, FakePlayerPacke public function addToSendBuffer(string $buffer) : void{ parent::addToSendBuffer($buffer); $rp = new ReflectionProperty(NetworkSession::class, 'packetPool'); - $rp->setAccessible(true); $packetPool = $rp->getValue($this); $packet = $packetPool->getPacket($buffer); $packet->decode(PacketSerializer::decoder($buffer, 0, $this->getPacketSerializerContext())); @@ -107,7 +106,6 @@ public function addToSendBuffer(string $buffer) : void{ protected function createPlayer(): void{ $get_prop = function(string $name) : mixed{ $rp = new ReflectionProperty(NetworkSession::class, $name); - $rp->setAccessible(true); return $rp->getValue($this); }; @@ -125,7 +123,6 @@ protected function createPlayer(): void{ private function onPlayerCreated(Player $player) : void{ // call parent private method $rm = new ReflectionMethod(NetworkSession::class, "onPlayerCreated"); - $rm->setAccessible(true); $rm->invoke($this, $player); $this->player_add_resolver->resolve($player); }