Merge branch 'minor-next' into major-next

This commit is contained in:
Dylan K. Taylor
2023-03-29 23:22:02 +01:00
8 changed files with 81 additions and 13 deletions

View File

@ -374,13 +374,21 @@ class InGamePacketHandler extends PacketHandler{
}
private function handleNormalTransaction(NormalTransactionData $data, int $itemStackRequestId) : bool{
//When the ItemStackRequest system is used, this transaction type is only used for dropping items by pressing Q.
//When the ItemStackRequest system is used, this transaction type is used for dropping items by pressing Q.
//I don't know why they don't just use ItemStackRequest for that too, which already supports dropping items by
//clicking them outside an open inventory menu, but for now it is what it is.
//Fortunately, this means we can be extremely strict about the validation criteria.
//Fortunately, this means we can be much stricter about the validation criteria.
if(count($data->getActions()) > 2){
throw new PacketHandlingException("Expected exactly 2 actions for dropping an item");
$actionCount = count($data->getActions());
if($actionCount > 2){
if($actionCount > 5){
throw new PacketHandlingException("Too many actions ($actionCount) in normal inventory transaction");
}
//Due to a bug in the game, this transaction type is still sent when a player edits a book. We don't need
//these transactions for editing books, since we have BookEditPacket, so we can just ignore them.
$this->session->getLogger()->debug("Ignoring normal inventory transaction with $actionCount actions (drop-item should have exactly 2 actions)");
return false;
}
$sourceSlot = null;
@ -398,11 +406,13 @@ class InGamePacketHandler extends PacketHandler{
$sourceSlot = $networkInventoryAction->inventorySlot;
$clientItemStack = $networkInventoryAction->oldItem->getItemStack();
}else{
throw new PacketHandlingException("Unexpected action type in drop item transaction");
$this->session->getLogger()->debug("Unexpected inventory action type $networkInventoryAction->sourceType in drop item transaction");
return false;
}
}
if($sourceSlot === null || $clientItemStack === null || $droppedCount === null){
throw new PacketHandlingException("Missing information in drop item transaction, need source slot, client item stack and dropped count");
$this->session->getLogger()->debug("Missing information in drop item transaction, need source slot, client item stack and dropped count");
return false;
}
$inventory = $this->player->getInventory();
@ -412,6 +422,9 @@ class InGamePacketHandler extends PacketHandler{
}
$sourceSlotItem = $inventory->getItem($sourceSlot);
if($sourceSlotItem->getCount() < $droppedCount){
return false;
}
$serverItemStack = TypeConverter::getInstance()->coreItemStackToNet($sourceSlotItem);
//because the client doesn't tell us the expected itemstack ID, we have to deep-compare our known
//itemstack info with the one the client sent. This is costly, but we don't have any other option :(
@ -542,8 +555,17 @@ class InGamePacketHandler extends PacketHandler{
}
private function handleSingleItemStackRequest(ItemStackRequest $request) : ItemStackResponse{
if(count($request->getActions()) > 20){
//TODO: we can probably lower this limit, but this will do for now
if(count($request->getActions()) > 60){
//recipe book auto crafting can affect all slots of the inventory when consuming inputs or producing outputs
//this means there could be as many as 50 CraftingConsumeInput actions or Place (taking the result) actions
//in a single request (there are certain ways items can be arranged which will result in the same stack
//being taken from multiple times, but this is behaviour with a calculable limit)
//this means there SHOULD be AT MOST 53 actions in a single request, but 60 is a nice round number.
//n64Stacks = ?
//n1Stacks = 45 - n64Stacks
//nItemsRequiredFor1Craft = 9
//nResults = floor((n1Stacks + (n64Stacks * 64)) / nItemsRequiredFor1Craft)
//nTakeActionsTotal = floor(64 / nResults) + max(1, 64 % nResults) + ((nResults * nItemsRequiredFor1Craft) - (n64Stacks * 64))
throw new PacketHandlingException("Too many actions in ItemStackRequest");
}
$executor = new ItemStackRequestExecutor($this->player, $this->inventoryManager, $request);