Skip to content
Open
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
21 changes: 21 additions & 0 deletions Content.Omu.Server/Toolshed/Errors/EntityNotOnAGridError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Diagnostics;
using Robust.Shared.Toolshed.Errors;
using Robust.Shared.Utility;

namespace Content.Omu.Server.Toolshed.Errors;

[Virtual]
public class EntityNotOnAGridError(EntityUid triggeringEnt) : IConError
{
protected readonly EntityUid TriggeringEnt = triggeringEnt;

public virtual FormattedMessage DescribeInner()
{
return FormattedMessage.FromUnformatted(
$"The entity {PrettyPrint.PrintUserFacingWithType(TriggeringEnt, out var _)} is not on a grid!");
}

public string? Expression { get; set; }
public Vector2i? IssueSpan { get; set; }
public StackTrace? Trace { get; set; }
}
18 changes: 18 additions & 0 deletions Content.Omu.Server/Toolshed/Errors/NoAttachedEntityError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Diagnostics;
using Robust.Shared.Toolshed.Errors;
using Robust.Shared.Utility;

namespace Content.Omu.Server.Toolshed.Errors;

public sealed class NoAttachedEntityError : IConError
{
public FormattedMessage DescribeInner()
{
return FormattedMessage.FromUnformatted(
"You must be attached to an entity to use this.");
}

public string? Expression { get; set; }
public Vector2i? IssueSpan { get; set; }
public StackTrace? Trace { get; set; }
}
11 changes: 11 additions & 0 deletions Content.Omu.Server/Toolshed/Errors/YouAreNotOnAGridError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Robust.Shared.Utility;

namespace Content.Omu.Server.Toolshed.Errors;

public sealed class YouAreNotOnAGridError(EntityUid you) : EntityNotOnAGridError(you)
{
public override FormattedMessage DescribeInner()
{
return FormattedMessage.FromUnformatted($"You ({PrettyPrint.PrintUserFacingWithType(TriggeringEnt, out var _)}) are not on a grid!!");
}
}
85 changes: 85 additions & 0 deletions Content.Omu.Server/Toolshed/OnCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Linq;
using Content.Omu.Server.Toolshed.Errors;
using Robust.Shared.Map;
using Robust.Shared.Toolshed;
using Robust.Shared.Toolshed.Errors;

namespace Content.Omu.Server.Toolshed;

[ToolshedCommand]
public sealed class OnCommand : ToolshedCommand
{
[CommandImplementation("grid")]
public IEnumerable<EntityUid> OnGrid([PipedArgument] IEnumerable<EntityUid> ents, EntityUid grid , [CommandInverted] bool inverted)
{
return FilterGrid(ents, grid, inverted);
}

[CommandImplementation("thisgrid")]
public IEnumerable<EntityUid> OnThisGrid(IInvocationContext ctx, [PipedArgument] IEnumerable<EntityUid> ents, [CommandInverted] bool inverted)
{
return Outer_OnThisX(ctx, ents, inverted, (ents, inverted, invoker)
=> (Transform(invoker).GridUid is { } filter_grid)
? FilterGrid(ents, filter_grid, inverted)
: ReportErrorAndBail(ctx, new YouAreNotOnAGridError(invoker))
);
}

[CommandImplementation("gridof")]
public IEnumerable<EntityUid> OnGridOf(IInvocationContext ctx, [PipedArgument] IEnumerable<EntityUid> ents, EntityUid other_ent, [CommandInverted] bool inverted)
{
return (Transform(other_ent).GridUid is { } filter_grid)
? FilterGrid(ents, filter_grid, inverted)
: ReportErrorAndBail(ctx, new EntityNotOnAGridError(other_ent))
;
}

[CommandImplementation("map")]
public IEnumerable<EntityUid> OnMap([PipedArgument] IEnumerable<EntityUid> ents, int map, [CommandInverted] bool inverted)
{
return FilterMap(ents, new MapId(map), inverted);
}

[CommandImplementation("thismap")]
public IEnumerable<EntityUid> OnThisMap(IInvocationContext ctx, [PipedArgument] IEnumerable<EntityUid> ents, [CommandInverted] bool inverted)
{
return Outer_OnThisX(ctx, ents, inverted, (ents, inverted, invoker) => FilterMap(ents, Transform(invoker).MapID, inverted));
}

[CommandImplementation("mapof")]
public IEnumerable<EntityUid> OnMapOf([PipedArgument] IEnumerable<EntityUid> ents, EntityUid other_ent, [CommandInverted] bool inverted)
{
return FilterMap(ents, Transform(other_ent).MapID, inverted);
}

private IEnumerable<EntityUid> Outer_OnThisX(IInvocationContext ctx, IEnumerable<EntityUid> ents, bool inverted, Func<IEnumerable<EntityUid>, bool, EntityUid, IEnumerable<EntityUid>> inner)
{
if (ctx.Session is null) return ReportErrorAndBail(ctx, new NotForServerConsoleError());
if (ctx.Session.AttachedEntity is { } invoker)
{
return inner(ents, inverted, invoker);
}
else return ReportErrorAndBail(ctx, new NoAttachedEntityError());
}

private IEnumerable<EntityUid> FilterMap(IEnumerable<EntityUid> ents, MapId map, bool inverted)
{
return FilterX(ents, e => Transform(e).MapID == map, inverted);
}

private IEnumerable<EntityUid> FilterGrid(IEnumerable<EntityUid> ents, EntityUid grid, bool inverted)
{
return FilterX(ents, e => Transform(e).GridUid == grid, inverted);
}

private IEnumerable<EntityUid> FilterX(IEnumerable<EntityUid> ents, Func<EntityUid, bool> cond, bool inverted)
{
return ents.Where(e => cond(e) ^ inverted);
}

private IEnumerable<EntityUid> ReportErrorAndBail(IInvocationContext ctx, IConError e)
{
ctx.ReportError(e);
return default!;
}
}
39 changes: 39 additions & 0 deletions Content.Omu.Server/Toolshed/ThisCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Robust.Shared.Map;
using Robust.Shared.Toolshed;
using Robust.Shared.Toolshed.Errors;

namespace Content.Omu.Server.Toolshed;

[ToolshedCommand]
public sealed class ThisCommand : ToolshedCommand
{
[CommandImplementation("grid")]
public EntityUid? ThisGrid(IInvocationContext ctx)
{
return CheckCommonPitfalls(ctx, null, invoker => Transform(invoker).GridUid);
}

[CommandImplementation("map")]
public int ThisMap(IInvocationContext ctx)
{
return CheckCommonPitfalls(ctx, (int) MapId.Nullspace, invoker => (int) Transform(invoker).MapID);
}

private T CheckCommonPitfalls<T>(IInvocationContext ctx, T fail_value, Func<EntityUid, T> and_then_do_this)
{
if (ctx.Session is null)
{
ctx.ReportError(new NotForServerConsoleError());
return fail_value;
}

if (ctx.Session.AttachedEntity is { } invoker)
{
return and_then_do_this(invoker);
}
else
{
return fail_value;
}
}
}
9 changes: 9 additions & 0 deletions Resources/Locale/en-US/_Omu/commands/toolshed-commands.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
command-description-on-grid = Filters the input entities by whether or not they are on the grid with the given ID.
command-description-on-thisgrid = Filters the input entities by whether or not they are on the grid you are currently on.
command-description-on-gridof = Filters the input entities by whether or not they are on the same grid as the given entity.
command-description-on-map = Filters the input entities by whether or not they are on the map with the given ID.
command-description-on-thismap = Filters the input entities by whether or not they are on the map you are currently on.
command-description-on-mapof = Filters the input entities by whether or not they are on the same map as the given entity.

command-description-this-grid = Returns the grid you are on.
command-description-this-map = Returns the ID of the map you are on.
2 changes: 2 additions & 0 deletions Resources/toolshedEngineCommandPerms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
- contains
- isnull
- isempty
- on
- this
- cd
- ls
- stopwatch
Expand Down
Loading