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

View File

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