using System; using Robust.Server.Interfaces.Console; using Robust.Server.Interfaces.Player; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Network; namespace Content.Server.Commands { /// /// Utilities for writing commands /// public static class CommandUtils { /// /// Gets the player session for the player with the indicated id, /// sending a failure to the performer if unable to. /// public static bool TryGetSessionByUsernameOrId(IConsoleShell shell, string usernameOrId, IPlayerSession performer, out IPlayerSession session) { var plyMgr = IoCManager.Resolve(); if (plyMgr.TryGetSessionByUsername(usernameOrId, out session)) return true; if (Guid.TryParse(usernameOrId, out var targetGuid)) { if (plyMgr.TryGetSessionById(new NetUserId(targetGuid), out session)) return true; shell.SendText(performer, "Unable to find user with that name/id."); return false; } shell.SendText(performer, "Unable to find user with that name/id."); return false; } /// /// Gets the attached entity for the player session with the indicated id, /// sending a failure to the performer if unable to. /// public static bool TryGetAttachedEntityByUsernameOrId(IConsoleShell shell, string usernameOrId, IPlayerSession performer, out IEntity attachedEntity) { attachedEntity = null; if (!TryGetSessionByUsernameOrId(shell, usernameOrId, performer, out var session)) return false; if (session.AttachedEntity == null) { shell.SendText(performer, "User has no attached entity."); return false; } attachedEntity = session.AttachedEntity; return true; } /// /// Checks if attached entity is null, returning false and sending a message /// to performer if not. /// public static bool ValidateAttachedEntity(IConsoleShell shell, IPlayerSession performer, IEntity attachedEntity) { if (attachedEntity != null) return true; shell.SendText(performer, "User has no attached entity."); return false; } } }