Skip to content

Commit

Permalink
ReflectionProperty::setAccessible() is no-op in PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Muqsit committed May 31, 2023
1 parent a95d4d1 commit 02d7680
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 41 deletions.
4 changes: 2 additions & 2 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
1 change: 0 additions & 1 deletion src/muqsit/fakeplayer/FakePlayerCommandExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions src/muqsit/fakeplayer/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 0 additions & 3 deletions src/muqsit/fakeplayer/network/FakePlayerNetworkSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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);
};

Expand All @@ -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);
}
Expand Down

0 comments on commit 02d7680

Please sign in to comment.