Use Block objects more instead of legacy ID/meta crap

This commit is contained in:
Dylan K. Taylor 2018-11-25 14:55:12 +00:00
parent 7399e9036a
commit d8ea8fa0f0
2 changed files with 15 additions and 29 deletions

View File

@ -29,7 +29,6 @@ use pocketmine\block\Fallable;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\event\entity\EntityBlockChangeEvent; use pocketmine\event\entity\EntityBlockChangeEvent;
use pocketmine\event\entity\EntityDamageEvent; use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\item\ItemFactory;
use pocketmine\level\Position; use pocketmine\level\Position;
use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
@ -111,7 +110,7 @@ class FallingBlock extends Entity{
$block = $this->level->getBlock($pos); $block = $this->level->getBlock($pos);
if($block->getId() > 0 and $block->isTransparent() and !$block->canBeReplaced()){ if($block->getId() > 0 and $block->isTransparent() and !$block->canBeReplaced()){
//FIXME: anvils are supposed to destroy torches //FIXME: anvils are supposed to destroy torches
$this->getLevel()->dropItem($this, ItemFactory::get($this->getBlock(), $this->getDamage())); $this->getLevel()->dropItem($this, $this->block->getItem());
}else{ }else{
$ev = new EntityBlockChangeEvent($this, $block, $blockTarget ?? $this->block); $ev = new EntityBlockChangeEvent($this, $block, $blockTarget ?? $this->block);
$ev->call(); $ev->call();
@ -126,12 +125,8 @@ class FallingBlock extends Entity{
return $hasUpdate; return $hasUpdate;
} }
public function getBlock() : int{ public function getBlock() : Block{
return $this->block->getId(); return $this->block;
}
public function getDamage() : int{
return $this->block->getDamage();
} }
public function saveNBT() : CompoundTag{ public function saveNBT() : CompoundTag{

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\entity\projectile; namespace pocketmine\entity\projectile;
use pocketmine\block\Block; use pocketmine\block\Block;
use pocketmine\block\BlockFactory;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\entity\Living; use pocketmine\entity\Living;
use pocketmine\event\entity\EntityCombustByEntityEvent; use pocketmine\event\entity\EntityCombustByEntityEvent;
@ -34,6 +35,7 @@ use pocketmine\event\entity\ProjectileHitBlockEvent;
use pocketmine\event\entity\ProjectileHitEntityEvent; use pocketmine\event\entity\ProjectileHitEntityEvent;
use pocketmine\event\entity\ProjectileHitEvent; use pocketmine\event\entity\ProjectileHitEvent;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\level\Position;
use pocketmine\math\RayTraceResult; use pocketmine\math\RayTraceResult;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\math\VoxelRayTrace; use pocketmine\math\VoxelRayTrace;
@ -49,12 +51,8 @@ abstract class Projectile extends Entity{
/** @var float */ /** @var float */
protected $damage = 0.0; protected $damage = 0.0;
/** @var Vector3|null */ /** @var Block|null */
protected $blockHit; protected $blockHit;
/** @var int|null */
protected $blockHitId;
/** @var int|null */
protected $blockHitData;
public function __construct(Level $level, CompoundTag $nbt, ?Entity $shootingEntity = null){ public function __construct(Level $level, CompoundTag $nbt, ?Entity $shootingEntity = null){
parent::__construct($level, $nbt); parent::__construct($level, $nbt);
@ -77,12 +75,12 @@ abstract class Projectile extends Entity{
$this->damage = $nbt->getDouble("damage", $this->damage); $this->damage = $nbt->getDouble("damage", $this->damage);
do{ do{
$blockHit = null; $blockPos = null;
$blockId = null; $blockId = null;
$blockData = null; $blockData = null;
if($nbt->hasTag("tileX", IntTag::class) and $nbt->hasTag("tileY", IntTag::class) and $nbt->hasTag("tileZ", IntTag::class)){ if($nbt->hasTag("tileX", IntTag::class) and $nbt->hasTag("tileY", IntTag::class) and $nbt->hasTag("tileZ", IntTag::class)){
$blockHit = new Vector3($nbt->getInt("tileX"), $nbt->getInt("tileY"), $nbt->getInt("tileZ")); $blockPos = new Position($nbt->getInt("tileX"), $nbt->getInt("tileY"), $nbt->getInt("tileZ"), $this->level);
}else{ }else{
break; break;
} }
@ -99,9 +97,7 @@ abstract class Projectile extends Entity{
break; break;
} }
$this->blockHit = $blockHit; $this->blockHit = BlockFactory::get($blockId, $blockData, $blockPos);
$this->blockHitId = $blockId;
$this->blockHitData = $blockData;
}while(false); }while(false);
} }
@ -151,8 +147,8 @@ abstract class Projectile extends Entity{
$nbt->setInt("tileZ", $this->blockHit->z); $nbt->setInt("tileZ", $this->blockHit->z);
//we intentionally use different ones to PC because we don't have stringy IDs //we intentionally use different ones to PC because we don't have stringy IDs
$nbt->setInt("blockId", $this->blockHitId); $nbt->setInt("blockId", $this->blockHit->getId());
$nbt->setByte("blockData", $this->blockHitData); $nbt->setByte("blockData", $this->blockHit->getDamage());
} }
return $nbt; return $nbt;
@ -163,11 +159,8 @@ abstract class Projectile extends Entity{
} }
public function onNearbyBlockChange() : void{ public function onNearbyBlockChange() : void{
if($this->blockHit !== null){ if($this->blockHit !== null and !$this->blockHit->isSameState($this->level->getBlock($this->blockHit))){
$blockIn = $this->level->getBlockAt($this->blockHit->x, $this->blockHit->y, $this->blockHit->z); $this->blockHit = null;
if($blockIn->getId() !== $this->blockHitId or $blockIn->getDamage() !== $this->blockHitData){
$this->blockHit = $this->blockHitId = $this->blockHitData = null;
}
} }
parent::onNearbyBlockChange(); parent::onNearbyBlockChange();
@ -257,7 +250,7 @@ abstract class Projectile extends Entity{
$this->motion->x = $this->motion->y = $this->motion->z = 0; $this->motion->x = $this->motion->y = $this->motion->z = 0;
}else{ }else{
$this->isCollided = $this->onGround = false; $this->isCollided = $this->onGround = false;
$this->blockHit = $this->blockHitId = $this->blockHitData = null; $this->blockHit = null;
//recompute angles... //recompute angles...
$f = sqrt(($this->motion->x ** 2) + ($this->motion->z ** 2)); $f = sqrt(($this->motion->x ** 2) + ($this->motion->z ** 2));
@ -334,8 +327,6 @@ abstract class Projectile extends Entity{
* @param RayTraceResult $hitResult * @param RayTraceResult $hitResult
*/ */
protected function onHitBlock(Block $blockHit, RayTraceResult $hitResult) : void{ protected function onHitBlock(Block $blockHit, RayTraceResult $hitResult) : void{
$this->blockHit = $blockHit->asVector3(); $this->blockHit = clone $blockHit;
$this->blockHitId = $blockHit->getId();
$this->blockHitData = $blockHit->getDamage();
} }
} }