Player: Simplified implementation of windows => window ID map

it is not necessary to reference the actual objects here. Doing so makes the implementation more confusing. Hashes are sufficient.
This commit is contained in:
Dylan K. Taylor 2018-01-22 18:58:19 +00:00
parent 69ddaacc28
commit e5ca22a9a6

View File

@ -207,8 +207,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
protected $xuid = "";
protected $windowCnt = 2;
/** @var \SplObjectStorage<Inventory> */
protected $windows;
/** @var int[] */
protected $windows = [];
/** @var Inventory[] */
protected $windowIndex = [];
/** @var bool[] */
@ -685,7 +685,6 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
*/
public function __construct(SourceInterface $interface, $clientID, string $ip, int $port){
$this->interface = $interface;
$this->windows = new \SplObjectStorage();
$this->perm = new PermissibleBase($this);
$this->namedtag = new CompoundTag();
$this->server = Server::getInstance();
@ -3327,7 +3326,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
}
$this->removeAllWindows(true);
$this->windows = null;
$this->windows = [];
$this->windowIndex = [];
$this->cursorInventory = null;
$this->craftingGrid = null;
@ -3723,13 +3722,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
* @return int
*/
public function getWindowId(Inventory $inventory) : int{
if($this->windows->contains($inventory)){
/** @var int $id */
$id = $this->windows[$inventory];
return $id;
}
return ContainerIds::NONE;
return $this->windows[spl_object_hash($inventory)] ?? ContainerIds::NONE;
}
/**
@ -3765,7 +3758,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$cnt = $forceId;
}
$this->windowIndex[$cnt] = $inventory;
$this->windows->attach($inventory, $cnt);
$this->windows[spl_object_hash($inventory)] = $cnt;
if($inventory->open($this)){
if($isPermanent){
$this->permanentWindows[$cnt] = true;
@ -3787,15 +3780,14 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
* @throws \BadMethodCallException if trying to remove a fixed inventory window without the `force` parameter as true
*/
public function removeWindow(Inventory $inventory, bool $force = false){
if($this->windows->contains($inventory)){
if(isset($this->windows[$hash = spl_object_hash($inventory)])){
/** @var int $id */
$id = $this->windows[$inventory];
$id = $this->windows[$hash];
if(!$force and isset($this->permanentWindows[$id])){
throw new \BadMethodCallException("Cannot remove fixed window $id (" . get_class($inventory) . ") from " . $this->getName());
}
$this->windows->detach($this->windowIndex[$id]);
unset($this->windowIndex[$id]);
unset($this->permanentWindows[$id]);
unset($this->windows[$hash], $this->windowIndex[$id], $this->permanentWindows[$id]);
}
$inventory->close($this);