Player: clean up container close handling

This commit is contained in:
Dylan K. Taylor 2018-07-20 11:36:56 +01:00
parent e16f20affa
commit 25cfcada26
2 changed files with 32 additions and 21 deletions

View File

@ -104,7 +104,6 @@ use pocketmine\network\mcpe\protocol\BlockEntityDataPacket;
use pocketmine\network\mcpe\protocol\BlockPickRequestPacket; use pocketmine\network\mcpe\protocol\BlockPickRequestPacket;
use pocketmine\network\mcpe\protocol\BookEditPacket; use pocketmine\network\mcpe\protocol\BookEditPacket;
use pocketmine\network\mcpe\protocol\ChunkRadiusUpdatedPacket; use pocketmine\network\mcpe\protocol\ChunkRadiusUpdatedPacket;
use pocketmine\network\mcpe\protocol\ContainerClosePacket;
use pocketmine\network\mcpe\protocol\DataPacket; use pocketmine\network\mcpe\protocol\DataPacket;
use pocketmine\network\mcpe\protocol\EntityEventPacket; use pocketmine\network\mcpe\protocol\EntityEventPacket;
use pocketmine\network\mcpe\protocol\InteractPacket; use pocketmine\network\mcpe\protocol\InteractPacket;
@ -2840,25 +2839,6 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return true; return true;
} }
public function handleContainerClose(ContainerClosePacket $packet) : bool{
if(!$this->spawned or $packet->windowId === 0){
return true;
}
$this->doCloseInventory();
if(isset($this->windowIndex[$packet->windowId])){
$this->server->getPluginManager()->callEvent(new InventoryCloseEvent($this->windowIndex[$packet->windowId], $this));
$this->removeWindow($this->windowIndex[$packet->windowId]);
return true;
}elseif($packet->windowId === 255){
//Closed a fake window
return true;
}
return false;
}
public function handleAdventureSettings(AdventureSettingsPacket $packet) : bool{ public function handleAdventureSettings(AdventureSettingsPacket $packet) : bool{
if($packet->entityUniqueId !== $this->getId()){ if($packet->entityUniqueId !== $this->getId()){
return false; //TODO return false; //TODO
@ -3752,6 +3732,10 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$this->craftingGrid = $grid; $this->craftingGrid = $grid;
} }
/**
* @internal Called to clean up crafting grid and cursor inventory when it is detected that the player closed their
* inventory.
*/
public function doCloseInventory() : void{ public function doCloseInventory() : void{
/** @var Inventory[] $inventories */ /** @var Inventory[] $inventories */
$inventories = [$this->craftingGrid, $this->cursorInventory]; $inventories = [$this->craftingGrid, $this->cursorInventory];
@ -3772,6 +3756,33 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
} }
} }
/**
* @internal Called by the network session when a player closes a window.
*
* @param int $windowId
*
* @return bool
*/
public function doCloseWindow(int $windowId) : bool{
if(!$this->spawned or $windowId === 0){
return false;
}
$this->doCloseInventory();
if(isset($this->windowIndex[$windowId])){
$this->server->getPluginManager()->callEvent(new InventoryCloseEvent($this->windowIndex[$windowId], $this));
$this->removeWindow($this->windowIndex[$windowId]);
return true;
}
if($windowId === 255){
//Closed a fake window
return true;
}
return false;
}
/** /**
* Returns the window ID which the inventory has for this player, or -1 if the window is not open to the player. * Returns the window ID which the inventory has for this player, or -1 if the window is not open to the player.
* *

View File

@ -146,7 +146,7 @@ class SimpleSessionHandler extends SessionHandler{
} }
public function handleContainerClose(ContainerClosePacket $packet) : bool{ public function handleContainerClose(ContainerClosePacket $packet) : bool{
return $this->player->handleContainerClose($packet); return $this->player->doCloseWindow($packet->windowId);
} }
public function handlePlayerHotbar(PlayerHotbarPacket $packet) : bool{ public function handlePlayerHotbar(PlayerHotbarPacket $packet) : bool{