From 44b5c23ee174ec6e3b0dc22417c429336bb7855c Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Wed, 6 May 2015 16:21:35 +0200 Subject: [PATCH] Fixed dead entities and Item picking, improved timing reports --- src/pocketmine/Player.php | 92 +++++++++++++-------------- src/pocketmine/entity/Entity.php | 26 ++++---- src/pocketmine/entity/FallingSand.php | 2 +- src/pocketmine/entity/Item.php | 2 +- src/pocketmine/entity/Living.php | 8 +-- src/pocketmine/entity/PrimedTNT.php | 2 +- src/pocketmine/entity/Projectile.php | 2 +- src/pocketmine/entity/Squid.php | 2 +- src/pocketmine/event/Timings.php | 5 +- src/pocketmine/level/Level.php | 5 +- 10 files changed, 73 insertions(+), 73 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 2f268bef7..170182b1a 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -312,7 +312,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ * @param Player $player */ public function spawnTo(Player $player){ - if($this->spawned === true and $player->spawned === true and $this->dead !== true and $player->dead !== true and $player->getLevel() === $this->level and $player->canSee($this) and !$this->isSpectator()){ + if($this->spawned === true and $player->spawned === true and $this->isAlive() and $player->isAdventure() and $player->getLevel() === $this->level and $player->canSee($this) and !$this->isSpectator()){ parent::spawnTo($player); } } @@ -634,7 +634,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ if($this->spawned){ foreach($this->level->getChunkEntities($x, $z) as $entity){ - if($entity !== $this and !$entity->closed and !$entity->dead){ + if($entity !== $this and !$entity->closed and $entity->isAlive()){ $entity->spawnTo($this); } } @@ -718,7 +718,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ foreach($this->usedChunks as $index => $c){ Level::getXZ($index, $chunkX, $chunkZ); foreach($this->level->getChunkEntities($chunkX, $chunkZ) as $entity){ - if($entity !== $this and !$entity->closed and !$entity->dead){ + if($entity !== $this and !$entity->closed and $entity->isAlive()){ $entity->spawnTo($this); } } @@ -1160,31 +1160,31 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ protected function checkNearEntities($tickDiff){ foreach($this->level->getNearbyEntities($this->boundingBox->grow(1, 0.5, 1), $this) as $entity){ - if($tickDiff > 1){ - $entity->scheduleUpdate(); + $entity->scheduleUpdate(); + + if(!$entity->isAlive()){ + continue; } if($entity instanceof Arrow and $entity->hadCollision){ - if($entity->dead !== true){ - $item = Item::get(Item::ARROW, 0, 1); - if($this->isSurvival() and !$this->inventory->canAddItem($item)){ - continue; - } - - $this->server->getPluginManager()->callEvent($ev = new InventoryPickupArrowEvent($this->inventory, $entity)); - if($ev->isCancelled()){ - continue; - } - - $pk = new TakeItemEntityPacket(); - $pk->eid = $this->getId(); - $pk->target = $entity->getId(); - Server::broadcastPacket($entity->getViewers(), $pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING)); - $this->inventory->addItem(clone $item); - $entity->kill(); + $item = Item::get(Item::ARROW, 0, 1); + if($this->isSurvival() and !$this->inventory->canAddItem($item)){ + continue; } + + $this->server->getPluginManager()->callEvent($ev = new InventoryPickupArrowEvent($this->inventory, $entity)); + if($ev->isCancelled()){ + continue; + } + + $pk = new TakeItemEntityPacket(); + $pk->eid = $this->getId(); + $pk->target = $entity->getId(); + Server::broadcastPacket($entity->getViewers(), $pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING)); + $this->inventory->addItem(clone $item); + $entity->kill(); }elseif($entity instanceof DroppedItem){ - if($entity->dead !== true and $entity->getPickupDelay() <= 0){ + if($entity->getPickupDelay() <= 0){ $item = $entity->getItem(); if($item instanceof Item){ @@ -1219,7 +1219,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } protected function processMovement($tickDiff){ - if($this->dead or !$this->spawned or $this->newPosition === null or $this->teleportPosition !== null){ + if(!$this->isAlive() or !$this->spawned or $this->newPosition === null or $this->teleportPosition !== null){ return; } @@ -1375,7 +1375,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->lastUpdate = $currentTick; - if($this->dead === true and $this->spawned){ + if(!$this->isAlive() and $this->spawned){ ++$this->deadTicks; if($this->deadTicks >= 10){ $this->despawnFromAll(); @@ -1639,8 +1639,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $spawnPosition = $this->getSpawn(); - $this->dead = false; - $pk = new StartGamePacket(); $pk->seed = -1; $pk->x = $this->x; @@ -1668,9 +1666,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $pk = new SetHealthPacket(); $pk->health = $this->getHealth(); $this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY)); - if($this->getHealth() <= 0){ - $this->dead = true; - } $pk = new SetDifficultyPacket(); $pk->difficulty = $this->server->getDifficulty(); @@ -1714,7 +1709,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $newPos = new Vector3($packet->x, $packet->y - $this->getEyeHeight(), $packet->z); $revert = false; - if($this->dead === true or $this->spawned !== true){ + if(!$this->isAlive() or $this->spawned !== true){ $revert = true; $this->forceMovement = new Vector3($this->x, $this->y, $this->z); } @@ -1736,7 +1731,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ break; case ProtocolInfo::PLAYER_EQUIPMENT_PACKET: - if($this->spawned === false or $this->dead === true){ + if($this->spawned === false or !$this->isAlive()){ break; } @@ -1800,7 +1795,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION, false); break; case ProtocolInfo::USE_ITEM_PACKET: - if($this->spawned === false or $this->dead === true or $this->blocked){ + if($this->spawned === false or !$this->isAlive() or $this->blocked){ break; } @@ -1909,7 +1904,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } break; case ProtocolInfo::PLAYER_ACTION_PACKET: - if($this->spawned === false or $this->blocked === true or ($this->dead === true and $packet->action !== 7)){ + if($this->spawned === false or $this->blocked === true or (!$this->isAlive() and $packet->action !== 7)){ break; } @@ -2027,7 +2022,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->stopSleep(); break; case 7: //Respawn - if($this->spawned === false or $this->dead === false){ + if($this->spawned === false or $this->isAlive()){ break; } @@ -2047,8 +2042,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->deadTicks = 0; $this->noDamageTicks = 60; - $this->setHealth(20); - $this->dead = false; + $this->setHealth($this->getMaxHealth()); $this->removeAllEffects(); $this->sendData($this); @@ -2069,7 +2063,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ break; case ProtocolInfo::REMOVE_BLOCK_PACKET: - if($this->spawned === false or $this->blocked === true or $this->dead === true){ + if($this->spawned === false or $this->blocked === true or !$this->isAlive()){ break; } $this->craftingType = 0; @@ -2112,7 +2106,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ break; case ProtocolInfo::INTERACT_PACKET: - if($this->spawned === false or $this->dead === true or $this->blocked){ + if($this->spawned === false or !$this->isAlive() or $this->blocked){ break; } @@ -2130,7 +2124,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $cancelled = true; } - if($target instanceof Entity and $this->getGamemode() !== Player::VIEW and $this->dead !== true and $target->dead !== true){ + if($target instanceof Entity and $this->getGamemode() !== Player::VIEW and $this->isAlive() and $target->isAlive()){ if($target instanceof DroppedItem or $target instanceof Arrow){ $this->kick("Attempting to attack an invalid entity"); $this->server->getLogger()->warning($this->getServer()->getLanguage()->translateString("pocketmine.player.invalidEntity", [$this->getName()])); @@ -2235,7 +2229,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ break; case ProtocolInfo::ANIMATE_PACKET: - if($this->spawned === false or $this->dead === true){ + if($this->spawned === false or !$this->isAlive()){ break; } @@ -2252,7 +2246,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ case ProtocolInfo::SET_HEALTH_PACKET: //Not used break; case ProtocolInfo::ENTITY_EVENT_PACKET: - if($this->spawned === false or $this->blocked === true or $this->dead === true){ + if($this->spawned === false or $this->blocked === true or !$this->isAlive()){ break; } $this->craftingType = 0; @@ -2326,7 +2320,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } break; case ProtocolInfo::DROP_ITEM_PACKET: - if($this->spawned === false or $this->blocked === true or $this->dead === true){ + if($this->spawned === false or $this->blocked === true or !$this->isAlive()){ break; } $packet->eid = $this->id; @@ -2346,7 +2340,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION, false); break; case ProtocolInfo::TEXT_PACKET: - if($this->spawned === false or $this->dead === true){ + if($this->spawned === false or !$this->isAlive()){ break; } $this->craftingType = 0; @@ -2385,7 +2379,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } break; case ProtocolInfo::CONTAINER_SET_SLOT_PACKET: - if($this->spawned === false or $this->blocked === true or $this->dead === true){ + if($this->spawned === false or $this->blocked === true or !$this->isAlive()){ break; } @@ -2512,7 +2506,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ break; case ProtocolInfo::TILE_ENTITY_DATA_PACKET: - if($this->spawned === false or $this->blocked === true or $this->dead === true){ + if($this->spawned === false or $this->blocked === true or !$this->isAlive()){ break; } $this->craftingType = 0; @@ -2753,7 +2747,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } public function kill(){ - if($this->dead === true or $this->spawned === false){ + if(!$this->isAlive() or $this->spawned === false){ return; } @@ -2862,7 +2856,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } - if($this->dead){ + if(!$this->isAlive()){ return; } @@ -2903,7 +2897,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } public function attack($damage, EntityDamageEvent $source){ - if($this->dead === true){ + if(!$this->isAlive()){ return; } diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 51e5ef53a..05ae54bc1 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -142,7 +142,6 @@ abstract class Entity extends Location implements Metadatable{ public $inBlock = false; public $positionChanged; public $motionChanged; - public $dead; public $deadTicks = 0; protected $age = 0; @@ -558,6 +557,10 @@ abstract class Entity extends Location implements Metadatable{ return $this->health; } + public function isAlive(){ + return $this->health > 0; + } + /** * Sets the health of the Entity. This won't send any update to the players * @@ -570,7 +573,7 @@ abstract class Entity extends Location implements Metadatable{ if($amount <= 0){ $this->health = 0; - if($this->dead !== true){ + if(!$this->isAlive()){ $this->kill(); } }elseif($amount <= $this->getMaxHealth() or $amount < $this->health){ @@ -708,20 +711,20 @@ abstract class Entity extends Location implements Metadatable{ public function entityBaseTick($tickDiff = 1){ - Timings::$tickEntityTimer->startTiming(); + Timings::$timerEntityBaseTick->startTiming(); //TODO: check vehicles $this->justCreated = false; $isPlayer = $this instanceof Player; - if($this->dead === true){ + if(!$this->isAlive()){ $this->removeAllEffects(); $this->despawnFromAll(); if(!$isPlayer){ $this->close(); } - Timings::$tickEntityTimer->stopTiming(); + Timings::$timerEntityBaseTick->stopTiming(); return false; } @@ -741,7 +744,7 @@ abstract class Entity extends Location implements Metadatable{ $this->checkBlockCollision(); - if($this->y < 0 and $this->dead !== true){ + if($this->y < 0 and !$this->isAlive()){ $ev = new EntityDamageEvent($this, EntityDamageEvent::CAUSE_VOID, 10); $this->attack($ev->getFinalDamage(), $ev); $hasUpdate = true; @@ -779,7 +782,7 @@ abstract class Entity extends Location implements Metadatable{ $this->age += $tickDiff; $this->ticksLived += $tickDiff; - Timings::$tickEntityTimer->stopTiming(); + Timings::$timerEntityBaseTick->stopTiming(); return $hasUpdate; } @@ -790,7 +793,7 @@ abstract class Entity extends Location implements Metadatable{ $diffMotion = ($this->motionX - $this->lastMotionX) ** 2 + ($this->motionY - $this->lastMotionY) ** 2 + ($this->motionZ - $this->lastMotionZ) ** 2; - if($diffPosition > 0.04 or $diffRotation > 2.25){ //0.2 ** 2, 1.5 ** 2 + if($diffPosition > 0.04 or $diffRotation > 2.25 or ($diffMotion > 0 and $this->getMotion()->lengthSquared() <= 0.0001)){ //0.2 ** 2, 1.5 ** 2 $this->lastX = $this->x; $this->lastY = $this->y; $this->lastZ = $this->z; @@ -805,7 +808,7 @@ abstract class Entity extends Location implements Metadatable{ } } - if($diffMotion > 0.0025){ //0.05 ** 2 + if($diffMotion > 0.0025 or ($diffMotion > 0 and $this->getMotion()->lengthSquared() <= 0.0001)){ //0.05 ** 2 $this->lastMotionX = $this->motionX; $this->lastMotionY = $this->motionY; $this->lastMotionZ = $this->motionZ; @@ -839,7 +842,7 @@ abstract class Entity extends Location implements Metadatable{ return false; } - if($this->dead === true){ + if(!$this->isAlive()){ ++$this->deadTicks; if($this->deadTicks >= 10){ $this->despawnFromAll(); @@ -1377,10 +1380,9 @@ abstract class Entity extends Location implements Metadatable{ } public function kill(){ - if($this->dead){ + if(!$this->isAlive()){ return; } - $this->dead = true; $this->setHealth(0); $this->scheduleUpdate(); } diff --git a/src/pocketmine/entity/FallingSand.php b/src/pocketmine/entity/FallingSand.php index dd2cc4185..830719109 100644 --- a/src/pocketmine/entity/FallingSand.php +++ b/src/pocketmine/entity/FallingSand.php @@ -94,7 +94,7 @@ class FallingSand extends Entity{ $hasUpdate = $this->entityBaseTick($tickDiff); - if(!$this->dead){ + if($this->isAlive()){ if($this->ticksLived === 1){ $block = $this->level->getBlock($pos = (new Vector3($this->x - 0.5, $this->y - 0.5, $this->z - 0.5))->floor()); if($block->getId() !== $this->blockId){ diff --git a/src/pocketmine/entity/Item.php b/src/pocketmine/entity/Item.php index 5300194a5..7c6179514 100644 --- a/src/pocketmine/entity/Item.php +++ b/src/pocketmine/entity/Item.php @@ -94,7 +94,7 @@ class Item extends Entity{ $hasUpdate = $this->entityBaseTick($tickDiff); - if(!$this->dead){ + if($this->isAlive()){ if($this->pickupDelay > 0 and $this->pickupDelay < 32767){ //Infinite delay $this->pickupDelay -= $tickDiff; diff --git a/src/pocketmine/entity/Living.php b/src/pocketmine/entity/Living.php index 50be6b59c..d12b7dd69 100644 --- a/src/pocketmine/entity/Living.php +++ b/src/pocketmine/entity/Living.php @@ -143,7 +143,7 @@ abstract class Living extends Entity implements Damageable{ } public function kill(){ - if($this->dead){ + if(!$this->isAlive()){ return; } parent::kill(); @@ -154,11 +154,11 @@ abstract class Living extends Entity implements Damageable{ } public function entityBaseTick($tickDiff = 1){ - Timings::$timerEntityBaseTick->startTiming(); + Timings::$timerLivingEntityBaseTick->startTiming(); $hasUpdate = parent::entityBaseTick($tickDiff); - if($this->dead !== true){ + if($this->isAlive()){ if($this->isInsideOfSolid()){ $hasUpdate = true; $ev = new EntityDamageEvent($this, EntityDamageEvent::CAUSE_SUFFOCATION, 1); @@ -200,7 +200,7 @@ abstract class Living extends Entity implements Damageable{ $this->attackTime -= $tickDiff; } - Timings::$timerEntityBaseTick->stopTiming(); + Timings::$timerLivingEntityBaseTick->stopTiming(); return $hasUpdate; } diff --git a/src/pocketmine/entity/PrimedTNT.php b/src/pocketmine/entity/PrimedTNT.php index 9862490a7..593f6ef1d 100644 --- a/src/pocketmine/entity/PrimedTNT.php +++ b/src/pocketmine/entity/PrimedTNT.php @@ -83,7 +83,7 @@ class PrimedTNT extends Entity implements Explosive{ $hasUpdate = $this->entityBaseTick($tickDiff); - if(!$this->dead){ + if($this->isAlive()){ $this->motionY -= $this->gravity; diff --git a/src/pocketmine/entity/Projectile.php b/src/pocketmine/entity/Projectile.php index 176d578f3..e6c37d8f0 100644 --- a/src/pocketmine/entity/Projectile.php +++ b/src/pocketmine/entity/Projectile.php @@ -88,7 +88,7 @@ abstract class Projectile extends Entity{ $hasUpdate = $this->entityBaseTick($tickDiff); - if(!$this->dead){ + if($this->isAlive()){ $movingObjectPosition = null; diff --git a/src/pocketmine/entity/Squid.php b/src/pocketmine/entity/Squid.php index 653058426..ebafb03ab 100644 --- a/src/pocketmine/entity/Squid.php +++ b/src/pocketmine/entity/Squid.php @@ -95,7 +95,7 @@ class Squid extends WaterAnimal implements Ageable{ $hasUpdate = parent::onUpdate($currentTick); - if(!$this->dead){ + if($this->isAlive()){ if($this->y > 62 and $this->swimDirection !== null){ $this->swimDirection->y = -0.5; diff --git a/src/pocketmine/event/Timings.php b/src/pocketmine/event/Timings.php index 985eeabcf..f6133adc2 100644 --- a/src/pocketmine/event/Timings.php +++ b/src/pocketmine/event/Timings.php @@ -71,6 +71,8 @@ abstract class Timings{ /** @var TimingsHandler */ public static $timerEntityBaseTick; /** @var TimingsHandler */ + public static $timerLivingEntityBaseTick; + /** @var TimingsHandler */ public static $timerEntityAI; /** @var TimingsHandler */ public static $timerEntityAICollision; @@ -121,7 +123,8 @@ abstract class Timings{ self::$activatedEntityTimer = new TimingsHandler("** activatedTickEntity"); self::$tickTileEntityTimer = new TimingsHandler("** tickTileEntity"); - self::$timerEntityBaseTick = new TimingsHandler("** livingEntityBaseTick"); + self::$timerEntityBaseTick = new TimingsHandler("** entityBaseTick"); + self::$timerLivingEntityBaseTick = new TimingsHandler("** livingEntityBaseTick"); self::$timerEntityAI = new TimingsHandler("** livingEntityAI"); self::$timerEntityAICollision = new TimingsHandler("** livingEntityAICollision"); self::$timerEntityAIMove = new TimingsHandler("** livingEntityAIMove"); diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index c45c63a7f..bd4b61b01 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -607,16 +607,17 @@ class Level implements ChunkManager, Metadatable{ $this->timings->entityTick->stopTiming(); $this->timings->tileEntityTick->startTiming(); + Timings::$tickTileEntityTimer->startTiming(); + //Update tiles that need update if(count($this->updateTiles) > 0){ - //Timings::$tickTileEntityTimer->startTiming(); foreach($this->updateTiles as $id => $tile){ if($tile->onUpdate() !== true){ unset($this->updateTiles[$id]); } } - //Timings::$tickTileEntityTimer->stopTiming(); } + Timings::$tickTileEntityTimer->stopTiming(); $this->timings->tileEntityTick->stopTiming(); $this->timings->doTickTiles->startTiming();