Clean up handling of armour sync

This commit is contained in:
Dylan K. Taylor 2019-05-19 16:56:10 +01:00
parent cd103cefcc
commit bca0833035
3 changed files with 17 additions and 51 deletions

View File

@ -33,6 +33,8 @@ use pocketmine\event\entity\EntityDeathEvent;
use pocketmine\event\entity\EntityEffectAddEvent; use pocketmine\event\entity\EntityEffectAddEvent;
use pocketmine\event\entity\EntityEffectRemoveEvent; use pocketmine\event\entity\EntityEffectRemoveEvent;
use pocketmine\inventory\ArmorInventory; use pocketmine\inventory\ArmorInventory;
use pocketmine\inventory\CallbackInventoryChangeListener;
use pocketmine\inventory\Inventory;
use pocketmine\item\Armor; use pocketmine\item\Armor;
use pocketmine\item\Consumable; use pocketmine\item\Consumable;
use pocketmine\item\Durable; use pocketmine\item\Durable;
@ -94,6 +96,13 @@ abstract class Living extends Entity implements Damageable{
$this->armorInventory = new ArmorInventory($this); $this->armorInventory = new ArmorInventory($this);
//TODO: load/save armor inventory contents //TODO: load/save armor inventory contents
$this->armorInventory->addChangeListeners(CallbackInventoryChangeListener::onAnyChange(
function(Inventory $unused) : void{
foreach($this->getViewers() as $viewer){
$viewer->getNetworkSession()->onMobArmorChange($this);
}
}
));
$health = $this->getMaxHealth(); $health = $this->getMaxHealth();

View File

@ -25,10 +25,6 @@ namespace pocketmine\inventory;
use pocketmine\entity\Living; use pocketmine\entity\Living;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\network\mcpe\protocol\MobArmorEquipmentPacket;
use pocketmine\Player;
use function array_merge;
use function array_search;
class ArmorInventory extends BaseInventory{ class ArmorInventory extends BaseInventory{
public const SLOT_HEAD = 0; public const SLOT_HEAD = 0;
@ -79,51 +75,4 @@ class ArmorInventory extends BaseInventory{
public function setBoots(Item $boots) : bool{ public function setBoots(Item $boots) : bool{
return $this->setItem(self::SLOT_FEET, $boots); return $this->setItem(self::SLOT_FEET, $boots);
} }
public function sendSlot(int $index, $target) : void{
if($target instanceof Player){
$target = [$target];
}
/** @var Player[] $target */
if(($k = array_search($this->holder, $target, true)) !== false){
$target[$k]->getNetworkSession()->syncInventorySlot($this, $index);
unset($target[$k]);
}
if(!empty($target)){
//TODO: this should be handled by change listeners
$pk = new MobArmorEquipmentPacket();
$pk->entityRuntimeId = $this->getHolder()->getId();
$pk->slots = $this->getContents(true);
$this->holder->getWorld()->getServer()->broadcastPacket($target, $pk);
}
}
public function sendContents($target) : void{
if($target instanceof Player){
$target = [$target];
}
$armor = $this->getContents(true);
if(($k = array_search($this->holder, $target, true)) !== false){
$target[$k]->getNetworkSession()->syncInventoryContents($this);
unset($target[$k]);
}
if(!empty($target)){
//TODO: this should be handled by change listeners
$pk = new MobArmorEquipmentPacket();
$pk->entityRuntimeId = $this->getHolder()->getId();
$pk->slots = $armor;
$this->holder->getWorld()->getServer()->broadcastPacket($target, $pk);
}
}
/**
* @return Player[]
*/
public function getViewers() : array{
return array_merge(parent::getViewers(), $this->holder->getViewers());
}
} }

View File

@ -48,6 +48,7 @@ use pocketmine\network\mcpe\protocol\ClientboundPacket;
use pocketmine\network\mcpe\protocol\DisconnectPacket; use pocketmine\network\mcpe\protocol\DisconnectPacket;
use pocketmine\network\mcpe\protocol\InventoryContentPacket; use pocketmine\network\mcpe\protocol\InventoryContentPacket;
use pocketmine\network\mcpe\protocol\InventorySlotPacket; use pocketmine\network\mcpe\protocol\InventorySlotPacket;
use pocketmine\network\mcpe\protocol\MobArmorEquipmentPacket;
use pocketmine\network\mcpe\protocol\MobEffectPacket; use pocketmine\network\mcpe\protocol\MobEffectPacket;
use pocketmine\network\mcpe\protocol\ModalFormRequestPacket; use pocketmine\network\mcpe\protocol\ModalFormRequestPacket;
use pocketmine\network\mcpe\protocol\MovePlayerPacket; use pocketmine\network\mcpe\protocol\MovePlayerPacket;
@ -817,6 +818,13 @@ class NetworkSession{
} }
} }
public function onMobArmorChange(Living $mob) : void{
$pk = new MobArmorEquipmentPacket();
$pk->entityRuntimeId = $mob->getId();
$pk->slots = $mob->getArmorInventory()->getContents(true); //beware this order might change in the future
$this->sendDataPacket($pk);
}
public function tick() : bool{ public function tick() : bool{
if($this->handler instanceof LoginSessionHandler){ if($this->handler instanceof LoginSessionHandler){
if(time() >= $this->connectTime + 10){ if(time() >= $this->connectTime + 10){