diff --git a/Content.Server/Shuttles/Commands/FTLDiskCommand.cs b/Content.Server/Shuttles/Commands/FTLDiskCommand.cs new file mode 100644 index 0000000000..b17c7c11a7 --- /dev/null +++ b/Content.Server/Shuttles/Commands/FTLDiskCommand.cs @@ -0,0 +1,183 @@ +using Content.Server.Administration; +using Content.Server.Labels; +using Content.Shared.Administration; +using Content.Shared.Hands.Components; +using Content.Shared.Hands.EntitySystems; +using Content.Shared.Shuttles.Components; +using Content.Shared.Storage; +using Content.Shared.Storage.EntitySystems; +using Robust.Shared.Console; +using Robust.Shared.Map.Components; +using Robust.Shared.Prototypes; +using Robust.Shared.Utility; + +namespace Content.Server.Shuttles.Commands; + +/// +/// Creates FTL disks, to maps, grids, or entities. +/// +[AdminCommand(AdminFlags.Fun)] + +public sealed class FTLDiskCommand : LocalizedCommands +{ + [Dependency] private readonly IEntityManager _entManager = default!; + [Dependency] private readonly IEntitySystemManager _entSystemManager = default!; + + public override string Command => "ftldisk"; + + [ValidatePrototypeId] + public const string CoordinatesDisk = "CoordinatesDisk"; + + [ValidatePrototypeId] + public const string DiskCase = "DiskCase"; + public override void Execute(IConsoleShell shell, string argStr, string[] args) + { + if (args.Length == 0) + { + shell.WriteError(Loc.GetString("shell-need-minimum-one-argument")); + return; + } + + var player = shell.Player; + + if (player == null) + { + shell.WriteLine(Loc.GetString("shell-only-players-can-run-this-command")); + return; + } + + if (player.AttachedEntity == null) + { + shell.WriteLine(Loc.GetString("shell-must-be-attached-to-entity")); + return; + } + + EntityUid entity = player.AttachedEntity.Value; + var coords = _entManager.GetComponent(entity).Coordinates; + + var handsSystem = _entSystemManager.GetEntitySystem(); + var labelSystem = _entSystemManager.GetEntitySystem(); + var mapSystem = _entSystemManager.GetEntitySystem(); + var storageSystem = _entSystemManager.GetEntitySystem(); + + foreach (var destinations in args) + { + DebugTools.AssertNotNull(destinations); + + // make sure destination is an id. + EntityUid dest; + + if (_entManager.TryParseNetEntity(destinations, out var nullableDest)) + { + DebugTools.AssertNotNull(nullableDest); + + dest = (EntityUid) nullableDest; + + // we need to go to a map, so check if the EntID is something else then try for its map + if (!_entManager.HasComponent(dest)) + { + if (!_entManager.TryGetComponent(dest, out var entTransform)) + { + shell.WriteLine(Loc.GetString("cmd-ftldisk-no-transform", ("destination", destinations))); + continue; + } + + if (!mapSystem.TryGetMap(entTransform.MapID, out var mapDest)) + { + shell.WriteLine(Loc.GetString("cmd-ftldisk-no-map", ("destination", destinations))); + continue; + } + + DebugTools.AssertNotNull(mapDest); + dest = mapDest!.Value; // explicit cast here should be fine since the previous if should catch it. + } + + // find and verify the map is not somehow unusable. + if (!_entManager.TryGetComponent(dest, out var mapComp)) // We have to check for a MapComponent here and above since we could have changed our dest entity. + { + shell.WriteLine(Loc.GetString("cmd-ftldisk-no-map-comp", ("destination", destinations), ("map", dest))); + continue; + } + if (mapComp.MapInitialized == false) + { + shell.WriteLine(Loc.GetString("cmd-ftldisk-map-not-init", ("destination", destinations), ("map", dest))); + continue; + } + if (mapComp.MapPaused == true) + { + shell.WriteLine(Loc.GetString("cmd-ftldisk-map-paused", ("destination", destinations), ("map", dest))); + continue; + } + + // check if our destination works already, if not, make it. + if (!_entManager.TryGetComponent(dest, out var ftlDestComp)) + { + FTLDestinationComponent ftlDest = _entManager.AddComponent(dest); + ftlDest.RequireCoordinateDisk = true; + + if (_entManager.HasComponent(dest)) + { + ftlDest.BeaconsOnly = true; + + shell.WriteLine(Loc.GetString("cmd-ftldisk-planet", ("destination", destinations), ("map", dest))); + } + } + else + { + // we don't do these automatically, since it isn't clear what the correct resolution is. Instead we provide feedback to the user and carry on like they know what theyre doing. + if (ftlDestComp.Enabled == false) + shell.WriteLine(Loc.GetString("cmd-ftldisk-already-dest-not-enabled", ("destination", destinations), ("map", dest))); + + if (ftlDestComp.BeaconsOnly == true) + shell.WriteLine(Loc.GetString("cmd-ftldisk-requires-ftl-point", ("destination", destinations), ("map", dest))); + } + + // create the FTL disk + EntityUid cdUid = _entManager.SpawnEntity(CoordinatesDisk, coords); + var cd = _entManager.EnsureComponent(cdUid); + cd.Destination = dest; + _entManager.Dirty(cdUid, cd); + + // create disk case + EntityUid cdCaseUid = _entManager.SpawnEntity(DiskCase, coords); + + // apply labels + if (_entManager.TryGetComponent(dest, out var meta) && meta != null && meta.EntityName != null) + { + labelSystem.Label(cdUid, meta.EntityName); + labelSystem.Label(cdCaseUid, meta.EntityName); + } + + // if the case has a storage, try to place the disk in there and then the case inhand + + if (_entManager.TryGetComponent(cdCaseUid, out var storage) && storageSystem.Insert(cdCaseUid, cdUid, out _, storageComp: storage, playSound: false)) + { + if (_entManager.TryGetComponent(entity, out var handsComponent) && handsSystem.TryGetEmptyHand(entity, out var emptyHand, handsComponent)) + { + handsSystem.TryPickup(entity, cdCaseUid, emptyHand, checkActionBlocker: false, handsComp: handsComponent); + } + } + else // the case was messed up, put disk inhand + { + _entManager.DeleteEntity(cdCaseUid); // something went wrong so just yeet the chaf + + if (_entManager.TryGetComponent(entity, out var handsComponent) && handsSystem.TryGetEmptyHand(entity, out var emptyHand, handsComponent)) + { + handsSystem.TryPickup(entity, cdUid, emptyHand, checkActionBlocker: false, handsComp: handsComponent); + } + } + } + else + { + shell.WriteLine(Loc.GetString("shell-invalid-entity-uid", ("uid", destinations))); + } + } + } + + public override CompletionResult GetCompletion(IConsoleShell shell, string[] args) + { + if (args.Length >= 1) + return CompletionResult.FromHintOptions(CompletionHelper.MapUids(_entManager), Loc.GetString("cmd-ftldisk-hint")); + return CompletionResult.Empty; + } +} diff --git a/Resources/Locale/en-US/shuttles/commands.ftl b/Resources/Locale/en-US/shuttles/commands.ftl new file mode 100644 index 0000000000..37583568e7 --- /dev/null +++ b/Resources/Locale/en-US/shuttles/commands.ftl @@ -0,0 +1,14 @@ +# FTLdiskburner +cmd-ftldisk-desc = Creates an FTL coordinates disk to sail to the map the given EntityID is/on +cmd-ftldisk-help = ftldisk [EntityID] + +cmd-ftldisk-no-transform = Entity {$destination} has no Transform Component! +cmd-ftldisk-no-map = Entity {$destination} has no map! +cmd-ftldisk-no-map-comp = Entity {$destination} is somehow on map {$map} with no map component. +cmd-ftldisk-map-not-init = Entity {$destination} is on map {$map} which is not initialized! Check it's safe to initialize, then initialize the map first or the players will be stuck in place! +cmd-ftldisk-map-paused = Entity {$desintation} is on map {$map} which is paused! Please unpause the map first or the players will be stuck in place. +cmd-ftldisk-planet = Entity {$desintation} is on planet map {$map} and will require an FTL point. It may already exist. +cmd-ftldisk-already-dest-not-enabled = Entity {$destination} is on map {$map} that already has an FTLDestinationComponent, but it is not Enabled! Set this manually for safety. +cmd-ftldisk-requires-ftl-point = Entity {$destination} is on map {$map} that requires a FTL point to travel to! It may already exist. + +cmd-ftldisk-hint = Map netID