Hotfix for Battery & Weapon Chargers. (#11976)

This commit is contained in:
Mervill 2022-10-16 15:05:35 -07:00 committed by GitHub
parent 8b1580ee30
commit a5a92c4dca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 7 deletions

View File

@ -6,6 +6,7 @@ using Content.Shared.PowerCell.Components;
using JetBrains.Annotations;
using Robust.Shared.Containers;
using Content.Shared.Power;
using System.Diagnostics.CodeAnalysis;
namespace Content.Server.Power.EntitySystems;
@ -46,7 +47,7 @@ internal sealed class ChargerSystem : EntitySystem
if (charger.Status == CellChargerStatus.Empty || charger.Status == CellChargerStatus.Charged || !slot.HasItem)
continue;
TransferPower(charger.Owner, charger, frameTime);
TransferPower(charger.Owner, slot.Item!.Value, charger, frameTime);
}
}
@ -87,7 +88,7 @@ internal sealed class ChargerSystem : EntitySystem
if (!TryComp(args.EntityUid, out PowerCellSlotComponent? cellSlot))
return;
if (!_itemSlotsSystem.TryGetSlot(args.EntityUid, cellSlot.CellSlotId, out ItemSlot? itemSlot))
return;
@ -159,19 +160,19 @@ internal sealed class ChargerSystem : EntitySystem
if (!_itemSlotsSystem.TryGetSlot(uid, component.SlotId, out ItemSlot? slot))
return CellChargerStatus.Off;
if (!_cellSystem.TryGetBatteryFromSlot(uid, out BatteryComponent? heldBattery))
return CellChargerStatus.Off;
if (!slot.HasItem)
return CellChargerStatus.Empty;
if (!SearchForBattery(slot.Item!.Value, out BatteryComponent? heldBattery))
return CellChargerStatus.Off;
if (heldBattery != null && Math.Abs(heldBattery.MaxCharge - heldBattery.CurrentCharge) < 0.01)
return CellChargerStatus.Charged;
return CellChargerStatus.Charging;
}
private void TransferPower(EntityUid uid, ChargerComponent component, float frameTime)
private void TransferPower(EntityUid uid, EntityUid targetEntity, ChargerComponent component, float frameTime)
{
if (!TryComp(uid, out ApcPowerReceiverComponent? receiverComponent))
return;
@ -179,7 +180,7 @@ internal sealed class ChargerSystem : EntitySystem
if (!receiverComponent.Powered)
return;
if (!_cellSystem.TryGetBatteryFromSlot(uid, out BatteryComponent? heldBattery))
if (!SearchForBattery(targetEntity, out BatteryComponent? heldBattery))
return;
heldBattery.CurrentCharge += component.ChargeRate * frameTime;
@ -191,4 +192,15 @@ internal sealed class ChargerSystem : EntitySystem
UpdateStatus(uid, component);
}
private bool SearchForBattery(EntityUid uid, [NotNullWhen(true)] out BatteryComponent? component)
{
// try get a battery directly on the inserted entity
if (!TryComp(uid, out component))
{
// or by checking for a power cell slot on the inserted entity
return _cellSystem.TryGetBatteryFromSlot(uid, out component);
}
return true;
}
}