mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-22 19:06:35 +00:00
Separate held item index change listener logic from PlayerInventory
This commit is contained in:
parent
01c867b608
commit
c70c0b55df
@ -264,7 +264,12 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->inventory->setHeldItemIndex($nbt->getInt("SelectedInventorySlot", 0), false);
|
$this->inventory->setHeldItemIndex($nbt->getInt("SelectedInventorySlot", 0));
|
||||||
|
$this->inventory->getHeldItemIndexChangeListeners()->add(function(int $oldIndex) : void{
|
||||||
|
foreach($this->getViewers() as $viewer){
|
||||||
|
$viewer->getNetworkSession()->onMobEquipmentChange($this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
$this->hungerManager->setFood((float) $nbt->getInt("foodLevel", (int) $this->hungerManager->getFood()));
|
$this->hungerManager->setFood((float) $nbt->getInt("foodLevel", (int) $this->hungerManager->getFood()));
|
||||||
$this->hungerManager->setExhaustion($nbt->getFloat("foodExhaustionLevel", $this->hungerManager->getExhaustion()));
|
$this->hungerManager->setExhaustion($nbt->getFloat("foodExhaustionLevel", $this->hungerManager->getExhaustion()));
|
||||||
@ -457,6 +462,7 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
|
|||||||
|
|
||||||
protected function onDispose() : void{
|
protected function onDispose() : void{
|
||||||
$this->inventory->removeAllViewers();
|
$this->inventory->removeAllViewers();
|
||||||
|
$this->inventory->getHeldItemIndexChangeListeners()->clear();
|
||||||
$this->enderChestInventory->removeAllViewers();
|
$this->enderChestInventory->removeAllViewers();
|
||||||
parent::onDispose();
|
parent::onDispose();
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\inventory;
|
namespace pocketmine\inventory;
|
||||||
|
|
||||||
|
use Ds\Set;
|
||||||
use pocketmine\entity\Human;
|
use pocketmine\entity\Human;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\player\Player;
|
use pocketmine\player\Player;
|
||||||
@ -35,8 +36,15 @@ class PlayerInventory extends BaseInventory{
|
|||||||
/** @var int */
|
/** @var int */
|
||||||
protected $itemInHandIndex = 0;
|
protected $itemInHandIndex = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Closure[]|Set
|
||||||
|
* @phpstan-var Set<\Closure(int $oldIndex) : void>
|
||||||
|
*/
|
||||||
|
protected $heldItemIndexChangeListeners;
|
||||||
|
|
||||||
public function __construct(Human $player){
|
public function __construct(Human $player){
|
||||||
$this->holder = $player;
|
$this->holder = $player;
|
||||||
|
$this->heldItemIndexChangeListeners = new Set();
|
||||||
parent::__construct(36);
|
parent::__construct(36);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,24 +82,26 @@ class PlayerInventory extends BaseInventory{
|
|||||||
* Sets which hotbar slot the player is currently loading.
|
* Sets which hotbar slot the player is currently loading.
|
||||||
*
|
*
|
||||||
* @param int $hotbarSlot 0-8 index of the hotbar slot to hold
|
* @param int $hotbarSlot 0-8 index of the hotbar slot to hold
|
||||||
* @param bool $send Whether to send updates back to the inventory holder. This should usually be true for plugin calls.
|
|
||||||
* It should only be false to prevent feedback loops of equipment packets between client and server.
|
|
||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException if the hotbar slot is out of range
|
* @throws \InvalidArgumentException if the hotbar slot is out of range
|
||||||
*/
|
*/
|
||||||
public function setHeldItemIndex(int $hotbarSlot, bool $send = true) : void{
|
public function setHeldItemIndex(int $hotbarSlot) : void{
|
||||||
$this->throwIfNotHotbarSlot($hotbarSlot);
|
$this->throwIfNotHotbarSlot($hotbarSlot);
|
||||||
|
|
||||||
|
$oldIndex = $this->itemInHandIndex;
|
||||||
$this->itemInHandIndex = $hotbarSlot;
|
$this->itemInHandIndex = $hotbarSlot;
|
||||||
|
|
||||||
if($this->holder instanceof Player and $send){
|
foreach($this->heldItemIndexChangeListeners as $callback){
|
||||||
$this->holder->getNetworkSession()->getInvManager()->syncSelectedHotbarSlot();
|
$callback($oldIndex);
|
||||||
}
|
|
||||||
foreach($this->holder->getViewers() as $viewer){
|
|
||||||
$viewer->getNetworkSession()->onMobEquipmentChange($this->holder);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Closure[]|Set
|
||||||
|
* @phpstan-return Set<\Closure(int $oldIndex) : void>
|
||||||
|
*/
|
||||||
|
public function getHeldItemIndexChangeListeners() : Set{ return $this->heldItemIndexChangeListeners; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the currently-held item.
|
* Returns the currently-held item.
|
||||||
*/
|
*/
|
||||||
|
@ -76,6 +76,8 @@ class InventoryManager{
|
|||||||
* @phpstan-var array<int, array<int, Item>>
|
* @phpstan-var array<int, array<int, Item>>
|
||||||
*/
|
*/
|
||||||
private $initiatedSlotChanges = [];
|
private $initiatedSlotChanges = [];
|
||||||
|
/** @var int */
|
||||||
|
private $clientSelectedHotbarSlot = -1;
|
||||||
|
|
||||||
public function __construct(Player $player, NetworkSession $session){
|
public function __construct(Player $player, NetworkSession $session){
|
||||||
$this->player = $player;
|
$this->player = $player;
|
||||||
@ -84,6 +86,10 @@ class InventoryManager{
|
|||||||
$this->add(ContainerIds::INVENTORY, $this->player->getInventory());
|
$this->add(ContainerIds::INVENTORY, $this->player->getInventory());
|
||||||
$this->add(ContainerIds::ARMOR, $this->player->getArmorInventory());
|
$this->add(ContainerIds::ARMOR, $this->player->getArmorInventory());
|
||||||
$this->add(ContainerIds::UI, $this->player->getCursorInventory());
|
$this->add(ContainerIds::UI, $this->player->getCursorInventory());
|
||||||
|
|
||||||
|
$this->player->getInventory()->getHeldItemIndexChangeListeners()->add(function() : void{
|
||||||
|
$this->syncSelectedHotbarSlot();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private function add(int $id, Inventory $inventory) : void{
|
private function add(int $id, Inventory $inventory) : void{
|
||||||
@ -206,13 +212,21 @@ class InventoryManager{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function onClientSelectHotbarSlot(int $slot) : void{
|
||||||
|
$this->clientSelectedHotbarSlot = $slot;
|
||||||
|
}
|
||||||
|
|
||||||
public function syncSelectedHotbarSlot() : void{
|
public function syncSelectedHotbarSlot() : void{
|
||||||
|
$selected = $this->player->getInventory()->getHeldItemIndex();
|
||||||
|
if($selected !== $this->clientSelectedHotbarSlot){
|
||||||
$this->session->sendDataPacket(MobEquipmentPacket::create(
|
$this->session->sendDataPacket(MobEquipmentPacket::create(
|
||||||
$this->player->getId(),
|
$this->player->getId(),
|
||||||
TypeConverter::getInstance()->coreItemStackToNet($this->player->getInventory()->getItemInHand()),
|
TypeConverter::getInstance()->coreItemStackToNet($this->player->getInventory()->getItemInHand()),
|
||||||
$this->player->getInventory()->getHeldItemIndex(),
|
$selected,
|
||||||
ContainerIds::INVENTORY
|
ContainerIds::INVENTORY
|
||||||
));
|
));
|
||||||
|
$this->clientSelectedHotbarSlot = $selected;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function syncCreative() : void{
|
public function syncCreative() : void{
|
||||||
|
@ -448,6 +448,7 @@ class InGamePacketHandler extends PacketHandler{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function handleMobEquipment(MobEquipmentPacket $packet) : bool{
|
public function handleMobEquipment(MobEquipmentPacket $packet) : bool{
|
||||||
|
$this->session->getInvManager()->onClientSelectHotbarSlot($packet->hotbarSlot);
|
||||||
if(!$this->player->selectHotbarSlot($packet->hotbarSlot)){
|
if(!$this->player->selectHotbarSlot($packet->hotbarSlot)){
|
||||||
$this->session->getInvManager()->syncSelectedHotbarSlot();
|
$this->session->getInvManager()->syncSelectedHotbarSlot();
|
||||||
}
|
}
|
||||||
|
@ -1373,7 +1373,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->inventory->setHeldItemIndex($hotbarSlot, false);
|
$this->inventory->setHeldItemIndex($hotbarSlot);
|
||||||
$this->setUsingItem(false);
|
$this->setUsingItem(false);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user