From 5460ccf41c3b044dec8acf28892cb1b9dcc5514c Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Fri, 23 May 2014 23:12:15 +0200 Subject: [PATCH] Server can be joined, again! (fixed weak references and blocks) --- src/pocketmine/Player.php | 11 ++++++++--- src/pocketmine/block/Block.php | 2 +- src/pocketmine/entity/Human.php | 4 ++++ src/pocketmine/inventory/BaseInventory.php | 9 +-------- src/pocketmine/inventory/PlayerInventory.php | 8 ++++---- src/pocketmine/level/Level.php | 9 +++------ 6 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 527b9a1fb..f4e17b6bd 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -2431,17 +2431,22 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ * Returns the created/existing window id * * @param Inventory $inventory + * @param int $forceId * * @return int */ - public function addWindow(Inventory $inventory){ + public function addWindow(Inventory $inventory, $forceId = null){ if($this->windows->contains($inventory)){ return $this->windows[$inventory]; } - $this->windowCnt = $cnt = max(2, ++$this->windowCnt % 99); + if($forceId === null){ + $this->windowCnt = $cnt = max(2, ++$this->windowCnt % 99); + }else{ + $cnt = (int) $forceId; + } $this->windowIndex[$cnt] = $inventory; $this->windows->attach($inventory, $cnt); - $this->inventory->onOpen($this); + $inventory->onOpen($this); return $cnt; } diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index 2927ede3f..53a2f5f08 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -682,7 +682,7 @@ abstract class Block extends Position implements Metadatable{ return $this->getLevel()->getBlock($v); } - return $v; + return Block::get(Item::AIR, 0, $v); } /** diff --git a/src/pocketmine/entity/Human.php b/src/pocketmine/entity/Human.php index 65d9896af..b207ae247 100644 --- a/src/pocketmine/entity/Human.php +++ b/src/pocketmine/entity/Human.php @@ -47,6 +47,10 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ protected function initEntity(){ $this->inventory = new PlayerInventory($this); + if($this instanceof Player){ + $this->addWindow($this->inventory, 0); + } + if(isset($this->namedtag->NameTag)){ $this->nameTag = $this->namedtag["NameTag"]; diff --git a/src/pocketmine/inventory/BaseInventory.php b/src/pocketmine/inventory/BaseInventory.php index 746eada30..b043bc238 100644 --- a/src/pocketmine/inventory/BaseInventory.php +++ b/src/pocketmine/inventory/BaseInventory.php @@ -59,12 +59,6 @@ abstract class BaseInventory implements Inventory{ $this->holder = $holder; $this->viewers = new \SplObjectStorage(); - //A holder can be a plugin, or an entity - if($this->holder instanceof Player){ - $this->holder->addWindow($this, 0); - } - - $this->type = $type; if($overrideSize !== null){ $this->size = (int) $overrideSize; @@ -138,13 +132,12 @@ abstract class BaseInventory implements Inventory{ Server::getInstance()->getPluginManager()->callEvent($ev = new EntityInventoryChangeEvent($holder, $this->getItem($index), $item, $index)); if($ev->isCancelled()){ $this->sendContents($this->getViewers()); - return false; } $item = $ev->getNewItem(); } - $old = $this->slots[$index]; + $old = $this->getItem($index); $this->slots[$index] = clone $item; $this->onSlotChange($index, $old); diff --git a/src/pocketmine/inventory/PlayerInventory.php b/src/pocketmine/inventory/PlayerInventory.php index ad78d5f27..f2068e581 100644 --- a/src/pocketmine/inventory/PlayerInventory.php +++ b/src/pocketmine/inventory/PlayerInventory.php @@ -211,16 +211,16 @@ class PlayerInventory extends BaseInventory{ if($index >= $this->getSize()){ //Armor change Server::getInstance()->getPluginManager()->callEvent($ev = new EntityArmorChangeEvent($this->getHolder(), $this->getItem($index), $item, $index)); - if($ev->isCancelled()){ - $this->sendArmorContents($this); - $this->sendContents($this); + if($ev->isCancelled() and $this->getHolder() instanceof Player){ + $this->sendArmorContents($this->getHolder()); + $this->sendContents($this->getHolder()); return false; } $item = $ev->getNewItem(); } - $old = $this->slots[$index]; + $old = $this->getItem($index); $this->slots[$index] = clone $item; $this->onSlotChange($index, $old); diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 620771963..618053864 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -479,14 +479,14 @@ class Level{ } /** + * @deprecated + * * @param Vector3 $pos * * @return Block */ public function getBlockRaw(Vector3 $pos){ - $b = $this->level->getBlock($pos->x, $pos->y, $pos->z); - - return Block::get($b[0], $b[1], new Position($pos->x, $pos->y, $pos->z, $this)); + return $this->getBlock($pos); } /** @@ -495,9 +495,6 @@ class Level{ * @return bool|Block */ public function getBlock(Vector3 $pos){ - if($pos instanceof Position and $pos->level !== $this){ - return false; - } $b = $this->level->getBlock($pos->x, $pos->y, $pos->z); return Block::get($b[0], $b[1], new Position($pos->x, $pos->y, $pos->z, $this));