Merge branch 'stable'

# Conflicts:
#	resources/vanilla
#	src/command/defaults/TeleportCommand.php
#	src/pocketmine/event/entity/EntityDespawnEvent.php
#	src/pocketmine/event/entity/EntityInventoryChangeEvent.php
#	src/pocketmine/event/entity/EntityLevelChangeEvent.php
#	src/pocketmine/event/entity/EntitySpawnEvent.php
#	tests/phpstan/configs/actual-problems.neon
This commit is contained in:
Dylan K. Taylor 2020-06-21 00:30:45 +01:00
commit 1f90aa07aa
24 changed files with 116 additions and 106 deletions

View File

@ -26,12 +26,12 @@ namespace pocketmine\command\defaults;
use pocketmine\command\Command; use pocketmine\command\Command;
use pocketmine\command\CommandSender; use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\entity\Location;
use pocketmine\lang\TranslationContainer; use pocketmine\lang\TranslationContainer;
use pocketmine\math\Vector3;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\TextFormat; use pocketmine\utils\TextFormat;
use function array_filter; use function array_shift;
use function array_values;
use function count; use function count;
use function round; use function round;
@ -47,84 +47,83 @@ class TeleportCommand extends VanillaCommand{
$this->setPermission("pocketmine.command.teleport"); $this->setPermission("pocketmine.command.teleport");
} }
private function findPlayer(CommandSender $sender, string $playerName) : ?Player{
$subject = $sender->getServer()->getPlayer($playerName);
if($subject === null){
$sender->sendMessage(TextFormat::RED . "Can't find player " . $playerName);
return null;
}
return $subject;
}
public function execute(CommandSender $sender, string $commandLabel, array $args){ public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){ if(!$this->testPermission($sender)){
return true; return true;
} }
$args = array_values(array_filter($args, function(string $arg) : bool{ switch(count($args)){
return $arg !== ""; case 1: // /tp targetPlayer
})); case 3: // /tp x y z
if(count($args) < 1 or count($args) > 6){ case 5: // /tp x y z yaw pitch - TODO: 5 args could be target x y z yaw :(
throw new InvalidCommandSyntaxException(); if(!($sender instanceof Player)){
} $sender->sendMessage(TextFormat::RED . "Please provide a player!");
$target = null;
$origin = $sender;
if(count($args) === 1 or count($args) === 3){
if($sender instanceof Player){
$target = $sender;
}else{
$sender->sendMessage(TextFormat::RED . "Please provide a player!");
return true;
}
if(count($args) === 1){
$target = $sender->getServer()->getPlayer($args[0]);
if($target === null){
$sender->sendMessage(TextFormat::RED . "Can't find player " . $args[0]);
return true; return true;
} }
}
}else{
$target = $sender->getServer()->getPlayer($args[0]);
if($target === null){
$sender->sendMessage(TextFormat::RED . "Can't find player " . $args[0]);
return true;
}
if(count($args) === 2){
$origin = $target;
$target = $sender->getServer()->getPlayer($args[1]);
if($target === null){
$sender->sendMessage(TextFormat::RED . "Can't find player " . $args[1]);
$subject = $sender;
$targetArgs = $args;
break;
case 2: // /tp player1 player2
case 4: // /tp player1 x y z - TODO: 4 args could be x y z yaw :(
case 6: // /tp player1 x y z yaw pitch
$subject = $this->findPlayer($sender, $args[0]);
if($subject === null){
return true; return true;
} }
} $targetArgs = $args;
array_shift($targetArgs);
break;
default:
throw new InvalidCommandSyntaxException();
} }
$targetLocation = $target->getLocation(); switch(count($targetArgs)){
case 1:
$targetPlayer = $this->findPlayer($sender, $targetArgs[0]);
if($targetPlayer === null){
return true;
}
if(count($args) < 3){ $subject->teleport($targetPlayer->getLocation());
$origin->teleport($targetLocation); Command::broadcastCommandMessage($sender, new TranslationContainer("commands.tp.success", [$subject->getName(), $targetPlayer->getName()]));
Command::broadcastCommandMessage($sender, new TranslationContainer("commands.tp.success", [$origin->getName(), $target->getName()]));
return true; return true;
}else{ case 3:
if(count($args) === 4 or count($args) === 6){ case 5:
$pos = 1; $base = $subject->getLocation();
}else{ if(count($targetArgs) === 5){
$pos = 0; $yaw = (float) $targetArgs[3];
} $pitch = (float) $targetArgs[4];
}else{
$yaw = $base->yaw;
$pitch = $base->pitch;
}
$x = $this->getRelativeDouble($targetLocation->x, $sender, $args[$pos++]); $x = $this->getRelativeDouble($base->x, $sender, $targetArgs[0]);
$y = $this->getRelativeDouble($targetLocation->y, $sender, $args[$pos++], 0, 256); $y = $this->getRelativeDouble($base->y, $sender, $targetArgs[1], 0, 256);
$z = $this->getRelativeDouble($targetLocation->z, $sender, $args[$pos++]); $z = $this->getRelativeDouble($base->z, $sender, $targetArgs[2]);
$yaw = $targetLocation->getYaw(); $targetLocation = new Location($x, $y, $z, $yaw, $pitch, $base->getWorldNonNull());
$pitch = $targetLocation->getPitch();
if(count($args) === 6 or (count($args) === 5 and $pos === 3)){ $subject->teleport($targetLocation);
$yaw = (float) $args[$pos++]; Command::broadcastCommandMessage($sender, new TranslationContainer("commands.tp.success.coordinates", [
$pitch = (float) $args[$pos++]; $subject->getName(),
} round($targetLocation->x, 2),
round($targetLocation->y, 2),
$target->teleport(new Vector3($x, $y, $z), $yaw, $pitch); round($targetLocation->z, 2)
Command::broadcastCommandMessage($sender, new TranslationContainer("commands.tp.success.coordinates", [$target->getName(), round($x, 2), round($y, 2), round($z, 2)])); ]));
return true;
return true; default:
throw new AssumptionFailedError("This branch should be unreachable (for now)");
} }
} }
} }

View File

@ -176,6 +176,8 @@ class TimingsCommand extends VanillaCommand{
fclose($fileTimings); fclose($fileTimings);
Command::broadcastCommandMessage($sender, new TranslationContainer("pocketmine.command.timings.timingsWrite", [$timings])); Command::broadcastCommandMessage($sender, new TranslationContainer("pocketmine.command.timings.timingsWrite", [$timings]));
} }
}else{
throw new InvalidCommandSyntaxException();
} }
return true; return true;

View File

@ -49,10 +49,6 @@ class WhitelistCommand extends VanillaCommand{
return true; return true;
} }
if(count($args) === 0 or count($args) > 2){
throw new InvalidCommandSyntaxException();
}
if(count($args) === 1){ if(count($args) === 1){
if($this->badPerm($sender, strtolower($args[0]))){ if($this->badPerm($sender, strtolower($args[0]))){
return false; return false;
@ -112,7 +108,7 @@ class WhitelistCommand extends VanillaCommand{
} }
} }
return true; throw new InvalidCommandSyntaxException();
} }
private function badPerm(CommandSender $sender, string $subcommand) : bool{ private function badPerm(CommandSender $sender, string $subcommand) : bool{

View File

@ -30,6 +30,7 @@ use pocketmine\event\CancellableTrait;
/** /**
* Called when an Entity, excluding players, changes a block directly * Called when an Entity, excluding players, changes a block directly
* @phpstan-extends EntityEvent<Entity>
*/ */
class EntityBlockChangeEvent extends EntityEvent implements Cancellable{ class EntityBlockChangeEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -27,6 +27,9 @@ use pocketmine\entity\Entity;
use pocketmine\event\Cancellable; use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait; use pocketmine\event\CancellableTrait;
/**
* @phpstan-extends EntityEvent<Entity>
*/
class EntityCombustEvent extends EntityEvent implements Cancellable{ class EntityCombustEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -31,6 +31,7 @@ use function max;
/** /**
* Called when an entity takes damage. * Called when an entity takes damage.
* @phpstan-extends EntityEvent<Entity>
*/ */
class EntityDamageEvent extends EntityEvent implements Cancellable{ class EntityDamageEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -26,6 +26,9 @@ namespace pocketmine\event\entity;
use pocketmine\entity\Living; use pocketmine\entity\Living;
use pocketmine\item\Item; use pocketmine\item\Item;
/**
* @phpstan-extends EntityEvent<Living>
*/
class EntityDeathEvent extends EntityEvent{ class EntityDeathEvent extends EntityEvent{
/** @var Item[] */ /** @var Item[] */
private $drops = []; private $drops = [];

View File

@ -27,6 +27,7 @@ use pocketmine\entity\Entity;
/** /**
* Called when a entity is despawned * Called when a entity is despawned
* @phpstan-extends EntityEvent<Entity>
*/ */
class EntityDespawnEvent extends EntityEvent{ class EntityDespawnEvent extends EntityEvent{

View File

@ -28,6 +28,9 @@ use pocketmine\entity\Entity;
use pocketmine\event\Cancellable; use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait; use pocketmine\event\CancellableTrait;
/**
* @phpstan-extends EntityEvent<Entity>
*/
class EntityEffectEvent extends EntityEvent implements Cancellable{ class EntityEffectEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -29,12 +29,19 @@ namespace pocketmine\event\entity;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\event\Event; use pocketmine\event\Event;
/**
* @phpstan-template TEntity of Entity
*/
abstract class EntityEvent extends Event{ abstract class EntityEvent extends Event{
/** @var Entity */ /**
* @var Entity
* @phpstan-var TEntity
*/
protected $entity; protected $entity;
/** /**
* @return Entity * @return Entity
* @phpstan-return TEntity
*/ */
public function getEntity(){ public function getEntity(){
return $this->entity; return $this->entity;

View File

@ -31,6 +31,7 @@ use pocketmine\world\Position;
/** /**
* Called when a entity explodes * Called when a entity explodes
* @phpstan-extends EntityEvent<Entity>
*/ */
class EntityExplodeEvent extends EntityEvent implements Cancellable{ class EntityExplodeEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -28,6 +28,9 @@ use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait; use pocketmine\event\CancellableTrait;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
/**
* @phpstan-extends EntityEvent<Entity>
*/
class EntityMotionEvent extends EntityEvent implements Cancellable{ class EntityMotionEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -27,6 +27,9 @@ use pocketmine\entity\Entity;
use pocketmine\event\Cancellable; use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait; use pocketmine\event\CancellableTrait;
/**
* @phpstan-extends EntityEvent<Entity>
*/
class EntityRegainHealthEvent extends EntityEvent implements Cancellable{ class EntityRegainHealthEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -31,6 +31,9 @@ use pocketmine\event\CancellableTrait;
use pocketmine\item\Item; use pocketmine\item\Item;
use function count; use function count;
/**
* @phpstan-extends EntityEvent<Living>
*/
class EntityShootBowEvent extends EntityEvent implements Cancellable{ class EntityShootBowEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -27,6 +27,7 @@ use pocketmine\entity\Entity;
/** /**
* Called when a entity is spawned * Called when a entity is spawned
* @phpstan-extends EntityEvent<Entity>
*/ */
class EntitySpawnEvent extends EntityEvent{ class EntitySpawnEvent extends EntityEvent{

View File

@ -28,6 +28,9 @@ use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait; use pocketmine\event\CancellableTrait;
use pocketmine\world\Position; use pocketmine\world\Position;
/**
* @phpstan-extends EntityEvent<Entity>
*/
class EntityTeleportEvent extends EntityEvent implements Cancellable{ class EntityTeleportEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -29,6 +29,7 @@ use pocketmine\event\CancellableTrait;
/** /**
* Called when a entity decides to explode * Called when a entity decides to explode
* @phpstan-extends EntityEvent<Entity>
*/ */
class ExplosionPrimeEvent extends EntityEvent implements Cancellable{ class ExplosionPrimeEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -27,6 +27,9 @@ use pocketmine\entity\object\ItemEntity;
use pocketmine\event\Cancellable; use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait; use pocketmine\event\CancellableTrait;
/**
* @phpstan-extends EntityEvent<ItemEntity>
*/
class ItemDespawnEvent extends EntityEvent implements Cancellable{ class ItemDespawnEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -25,6 +25,9 @@ namespace pocketmine\event\entity;
use pocketmine\entity\object\ItemEntity; use pocketmine\entity\object\ItemEntity;
/**
* @phpstan-extends EntityEvent<ItemEntity>
*/
class ItemSpawnEvent extends EntityEvent{ class ItemSpawnEvent extends EntityEvent{
public function __construct(ItemEntity $item){ public function __construct(ItemEntity $item){

View File

@ -28,6 +28,7 @@ use pocketmine\math\RayTraceResult;
/** /**
* @allowHandle * @allowHandle
* @phpstan-extends EntityEvent<Projectile>
*/ */
abstract class ProjectileHitEvent extends EntityEvent{ abstract class ProjectileHitEvent extends EntityEvent{
/** @var RayTraceResult */ /** @var RayTraceResult */

View File

@ -27,6 +27,9 @@ use pocketmine\entity\projectile\Projectile;
use pocketmine\event\Cancellable; use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait; use pocketmine\event\CancellableTrait;
/**
* @phpstan-extends EntityEvent<Projectile>
*/
class ProjectileLaunchEvent extends EntityEvent implements Cancellable{ class ProjectileLaunchEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -28,6 +28,9 @@ use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait; use pocketmine\event\CancellableTrait;
use pocketmine\event\entity\EntityEvent; use pocketmine\event\entity\EntityEvent;
/**
* @phpstan-extends EntityEvent<Human>
*/
class PlayerExhaustEvent extends EntityEvent implements Cancellable{ class PlayerExhaustEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -30,6 +30,7 @@ use pocketmine\event\entity\EntityEvent;
/** /**
* Called when a player gains or loses XP levels and/or progress. * Called when a player gains or loses XP levels and/or progress.
* @phpstan-extends EntityEvent<Human>
*/ */
class PlayerExperienceChangeEvent extends EntityEvent implements Cancellable{ class PlayerExperienceChangeEvent extends EntityEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;

View File

@ -35,46 +35,11 @@ parameters:
count: 1 count: 1
path: ../../../src/block/inventory/DoubleChestInventory.php path: ../../../src/block/inventory/DoubleChestInventory.php
-
message: "#^Call to an undefined method pocketmine\\\\command\\\\CommandSender\\:\\:teleport\\(\\)\\.$#"
count: 1
path: ../../../src/command/defaults/TeleportCommand.php
-
message: "#^Method pocketmine\\\\event\\\\entity\\\\EntityDeathEvent\\:\\:getEntity\\(\\) should return pocketmine\\\\entity\\\\Living but returns pocketmine\\\\entity\\\\Entity\\.$#"
count: 1
path: ../../../src/event/entity/EntityDeathEvent.php
-
message: "#^Method pocketmine\\\\event\\\\entity\\\\EntityShootBowEvent\\:\\:getEntity\\(\\) should return pocketmine\\\\entity\\\\Living but returns pocketmine\\\\entity\\\\Entity\\.$#"
count: 1
path: ../../../src/event/entity/EntityShootBowEvent.php
- -
message: "#^Property pocketmine\\\\event\\\\entity\\\\EntityShootBowEvent\\:\\:\\$projectile \\(pocketmine\\\\entity\\\\projectile\\\\Projectile\\) does not accept pocketmine\\\\entity\\\\Entity\\.$#" message: "#^Property pocketmine\\\\event\\\\entity\\\\EntityShootBowEvent\\:\\:\\$projectile \\(pocketmine\\\\entity\\\\projectile\\\\Projectile\\) does not accept pocketmine\\\\entity\\\\Entity\\.$#"
count: 1 count: 1
path: ../../../src/event/entity/EntityShootBowEvent.php path: ../../../src/event/entity/EntityShootBowEvent.php
-
message: "#^Method pocketmine\\\\event\\\\entity\\\\ItemDespawnEvent\\:\\:getEntity\\(\\) should return pocketmine\\\\entity\\\\object\\\\ItemEntity but returns pocketmine\\\\entity\\\\Entity\\.$#"
count: 1
path: ../../../src/event/entity/ItemDespawnEvent.php
-
message: "#^Method pocketmine\\\\event\\\\entity\\\\ItemSpawnEvent\\:\\:getEntity\\(\\) should return pocketmine\\\\entity\\\\object\\\\ItemEntity but returns pocketmine\\\\entity\\\\Entity\\.$#"
count: 1
path: ../../../src/event/entity/ItemSpawnEvent.php
-
message: "#^Method pocketmine\\\\event\\\\entity\\\\ProjectileHitEvent\\:\\:getEntity\\(\\) should return pocketmine\\\\entity\\\\projectile\\\\Projectile but returns pocketmine\\\\entity\\\\Entity\\.$#"
count: 1
path: ../../../src/event/entity/ProjectileHitEvent.php
-
message: "#^Method pocketmine\\\\event\\\\entity\\\\ProjectileLaunchEvent\\:\\:getEntity\\(\\) should return pocketmine\\\\entity\\\\projectile\\\\Projectile but returns pocketmine\\\\entity\\\\Entity\\.$#"
count: 1
path: ../../../src/event/entity/ProjectileLaunchEvent.php
- -
message: "#^Cannot instantiate interface pocketmine\\\\world\\\\format\\\\io\\\\WritableWorldProvider\\.$#" message: "#^Cannot instantiate interface pocketmine\\\\world\\\\format\\\\io\\\\WritableWorldProvider\\.$#"
count: 1 count: 1