From 75c0d8324cf9fc7b4065dcd936542804847faef4 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sun, 2 Nov 2014 13:26:58 +0100 Subject: [PATCH] Fixed some inventory events not firing on players --- src/pocketmine/Player.php | 21 ++++-------- src/pocketmine/entity/Entity.php | 4 ++- src/pocketmine/inventory/BaseInventory.php | 2 +- src/pocketmine/inventory/PlayerInventory.php | 36 +++++++++++++++----- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index ff7736e94a..deeeae67e5 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -140,7 +140,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ protected $sendIndex = 0; - public $blocked = true; + public $blocked = false; public $achievements = []; public $lastCorrect; /** @var SimpleTransactionGroup */ @@ -625,13 +625,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->level->requestChunk($X, $Z, $this, LevelProvider::ORDER_ZXY); } - - if(count($this->usedChunks) < 16 and $this->spawned === true){ - $this->blocked = true; - }elseif($this->spawned === true){ - $this->blocked = false; //TODO: reason of block to revert - } - if(count($this->usedChunks) >= 56 and $this->spawned === false){ $spawned = 0; foreach($this->usedChunks as $d){ @@ -646,8 +639,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->spawned = true; - $this->blocked = false; - $pk = new SetTimePacket(); $pk->time = $this->level->getTime(); $pk->started = $this->level->stopTime == false; @@ -1255,7 +1246,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } } - if($this->nextChunkOrderRun-- <= 0){ + if($this->nextChunkOrderRun-- <= 0 or $this->chunk === null){ $this->orderChunks(); } @@ -1561,7 +1552,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } break; case ProtocolInfo::USE_ITEM_PACKET: - if($this->spawned === false or $this->dead === true){ + if($this->spawned === false or $this->dead === true or $this->blocked){ break; } @@ -1569,7 +1560,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->craftingType = 0; - if(($this->spawned === false or $this->blocked === true or $this->dead === true) and $packet->face >= 0 and $packet->face <= 5){ + if($packet->face >= 0 and $packet->face <= 5){ $target = $this->level->getBlock($blockVector); $block = $target->getSide($packet->face); @@ -1798,7 +1789,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ break; case ProtocolInfo::INTERACT_PACKET: - if($this->spawned === false or $this->dead === true){ + if($this->spawned === false or $this->dead === true or $this->blocked){ break; } @@ -1816,7 +1807,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $cancelled = true; } - if($target instanceof Entity and $this->getGamemode() !== Player::VIEW and $this->blocked === false and $this->dead !== true and $target->dead !== true){ + if($target instanceof Entity and $this->getGamemode() !== Player::VIEW and $this->dead !== true and $target->dead !== true){ if($target instanceof DroppedItem or $target instanceof Arrow){ $this->kick("Attempting to attack an invalid entity"); $this->server->getLogger()->warning("Player ". $this->getName() ." tried to attack an invalid entity"); diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 0ddd9d4160..d517909f57 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -738,7 +738,9 @@ abstract class Entity extends Location implements Metadatable{ } $this->level->removeEntity($this); - $this->chunk->removeEntity($this); + if($this->chunk !== null){ + $this->chunk->removeEntity($this); + } $this->despawnFromAll(); if($this instanceof Player){ foreach($this->usedChunks as $index => $d){ diff --git a/src/pocketmine/inventory/BaseInventory.php b/src/pocketmine/inventory/BaseInventory.php index 7f5ccf342e..855244cc9c 100644 --- a/src/pocketmine/inventory/BaseInventory.php +++ b/src/pocketmine/inventory/BaseInventory.php @@ -235,7 +235,7 @@ abstract class BaseInventory implements Inventory{ for($i = 0; $i < $this->getSize(); ++$i){ $item = $this->getItem($i); foreach($slots as $index => $slot){ - if($item->getID() === Item::AIR){ + if($item->getID() === Item::AIR or $item->getCount() <= 0){ $amount = min($slot->getMaxStackSize(), $slot->getCount(), $this->getMaxStackSize()); $slot->setCount($slot->getCount() - $amount); $item = clone $slot; diff --git a/src/pocketmine/inventory/PlayerInventory.php b/src/pocketmine/inventory/PlayerInventory.php index ec090c9d6f..5db5c6651e 100644 --- a/src/pocketmine/inventory/PlayerInventory.php +++ b/src/pocketmine/inventory/PlayerInventory.php @@ -23,6 +23,7 @@ namespace pocketmine\inventory; use pocketmine\entity\Human; use pocketmine\event\entity\EntityArmorChangeEvent; +use pocketmine\event\entity\EntityInventoryChangeEvent; use pocketmine\event\player\PlayerItemHeldEvent; use pocketmine\item\Item; use pocketmine\network\protocol\ContainerSetContentPacket; @@ -200,6 +201,8 @@ class PlayerInventory extends BaseInventory{ public function setItem($index, Item $item, $source = null){ if($index < 0 or $index >= $this->size){ return false; + }elseif($item->getID() === 0){ + $this->clear($index, $source); } if($index >= $this->getSize()){ //Armor change @@ -211,15 +214,21 @@ class PlayerInventory extends BaseInventory{ return false; } $item = $ev->getNewItem(); + }else{ + Server::getInstance()->getPluginManager()->callEvent($ev = new EntityInventoryChangeEvent($this->getHolder(), $this->getItem($index), $item, $index)); + if($ev->isCancelled()){ + $this->sendArmorContents($this->getViewers()); + $this->sendContents($this->getViewers()); + + return false; + } + $item = $ev->getNewItem(); } - if($item->getID() === 0){ - $this->clear($index, $source); - }else{ - $old = $this->getItem($index); - $this->slots[$index] = clone $item; - $this->onSlotChange($index, $old, $source); - } + + $old = $this->getItem($index); + $this->slots[$index] = clone $item; + $this->onSlotChange($index, $old, $source); return true; } @@ -234,7 +243,16 @@ class PlayerInventory extends BaseInventory{ $this->sendArmorContents($this->getViewers()); $this->sendContents($this->getViewers()); - return; + return false; + } + $item = $ev->getNewItem(); + }else{ + Server::getInstance()->getPluginManager()->callEvent($ev = new EntityInventoryChangeEvent($this->getHolder(), $old, $item, $index)); + if($ev->isCancelled()){ + $this->sendArmorContents($this->getViewers()); + $this->sendContents($this->getViewers()); + + return false; } $item = $ev->getNewItem(); } @@ -246,6 +264,8 @@ class PlayerInventory extends BaseInventory{ $this->onSlotChange($index, $old, $source); } + + return true; } /**