Return null on unmatched inventory action and log details

This commit is contained in:
Dylan K. Taylor 2017-09-20 18:38:14 +01:00
parent 6897cb4774
commit fd7fb10223
2 changed files with 17 additions and 17 deletions

View File

@ -158,7 +158,6 @@ use pocketmine\resourcepacks\ResourcePack;
use pocketmine\tile\ItemFrame;
use pocketmine\tile\Spawnable;
use pocketmine\tile\Tile;
use pocketmine\utils\MainLogger;
use pocketmine\utils\TextFormat;
use pocketmine\utils\UUID;
@ -2174,14 +2173,15 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
/** @var InventoryAction[] $actions */
$actions = [];
foreach($packet->actions as $networkInventoryAction){
try{
$action = $networkInventoryAction->createInventoryAction($this);
$actions[] = $action;
}catch(\Throwable $e){
MainLogger::getLogger()->debug("Unhandled inventory action from " . $this->getName() . ": " . $e->getMessage());
$action = $networkInventoryAction->createInventoryAction($this);
if($action === null){
$this->server->getLogger()->debug("Unmatched inventory action from " . $this->getName() . ": " . json_encode($networkInventoryAction));
$this->sendAllInventories();
return false;
}
$actions[] = $action;
}
switch($packet->transactionType){
@ -2196,7 +2196,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
}
}
MainLogger::getLogger()->debug("Failed to execute inventory transaction from " . $this->getName() . " with actions: " . json_encode($packet->actions));
$this->server->getLogger()->debug("Failed to execute inventory transaction from " . $this->getName() . " with actions: " . json_encode($packet->actions));
//TODO: check more stuff that might need reversion
return false; //oops!

View File

@ -146,7 +146,7 @@ class NetworkInventoryAction{
/**
* @param Player $player
*
* @return InventoryAction
* @return InventoryAction|null
*/
public function createInventoryAction(Player $player){
switch($this->sourceType){
@ -162,13 +162,13 @@ class NetworkInventoryAction{
return new SlotChangeAction($window, $this->inventorySlot, $this->oldItem, $this->newItem);
}
throw new \InvalidStateException("Player " . $player->getName() . " has no open container with window ID $this->windowId");
return null;
case self::SOURCE_WORLD:
if($this->inventorySlot !== self::ACTION_MAGIC_SLOT_DROP_ITEM){
throw new \UnexpectedValueException("Only expecting drop-item world actions from the client!");
if($this->inventorySlot === self::ACTION_MAGIC_SLOT_DROP_ITEM){
return new DropItemAction($this->oldItem, $this->newItem);
}
return new DropItemAction($this->oldItem, $this->newItem);
return null;
case self::SOURCE_CREATIVE:
switch($this->inventorySlot){
case self::ACTION_MAGIC_SLOT_CREATIVE_DELETE_ITEM:
@ -178,7 +178,7 @@ class NetworkInventoryAction{
$type = CreativeInventoryAction::TYPE_CREATE_ITEM;
break;
default:
throw new \UnexpectedValueException("Unexpected creative action type $this->inventorySlot");
return null;
}
@ -198,16 +198,16 @@ class NetworkInventoryAction{
//DROP_CONTENTS doesn't bother telling us what slot the item is in, so we find it ourselves
$inventorySlot = $window->first($this->oldItem, true);
if($inventorySlot === -1){
throw new \InvalidStateException("Fake container " . get_class($window) . " for " . $player->getName() . " does not contain $this->oldItem");
return null;
}
return new SlotChangeAction($window, $inventorySlot, $this->oldItem, $this->newItem);
}
//TODO: more stuff
throw new \UnexpectedValueException("Player " . $player->getName() . " has no open container with window ID $this->windowId");
default:
throw new \UnexpectedValueException("Unknown inventory source type $this->sourceType");
return null;
}
return null;
}
}