prevent magnet deconstruction when active (#19849)

* raise ToolUseAttemptEvent on target as well as tool

* prevent using tools on magnet when active

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2023-09-05 17:20:54 +01:00
committed by GitHub
parent ce54689e0c
commit a096e5be7e
3 changed files with 27 additions and 4 deletions

View File

@@ -25,6 +25,7 @@ using Content.Shared.CCVar;
using Content.Shared.Construction.EntitySystems;
using Content.Shared.Random;
using Content.Shared.Random.Helpers;
using Content.Shared.Tools.Components;
using Robust.Server.Maps;
using Robust.Shared.Map.Components;
using Robust.Shared.Timing;
@@ -68,6 +69,7 @@ namespace Content.Server.Salvage
SubscribeLocalEvent<SalvageMagnetComponent, RefreshPartsEvent>(OnRefreshParts);
SubscribeLocalEvent<SalvageMagnetComponent, UpgradeExamineEvent>(OnUpgradeExamine);
SubscribeLocalEvent<SalvageMagnetComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<SalvageMagnetComponent, ToolUseAttemptEvent>(OnToolUseAttempt);
SubscribeLocalEvent<SalvageMagnetComponent, ComponentShutdown>(OnMagnetRemoval);
SubscribeLocalEvent<GridRemovalEvent>(OnGridRemoval);
@@ -232,6 +234,15 @@ namespace Content.Server.Salvage
}
}
private void OnToolUseAttempt(EntityUid uid, SalvageMagnetComponent comp, ToolUseAttemptEvent args)
{
// prevent reconstruct exploit to "leak" wrecks or skip cooldowns
if (comp.MagnetState != MagnetState.Inactive)
{
args.Cancel();
}
}
private void OnInteractHand(EntityUid uid, SalvageMagnetComponent component, InteractHandEvent args)
{
if (args.Handled)

View File

@@ -22,7 +22,8 @@ namespace Content.Shared.Tools.Components
}
/// <summary>
/// Attempt event called *before* any do afters to see if the tool usage should succeed or not.
/// Attempt event called *before* any do afters to see if the tool usage should succeed or not.
/// Raised on both the tool and then target.
/// </summary>
public sealed class ToolUseAttemptEvent : CancellableEntityEventArgs
{

View File

@@ -180,16 +180,27 @@ public abstract partial class SharedToolSystem : EntitySystem
if (!Resolve(tool, ref toolComponent))
return false;
// check if the tool can do what's required
if (!toolComponent.Qualities.ContainsAll(toolQualitiesNeeded))
return false;
// check if the user allows using the tool
var ev = new ToolUserAttemptUseEvent(target);
RaiseLocalEvent(user, ref ev);
if (ev.Cancelled)
return false;
if (!toolComponent.Qualities.ContainsAll(toolQualitiesNeeded))
// check if the tool allows being used
var beforeAttempt = new ToolUseAttemptEvent(user);
RaiseLocalEvent(tool, beforeAttempt);
if (beforeAttempt.Cancelled)
return false;
var beforeAttempt = new ToolUseAttemptEvent(user);
RaiseLocalEvent(tool, beforeAttempt, false);
// check if the target allows using the tool
if (target != null && target != tool)
{
RaiseLocalEvent(target.Value, beforeAttempt);
}
return !beforeAttempt.Cancelled;
}