|
| 1 | +using System; |
1 | 2 | using System.Collections;
|
2 | 3 | using NitroxClient.Communication;
|
3 | 4 | using NitroxClient.GameLogic.Helper;
|
4 | 5 | using NitroxClient.GameLogic.Spawning.Abstract;
|
| 6 | +using NitroxClient.GameLogic.Spawning.Metadata; |
5 | 7 | using NitroxClient.GameLogic.Spawning.WorldEntities;
|
6 | 8 | using NitroxClient.MonoBehaviours;
|
7 | 9 | using NitroxClient.Unity.Helper;
|
8 | 10 | using NitroxModel.DataStructures.GameLogic.Entities;
|
| 11 | +using NitroxModel.DataStructures.GameLogic.Entities.Metadata; |
9 | 12 | using NitroxModel.DataStructures.Util;
|
10 | 13 | using NitroxModel.Packets;
|
11 | 14 | using NitroxModel_Subnautica.DataStructures;
|
12 | 15 | using UnityEngine;
|
| 16 | +using UWE; |
13 | 17 |
|
14 | 18 | namespace NitroxClient.GameLogic.Spawning;
|
15 | 19 |
|
16 |
| -public class InventoryItemEntitySpawner : SyncEntitySpawner<InventoryItemEntity> |
| 20 | +public class InventoryItemEntitySpawner(EntityMetadataManager entityMetadataManager) : SyncEntitySpawner<InventoryItemEntity> |
17 | 21 | {
|
| 22 | + private readonly EntityMetadataManager entityMetadataManager = entityMetadataManager; |
| 23 | + |
18 | 24 | protected override IEnumerator SpawnAsync(InventoryItemEntity entity, TaskResult<Optional<GameObject>> result)
|
19 | 25 | {
|
20 | 26 | if (!CanSpawn(entity, out GameObject parentObject, out ItemsContainer container, out string errorLog))
|
@@ -87,15 +93,86 @@ private void SetupObject(InventoryItemEntity entity, GameObject gameObject, Game
|
87 | 93 | Pickupable pickupable = gameObject.RequireComponent<Pickupable>();
|
88 | 94 | pickupable.Initialize();
|
89 | 95 |
|
| 96 | + InventoryItem inventoryItem = new(pickupable); |
| 97 | + |
90 | 98 | // Items eventually get "secured" once a player gets into a SubRoot (or for other reasons) so we need to force this state by default
|
91 | 99 | // so that player don't risk their whole inventory if they reconnect in the water.
|
92 | 100 | pickupable.destroyOnDeath = false;
|
93 | 101 |
|
| 102 | + bool isPlanter = parentObject.TryGetComponent(out Planter planter); |
| 103 | + bool subscribedValue = false; |
| 104 | + if (isPlanter) |
| 105 | + { |
| 106 | + subscribedValue = planter.subscribed; |
| 107 | + planter.Subscribe(false); |
| 108 | + } |
| 109 | + |
94 | 110 | using (PacketSuppressor<EntityReparented>.Suppress())
|
95 | 111 | using (PacketSuppressor<PlayerQuickSlotsBindingChanged>.Suppress())
|
| 112 | + using (PacketSuppressor<EntityMetadataUpdate>.Suppress()) |
96 | 113 | {
|
97 |
| - container.UnsafeAdd(new InventoryItem(pickupable)); |
| 114 | + container.UnsafeAdd(inventoryItem); |
98 | 115 | Log.Debug($"Received: Added item {pickupable.GetTechType()} ({entity.Id}) to container {parentObject.GetFullHierarchyPath()}");
|
99 | 116 | }
|
| 117 | + |
| 118 | + if (isPlanter) |
| 119 | + { |
| 120 | + planter.Subscribe(subscribedValue); |
| 121 | + |
| 122 | + if (entity.Metadata is PlantableMetadata metadata) |
| 123 | + { |
| 124 | + PostponeAddNotification(() => planter.subscribed, () => planter, true, () => |
| 125 | + { |
| 126 | + // Adapted from Planter.AddItem(InventoryItem) to be able to call directly AddItem(Plantable, slotID) with our parameters |
| 127 | + Plantable plantable = pickupable.GetComponent<Plantable>(); |
| 128 | + pickupable.SetTechTypeOverride(plantable.plantTechType, false); |
| 129 | + inventoryItem.isEnabled = false; |
| 130 | + planter.AddItem(plantable, metadata.SlotID); |
| 131 | + |
| 132 | + // Reapply the plantable metadata after the GrowingPlant (or the GrownPlant) is spawned |
| 133 | + entityMetadataManager.ApplyMetadata(plantable.gameObject, metadata); |
| 134 | + |
| 135 | + // Plant spawning occurs in multiple steps over frames: |
| 136 | + // spawning the item, adding it to the planter, having the GrowingPlant created, and eventually having it create a GrownPlant (when progress == 1) |
| 137 | + // therefore we give the metadata to the object so it can be used when required |
| 138 | + if (metadata.FruitPlantMetadata != null && plantable.growingPlant && plantable.growingPlant.GetProgress() == 1f) |
| 139 | + { |
| 140 | + MetadataHolder.AddMetadata(plantable.growingPlant.gameObject, metadata.FruitPlantMetadata); |
| 141 | + } |
| 142 | + }); |
| 143 | + } |
| 144 | + } |
| 145 | + else if (parentObject.TryGetComponent(out Trashcan trashcan)) |
| 146 | + { |
| 147 | + PostponeAddNotification(() => trashcan.subscribed, () => trashcan, false, () => |
| 148 | + { |
| 149 | + trashcan.AddItem(inventoryItem); |
| 150 | + }); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static void PostponeAddNotification(Func<bool> subscribed, Func<bool> instanceValid, bool callbackIfAlreadySubscribed, Action callback) |
| 155 | + { |
| 156 | + IEnumerator PostponedAddCallback() |
| 157 | + { |
| 158 | + yield return new WaitUntil(() => subscribed() || !instanceValid()); |
| 159 | + if (instanceValid()) |
| 160 | + { |
| 161 | + using (PacketSuppressor<EntityReparented>.Suppress()) |
| 162 | + using (PacketSuppressor<EntityMetadataUpdate>.Suppress()) |
| 163 | + { |
| 164 | + callback(); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if (!subscribed()) |
| 170 | + { |
| 171 | + CoroutineHost.StartCoroutine(PostponedAddCallback()); |
| 172 | + } |
| 173 | + else if (callbackIfAlreadySubscribed) |
| 174 | + { |
| 175 | + callback(); |
| 176 | + } |
100 | 177 | }
|
101 | 178 | }
|
0 commit comments