From a4563d2e753d85994503c377a390921bbd469101 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Thu, 4 Feb 2021 01:24:37 +0100 Subject: [PATCH] Fix uplink item spawning. Actually spawns for the person who USES the uplink, not who owns it. Can't believe that was an actual bug. Also puts it in your active hand if possible. --- .../Components/GUI/HandsComponent.cs | 21 ++++++++++++++++++- .../Components/PDA/PDAComponent.cs | 10 ++++++++- .../Interfaces/PDA/IPDAUplinkManager.cs | 12 +++++++++-- Content.Server/PDA/PDAUplinkManager.cs | 20 ++++++++---------- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/Content.Server/GameObjects/Components/GUI/HandsComponent.cs b/Content.Server/GameObjects/Components/GUI/HandsComponent.cs index 0139761d21..2c7aad758e 100644 --- a/Content.Server/GameObjects/Components/GUI/HandsComponent.cs +++ b/Content.Server/GameObjects/Components/GUI/HandsComponent.cs @@ -198,14 +198,33 @@ namespace Content.Server.GameObjects.Components.GUI return success; } + /// + /// Drops the item if doesn't have hands. + /// + public static void PutInHandOrDropStatic(IEntity mob, ItemComponent item, bool mobCheck = true) + { + if (!mob.TryGetComponent(out HandsComponent? hands)) + { + DropAtFeet(mob, item); + return; + } + + hands.PutInHandOrDrop(item, mobCheck); + } + public void PutInHandOrDrop(ItemComponent item, bool mobCheck = true) { if (!PutInHand(item, mobCheck)) { - item.Owner.Transform.Coordinates = Owner.Transform.Coordinates; + DropAtFeet(Owner, item); } } + private static void DropAtFeet(IEntity mob, ItemComponent item) + { + item.Owner.Transform.Coordinates = mob.Transform.Coordinates; + } + public bool CanPutInHand(ItemComponent item, bool mobCheck = true) { if (mobCheck && !ActionBlockerSystem.CanPickup(Owner)) diff --git a/Content.Server/GameObjects/Components/PDA/PDAComponent.cs b/Content.Server/GameObjects/Components/PDA/PDAComponent.cs index efd7c1da4b..cf65690de8 100644 --- a/Content.Server/GameObjects/Components/PDA/PDAComponent.cs +++ b/Content.Server/GameObjects/Components/PDA/PDAComponent.cs @@ -135,12 +135,20 @@ namespace Content.Server.GameObjects.Components.PDA case PDAUplinkBuyListingMessage buyMsg: { - if (!_uplinkManager.TryPurchaseItem(_syndicateUplinkAccount, buyMsg.ItemId)) + if (message.Session.AttachedEntity == null) + break; + + if (!_uplinkManager.TryPurchaseItem(_syndicateUplinkAccount, buyMsg.ItemId, + message.Session.AttachedEntity.Transform.Coordinates, out var entity)) { SendNetworkMessage(new PDAUplinkInsufficientFundsMessage(), message.Session.ConnectedClient); break; } + HandsComponent.PutInHandOrDropStatic( + message.Session.AttachedEntity, + entity.GetComponent()); + SendNetworkMessage(new PDAUplinkBuySuccessMessage(), message.Session.ConnectedClient); break; } diff --git a/Content.Server/Interfaces/PDA/IPDAUplinkManager.cs b/Content.Server/Interfaces/PDA/IPDAUplinkManager.cs index f58fc5d755..0b9f78dba3 100644 --- a/Content.Server/Interfaces/PDA/IPDAUplinkManager.cs +++ b/Content.Server/Interfaces/PDA/IPDAUplinkManager.cs @@ -1,11 +1,15 @@ +#nullable enable using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using Content.Shared.GameObjects.Components.PDA; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.Map; namespace Content.Server.Interfaces.PDA { public interface IPDAUplinkManager { - public IReadOnlyDictionary FetchListings => null; + public IReadOnlyDictionary FetchListings { get; } void Initialize(); @@ -13,7 +17,11 @@ namespace Content.Server.Interfaces.PDA public bool ChangeBalance(UplinkAccount acc, int amt); - public bool TryPurchaseItem(UplinkAccount acc, string itemId); + public bool TryPurchaseItem( + UplinkAccount? acc, + string itemId, + EntityCoordinates spawnCoords, + [NotNullWhen(true)] out IEntity? purchasedItem); } } diff --git a/Content.Server/PDA/PDAUplinkManager.cs b/Content.Server/PDA/PDAUplinkManager.cs index 2cdae4e8ca..b4d96e6980 100644 --- a/Content.Server/PDA/PDAUplinkManager.cs +++ b/Content.Server/PDA/PDAUplinkManager.cs @@ -1,4 +1,6 @@ +#nullable enable using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using Content.Server.GameObjects.Components.GUI; using Content.Server.GameObjects.Components.Items.Storage; @@ -8,6 +10,7 @@ using Content.Shared.GameObjects.Components.PDA; using Content.Shared.Prototypes.PDA; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.IoC; +using Robust.Shared.Map; using Robust.Shared.Prototypes; namespace Content.Server.PDA @@ -17,14 +20,13 @@ namespace Content.Server.PDA [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IEntityManager _entityManager = default!; - private List _accounts; - private Dictionary _listings; + private readonly List _accounts = new(); + private readonly Dictionary _listings = new(); public IReadOnlyDictionary FetchListings => _listings; public void Initialize() { - _listings = new Dictionary(); foreach (var item in _prototypeManager.EnumeratePrototypes()) { var newListing = new UplinkListingData(item.ListingName, item.ItemId, item.Price, item.Category, @@ -32,8 +34,6 @@ namespace Content.Server.PDA RegisterUplinkListing(newListing); } - - _accounts = new List(); } private void RegisterUplinkListing(UplinkListingData listing) @@ -53,7 +53,7 @@ namespace Content.Server.PDA { var entity = _entityManager.GetEntity(acc.AccountHolder); - if (entity.TryGetComponent(out MindComponent mindComponent) && !mindComponent.HasMind) + if (entity.TryGetComponent(out MindComponent? mindComponent) && !mindComponent.HasMind) { return false; } @@ -86,8 +86,9 @@ namespace Content.Server.PDA return true; } - public bool TryPurchaseItem(UplinkAccount acc, string itemId) + public bool TryPurchaseItem(UplinkAccount? acc, string itemId, EntityCoordinates spawnCoords, [NotNullWhen(true)] out IEntity? purchasedItem) { + purchasedItem = null; if (acc == null) { return false; @@ -108,10 +109,7 @@ namespace Content.Server.PDA return false; } - var player = _entityManager.GetEntity(acc.AccountHolder); - var hands = player.GetComponent(); - hands.PutInHandOrDrop(_entityManager.SpawnEntity(listing.ItemId, - player.Transform.Coordinates).GetComponent()); + purchasedItem = _entityManager.SpawnEntity(listing.ItemId, spawnCoords); return true; } }