Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
86be6f6
Add cleanup filters "nopower" and "haspower". Add option to stop all …
rexxar-tc Jun 4, 2018
8773f93
Ask Jenkins to please build this against the Torch/client-mod branch.…
rexxar-tc Jun 11, 2018
2121aa6
Add !utility listgrids command to show the user all grids they own. C…
rexxar-tc Jun 11, 2018
e177313
Put MOTD in a dialog because that's nicer than chat.
rexxar-tc Jun 11, 2018
aa8322f
Add 'insideplanet' argument to grid cleanup. Finds grids that are mor…
rexxar-tc Jun 11, 2018
b3ed0f9
Add logging to cleanup
rexxar-tc Jun 11, 2018
250a28d
Remove !utlity listrids because !grids list is apparently a thing.
rexxar-tc Jun 11, 2018
661c8e7
Fix #25 !grids static large command not syncing
rexxar-tc Jun 11, 2018
fdb825a
Implement #38 : cleanup voxels with no grids nearby.
rexxar-tc Jun 11, 2018
10d2567
Add separate MOTD for new users.
rexxar-tc Jun 11, 2018
dfbc5dd
Make AutoCommands suck less.
rexxar-tc Jun 14, 2018
73053d2
Add !cleanup help command.
rexxar-tc Jun 16, 2018
2d9b0fe
Un-break cleanup.
rexxar-tc Jun 18, 2018
dd5b48c
Remove global static logger to make Equinox happy
rexxar-tc Jun 18, 2018
f7a9a21
Complete refactor of auto commands! I even tested it this time!
rexxar-tc Jul 3, 2018
5af3086
Fix a typo in autocommand helptext
rexxar-tc Jul 4, 2018
300dfea
!cleanup list now prints to a dialog window.
rexxar-tc Jul 4, 2018
8c05800
Implement info commands
rexxar-tc Jul 6, 2018
770f7d0
Fix crashes in autocommands
rexxar-tc Jul 6, 2018
6928828
client-mod branch deleted, build against master again
rexxar-tc Jul 7, 2018
58122b0
Add !voxels reset planets
rexxar-tc Jul 8, 2018
dcee1aa
Change cleanup conditions to public. Might change behavior??
rexxar-tc Jul 8, 2018
cf47d52
Fix borked cleanup
rexxar-tc Jul 9, 2018
d726efd
Add %player% wildcard to MOTD. Replaces with receiving player's name.
rexxar-tc Jul 9, 2018
085796f
Fix haspower check crashing with a key not present error
rexxar-tc Jul 9, 2018
de1542c
Add DayOfWeek option to auto commands.
rexxar-tc Jul 9, 2018
9921a0e
Don't be stupid and use ViewModels
rexxar-tc Jul 10, 2018
111442e
Default cleanup to not search for grids with pilots.
rexxar-tc Jul 18, 2018
ece8376
Delay sending MOTD until the player spawns with a character.
rexxar-tc Jul 18, 2018
d695c86
Be careful using copy and paste
Jimmacle Jul 19, 2018
9220a94
Fix entity refresh reflection
Jimmacle Jul 19, 2018
0bc78d2
Actually fix entities refresh command
Jimmacle Jul 19, 2018
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add !cleanup help command.
Add insideplanet and playerdistance conditions
  • Loading branch information
rexxar-tc committed Jun 16, 2018
commit 73053d239d154d300edd81d738a4a6a71d6e6d70
159 changes: 123 additions & 36 deletions Essentials/Commands/CleanupModule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;
Expand All @@ -9,7 +11,10 @@
using Torch.Commands;
using NLog;
using Sandbox.Game.EntityComponents;
using Sandbox.Game.Multiplayer;
using SpaceEngineers.Game.Entities.Blocks;
using Torch.Mod;
using Torch.Mod.Messages;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
using Vector3D = VRageMath.Vector3D;
Expand Down Expand Up @@ -49,6 +54,65 @@ public void Delete()
EssentialsPlugin.Log.Info($"Cleanup deleted {count} grids matching conditions {string.Join(", ", Context.Args)}");
}

[Command("help", "Lists all cleanup conditions.")]
public void Help()
{
var sb = new StringBuilder();
foreach (var c in _conditionLookup)
{
sb.AppendLine($"{c.Command}{(string.IsNullOrEmpty(c.InvertCommand) ? string.Empty : $" ({c.InvertCommand})")}:");
sb.AppendLine($" {c.HelpText}");
}

if(!Context.SentBySelf)
ModCommunication.SendMessageTo(new DialogMessage("Cleanup help", null, sb.ToString()), Context.Player.SteamUserId);
else
Context.Respond(sb.ToString());
}

public CleanupModule()
{
var methods = typeof(CleanupModule).GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
_conditionLookup = new List<Condition>(methods.Length);
foreach (var m in methods)
{
var a = m.GetCustomAttribute<ConditionAttribute>();
if (a == null)
continue;

if (m.ReturnType != typeof(bool))
{
EssentialsPlugin.Log.Warn($"Command {a.Command} does not return a bool! Skipping!");
continue;
}
var p = m.GetParameters();
if (p.Length == 0 || p[0].ParameterType != typeof(MyCubeGrid))
{
EssentialsPlugin.Log.Warn($"Command {a.Command} does not accept MyCubeGrid as first parameter! Skipping!");
continue;
}

Func<MyCubeGrid, string, bool> func;

if (p.Length == 1)
func = (grid, s) => (bool)m.Invoke(this, new object[] {grid});
else if (p.Length > 1 && p[1].ParameterType == typeof(string))
func = (grid, s) => (bool)m.Invoke(this, new object[] {grid, s});
else
throw new InvalidBranchException();

var c = new Condition();
c.Command = a.Command;
c.InvertCommand = a.InvertCommand;
c.HelpText = a.HelpText;
c.Action = func;

_conditionLookup.Add(c);
}
}

private List<Condition> _conditionLookup;

private IEnumerable<MyCubeGrid> ScanConditions(IReadOnlyList<string> args)
{
var conditions = new List<Func<MyCubeGrid, bool>>();
Expand All @@ -61,44 +125,17 @@ private IEnumerable<MyCubeGrid> ScanConditions(IReadOnlyList<string> args)
var arg = args[i];
var parameter = args[i + 1];

switch (arg)
foreach (var condition in _conditionLookup)
{
case "hastype":
conditions.Add(g => g.HasBlockType(parameter));
break;
case "notype":
conditions.Add(g => !g.HasBlockType(parameter));
break;
case "hassubtype":
conditions.Add(g => g.HasBlockSubtype(parameter));
break;
case "nosubtype":
conditions.Add(g => !g.HasBlockSubtype(parameter));
break;
case "blockslessthan":
conditions.Add(g => BlocksLessThan(g, parameter));
break;
case "blocksgreaterthan":
conditions.Add(g => BlocksGreaterThan(g, parameter));
break;
case "ownedby":
conditions.Add(g => OwnedBy(g, parameter));
break;
case "name":
conditions.Add(g => NameMatches(g, parameter));
break;
case "nopower":
conditions.Add(g=>!HasPower(g));
break;
case "haspower":
conditions.Add(g => HasPower(g));
break;
case "insideplanet":
conditions.Add(g => InsidePlanet(g));
break;
default:
if (condition.Command.Equals(arg))
conditions.Add(g => condition.Action(g, parameter));
else if (condition.InvertCommand.Equals(arg))
conditions.Add(g => !condition.Action(g, parameter));
else
{
Context.Respond($"Unknown argument '{arg}'");
yield break;
}
}
}

Expand All @@ -110,12 +147,17 @@ private IEnumerable<MyCubeGrid> ScanConditions(IReadOnlyList<string> args)
}
}

[Condition("name", helpText: "Finds grids with a matching name. Accepts regex format.")]
private bool NameMatches(MyCubeGrid grid, string str)
{
if (string.IsNullOrEmpty(grid.DisplayName))
return false;

var regex = new Regex(str);
return regex.IsMatch(grid.DisplayName ?? "");
return regex.IsMatch(grid.DisplayName);
}

[Condition("blockslessthan", helpText: "Finds grids with less than the given number of blocks.")]
private bool BlocksLessThan(MyCubeGrid grid, string str)
{
if (int.TryParse(str, out int count))
Expand All @@ -124,6 +166,7 @@ private bool BlocksLessThan(MyCubeGrid grid, string str)
return false;
}

[Condition("blocksgreaterthan", helpText: "Finds grids with more than the given number of blocks.")]
private bool BlocksGreaterThan(MyCubeGrid grid, string str)
{
if (int.TryParse(str, out int count))
Expand All @@ -132,6 +175,7 @@ private bool BlocksGreaterThan(MyCubeGrid grid, string str)
return false;
}

[Condition("haspower", "nopower", "Finds grids with, or without power.")]
private bool HasPower(MyCubeGrid grid)
{
foreach (var b in grid.GetFatBlocks())
Expand All @@ -147,6 +191,7 @@ private bool HasPower(MyCubeGrid grid)
return false;
}

[Condition("insideplanet", helpText: "Finds grids that are trapped inside planets.")]
private bool InsidePlanet(MyCubeGrid grid)
{
var s = grid.PositionComp.WorldVolume;
Expand All @@ -170,6 +215,25 @@ private bool InsidePlanet(MyCubeGrid grid)
return false;
}

[Condition("playerdistancelessthan", "playerdistancegreaterthan", "Finds grids that are further than the given distance from players.")]
private bool PlayerDistanceLessThan(MyCubeGrid grid, string str)
{
double dist;
if (!double.TryParse(str, out dist))
{
Context.Respond("Couldn't parse distance");
return false;
}
dist *= dist;
foreach (var player in MySession.Static.Players.GetOnlinePlayers())
{
if (Vector3D.DistanceSquared(player.GetPosition(), grid.PositionComp.GetPosition()) < dist)
return true;
}
return false;
}

[Condition("ownedby", helpText: "Finds grids owned by the given player. Can specify player name, IdentityId, 'nobody', or 'pirates'.")]
private bool OwnedBy(MyCubeGrid grid, string str)
{
long identityId;
Expand All @@ -194,5 +258,28 @@ private bool OwnedBy(MyCubeGrid grid, string str)

return grid.BigOwners.Contains(identityId);
}

private class Condition
{
public string Command;
public string InvertCommand;
public string HelpText;
public Func<MyCubeGrid, string, bool> Action;
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
private sealed class ConditionAttribute : Attribute
{
public string Command;
public string InvertCommand;
public string HelpText;

public ConditionAttribute(string command, string invertCommand = null, string helpText = null)
{
Command = command;
InvertCommand = invertCommand;
HelpText = helpText;
}
}
}
}