Clean up inventory content syncing, fixes #5441

these remnants should have been cleaned up in 4.11, but I somehow managed to skip over them.
This commit is contained in:
Dylan K. Taylor 2022-12-04 23:05:30 +00:00
parent 774e23137e
commit b5cfab497d
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 9 additions and 28 deletions

View File

@ -191,8 +191,10 @@ class InventoryManager{
*/ */
public function addPredictedSlotChanges(array $networkInventoryActions) : void{ public function addPredictedSlotChanges(array $networkInventoryActions) : void{
foreach($networkInventoryActions as $action){ foreach($networkInventoryActions as $action){
if($action->sourceType === NetworkInventoryAction::SOURCE_CONTAINER && isset($this->windowMap[$action->windowId])){ if($action->sourceType === NetworkInventoryAction::SOURCE_CONTAINER && (
//this won't cover stuff like crafting grid due to too much magic isset($this->windowMap[$action->windowId]) ||
($action->windowId === ContainerIds::UI && isset($this->complexSlotToWindowMap[$action->inventorySlot]))
)){
try{ try{
$item = TypeConverter::getInstance()->netItemStackToCore($action->newItem->getItemStack()); $item = TypeConverter::getInstance()->netItemStackToCore($action->newItem->getItemStack());
}catch(TypeConversionException $e){ }catch(TypeConversionException $e){

View File

@ -324,9 +324,7 @@ class InGamePacketHandler extends PacketHandler{
$result = $this->handleReleaseItemTransaction($packet->trData); $result = $this->handleReleaseItemTransaction($packet->trData);
} }
if(!$result){ if($this->craftingTransaction === null){ //don't sync if we're waiting to complete a crafting transaction
$this->inventoryManager->syncAll();
}else{
$this->inventoryManager->syncMismatchedPredictedSlotChanges(); $this->inventoryManager->syncMismatchedPredictedSlotChanges();
} }
return $result; return $result;
@ -361,6 +359,7 @@ class InGamePacketHandler extends PacketHandler{
return false; return false;
} }
} }
$this->inventoryManager->addPredictedSlotChanges($data->getActions());
if($isCraftingPart){ if($isCraftingPart){
if($this->craftingTransaction === null){ if($this->craftingTransaction === null){
@ -381,15 +380,9 @@ class InGamePacketHandler extends PacketHandler{
} }
$this->player->setUsingItem(false); $this->player->setUsingItem(false);
try{ try{
$this->inventoryManager->onTransactionStart($this->craftingTransaction);
$this->craftingTransaction->execute(); $this->craftingTransaction->execute();
}catch(TransactionException $e){ }catch(TransactionException $e){
$this->session->getLogger()->debug("Failed to execute crafting transaction: " . $e->getMessage()); $this->session->getLogger()->debug("Failed to execute crafting transaction: " . $e->getMessage());
//TODO: only sync slots that the client tried to change
foreach($this->craftingTransaction->getInventories() as $inventory){
$this->inventoryManager->syncContents($inventory);
}
return false; return false;
}finally{ }finally{
$this->craftingTransaction = null; $this->craftingTransaction = null;
@ -409,18 +402,12 @@ class InGamePacketHandler extends PacketHandler{
$this->player->setUsingItem(false); $this->player->setUsingItem(false);
$transaction = new InventoryTransaction($this->player, $actions); $transaction = new InventoryTransaction($this->player, $actions);
$this->inventoryManager->onTransactionStart($transaction);
try{ try{
$transaction->execute(); $transaction->execute();
}catch(TransactionException $e){ }catch(TransactionException $e){
$logger = $this->session->getLogger(); $logger = $this->session->getLogger();
$logger->debug("Failed to execute inventory transaction: " . $e->getMessage()); $logger->debug("Failed to execute inventory transaction: " . $e->getMessage());
$logger->debug("Actions: " . json_encode($data->getActions())); $logger->debug("Actions: " . json_encode($data->getActions()));
foreach($transaction->getInventories() as $inventory){
$this->inventoryManager->syncContents($inventory);
}
return false; return false;
} }
} }
@ -430,7 +417,6 @@ class InGamePacketHandler extends PacketHandler{
private function handleUseItemTransaction(UseItemTransactionData $data) : bool{ private function handleUseItemTransaction(UseItemTransactionData $data) : bool{
$this->player->selectHotbarSlot($data->getHotbarSlot()); $this->player->selectHotbarSlot($data->getHotbarSlot());
$this->inventoryManager->addPredictedSlotChanges($data->getActions());
switch($data->getActionType()){ switch($data->getActionType()){
case UseItemTransactionData::ACTION_CLICK_BLOCK: case UseItemTransactionData::ACTION_CLICK_BLOCK:
@ -516,9 +502,7 @@ class InGamePacketHandler extends PacketHandler{
} }
$this->player->selectHotbarSlot($data->getHotbarSlot()); $this->player->selectHotbarSlot($data->getHotbarSlot());
$this->inventoryManager->addPredictedSlotChanges($data->getActions());
//TODO: use transactiondata for rollbacks here
switch($data->getActionType()){ switch($data->getActionType()){
case UseItemOnEntityTransactionData::ACTION_INTERACT: case UseItemOnEntityTransactionData::ACTION_INTERACT:
$this->player->interactEntity($target, $data->getClickPosition()); $this->player->interactEntity($target, $data->getClickPosition());
@ -533,14 +517,9 @@ class InGamePacketHandler extends PacketHandler{
private function handleReleaseItemTransaction(ReleaseItemTransactionData $data) : bool{ private function handleReleaseItemTransaction(ReleaseItemTransactionData $data) : bool{
$this->player->selectHotbarSlot($data->getHotbarSlot()); $this->player->selectHotbarSlot($data->getHotbarSlot());
$this->inventoryManager->addPredictedSlotChanges($data->getActions());
//TODO: use transactiondata for rollbacks here (resending entire inventory is very wasteful) if($data->getActionType() == ReleaseItemTransactionData::ACTION_RELEASE){
switch($data->getActionType()){ $this->player->releaseHeldItem();
case ReleaseItemTransactionData::ACTION_RELEASE:
if(!$this->player->releaseHeldItem()){
$this->inventoryManager->syncContents($this->player->getInventory());
}
return true; return true;
} }