Predict GetVerbsEvent in PowerSwitchableSystem (#39589)

* Prediction

* Update
This commit is contained in:
Winkarst-cpu
2025-08-13 10:54:02 +03:00
committed by GitHub
parent cea8dea005
commit 1f4dfcdcf9
5 changed files with 62 additions and 60 deletions

View File

@@ -9,6 +9,18 @@ namespace Content.Shared.Power.Generator;
/// <seealso cref="PortableGeneratorComponent"/>
public abstract class SharedPortableGeneratorSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FuelGeneratorComponent, SwitchPowerCheckEvent>(OnSwitchPowerCheck);
}
private void OnSwitchPowerCheck(EntityUid uid, FuelGeneratorComponent comp, ref SwitchPowerCheckEvent args)
{
if (comp.On)
args.DisableMessage = Loc.GetString("fuel-generator-verb-disable-on");
}
}
/// <summary>

View File

@@ -1,4 +1,6 @@
using Content.Shared.Examine;
using Content.Shared.Verbs;
using Robust.Shared.Utility;
namespace Content.Shared.Power.Generator;
@@ -11,6 +13,7 @@ public abstract class SharedPowerSwitchableSystem : EntitySystem
public override void Initialize()
{
SubscribeLocalEvent<PowerSwitchableComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<PowerSwitchableComponent, GetVerbsEvent<InteractionVerb>>(GetVerbs);
}
private void OnExamined(EntityUid uid, PowerSwitchableComponent comp, ExaminedEvent args)
@@ -20,6 +23,41 @@ public abstract class SharedPowerSwitchableSystem : EntitySystem
args.PushMarkup(Loc.GetString(comp.ExamineText, ("voltage", voltage)));
}
private void GetVerbs(EntityUid uid, PowerSwitchableComponent comp, GetVerbsEvent<InteractionVerb> args)
{
if (!args.CanAccess || !args.CanInteract)
return;
var voltage = VoltageColor(GetNextVoltage(uid, comp));
var msg = Loc.GetString("power-switchable-switch-voltage", ("voltage", voltage));
InteractionVerb verb = new()
{
Act = () =>
{
// don't need to check it again since if its disabled server wont let the verb act
Cycle(uid, args.User, comp);
},
Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/zap.svg.192dpi.png")),
Text = msg
};
var ev = new SwitchPowerCheckEvent();
RaiseLocalEvent(uid, ref ev);
if (ev.DisableMessage != null)
{
verb.Message = ev.DisableMessage;
verb.Disabled = true;
}
args.Verbs.Add(verb);
}
/// <summary>
/// Cycles voltage then updates nodes and optionally power supplier to match it.
/// </summary>
public virtual void Cycle(EntityUid uid, EntityUid user, PowerSwitchableComponent? comp = null) { }
/// <summary>
/// Helper to get the colored markup string for a voltage type.
/// </summary>
@@ -70,3 +108,10 @@ public abstract class SharedPowerSwitchableSystem : EntitySystem
return comp.Cables[NextIndex(uid, comp)].Voltage;
}
}
/// <summary>
/// Raised on a <see cref="PowerSwitchableComponent"/> to see if its verb should work.
/// If <see cref="DisableMessage"/> is non-null, the verb is disabled with that as the message.
/// </summary>
[ByRefEvent]
public record struct SwitchPowerCheckEvent(string? DisableMessage = null);