Position: add getLevelNonNull()

this allows assuming that a position has a valid world in places where it's never expected to not be valid. Since this is the vast majority of usages, it eliminates a lot of possible null-pointer warnings given by static analysers.
TODO: Consider whether we can make Position->getLevel/World use this behaviour out of the box in the next major version.
This commit is contained in:
Dylan K. Taylor
2020-04-14 11:08:37 +01:00
parent 6e08b622b3
commit a2543ff80d
76 changed files with 229 additions and 183 deletions

View File

@@ -74,7 +74,7 @@ class ChestInventory extends ContainerInventory{
if(count($this->getViewers()) === 1 and $this->getHolder()->isValid()){
//TODO: this crap really shouldn't be managed by the inventory
$this->broadcastBlockEventPacket(true);
$this->getHolder()->getLevel()->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), $this->getOpenSound());
$this->getHolder()->getLevelNonNull()->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), $this->getOpenSound());
}
}
@@ -82,7 +82,7 @@ class ChestInventory extends ContainerInventory{
if(count($this->getViewers()) === 1 and $this->getHolder()->isValid()){
//TODO: this crap really shouldn't be managed by the inventory
$this->broadcastBlockEventPacket(false);
$this->getHolder()->getLevel()->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), $this->getCloseSound());
$this->getHolder()->getLevelNonNull()->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), $this->getCloseSound());
}
parent::onClose($who);
}
@@ -96,6 +96,6 @@ class ChestInventory extends ContainerInventory{
$pk->z = (int) $holder->z;
$pk->eventType = 1; //it's always 1 for a chest
$pk->eventData = $isOpen ? 1 : 0;
$holder->getLevel()->broadcastPacketToViewers($holder, $pk);
$holder->getLevelNonNull()->broadcastPacketToViewers($holder, $pk);
}
}

View File

@@ -56,7 +56,7 @@ class EnderChestInventory extends ChestInventory{
*/
public function setHolderPosition(EnderChest $enderChest){
$this->holder->setComponents($enderChest->getFloorX(), $enderChest->getFloorY(), $enderChest->getFloorZ());
$this->holder->setLevel($enderChest->getLevel());
$this->holder->setLevel($enderChest->getLevelNonNull());
}
protected function getOpenSound() : int{

View File

@@ -173,7 +173,7 @@ class PlayerInventory extends BaseInventory{
$this->sendSlot($this->getHeldItemIndex(), $target);
}
}else{
$this->getHolder()->getLevel()->getServer()->broadcastPacket($target, $pk);
$this->getHolder()->getLevelNonNull()->getServer()->broadcastPacket($target, $pk);
if(in_array($this->getHolder(), $target, true)){
$this->sendSlot($this->getHeldItemIndex(), $this->getHolder());
}