Skip to content

Commit a183bf2

Browse files
committed
Add commands
1 parent f1bb697 commit a183bf2

8 files changed

Lines changed: 214 additions & 7 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Sandbox.Game.Entities;
7+
using Torch.Commands;
8+
using VRage.Game.ModAPI;
9+
10+
namespace Essentials
11+
{
12+
[Category("delete")]
13+
public class DeleteModule : CommandModule
14+
{
15+
[Command("grids nosubtype", "Delete all grids that don't have a block of the given subtype.")]
16+
public void DeleteBySubtype(string subtype)
17+
{
18+
var count = 0;
19+
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>())
20+
{
21+
if (ShouldRemove(grid))
22+
{
23+
grid.Close();
24+
count++;
25+
}
26+
}
27+
28+
Context.Respond($"Deleted {count} grids missing the block subtype '{subtype}'.");
29+
30+
bool ShouldRemove(MyCubeGrid grid)
31+
{
32+
foreach (var block in grid.GetBlocks())
33+
{
34+
var id = block.BlockDefinition.Id.SubtypeId.String;
35+
if (string.Compare(id, subtype, StringComparison.InvariantCultureIgnoreCase) == 0)
36+
return false;
37+
}
38+
39+
return true;
40+
}
41+
}
42+
43+
[Command("grids ownedby", "Delete grids that the given player owns the majority of.")]
44+
public void DeleteByOwner(string name)
45+
{
46+
var player = Utilities.GetPlayerByNameOrId(name);
47+
if (player == null)
48+
{
49+
Context.Respond($"Player '{name}' not found.");
50+
return;
51+
}
52+
53+
var count = 0;
54+
foreach (var grid in MyEntities.GetEntities().OfType<IMyCubeGrid>())
55+
{
56+
if (grid.BigOwners.Contains(player.IdentityId))
57+
{
58+
grid.Close();
59+
count++;
60+
}
61+
}
62+
63+
Context.Respond($"Deleted {count} grids owned by '{name}.'");
64+
}
65+
66+
[Command("grids blockslessthan", "Delete grids with fewer than X blocks.")]
67+
public void DeleteBlocksLessThan(int minBlocks)
68+
{
69+
var count = 0;
70+
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>())
71+
{
72+
if (grid.BlocksCount < minBlocks)
73+
{
74+
grid.Close();
75+
count++;
76+
}
77+
}
78+
79+
Context.Respond($"Deleted {count} grids with less than {minBlocks} blocks.");
80+
}
81+
82+
public void DeleteBlocksGreaterThan(int maxBlocks)
83+
{
84+
var count = 0;
85+
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>())
86+
{
87+
if (grid.BlocksCount > maxBlocks)
88+
{
89+
grid.Close();
90+
count++;
91+
}
92+
}
93+
94+
Context.Respond($"Deleted {count} grids with greater than {maxBlocks} blocks.");
95+
}
96+
97+
[Command("floating", "Delete all floating objects.")]
98+
public void DeleteFloating()
99+
{
100+
var count = 0;
101+
foreach (var floating in MyEntities.GetEntities().OfType<IMyFloatingObject>())
102+
{
103+
floating.Close();
104+
count++;
105+
}
106+
107+
Context.Respond($"Deleted {count} floating objects.");
108+
}
109+
}
110+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace Essentials
1616
{
1717
[Category("entity")]
18-
public class EntityCommands : CommandModule
18+
public class EntityModule : CommandModule
1919
{
2020
[Command("stop", "Stops an entity from moving")]
2121
[Permission(MyPromoteLevel.SpaceMaster)]
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
using VRage.Game.Entity.EntityComponents;
1111
using VRage.Game.ModAPI;
1212
using VRage.ModAPI;
13+
using VRageMath;
14+
using VRage.Game;
1315

1416
namespace Essentials
1517
{
1618
[Category("grid")]
17-
public class GridCommands : CommandModule
19+
public class GridModule : CommandModule
1820
{
1921
[Command("setowner", "Sets grid ownership to the given player or ID.", "Usage: setowner <grid> <newowner>")]
2022
[Permission(MyPromoteLevel.SpaceMaster)]
@@ -49,9 +51,29 @@ public void SetOwner(string gridName, string playerName)
4951
if (ownerComp == null)
5052
return false;
5153

54+
cubeBlock?.ChangeOwner(0, MyOwnershipShareModeEnum.All);
5255
cubeBlock?.ChangeOwner(identityId, ownerComp.ShareMode);
5356
return false;
5457
});
5558
}
59+
60+
[Command("static large", "Makes all large grids static.")]
61+
public void StaticLarge()
62+
{
63+
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>().Where(g => g.GridSizeEnum == MyCubeSize.Large))
64+
grid.ConvertToStatic();
65+
}
66+
67+
[Command("list", "List all grids owned by you.")]
68+
public void List()
69+
{
70+
var sb = new StringBuilder("Grids:\n");
71+
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>())
72+
{
73+
if (grid.BigOwners.Contains(Context.Player.IdentityId))
74+
sb.AppendLine($"{grid.DisplayName}: {grid.PositionComp.GetPosition().ToString("N")}");
75+
}
76+
Context.Respond(sb.ToString());
77+
}
5678
}
5779
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Essentials
1212
{
13-
public class PlayerCommands : CommandModule
13+
public class PlayerModule : CommandModule
1414
{
1515
[Command("w", "Send a private message to another player.")]
1616
public void Whisper(string playerName)

Essentials/Commands/WorldModule.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Sandbox.Game.Entities;
7+
using Sandbox.Game.World;
8+
using Torch.Commands;
9+
10+
namespace Essentials.Commands
11+
{
12+
public class WorldModule : CommandModule
13+
{
14+
[Command("identity clean", "Remove identities that have not logged on in X days.")]
15+
public void CleanIdentities(int days)
16+
{
17+
var idents = MySession.Static.Players.GetAllIdentities().ToList();
18+
var cutoff = DateTime.Now - TimeSpan.FromDays(days);
19+
foreach (var identity in idents)
20+
{
21+
if (identity.LastLoginTime < cutoff)
22+
MySession.Static.Players.RemoveIdentity(identity.IdentityId);
23+
}
24+
}
25+
26+
[Command("identity purge", "Remove identities AND the grids they own if they have not logged on in X days.")]
27+
public void PurgeIdentities(int days)
28+
{
29+
var grids = MyEntities.GetEntities().OfType<MyCubeGrid>().ToList();
30+
var idents = MySession.Static.Players.GetAllIdentities().ToList();
31+
var cutoff = DateTime.Now - TimeSpan.FromDays(days);
32+
foreach (var identity in idents)
33+
{
34+
if (identity.LastLoginTime < cutoff)
35+
{
36+
MySession.Static.Players.RemoveIdentity(identity.IdentityId);
37+
foreach (var grid in grids)
38+
{
39+
if (grid.BigOwners.Contains(identity.IdentityId))
40+
grid.Close();
41+
}
42+
}
43+
}
44+
45+
}
46+
}
47+
}

Essentials/Essentials.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,16 @@
129129
<Reference Include="WindowsBase" />
130130
</ItemGroup>
131131
<ItemGroup>
132-
<Compile Include="EntityCommands.cs" />
132+
<Compile Include="Commands\WorldModule.cs" />
133+
<Compile Include="EssentialsConfig.cs" />
134+
<Compile Include="Commands\DeleteModule.cs" />
135+
<Compile Include="Commands\EntityModule.cs" />
133136
<Compile Include="EssentialsControl.xaml.cs">
134137
<DependentUpon>EssentialsControl.xaml</DependentUpon>
135138
</Compile>
136139
<Compile Include="EssentialsPlugin.cs" />
137-
<Compile Include="GridCommands.cs" />
138-
<Compile Include="PlayerCommands.cs" />
140+
<Compile Include="Commands\GridModule.cs" />
141+
<Compile Include="Commands\PlayerModule.cs" />
139142
<Compile Include="Properties\AssemblyInfo.cs" />
140143
<Compile Include="Utilities.cs" />
141144
</ItemGroup>

Essentials/EssentialsConfig.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Essentials
8+
{
9+
public class EssentialsConfig
10+
{
11+
12+
}
13+
}

Essentials/EssentialsPlugin.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using System.Windows.Controls;
77
using Torch;
8+
using Torch.API;
89
using Torch.API.Plugins;
910

1011
namespace Essentials
@@ -13,17 +14,28 @@ namespace Essentials
1314
public class EssentialsPlugin : TorchPluginBase, IWpfPlugin
1415
{
1516
private EssentialsControl _control;
17+
private Persistent<EssentialsConfig> _config;
1618

1719
/// <inheritdoc />
1820
public UserControl GetControl() => _control ?? (_control = new EssentialsControl());
1921

22+
/// <inheritdoc />
23+
public override void Init(ITorchBase torch)
24+
{
25+
base.Init(torch);
26+
27+
}
28+
2029
/// <inheritdoc />
2130
public override void Update()
2231
{
2332

2433
}
2534

2635
/// <inheritdoc />
27-
public override void Dispose() {}
36+
public override void Dispose()
37+
{
38+
39+
}
2840
}
2941
}

0 commit comments

Comments
 (0)