Eliminate repeated calls to Position->getWorld()

as well as improving readability, it also improves performance in some areas.
This commit is contained in:
Dylan K. Taylor
2022-07-20 20:44:05 +01:00
parent 24e72ec109
commit 2940547026
39 changed files with 185 additions and 134 deletions

View File

@ -53,13 +53,14 @@ class Grass extends Opaque{
}
public function onRandomTick() : void{
$lightAbove = $this->position->getWorld()->getFullLightAt($this->position->x, $this->position->y + 1, $this->position->z);
if($lightAbove < 4 && $this->position->getWorld()->getBlockAt($this->position->x, $this->position->y + 1, $this->position->z)->getLightFilter() >= 2){
$world = $this->position->getWorld();
$lightAbove = $world->getFullLightAt($this->position->x, $this->position->y + 1, $this->position->z);
if($lightAbove < 4 && $world->getBlockAt($this->position->x, $this->position->y + 1, $this->position->z)->getLightFilter() >= 2){
//grass dies
$ev = new BlockSpreadEvent($this, $this, VanillaBlocks::DIRT());
$ev->call();
if(!$ev->isCancelled()){
$this->position->getWorld()->setBlock($this->position, $ev->getNewState(), false);
$world->setBlock($this->position, $ev->getNewState(), false);
}
}elseif($lightAbove >= 9){
//try grass spread
@ -68,12 +69,12 @@ class Grass extends Opaque{
$y = mt_rand($this->position->y - 3, $this->position->y + 1);
$z = mt_rand($this->position->z - 1, $this->position->z + 1);
$b = $this->position->getWorld()->getBlockAt($x, $y, $z);
$b = $world->getBlockAt($x, $y, $z);
if(
!($b instanceof Dirt) ||
$b->isCoarse() ||
$this->position->getWorld()->getFullLightAt($x, $y + 1, $z) < 4 ||
$this->position->getWorld()->getBlockAt($x, $y + 1, $z)->getLightFilter() >= 2
$world->getFullLightAt($x, $y + 1, $z) < 4 ||
$world->getBlockAt($x, $y + 1, $z)->getLightFilter() >= 2
){
continue;
}
@ -81,7 +82,7 @@ class Grass extends Opaque{
$ev = new BlockSpreadEvent($b, $this, VanillaBlocks::GRASS());
$ev->call();
if(!$ev->isCancelled()){
$this->position->getWorld()->setBlock($b->position, $ev->getNewState(), false);
$world->setBlock($b->position, $ev->getNewState(), false);
}
}
}
@ -91,23 +92,24 @@ class Grass extends Opaque{
if($face !== Facing::UP){
return false;
}
$world = $this->position->getWorld();
if($item instanceof Fertilizer){
$item->pop();
TallGrassObject::growGrass($this->position->getWorld(), $this->position, new Random(mt_rand()), 8, 2);
TallGrassObject::growGrass($world, $this->position, new Random(mt_rand()), 8, 2);
return true;
}elseif($item instanceof Hoe){
$item->applyDamage(1);
$newBlock = VanillaBlocks::FARMLAND();
$this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new ItemUseOnBlockSound($newBlock));
$this->position->getWorld()->setBlock($this->position, $newBlock);
$world->addSound($this->position->add(0.5, 0.5, 0.5), new ItemUseOnBlockSound($newBlock));
$world->setBlock($this->position, $newBlock);
return true;
}elseif($item instanceof Shovel && $this->getSide(Facing::UP)->getId() === BlockLegacyIds::AIR){
$item->applyDamage(1);
$newBlock = VanillaBlocks::GRASS_PATH();
$this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new ItemUseOnBlockSound($newBlock));
$this->position->getWorld()->setBlock($this->position, $newBlock);
$world->addSound($this->position->add(0.5, 0.5, 0.5), new ItemUseOnBlockSound($newBlock));
$world->setBlock($this->position, $newBlock);
return true;
}