Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alt-Z #135

Merged
merged 2 commits into from
Dec 4, 2024
Merged

Alt-Z #135

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ public bool TryUseItemInHand(EntityUid uid, bool altInteract = false, HandsCompo
return false;

if (altInteract)
return _interactionSystem.AltInteract(uid, held);
if(hand == handsComp.ActiveHand) // WD EDIT START
return _interactionSystem.ActiveHandAltInteract(uid, held) || _interactionSystem.AltInteract(uid, held); // todo: should these be merged into one method?
else // WD EDIT END
return _interactionSystem.AltInteract(uid, held);
else
return _interactionSystem.UseInHandInteraction(uid, held);
}
Expand Down
20 changes: 19 additions & 1 deletion Content.Shared/Interaction/SharedInteractionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,14 +1090,32 @@ public bool UseInHandInteraction(
public bool AltInteract(EntityUid user, EntityUid target)
{
// Get list of alt-interact verbs
var verbs = _verbSystem.GetLocalVerbs(target, user, typeof(AlternativeVerb));
var verbs = _verbSystem.GetLocalVerbs(target, user, typeof(AlternativeVerb)).Where(verb => ((AlternativeVerb) verb).InActiveHandOnly == false); // WD EDIT

if (!verbs.Any())
return false;

_verbSystem.ExecuteVerb(verbs.First(), user, target);
return true;
}

// WD EDIT START
/// <summary>
/// Very alternative interactions on an entity.
/// </summary>
/// <returns>True if the interaction was handled, false otherwise.</returns>
public bool ActiveHandAltInteract(EntityUid user, EntityUid target)
{
// Get list of alt-interact verbs
var verbs = _verbSystem.GetLocalVerbs(target, user, typeof(AlternativeVerb)).Where(verb => ((AlternativeVerb)verb).InActiveHandOnly == true);

if (!verbs.Any())
return false;

_verbSystem.ExecuteVerb(verbs.First(), user, target);
return true;
}
// WD EDIT END
#endregion

public void DroppedInteraction(EntityUid user, EntityUid item)
Expand Down
11 changes: 11 additions & 0 deletions Content.Shared/Verbs/Verb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,17 @@ public sealed class AlternativeVerb : Verb
{
public override int TypePriority => 2;
public new static string DefaultTextStyleClass = "AlternativeVerb";
// WD EDIT START
/// <summary>
/// If true, this verb can only be triggered by alt-interacting (alt-Z) with the parent item
/// while holding it in the active hand, or via context menu. The latter does not require holding in active hand.
/// </summary>
/// <remarks>
/// On alt-Z, if item returns no <see cref="AlternativeVerb"/> with <see cref="InActiveHandOnly"/> being true, other <see cref="AlternativeVerb"/>s will be considered.
/// This is not true for the reverse: alt-clicking an item not in your main hand will not proc an <see cref="AlternativeVerb"/> if it's <see cref="InActiveHandOnly"/> is true.
/// </remarks>
public bool InActiveHandOnly = false; // todo: better name?
// WD EDIT END
public override bool DefaultDoContactInteraction => true;

public AlternativeVerb() : base()
Expand Down
7 changes: 7 additions & 0 deletions Content.Shared/Wieldable/Components/WieldableComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public sealed partial class WieldableComponent : Component
public string? WieldedInhandPrefix = "wielded";

public string? OldInhandPrefix = null;
// WD EDIT START
/// <summary>
/// Requires item to be alt-used in hand (alt-Z / alt-click in active hand) to be wielded.
/// </summary>
[DataField]
public bool AltUseInHand = false;
// WD EDIT END
}

[Serializable, NetSerializable]
Expand Down
35 changes: 34 additions & 1 deletion Content.Shared/Wieldable/WieldableSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public override void Initialize()
SubscribeLocalEvent<WieldableComponent, GotUnequippedHandEvent>(OnItemLeaveHand);
SubscribeLocalEvent<WieldableComponent, VirtualItemDeletedEvent>(OnVirtualItemDeleted);
SubscribeLocalEvent<WieldableComponent, GetVerbsEvent<InteractionVerb>>(AddToggleWieldVerb);
SubscribeLocalEvent<WieldableComponent, GetVerbsEvent<AlternativeVerb>>(AddAltWieldVerb); // WD EDIT
SubscribeLocalEvent<WieldableComponent, HandDeselectedEvent>(OnDeselectWieldable);

SubscribeLocalEvent<MeleeRequiresWieldComponent, AttemptMeleeEvent>(OnMeleeAttempt);
Expand Down Expand Up @@ -143,9 +144,41 @@ private void AddToggleWieldVerb(EntityUid uid, WieldableComponent component, Get
args.Verbs.Add(verb);
}

// WD EDIT START
/// <summary>
/// Copypasted <see cref="AddToggleWieldVerb(EntityUid, WieldableComponent, GetVerbsEvent{InteractionVerb})"/>
/// </summary>
private void AddAltWieldVerb(EntityUid uid, WieldableComponent component, GetVerbsEvent<AlternativeVerb> args)
{
if (!component.AltUseInHand)
return;

if (args.Hands == null || !args.CanAccess || !args.CanInteract)
return;

if (!_handsSystem.IsHolding(args.User, uid, out _, args.Hands))
return;

// TODO VERB TOOLTIPS Make CanWield or some other function return string, set as verb tooltip and disable
// verb. Or just don't add it to the list if the action is not executable.

// TODO VERBS ICON
AlternativeVerb verb = new()
{
Text = component.Wielded ? Loc.GetString("wieldable-verb-text-unwield") : Loc.GetString("wieldable-verb-text-wield"),
Act = component.Wielded
? () => TryUnwield(uid, component, args.User)
: () => TryWield(uid, component, args.User),
InActiveHandOnly = true
};

args.Verbs.Add(verb);
}
// WD EDIT END
Comment on lines +147 to +177
Copy link
Contributor

@coderabbitai coderabbitai bot Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Необходимо устранить дублирование кода.

Метод AddAltWieldVerb практически полностью дублирует AddToggleWieldVerb. Предлагаю выделить общую логику в отдельный приватный метод.

+private void AddWieldVerb<T>(EntityUid uid, WieldableComponent component, GetVerbsEvent<T> args, bool requireActiveHand = false) where T : BaseVerb
+{
+    if (requireActiveHand && !component.AltUseInHand)
+        return;
+
+    if (args.Hands == null || !args.CanAccess || !args.CanInteract)
+        return;
+
+    if (!_handsSystem.IsHolding(args.User, uid, out _, args.Hands))
+        return;
+
+    var verb = Activator.CreateInstance<T>();
+    verb.Text = component.Wielded ? Loc.GetString("wieldable-verb-text-unwield") : Loc.GetString("wieldable-verb-text-wield");
+    verb.Act = component.Wielded
+        ? () => TryUnwield(uid, component, args.User)
+        : () => TryWield(uid, component, args.User);
+    
+    if (verb is AlternativeVerb altVerb)
+        altVerb.InActiveHandOnly = true;
+
+    args.Verbs.Add(verb);
+}

Затем используйте этот метод:

-private void AddToggleWieldVerb(EntityUid uid, WieldableComponent component, GetVerbsEvent<InteractionVerb> args)
-{
-    // ... existing implementation
-}
+private void AddToggleWieldVerb(EntityUid uid, WieldableComponent component, GetVerbsEvent<InteractionVerb> args)
+{
+    AddWieldVerb(uid, component, args);
+}

-private void AddAltWieldVerb(EntityUid uid, WieldableComponent component, GetVerbsEvent<AlternativeVerb> args)
-{
-    // ... existing implementation
-}
+private void AddAltWieldVerb(EntityUid uid, WieldableComponent component, GetVerbsEvent<AlternativeVerb> args)
+{
+    AddWieldVerb(uid, component, args, true);
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// WD EDIT START
/// <summary>
/// Copypasted <see cref="AddToggleWieldVerb(EntityUid, WieldableComponent, GetVerbsEvent{InteractionVerb})"/>
/// </summary>
private void AddAltWieldVerb(EntityUid uid, WieldableComponent component, GetVerbsEvent<AlternativeVerb> args)
{
if (!component.AltUseInHand)
return;
if (args.Hands == null || !args.CanAccess || !args.CanInteract)
return;
if (!_handsSystem.IsHolding(args.User, uid, out _, args.Hands))
return;
// TODO VERB TOOLTIPS Make CanWield or some other function return string, set as verb tooltip and disable
// verb. Or just don't add it to the list if the action is not executable.
// TODO VERBS ICON
AlternativeVerb verb = new()
{
Text = component.Wielded ? Loc.GetString("wieldable-verb-text-unwield") : Loc.GetString("wieldable-verb-text-wield"),
Act = component.Wielded
? () => TryUnwield(uid, component, args.User)
: () => TryWield(uid, component, args.User),
InActiveHandOnly = true
};
args.Verbs.Add(verb);
}
// WD EDIT END
// WD EDIT START
private void AddWieldVerb<T>(EntityUid uid, WieldableComponent component, GetVerbsEvent<T> args, bool requireActiveHand = false) where T : BaseVerb
{
if (requireActiveHand && !component.AltUseInHand)
return;
if (args.Hands == null || !args.CanAccess || !args.CanInteract)
return;
if (!_handsSystem.IsHolding(args.User, uid, out _, args.Hands))
return;
var verb = Activator.CreateInstance<T>();
verb.Text = component.Wielded ? Loc.GetString("wieldable-verb-text-unwield") : Loc.GetString("wieldable-verb-text-wield");
verb.Act = component.Wielded
? () => TryUnwield(uid, component, args.User)
: () => TryWield(uid, component, args.User);
if (verb is AlternativeVerb altVerb)
altVerb.InActiveHandOnly = true;
args.Verbs.Add(verb);
}
private void AddToggleWieldVerb(EntityUid uid, WieldableComponent component, GetVerbsEvent<InteractionVerb> args)
{
AddWieldVerb(uid, component, args);
}
private void AddAltWieldVerb(EntityUid uid, WieldableComponent component, GetVerbsEvent<AlternativeVerb> args)
{
AddWieldVerb(uid, component, args, true);
}
// WD EDIT END

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Шило на мыло, тем более в там, куда почти не ходят и что (почти) никогда не лапают.
Не вижу смысла.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RedFoxIV, понимаю. Спасибо за ваше мнение.


private void OnUseInHand(EntityUid uid, WieldableComponent component, UseInHandEvent args)
{
if (args.Handled)
if (args.Handled || component.AltUseInHand) // WD EDIT
return;

if (!component.Wielded)
Expand Down
Loading