fix retarded code (#744)

This commit is contained in:
Valtos
2024-10-24 17:15:00 +03:00
committed by GitHub
parent 350ce8e297
commit 64ab042551
4 changed files with 88 additions and 29 deletions

View File

@@ -74,14 +74,12 @@ public sealed class SubdermalImplantSystem : SharedSubdermalImplantSystem
return; return;
// same as store code, but message is only shown to yourself // same as store code, but message is only shown to yourself
args.Handled = _store.TryAddCurrency(_store.GetCurrencyValue(args.Used, currency), uid, store); if (!_store.TryAddCurrency((args.Used, currency), (uid, store)))
if (!args.Handled)
return; return;
args.Handled = true;
var msg = Loc.GetString("store-currency-inserted-implant", ("used", args.Used)); var msg = Loc.GetString("store-currency-inserted-implant", ("used", args.Used));
_popup.PopupEntity(msg, args.User, args.User); _popup.PopupEntity(msg, args.User, args.User);
QueueDel(args.Used);
} }
private void OnFreedomImplant(EntityUid uid, SubdermalImplantComponent component, UseFreedomImplantEvent args) private void OnFreedomImplant(EntityUid uid, SubdermalImplantComponent component, UseFreedomImplantEvent args)

View File

@@ -91,19 +91,68 @@ namespace Content.Server.Stack
/// </summary> /// </summary>
public List<EntityUid> SpawnMultiple(string entityPrototype, int amount, EntityCoordinates spawnPosition) public List<EntityUid> SpawnMultiple(string entityPrototype, int amount, EntityCoordinates spawnPosition)
{ {
var proto = _prototypeManager.Index<EntityPrototype>(entityPrototype); if (amount <= 0)
proto.TryGetComponent<StackComponent>(out var stack); {
var maxCountPerStack = GetMaxCount(stack); Log.Error(
$"Attempted to spawn an invalid stack: {entityPrototype}, {amount}. Trace: {Environment.StackTrace}");
return new();
}
var spawns = CalculateSpawns(entityPrototype, amount);
var spawnedEnts = new List<EntityUid>(); var spawnedEnts = new List<EntityUid>();
foreach (var count in spawns)
{
var entity = SpawnAtPosition(entityPrototype, spawnPosition);
spawnedEnts.Add(entity);
SetCount(entity, count);
}
return spawnedEnts;
}
/// <inheritdoc cref="SpawnMultiple(string,int,EntityCoordinates)"/>
public List<EntityUid> SpawnMultiple(string entityPrototype, int amount, EntityUid target)
{
if (amount <= 0)
{
Log.Error(
$"Attempted to spawn an invalid stack: {entityPrototype}, {amount}. Trace: {Environment.StackTrace}");
return new();
}
var spawns = CalculateSpawns(entityPrototype, amount);
var spawnedEnts = new List<EntityUid>();
foreach (var count in spawns)
{
var entity = SpawnNextToOrDrop(entityPrototype, target);
spawnedEnts.Add(entity);
SetCount(entity, count);
}
return spawnedEnts;
}
/// <summary>
/// Calculates how many stacks to spawn that total up to <paramref name="amount"/>.
/// </summary>
/// <param name="entityPrototype">The stack to spawn.</param>
/// <param name="amount">The amount of pieces across all stacks.</param>
/// <returns>The list of stack counts per entity.</returns>
private List<int> CalculateSpawns(string entityPrototype, int amount)
{
var proto = _prototypeManager.Index<EntityPrototype>(entityPrototype);
proto.TryGetComponent<StackComponent>(out var stack, EntityManager.ComponentFactory);
var maxCountPerStack = GetMaxCount(stack);
var amounts = new List<int>();
while (amount > 0) while (amount > 0)
{ {
var entity = Spawn(entityPrototype, spawnPosition);
spawnedEnts.Add(entity);
var countAmount = Math.Min(maxCountPerStack, amount); var countAmount = Math.Min(maxCountPerStack, amount);
SetCount(entity, countAmount);
amount -= countAmount; amount -= countAmount;
amounts.Add(countAmount);
} }
return spawnedEnts;
return amounts;
} }
private void OnStackAlternativeInteract(EntityUid uid, StackComponent stack, GetVerbsEvent<AlternativeVerb> args) private void OnStackAlternativeInteract(EntityUid uid, StackComponent stack, GetVerbsEvent<AlternativeVerb> args)

View File

@@ -297,6 +297,9 @@ public sealed partial class StoreSystem
/// </remarks> /// </remarks>
private void OnRequestWithdraw(EntityUid uid, StoreComponent component, StoreRequestWithdrawMessage msg) private void OnRequestWithdraw(EntityUid uid, StoreComponent component, StoreRequestWithdrawMessage msg)
{ {
if (msg.Amount <= 0)
return;
//make sure we have enough cash in the bank and we actually support this currency //make sure we have enough cash in the bank and we actually support this currency
if (!component.Balance.TryGetValue(msg.Currency, out var currentAmount) || currentAmount < msg.Amount) if (!component.Balance.TryGetValue(msg.Currency, out var currentAmount) || currentAmount < msg.Amount)
return; return;
@@ -320,7 +323,8 @@ public sealed partial class StoreSystem
var cashId = proto.Cash[value]; var cashId = proto.Cash[value];
var amountToSpawn = (int) MathF.Floor((float) (amountRemaining / value)); var amountToSpawn = (int) MathF.Floor((float) (amountRemaining / value));
var ents = _stack.SpawnMultiple(cashId, amountToSpawn, coordinates); var ents = _stack.SpawnMultiple(cashId, amountToSpawn, coordinates);
_hands.PickupOrDrop(buyer, ents.First()); if (ents.FirstOrDefault() is {} ent)
_hands.PickupOrDrop(buyer, ent);
amountRemaining -= value * amountToSpawn; amountRemaining -= value * amountToSpawn;
} }

View File

@@ -94,14 +94,12 @@ public sealed partial class StoreSystem : EntitySystem
if (ev.Cancelled) if (ev.Cancelled)
return; return;
args.Handled = TryAddCurrency(GetCurrencyValue(uid, component), args.Target.Value, store); if (!TryAddCurrency((uid, component), (args.Target.Value, store)))
return;
if (args.Handled) args.Handled = true;
{ var msg = Loc.GetString("store-currency-inserted", ("used", args.Used), ("target", args.Target));
var msg = Loc.GetString("store-currency-inserted", ("used", args.Used), ("target", args.Target)); _popup.PopupEntity(msg, args.Target.Value, args.User);
_popup.PopupEntity(msg, args.Target.Value, args.User);
QueueDel(args.Used);
}
} }
private void OnImplantActivate(EntityUid uid, StoreComponent component, OpenUplinkImplantEvent args) private void OnImplantActivate(EntityUid uid, StoreComponent component, OpenUplinkImplantEvent args)
@@ -123,19 +121,29 @@ public sealed partial class StoreSystem : EntitySystem
} }
/// <summary> /// <summary>
/// Tries to add a currency to a store's balance. /// Tries to add a currency to a store's balance. Note that if successful, this will consume the currency in the process.
/// </summary> /// </summary>
/// <param name="currencyEnt"></param> public bool TryAddCurrency(Entity<CurrencyComponent?> currency, Entity<StoreComponent?> store)
/// <param name="storeEnt"></param>
/// <param name="currency">The currency to add</param>
/// <param name="store">The store to add it to</param>
/// <returns>Whether or not the currency was succesfully added</returns>
[PublicAPI]
public bool TryAddCurrency(EntityUid currencyEnt, EntityUid storeEnt, StoreComponent? store = null, CurrencyComponent? currency = null)
{ {
if (!Resolve(currencyEnt, ref currency) || !Resolve(storeEnt, ref store)) if (!Resolve(currency.Owner, ref currency.Comp))
return false; return false;
return TryAddCurrency(GetCurrencyValue(currencyEnt, currency), storeEnt, store); if (!Resolve(store.Owner, ref store.Comp))
return false;
var value = currency.Comp.Price;
if (TryComp(currency.Owner, out StackComponent? stack) && stack.Count != 1)
{
value = currency.Comp.Price
.ToDictionary(v => v.Key, p => p.Value * stack.Count);
}
if (!TryAddCurrency(value, store, store.Comp))
return false;
// Avoid having the currency accidentally be re-used. E.g., if multiple clients try to use the currency in the
// same tick
currency.Comp.Price.Clear();
if (stack != null)
_stack.SetCount(currency.Owner, 0, stack);
QueueDel(currency);
return true;
} }
/// <summary> /// <summary>