Fixed some inventory events not firing on players

This commit is contained in:
Shoghi Cervantes
2014-11-02 13:26:58 +01:00
parent da4334f06b
commit 75c0d8324c
4 changed files with 38 additions and 25 deletions

View File

@ -140,7 +140,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
protected $sendIndex = 0; protected $sendIndex = 0;
public $blocked = true; public $blocked = false;
public $achievements = []; public $achievements = [];
public $lastCorrect; public $lastCorrect;
/** @var SimpleTransactionGroup */ /** @var SimpleTransactionGroup */
@ -625,13 +625,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->level->requestChunk($X, $Z, $this, LevelProvider::ORDER_ZXY); $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){ if(count($this->usedChunks) >= 56 and $this->spawned === false){
$spawned = 0; $spawned = 0;
foreach($this->usedChunks as $d){ foreach($this->usedChunks as $d){
@ -646,8 +639,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->spawned = true; $this->spawned = true;
$this->blocked = false;
$pk = new SetTimePacket(); $pk = new SetTimePacket();
$pk->time = $this->level->getTime(); $pk->time = $this->level->getTime();
$pk->started = $this->level->stopTime == false; $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(); $this->orderChunks();
} }
@ -1561,7 +1552,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
} }
break; break;
case ProtocolInfo::USE_ITEM_PACKET: 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; break;
} }
@ -1569,7 +1560,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->craftingType = 0; $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); $target = $this->level->getBlock($blockVector);
$block = $target->getSide($packet->face); $block = $target->getSide($packet->face);
@ -1798,7 +1789,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
break; break;
case ProtocolInfo::INTERACT_PACKET: case ProtocolInfo::INTERACT_PACKET:
if($this->spawned === false or $this->dead === true){ if($this->spawned === false or $this->dead === true or $this->blocked){
break; break;
} }
@ -1816,7 +1807,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$cancelled = true; $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){ if($target instanceof DroppedItem or $target instanceof Arrow){
$this->kick("Attempting to attack an invalid entity"); $this->kick("Attempting to attack an invalid entity");
$this->server->getLogger()->warning("Player ". $this->getName() ." tried to attack an invalid entity"); $this->server->getLogger()->warning("Player ". $this->getName() ." tried to attack an invalid entity");

View File

@ -738,7 +738,9 @@ abstract class Entity extends Location implements Metadatable{
} }
$this->level->removeEntity($this); $this->level->removeEntity($this);
$this->chunk->removeEntity($this); if($this->chunk !== null){
$this->chunk->removeEntity($this);
}
$this->despawnFromAll(); $this->despawnFromAll();
if($this instanceof Player){ if($this instanceof Player){
foreach($this->usedChunks as $index => $d){ foreach($this->usedChunks as $index => $d){

View File

@ -235,7 +235,7 @@ abstract class BaseInventory implements Inventory{
for($i = 0; $i < $this->getSize(); ++$i){ for($i = 0; $i < $this->getSize(); ++$i){
$item = $this->getItem($i); $item = $this->getItem($i);
foreach($slots as $index => $slot){ 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()); $amount = min($slot->getMaxStackSize(), $slot->getCount(), $this->getMaxStackSize());
$slot->setCount($slot->getCount() - $amount); $slot->setCount($slot->getCount() - $amount);
$item = clone $slot; $item = clone $slot;

View File

@ -23,6 +23,7 @@ namespace pocketmine\inventory;
use pocketmine\entity\Human; use pocketmine\entity\Human;
use pocketmine\event\entity\EntityArmorChangeEvent; use pocketmine\event\entity\EntityArmorChangeEvent;
use pocketmine\event\entity\EntityInventoryChangeEvent;
use pocketmine\event\player\PlayerItemHeldEvent; use pocketmine\event\player\PlayerItemHeldEvent;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\network\protocol\ContainerSetContentPacket; use pocketmine\network\protocol\ContainerSetContentPacket;
@ -200,6 +201,8 @@ class PlayerInventory extends BaseInventory{
public function setItem($index, Item $item, $source = null){ public function setItem($index, Item $item, $source = null){
if($index < 0 or $index >= $this->size){ if($index < 0 or $index >= $this->size){
return false; return false;
}elseif($item->getID() === 0){
$this->clear($index, $source);
} }
if($index >= $this->getSize()){ //Armor change if($index >= $this->getSize()){ //Armor change
@ -211,15 +214,21 @@ class PlayerInventory extends BaseInventory{
return false; return false;
} }
$item = $ev->getNewItem(); $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); $old = $this->getItem($index);
}else{ $this->slots[$index] = clone $item;
$old = $this->getItem($index); $this->onSlotChange($index, $old, $source);
$this->slots[$index] = clone $item;
$this->onSlotChange($index, $old, $source);
}
return true; return true;
} }
@ -234,7 +243,16 @@ class PlayerInventory extends BaseInventory{
$this->sendArmorContents($this->getViewers()); $this->sendArmorContents($this->getViewers());
$this->sendContents($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(); $item = $ev->getNewItem();
} }
@ -246,6 +264,8 @@ class PlayerInventory extends BaseInventory{
$this->onSlotChange($index, $old, $source); $this->onSlotChange($index, $old, $source);
} }
return true;
} }
/** /**