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

This commit is contained in:
Joonas Rikkonen
2018-08-09 12:08:47 +03:00
parent f1a8db5b47
commit 45939a9144
3 changed files with 28 additions and 8 deletions
@@ -12,6 +12,7 @@ namespace Barotrauma.Items.Components
{ {
public readonly ItemPrefab TargetItem; public readonly ItemPrefab TargetItem;
//TODO: refactor this (maybe make it a struct)
public readonly List<Tuple<ItemPrefab, int, float, bool>> RequiredItems; public readonly List<Tuple<ItemPrefab, int, float, bool>> RequiredItems;
public readonly float RequiredTime; public readonly float RequiredTime;
@@ -1659,8 +1659,25 @@ namespace Barotrauma
{ {
msg.Write(ParentInventory.Owner.ID); msg.Write(ParentInventory.Owner.ID);
int index = ParentInventory.FindIndex(this); //find the index of the ItemContainer this item is inside to get the item to
msg.Write(index < 0 ? (byte)255 : (byte)index); //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; byte teamID = 0;
@@ -1698,10 +1715,12 @@ namespace Barotrauma
Vector2 pos = Vector2.Zero; Vector2 pos = Vector2.Zero;
Submarine sub = null; Submarine sub = null;
int itemContainerIndex = -1;
int inventorySlotIndex = -1; int inventorySlotIndex = -1;
if (inventoryId > 0) if (inventoryId > 0)
{ {
itemContainerIndex = msg.ReadByte();
inventorySlotIndex = msg.ReadByte(); inventorySlotIndex = msg.ReadByte();
} }
else else
@@ -1741,10 +1760,9 @@ namespace Barotrauma
} }
else if (inventoryOwner is Item) else if (inventoryOwner is Item)
{ {
var containers = (inventoryOwner as Item).GetComponents<Items.Components.ItemContainer>(); if ((inventoryOwner as Item).components[itemContainerIndex] is ItemContainer container)
if (containers != null && containers.Any())
{ {
inventory = containers.Last().Inventory; inventory = container.Inventory;
} }
} }
} }
@@ -49,7 +49,6 @@ namespace Barotrauma
public Entity Spawn() public Entity Spawn()
{ {
Item spawnedItem = null; Item spawnedItem = null;
if (Inventory != null) if (Inventory != null)
{ {
spawnedItem = new Item(Prefab, Vector2.Zero, null); spawnedItem = new Item(Prefab, Vector2.Zero, null);
@@ -59,8 +58,6 @@ namespace Barotrauma
{ {
spawnedItem = new Item(Prefab, Position, Submarine); spawnedItem = new Item(Prefab, Position, Submarine);
} }
spawnedItem.Condition = Condition;
return spawnedItem; return spawnedItem;
} }
} }
@@ -159,6 +156,10 @@ namespace Barotrauma
if (spawnedEntity != null) if (spawnedEntity != null)
{ {
CreateNetworkEvent(spawnedEntity, false); CreateNetworkEvent(spawnedEntity, false);
if (spawnedEntity is Item)
{
((Item)spawnedEntity).Condition = ((ItemSpawnInfo)entitySpawnInfo).Condition;
}
} }
} }