Add uplink command minor revision and cleanup (#38532)
* commit * Update AddUplinkCommand.cs
This commit is contained in:
@@ -1,104 +1,90 @@
|
||||
using Content.Server.Administration;
|
||||
using Content.Shared.Administration;
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.Console;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Server.Traitor.Uplink.Commands
|
||||
namespace Content.Server.Traitor.Uplink.Commands;
|
||||
|
||||
[AdminCommand(AdminFlags.Admin)]
|
||||
public sealed class AddUplinkCommand : LocalizedEntityCommands
|
||||
{
|
||||
[AdminCommand(AdminFlags.Admin)]
|
||||
public sealed class AddUplinkCommand : IConsoleCommand
|
||||
[Dependency] private readonly UplinkSystem _uplinkSystem = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
|
||||
public override string Command => "adduplink";
|
||||
|
||||
public override void Execute(IConsoleShell shell, string argStr, string[] args)
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entManager = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
|
||||
public string Command => "adduplink";
|
||||
|
||||
public string Description => Loc.GetString("add-uplink-command-description");
|
||||
|
||||
public string Help => Loc.GetString("add-uplink-command-help");
|
||||
|
||||
|
||||
public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
|
||||
if (args.Length > 3)
|
||||
{
|
||||
return args.Length switch
|
||||
{
|
||||
1 => CompletionResult.FromHintOptions(CompletionHelper.SessionNames(), Loc.GetString("add-uplink-command-completion-1")),
|
||||
2 => CompletionResult.FromHint(Loc.GetString("add-uplink-command-completion-2")),
|
||||
3 => CompletionResult.FromHint(Loc.GetString("add-uplink-command-completion-3")),
|
||||
_ => CompletionResult.Empty
|
||||
};
|
||||
shell.WriteError(Loc.GetString("shell-wrong-arguments-number"));
|
||||
return;
|
||||
}
|
||||
|
||||
public void Execute(IConsoleShell shell, string argStr, string[] args)
|
||||
ICommonSession? session;
|
||||
if (args.Length > 0)
|
||||
{
|
||||
if (args.Length > 3)
|
||||
// Get player entity
|
||||
if (!_playerManager.TryGetSessionByUsername(args[0], out session))
|
||||
{
|
||||
shell.WriteError(Loc.GetString("shell-wrong-arguments-number"));
|
||||
shell.WriteLine(Loc.GetString("shell-target-player-does-not-exist"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
session = shell.Player;
|
||||
|
||||
if (session?.AttachedEntity is not { } user)
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("add-uplink-command-error-1"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Get target item
|
||||
EntityUid? uplinkEntity = null;
|
||||
if (args.Length >= 2)
|
||||
{
|
||||
if (!int.TryParse(args[1], out var itemId))
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("shell-entity-uid-must-be-number"));
|
||||
return;
|
||||
}
|
||||
|
||||
ICommonSession? session;
|
||||
if (args.Length > 0)
|
||||
{
|
||||
// Get player entity
|
||||
if (!_playerManager.TryGetSessionByUsername(args[0], out session))
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("shell-target-player-does-not-exist"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
session = shell.Player;
|
||||
}
|
||||
var eNet = new NetEntity(itemId);
|
||||
|
||||
if (session?.AttachedEntity is not { } user)
|
||||
if (!EntityManager.TryGetEntity(eNet, out var eUid))
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("add-uplink-command-error-1"));
|
||||
shell.WriteLine(Loc.GetString("shell-invalid-entity-id"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Get target item
|
||||
EntityUid? uplinkEntity = null;
|
||||
if (args.Length >= 2)
|
||||
uplinkEntity = eUid;
|
||||
}
|
||||
|
||||
var isDiscounted = false;
|
||||
if (args.Length >= 3)
|
||||
{
|
||||
if (!bool.TryParse(args[2], out isDiscounted))
|
||||
{
|
||||
if (!int.TryParse(args[1], out var itemID))
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("shell-entity-uid-must-be-number"));
|
||||
return;
|
||||
}
|
||||
|
||||
var eNet = new NetEntity(itemID);
|
||||
|
||||
if (!_entManager.TryGetEntity(eNet, out var eUid))
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("shell-invalid-entity-id"));
|
||||
return;
|
||||
}
|
||||
|
||||
uplinkEntity = eUid;
|
||||
}
|
||||
|
||||
bool isDiscounted = false;
|
||||
if (args.Length >= 3)
|
||||
{
|
||||
if (!bool.TryParse(args[2], out isDiscounted))
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("shell-invalid-bool"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Finally add uplink
|
||||
var uplinkSys = _entManager.System<UplinkSystem>();
|
||||
if (!uplinkSys.AddUplink(user, 20, uplinkEntity: uplinkEntity, giveDiscounts: isDiscounted))
|
||||
{
|
||||
shell.WriteLine(Loc.GetString("add-uplink-command-error-2"));
|
||||
shell.WriteLine(Loc.GetString("shell-invalid-bool"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Finally add uplink
|
||||
if (!_uplinkSystem.AddUplink(user, 20, uplinkEntity: uplinkEntity, giveDiscounts: isDiscounted))
|
||||
shell.WriteLine(Loc.GetString("add-uplink-command-error-2"));
|
||||
}
|
||||
|
||||
public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
|
||||
{
|
||||
return args.Length switch
|
||||
{
|
||||
1 => CompletionResult.FromHintOptions(CompletionHelper.SessionNames(), Loc.GetString("add-uplink-command-completion-1")),
|
||||
2 => CompletionResult.FromHint(Loc.GetString("add-uplink-command-completion-2")),
|
||||
3 => CompletionResult.FromHint(Loc.GetString("add-uplink-command-completion-3")),
|
||||
_ => CompletionResult.Empty,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
add-uplink-command-description = Creates uplink on selected item and link it to users account
|
||||
add-uplink-command-help = Usage: adduplink [username] [item-id]
|
||||
cmd-adduplink-desc = Creates uplink on selected item and link it to users account
|
||||
cmd-adduplink-help = Usage: adduplink [username] [item-id] [discountEnabled]
|
||||
|
||||
add-uplink-command-completion-1 = Username (defaults to self)
|
||||
add-uplink-command-completion-2 = Uplink uid (default to PDA)
|
||||
|
||||
Reference in New Issue
Block a user