Typehinted up Entity API and some cleanup

This commit is contained in:
Dylan K. Taylor 2017-08-30 12:17:56 +01:00
parent fd52022065
commit adc6b03d4c
23 changed files with 146 additions and 98 deletions

View File

@ -23,39 +23,7 @@ declare(strict_types=1);
namespace pocketmine\event\entity; namespace pocketmine\event\entity;
use pocketmine\entity\Entity; class EntityArmorChangeEvent extends EntityInventoryChangeEvent{
use pocketmine\event\Cancellable;
use pocketmine\item\Item;
class EntityArmorChangeEvent extends EntityEvent implements Cancellable{
public static $handlerList = null; public static $handlerList = null;
private $oldItem;
private $newItem;
private $slot;
public function __construct(Entity $entity, Item $oldItem, Item $newItem, $slot){
$this->entity = $entity;
$this->oldItem = $oldItem;
$this->newItem = $newItem;
$this->slot = (int) $slot;
}
public function getSlot(){
return $this->slot;
}
public function getNewItem(){
return $this->newItem;
}
public function setNewItem(Item $item){
$this->newItem = $item;
}
public function getOldItem(){
return $this->oldItem;
}
} }

View File

@ -33,7 +33,9 @@ use pocketmine\event\Cancellable;
class EntityBlockChangeEvent extends EntityEvent implements Cancellable{ class EntityBlockChangeEvent extends EntityEvent implements Cancellable{
public static $handlerList = null; public static $handlerList = null;
/** @var Block */
private $from; private $from;
/** @var Block */
private $to; private $to;
public function __construct(Entity $entity, Block $from, Block $to){ public function __construct(Entity $entity, Block $from, Block $to){
@ -45,14 +47,14 @@ class EntityBlockChangeEvent extends EntityEvent implements Cancellable{
/** /**
* @return Block * @return Block
*/ */
public function getBlock(){ public function getBlock() : Block{
return $this->from; return $this->from;
} }
/** /**
* @return Block * @return Block
*/ */
public function getTo(){ public function getTo() : Block{
return $this->to; return $this->to;
} }

View File

@ -28,6 +28,7 @@ use pocketmine\entity\Entity;
class EntityCombustByBlockEvent extends EntityCombustEvent{ class EntityCombustByBlockEvent extends EntityCombustEvent{
/** @var Block */
protected $combuster; protected $combuster;
/** /**
@ -35,7 +36,7 @@ class EntityCombustByBlockEvent extends EntityCombustEvent{
* @param Entity $combustee * @param Entity $combustee
* @param int $duration * @param int $duration
*/ */
public function __construct(Block $combuster, Entity $combustee, $duration){ public function __construct(Block $combuster, Entity $combustee, int $duration){
parent::__construct($combustee, $duration); parent::__construct($combustee, $duration);
$this->combuster = $combuster; $this->combuster = $combuster;
} }
@ -43,7 +44,7 @@ class EntityCombustByBlockEvent extends EntityCombustEvent{
/** /**
* @return Block * @return Block
*/ */
public function getCombuster(){ public function getCombuster() : Block{
return $this->combuster; return $this->combuster;
} }

View File

@ -27,6 +27,7 @@ use pocketmine\entity\Entity;
class EntityCombustByEntityEvent extends EntityCombustEvent{ class EntityCombustByEntityEvent extends EntityCombustEvent{
/** @var Entity */
protected $combuster; protected $combuster;
/** /**
@ -34,7 +35,7 @@ class EntityCombustByEntityEvent extends EntityCombustEvent{
* @param Entity $combustee * @param Entity $combustee
* @param int $duration * @param int $duration
*/ */
public function __construct(Entity $combuster, Entity $combustee, $duration){ public function __construct(Entity $combuster, Entity $combustee, int $duration){
parent::__construct($combustee, $duration); parent::__construct($combustee, $duration);
$this->combuster = $combuster; $this->combuster = $combuster;
} }
@ -42,7 +43,7 @@ class EntityCombustByEntityEvent extends EntityCombustEvent{
/** /**
* @return Entity * @return Entity
*/ */
public function getCombuster(){ public function getCombuster() : Entity{
return $this->combuster; return $this->combuster;
} }

View File

@ -35,17 +35,21 @@ class EntityCombustEvent extends EntityEvent implements Cancellable{
* @param Entity $combustee * @param Entity $combustee
* @param int $duration * @param int $duration
*/ */
public function __construct(Entity $combustee, $duration){ public function __construct(Entity $combustee, int $duration){
$this->entity = $combustee; $this->entity = $combustee;
$this->duration = $duration; $this->duration = $duration;
} }
public function getDuration(){ /**
* Returns the duration in seconds the entity will burn for.
* @return int
*/
public function getDuration() : int{
return $this->duration; return $this->duration;
} }
public function setDuration($duration){ public function setDuration(int $duration){
$this->duration = (int) $duration; $this->duration = $duration;
} }
} }

View File

@ -49,7 +49,7 @@ class EntityDamageByBlockEvent extends EntityDamageEvent{
/** /**
* @return Block * @return Block
*/ */
public function getDamager(){ public function getDamager() : Block{
return $this->damager; return $this->damager;
} }

View File

@ -52,7 +52,7 @@ class EntityDeathEvent extends EntityEvent{
/** /**
* @return Item[] * @return Item[]
*/ */
public function getDrops(){ public function getDrops() : array{
return $this->drops; return $this->drops;
} }

View File

@ -50,42 +50,42 @@ class EntityDespawnEvent extends EntityEvent{
/** /**
* @return int * @return int
*/ */
public function getType(){ public function getType() : int{
return $this->entityType; return $this->entityType;
} }
/** /**
* @return bool * @return bool
*/ */
public function isCreature(){ public function isCreature() : bool{
return $this->entity instanceof Creature; return $this->entity instanceof Creature;
} }
/** /**
* @return bool * @return bool
*/ */
public function isHuman(){ public function isHuman() : bool{
return $this->entity instanceof Human; return $this->entity instanceof Human;
} }
/** /**
* @return bool * @return bool
*/ */
public function isProjectile(){ public function isProjectile() : bool{
return $this->entity instanceof Projectile; return $this->entity instanceof Projectile;
} }
/** /**
* @return bool * @return bool
*/ */
public function isVehicle(){ public function isVehicle() : bool{
return $this->entity instanceof Vehicle; return $this->entity instanceof Vehicle;
} }
/** /**
* @return bool * @return bool
*/ */
public function isItem(){ public function isItem() : bool{
return $this->entity instanceof Item; return $this->entity instanceof Item;
} }

View File

@ -42,6 +42,9 @@ class EntityEatBlockEvent extends EntityEatEvent{
return parent::getResidue(); return parent::getResidue();
} }
/**
* @param Block $residue
*/
public function setResidue($residue){ public function setResidue($residue){
if(!($residue instanceof Block)){ if(!($residue instanceof Block)){
throw new \InvalidArgumentException("Eating a Block can only result in a Block residue"); throw new \InvalidArgumentException("Eating a Block can only result in a Block residue");

View File

@ -37,6 +37,7 @@ class EntityEatEvent extends EntityEvent implements Cancellable{
private $foodRestore; private $foodRestore;
/** @var float */ /** @var float */
private $saturationRestore; private $saturationRestore;
/** @var mixed */
private $residue; private $residue;
/** @var Effect[] */ /** @var Effect[] */
private $additionalEffects; private $additionalEffects;
@ -50,7 +51,7 @@ class EntityEatEvent extends EntityEvent implements Cancellable{
$this->additionalEffects = $foodSource->getAdditionalEffects(); $this->additionalEffects = $foodSource->getAdditionalEffects();
} }
public function getFoodSource(){ public function getFoodSource() : FoodSource{
return $this->foodSource; return $this->foodSource;
} }
@ -70,10 +71,17 @@ class EntityEatEvent extends EntityEvent implements Cancellable{
$this->saturationRestore = $saturationRestore; $this->saturationRestore = $saturationRestore;
} }
/**
* Returns the result of eating the food source.
* @return mixed
*/
public function getResidue(){ public function getResidue(){
return $this->residue; return $this->residue;
} }
/**
* @param mixed $residue
*/
public function setResidue($residue){ public function setResidue($residue){
$this->residue = $residue; $this->residue = $residue;
} }
@ -81,7 +89,7 @@ class EntityEatEvent extends EntityEvent implements Cancellable{
/** /**
* @return Effect[] * @return Effect[]
*/ */
public function getAdditionalEffects(){ public function getAdditionalEffects() : array{
return $this->additionalEffects; return $this->additionalEffects;
} }

View File

@ -39,6 +39,9 @@ class EntityEatItemEvent extends EntityEatEvent{
return parent::getResidue(); return parent::getResidue();
} }
/**
* @param Item $residue
*/
public function setResidue($residue){ public function setResidue($residue){
if(!($residue instanceof Item)){ if(!($residue instanceof Item)){
throw new \InvalidArgumentException("Eating an Item can only result in an Item residue"); throw new \InvalidArgumentException("Eating an Item can only result in an Item residue");

View File

@ -26,28 +26,43 @@ namespace pocketmine\event\entity;
use pocketmine\entity\Effect; use pocketmine\entity\Effect;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
/**
* Called when an effect is added to an Entity.
*/
class EntityEffectAddEvent extends EntityEffectEvent{ class EntityEffectAddEvent extends EntityEffectEvent{
public static $handlerList = null; public static $handlerList = null;
/** @var bool */ /** @var bool */
private $modify; private $modify;
/** @var Effect */ /** @var Effect|null */
private $oldEffect; private $oldEffect;
public function __construct(Entity $entity, Effect $effect, $modify, $oldEffect){ /**
* @param Entity $entity
* @param Effect $effect
* @param bool $modify
* @param Effect|null $oldEffect
*/
public function __construct(Entity $entity, Effect $effect, bool $modify, Effect $oldEffect = null){
parent::__construct($entity, $effect); parent::__construct($entity, $effect);
$this->modify = $modify; $this->modify = $modify;
$this->oldEffect = $oldEffect; $this->oldEffect = $oldEffect;
} }
/**
* Returns whether the effect addition will replace an existing effect already applied to the entity.
*
* TODO: isn't this pointless? An effect will only modify an existing effect if oldEffect is non-null anyway...
*
* @return bool
*/
public function willModify() : bool{ public function willModify() : bool{
return $this->modify; return $this->modify;
} }
public function setWillModify(bool $modify){ /**
$this->modify = $modify; * @return bool
} */
public function hasOldEffect() : bool{ public function hasOldEffect() : bool{
return $this->oldEffect instanceof Effect; return $this->oldEffect instanceof Effect;
} }

View File

@ -23,6 +23,9 @@ declare(strict_types=1);
namespace pocketmine\event\entity; namespace pocketmine\event\entity;
/**
* Called when an effect is removed from an entity.
*/
class EntityEffectRemoveEvent extends EntityEffectEvent{ class EntityEffectRemoveEvent extends EntityEffectEvent{
public static $handlerList = null; public static $handlerList = null;

View File

@ -33,6 +33,9 @@ abstract class EntityEvent extends Event{
/** @var Entity */ /** @var Entity */
protected $entity; protected $entity;
/**
* @return Entity
*/
public function getEntity(){ public function getEntity(){
return $this->entity; return $this->entity;
} }

View File

@ -51,7 +51,7 @@ class EntityExplodeEvent extends EntityEvent implements Cancellable{
* @param Block[] $blocks * @param Block[] $blocks
* @param float $yield * @param float $yield
*/ */
public function __construct(Entity $entity, Position $position, array $blocks, $yield){ public function __construct(Entity $entity, Position $position, array $blocks, float $yield){
$this->entity = $entity; $this->entity = $entity;
$this->position = $position; $this->position = $position;
$this->blocks = $blocks; $this->blocks = $blocks;
@ -61,14 +61,14 @@ class EntityExplodeEvent extends EntityEvent implements Cancellable{
/** /**
* @return Position * @return Position
*/ */
public function getPosition(){ public function getPosition() : Position{
return $this->position; return $this->position;
} }
/** /**
* @return Block[] * @return Block[]
*/ */
public function getBlockList(){ public function getBlockList() : array{
return $this->blocks; return $this->blocks;
} }
@ -82,14 +82,14 @@ class EntityExplodeEvent extends EntityEvent implements Cancellable{
/** /**
* @return float * @return float
*/ */
public function getYield(){ public function getYield() : float{
return $this->yield; return $this->yield;
} }
/** /**
* @param float $yield * @param float $yield
*/ */
public function setYield($yield){ public function setYield(float $yield){
$this->yield = $yield; $this->yield = $yield;
} }

View File

@ -27,33 +27,54 @@ use pocketmine\entity\Entity;
use pocketmine\event\Cancellable; use pocketmine\event\Cancellable;
use pocketmine\item\Item; use pocketmine\item\Item;
/**
* Called before a slot in an entity's inventory changes.
*/
class EntityInventoryChangeEvent extends EntityEvent implements Cancellable{ class EntityInventoryChangeEvent extends EntityEvent implements Cancellable{
public static $handlerList = null; public static $handlerList = null;
/** @var Item */
private $oldItem; private $oldItem;
/** @var Item */
private $newItem; private $newItem;
/** @var int */
private $slot; private $slot;
public function __construct(Entity $entity, Item $oldItem, Item $newItem, $slot){ public function __construct(Entity $entity, Item $oldItem, Item $newItem, int $slot){
$this->entity = $entity; $this->entity = $entity;
$this->oldItem = $oldItem; $this->oldItem = $oldItem;
$this->newItem = $newItem; $this->newItem = $newItem;
$this->slot = (int) $slot; $this->slot = $slot;
} }
public function getSlot(){ /**
* Returns the inventory slot number affected by the event.
* @return int
*/
public function getSlot() : int{
return $this->slot; return $this->slot;
} }
public function getNewItem(){ /**
* Returns the item which will be in the slot after the event.
* @return Item
*/
public function getNewItem() : Item{
return $this->newItem; return $this->newItem;
} }
/**
* @param Item $item
*/
public function setNewItem(Item $item){ public function setNewItem(Item $item){
$this->newItem = $item; $this->newItem = $item;
} }
public function getOldItem(){ /**
* Returns the item currently in the slot.
* @return Item
*/
public function getOldItem() : Item{
return $this->oldItem; return $this->oldItem;
} }

View File

@ -30,7 +30,9 @@ use pocketmine\level\Level;
class EntityLevelChangeEvent extends EntityEvent implements Cancellable{ class EntityLevelChangeEvent extends EntityEvent implements Cancellable{
public static $handlerList = null; public static $handlerList = null;
/** @var Level */
private $originLevel; private $originLevel;
/** @var Level */
private $targetLevel; private $targetLevel;
public function __construct(Entity $entity, Level $originLevel, Level $targetLevel){ public function __construct(Entity $entity, Level $originLevel, Level $targetLevel){
@ -39,11 +41,11 @@ class EntityLevelChangeEvent extends EntityEvent implements Cancellable{
$this->targetLevel = $targetLevel; $this->targetLevel = $targetLevel;
} }
public function getOrigin(){ public function getOrigin() : Level{
return $this->originLevel; return $this->originLevel;
} }
public function getTarget(){ public function getTarget() : Level{
return $this->targetLevel; return $this->targetLevel;
} }
} }

View File

@ -30,6 +30,7 @@ use pocketmine\math\Vector3;
class EntityMotionEvent extends EntityEvent implements Cancellable{ class EntityMotionEvent extends EntityEvent implements Cancellable{
public static $handlerList = null; public static $handlerList = null;
/** @var Vector3 */
private $mot; private $mot;
public function __construct(Entity $entity, Vector3 $mot){ public function __construct(Entity $entity, Vector3 $mot){
@ -40,7 +41,7 @@ class EntityMotionEvent extends EntityEvent implements Cancellable{
/** /**
* @return Vector3 * @return Vector3
*/ */
public function getVector(){ public function getVector() : Vector3{
return $this->mot; return $this->mot;
} }

View File

@ -35,7 +35,9 @@ class EntityRegainHealthEvent extends EntityEvent implements Cancellable{
const CAUSE_CUSTOM = 3; const CAUSE_CUSTOM = 3;
const CAUSE_SATURATION = 4; const CAUSE_SATURATION = 4;
/** @var float */
private $amount; private $amount;
/** @var int */
private $reason; private $reason;
@ -44,27 +46,31 @@ class EntityRegainHealthEvent extends EntityEvent implements Cancellable{
* @param float $amount * @param float $amount
* @param int $regainReason * @param int $regainReason
*/ */
public function __construct(Entity $entity, $amount, $regainReason){ public function __construct(Entity $entity, float $amount, int $regainReason){
$this->entity = $entity; $this->entity = $entity;
$this->amount = $amount; $this->amount = $amount;
$this->reason = (int) $regainReason; $this->reason = $regainReason;
} }
/** /**
* @return float * @return float
*/ */
public function getAmount(){ public function getAmount() : float{
return $this->amount; return $this->amount;
} }
/** /**
* @param float $amount * @param float $amount
*/ */
public function setAmount($amount){ public function setAmount(float $amount){
$this->amount = $amount; $this->amount = $amount;
} }
public function getRegainReason(){ /**
* Returns one of the CAUSE_* constants to indicate why this regeneration occurred.
* @return int
*/
public function getRegainReason() : int{
return $this->reason; return $this->reason;
} }

View File

@ -45,7 +45,7 @@ class EntityShootBowEvent extends EntityEvent implements Cancellable{
* @param Projectile $projectile * @param Projectile $projectile
* @param float $force * @param float $force
*/ */
public function __construct(Living $shooter, Item $bow, Projectile $projectile, $force){ public function __construct(Living $shooter, Item $bow, Projectile $projectile, float $force){
$this->entity = $shooter; $this->entity = $shooter;
$this->bow = $bow; $this->bow = $bow;
$this->projectile = $projectile; $this->projectile = $projectile;
@ -62,14 +62,18 @@ class EntityShootBowEvent extends EntityEvent implements Cancellable{
/** /**
* @return Item * @return Item
*/ */
public function getBow(){ public function getBow() : Item{
return $this->bow; return $this->bow;
} }
/** /**
* Returns the entity considered as the projectile in this event.
*
* NOTE: This might not return a Projectile if a plugin modified the target entity.
*
* @return Entity * @return Entity
*/ */
public function getProjectile(){ public function getProjectile() : Entity{
return $this->projectile; return $this->projectile;
} }
@ -89,14 +93,14 @@ class EntityShootBowEvent extends EntityEvent implements Cancellable{
/** /**
* @return float * @return float
*/ */
public function getForce(){ public function getForce() : float{
return $this->force; return $this->force;
} }
/** /**
* @param float $force * @param float $force
*/ */
public function setForce($force){ public function setForce(float $force){
$this->force = $force; $this->force = $force;
} }

View File

@ -51,49 +51,49 @@ class EntitySpawnEvent extends EntityEvent{
/** /**
* @return Position * @return Position
*/ */
public function getPosition(){ public function getPosition() : Position{
return $this->entity->getPosition(); return $this->entity->getPosition();
} }
/** /**
* @return int * @return int
*/ */
public function getType(){ public function getType() : int{
return $this->entityType; return $this->entityType;
} }
/** /**
* @return bool * @return bool
*/ */
public function isCreature(){ public function isCreature() : bool{
return $this->entity instanceof Creature; return $this->entity instanceof Creature;
} }
/** /**
* @return bool * @return bool
*/ */
public function isHuman(){ public function isHuman() : bool{
return $this->entity instanceof Human; return $this->entity instanceof Human;
} }
/** /**
* @return bool * @return bool
*/ */
public function isProjectile(){ public function isProjectile() : bool{
return $this->entity instanceof Projectile; return $this->entity instanceof Projectile;
} }
/** /**
* @return bool * @return bool
*/ */
public function isVehicle(){ public function isVehicle() : bool{
return $this->entity instanceof Vehicle; return $this->entity instanceof Vehicle;
} }
/** /**
* @return bool * @return bool
*/ */
public function isItem(){ public function isItem() : bool{
return $this->entity instanceof Item; return $this->entity instanceof Item;
} }

View File

@ -44,11 +44,12 @@ class EntityTeleportEvent extends EntityEvent implements Cancellable{
/** /**
* @return Position * @return Position
*/ */
public function getFrom(){ public function getFrom() : Position{
return $this->from; return $this->from;
} }
/** /**
* @deprecated This method has no effect or use.
* @param Position $from * @param Position $from
*/ */
public function setFrom(Position $from){ public function setFrom(Position $from){
@ -58,7 +59,7 @@ class EntityTeleportEvent extends EntityEvent implements Cancellable{
/** /**
* @return Position * @return Position
*/ */
public function getTo(){ public function getTo() : Position{
return $this->to; return $this->to;
} }

View File

@ -32,14 +32,16 @@ use pocketmine\event\Cancellable;
class ExplosionPrimeEvent extends EntityEvent implements Cancellable{ class ExplosionPrimeEvent extends EntityEvent implements Cancellable{
public static $handlerList = null; public static $handlerList = null;
/** @var float */
protected $force; protected $force;
/** @var bool */
private $blockBreaking; private $blockBreaking;
/** /**
* @param Entity $entity * @param Entity $entity
* @param float $force * @param float $force
*/ */
public function __construct(Entity $entity, $force){ public function __construct(Entity $entity, float $force){
$this->entity = $entity; $this->entity = $entity;
$this->force = $force; $this->force = $force;
$this->blockBreaking = true; $this->blockBreaking = true;
@ -48,26 +50,26 @@ class ExplosionPrimeEvent extends EntityEvent implements Cancellable{
/** /**
* @return float * @return float
*/ */
public function getForce(){ public function getForce() : float{
return $this->force; return $this->force;
} }
public function setForce($force){ public function setForce(float $force){
$this->force = (float) $force; $this->force = $force;
} }
/** /**
* @return bool * @return bool
*/ */
public function isBlockBreaking(){ public function isBlockBreaking() : bool{
return $this->blockBreaking; return $this->blockBreaking;
} }
/** /**
* @param bool $affectsBlocks * @param bool $affectsBlocks
*/ */
public function setBlockBreaking($affectsBlocks){ public function setBlockBreaking(bool $affectsBlocks){
$this->blockBreaking = (bool) $affectsBlocks; $this->blockBreaking = $affectsBlocks;
} }
} }