Skip to content

Commit

Permalink
Tracking entities killed by blocks (fire, lava, etc...)
Browse files Browse the repository at this point in the history
  • Loading branch information
matcracker committed Apr 12, 2020
1 parent cc263e4 commit 7282731
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
9 changes: 6 additions & 3 deletions src/matcracker/BedcoreProtect/listeners/EntityListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use pocketmine\entity\object\FallingBlock;
use pocketmine\entity\object\Painting;
use pocketmine\event\entity\EntityBlockChangeEvent;
use pocketmine\event\entity\EntityDamageByBlockEvent;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDeathEvent;
use pocketmine\event\entity\EntityDespawnEvent;
Expand Down Expand Up @@ -112,12 +113,14 @@ public function trackEntityDeath(EntityDeathEvent $event): void
{
$entity = $event->getEntity();
if ($this->plugin->getParsedConfig()->isEnabledWorld($entity->getLevel()) && $this->plugin->getParsedConfig()->getEntityKills()) {
$ev = $entity->getLastDamageCause();
if ($ev instanceof EntityDamageByEntityEvent) {
$damager = $ev->getDamager();
$damageEvent = $entity->getLastDamageCause();
if ($damageEvent instanceof EntityDamageByEntityEvent) {
$damager = $damageEvent->getDamager();
if ($damager !== null) {
$this->entitiesQueries->addEntityLogByEntity($damager, $entity, Action::KILL());
}
} elseif ($damageEvent instanceof EntityDamageByBlockEvent) {
$this->entitiesQueries->addEntityLogByBlock($entity, $damageEvent->getDamager(), Action::KILL());
}
}
}
Expand Down
49 changes: 36 additions & 13 deletions src/matcracker/BedcoreProtect/storage/queries/EntitiesQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use matcracker\BedcoreProtect\math\Area;
use matcracker\BedcoreProtect\storage\QueryManager;
use matcracker\BedcoreProtect\utils\Utils;
use pocketmine\block\Block;
use pocketmine\entity\Entity;
use pocketmine\entity\Living;
use pocketmine\Player;
Expand Down Expand Up @@ -87,19 +88,7 @@ function () use ($damager, $entity, $action): Generator {

/** @var int $lastId */
$lastId = yield $this->addRawLog(Utils::getEntityUniqueId($damager), $entity, $action);

$entity->saveNBT();

if ($entity instanceof Living) {
$entity->namedtag->setFloat("Health", $entity->getMaxHealth());
}

yield $this->executeInsert(QueriesConst::ADD_ENTITY_LOG, [
'log_id' => $lastId,
'uuid' => Utils::getEntityUniqueId($entity),
'id' => $entity->getId(),
'nbt' => Utils::serializeNBT($entity->namedtag)
]);
yield $this->addEntityLog($lastId, $entity);
},
static function (): void {
//NOOP
Expand All @@ -122,6 +111,40 @@ final public function addEntityGenerator(Entity $entity): Generator
);
}

final protected function addEntityLog(int $logId, Entity $entity): Generator
{
$entity->saveNBT();

if ($entity instanceof Living) {
$entity->namedtag->setFloat("Health", $entity->getMaxHealth());
}

return $this->executeInsert(QueriesConst::ADD_ENTITY_LOG, [
'log_id' => $logId,
'uuid' => Utils::getEntityUniqueId($entity),
'id' => $entity->getId(),
'nbt' => Utils::serializeNBT($entity->namedtag)
]);
}

public function addEntityLogByBlock(Entity $entity, Block $block, Action $action): void
{
Await::f2c(
function () use ($entity, $block, $action): Generator {
yield $this->addEntityGenerator($entity);

$name = $block->getName();
/** @var int $lastId */
$lastId = yield $this->addRawLog("{$name}-uuid", $block->asPosition(), $action);

yield $this->addEntityLog($lastId, $entity);
},
static function (): void {
//NOOP
}
);
}

final public function addEntity(Entity $entity): void
{
Await::f2c(
Expand Down

0 comments on commit 7282731

Please sign in to comment.