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