From 45939a91440f1a3cd0ef2bc7596e0a4755fc725d Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Thu, 9 Aug 2018 12:08:47 +0300 Subject: [PATCH] Fixed oxygen tank & welding fuel tank crafting causing everyone to desync. The item condition NetEntityEvent was created before the spawn event of the item, preventing clients from reading the condition event because the item doesn't exist yet. Closes #617 --- .../Items/Components/Machines/Fabricator.cs | 1 + .../BarotraumaShared/Source/Items/Item.cs | 28 +++++++++++++++---- .../Source/Networking/EntitySpawner.cs | 7 +++-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/Machines/Fabricator.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/Machines/Fabricator.cs index ca08ce693..9c1693851 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/Machines/Fabricator.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/Machines/Fabricator.cs @@ -12,6 +12,7 @@ namespace Barotrauma.Items.Components { public readonly ItemPrefab TargetItem; + //TODO: refactor this (maybe make it a struct) public readonly List> RequiredItems; public readonly float RequiredTime; diff --git a/Barotrauma/BarotraumaShared/Source/Items/Item.cs b/Barotrauma/BarotraumaShared/Source/Items/Item.cs index bad381cb9..bd0b18f85 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Item.cs @@ -1659,8 +1659,25 @@ namespace Barotrauma { msg.Write(ParentInventory.Owner.ID); - int index = ParentInventory.FindIndex(this); - msg.Write(index < 0 ? (byte)255 : (byte)index); + //find the index of the ItemContainer this item is inside to get the item to + //spawn in the correct inventory in multi-inventory items like fabricators + byte containerIndex = 0; + if (Container != null) + { + for (int i = 0; i < Container.components.Count; i++) + { + if (Container.components[i] is ItemContainer container && + container.Inventory == ParentInventory) + { + containerIndex = (byte)i; + break; + } + } + } + msg.Write(containerIndex); + + int slotIndex = ParentInventory.FindIndex(this); + msg.Write(slotIndex < 0 ? (byte)255 : (byte)slotIndex); } byte teamID = 0; @@ -1698,10 +1715,12 @@ namespace Barotrauma Vector2 pos = Vector2.Zero; Submarine sub = null; + int itemContainerIndex = -1; int inventorySlotIndex = -1; if (inventoryId > 0) { + itemContainerIndex = msg.ReadByte(); inventorySlotIndex = msg.ReadByte(); } else @@ -1741,10 +1760,9 @@ namespace Barotrauma } else if (inventoryOwner is Item) { - var containers = (inventoryOwner as Item).GetComponents(); - if (containers != null && containers.Any()) + if ((inventoryOwner as Item).components[itemContainerIndex] is ItemContainer container) { - inventory = containers.Last().Inventory; + inventory = container.Inventory; } } } diff --git a/Barotrauma/BarotraumaShared/Source/Networking/EntitySpawner.cs b/Barotrauma/BarotraumaShared/Source/Networking/EntitySpawner.cs index 14bd2ad22..4dc229269 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/EntitySpawner.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/EntitySpawner.cs @@ -49,7 +49,6 @@ namespace Barotrauma public Entity Spawn() { Item spawnedItem = null; - if (Inventory != null) { spawnedItem = new Item(Prefab, Vector2.Zero, null); @@ -59,8 +58,6 @@ namespace Barotrauma { spawnedItem = new Item(Prefab, Position, Submarine); } - spawnedItem.Condition = Condition; - return spawnedItem; } } @@ -159,6 +156,10 @@ namespace Barotrauma if (spawnedEntity != null) { CreateNetworkEvent(spawnedEntity, false); + if (spawnedEntity is Item) + { + ((Item)spawnedEntity).Condition = ((ItemSpawnInfo)entitySpawnInfo).Condition; + } } }