InventoryManager: Do not sync slots the client correctly predicted during using items

closes #4825
This commit is contained in:
Dylan K. Taylor 2022-02-22 16:45:21 +00:00
parent f04099c5de
commit 4f25ab10e9
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 16 additions and 0 deletions

View File

@ -50,6 +50,7 @@ use pocketmine\network\mcpe\protocol\types\BlockPosition;
use pocketmine\network\mcpe\protocol\types\inventory\ContainerIds;
use pocketmine\network\mcpe\protocol\types\inventory\CreativeContentEntry;
use pocketmine\network\mcpe\protocol\types\inventory\ItemStackWrapper;
use pocketmine\network\mcpe\protocol\types\inventory\NetworkInventoryAction;
use pocketmine\network\mcpe\protocol\types\inventory\WindowTypes;
use pocketmine\player\Player;
use pocketmine\utils\AssumptionFailedError;
@ -145,6 +146,18 @@ class InventoryManager{
}
}
/**
* @param NetworkInventoryAction[] $networkInventoryActions
*/
public function addPredictedSlotChanges(array $networkInventoryActions) : void{
foreach($networkInventoryActions as $action){
if($action->sourceType === NetworkInventoryAction::SOURCE_CONTAINER && isset($this->windowMap[$action->windowId])){
//this won't cover stuff like crafting grid due to too much magic
$this->initiatedSlotChanges[$action->windowId][$action->inventorySlot] = TypeConverter::getInstance()->netItemStackToCore($action->newItem->getItemStack());
}
}
}
public function onCurrentWindowChange(Inventory $inventory) : void{
$this->onCurrentWindowRemove();
$this->add($this->lastInventoryNetworkId = max(ContainerIds::FIRST, ($this->lastInventoryNetworkId + 1) % self::RESERVED_WINDOW_ID_RANGE_START), $inventory);

View File

@ -411,6 +411,7 @@ class InGamePacketHandler extends PacketHandler{
private function handleUseItemTransaction(UseItemTransactionData $data) : bool{
$this->player->selectHotbarSlot($data->getHotbarSlot());
$this->inventoryManager->addPredictedSlotChanges($data->getActions());
switch($data->getActionType()){
case UseItemTransactionData::ACTION_CLICK_BLOCK:
@ -500,6 +501,7 @@ class InGamePacketHandler extends PacketHandler{
}
$this->player->selectHotbarSlot($data->getHotbarSlot());
$this->inventoryManager->addPredictedSlotChanges($data->getActions());
//TODO: use transactiondata for rollbacks here
switch($data->getActionType()){
@ -520,6 +522,7 @@ class InGamePacketHandler extends PacketHandler{
private function handleReleaseItemTransaction(ReleaseItemTransactionData $data) : bool{
$this->player->selectHotbarSlot($data->getHotbarSlot());
$this->inventoryManager->addPredictedSlotChanges($data->getActions());
//TODO: use transactiondata for rollbacks here (resending entire inventory is very wasteful)
switch($data->getActionType()){