Use more appropriate exceptions in the protocol layer

This commit is contained in:
Dylan K. Taylor 2019-01-03 17:57:06 +00:00
parent 504cc3bf8b
commit 0f941410f6
7 changed files with 21 additions and 8 deletions

View File

@ -111,7 +111,7 @@ class BatchPacket extends DataPacket{
$pk = PacketPool::getPacket($buf);
if(!$pk->canBeBatched()){
throw new \InvalidArgumentException("Received invalid " . get_class($pk) . " inside BatchPacket");
throw new \UnexpectedValueException("Received invalid " . get_class($pk) . " inside BatchPacket");
}
$session->handleDataPacket($pk);

View File

@ -109,7 +109,7 @@ class BookEditPacket extends DataPacket{
$this->putString($this->xuid);
break;
default:
throw new \UnexpectedValueException("Unknown book edit type $this->type!");
throw new \InvalidArgumentException("Unknown book edit type $this->type!");
}
}

View File

@ -91,7 +91,7 @@ class ClientboundMapItemDataPacket extends DataPacket{
}elseif($object->type === MapTrackedObject::TYPE_ENTITY){
$object->entityUniqueId = $this->getEntityUniqueId();
}else{
throw new \UnexpectedValueException("Unknown map object type");
throw new \UnexpectedValueException("Unknown map object type $object->type");
}
$this->trackedEntities[] = $object;
}
@ -163,7 +163,7 @@ class ClientboundMapItemDataPacket extends DataPacket{
}elseif($object->type === MapTrackedObject::TYPE_ENTITY){
$this->putEntityUniqueId($object->entityUniqueId);
}else{
throw new \UnexpectedValueException("Unknown map object type");
throw new \InvalidArgumentException("Unknown map object type $object->type");
}
}

View File

@ -68,12 +68,20 @@ abstract class DataPacket extends NetworkBinaryStream{
return false;
}
/**
* @throws \OutOfBoundsException
* @throws \UnexpectedValueException
*/
public function decode(){
$this->offset = 0;
$this->decodeHeader();
$this->decodePayload();
}
/**
* @throws \OutOfBoundsException
* @throws \UnexpectedValueException
*/
protected function decodeHeader(){
$pid = $this->getUnsignedVarInt();
if($pid !== static::NETWORK_ID){
@ -83,6 +91,9 @@ abstract class DataPacket extends NetworkBinaryStream{
/**
* Note for plugin developers: If you're adding your own packets, you should perform decoding in here.
*
* @throws \OutOfBoundsException
* @throws \UnexpectedValueException
*/
protected function decodePayload(){

View File

@ -147,7 +147,7 @@ class InventoryTransactionPacket extends DataPacket{
$this->putVector3($this->trData->headPos);
break;
default:
throw new \UnexpectedValueException("Unknown transaction type $this->transactionType");
throw new \InvalidArgumentException("Unknown transaction type $this->transactionType");
}
}

View File

@ -81,7 +81,7 @@ class SetScorePacket extends DataPacket{
$this->putString($entry->customName);
break;
default:
throw new \UnexpectedValueException("Unknown entry type $entry->type");
throw new \InvalidArgumentException("Unknown entry type $entry->type");
}
}
}

View File

@ -151,7 +151,7 @@ class NetworkInventoryAction{
$packet->putVarInt($this->windowId);
break;
default:
throw new \UnexpectedValueException("Unknown inventory action source type $this->sourceType");
throw new \InvalidArgumentException("Unknown inventory action source type $this->sourceType");
}
$packet->putUnsignedVarInt($this->inventorySlot);
@ -163,6 +163,8 @@ class NetworkInventoryAction{
* @param Player $player
*
* @return InventoryAction|null
*
* @throws \UnexpectedValueException
*/
public function createInventoryAction(Player $player){
switch($this->sourceType){
@ -172,7 +174,7 @@ 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");
throw new \UnexpectedValueException("Player " . $player->getName() . " has no open container with window ID $this->windowId");
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!");