Skip to content

Commit 8df1d17

Browse files
committed
Rewrite cleanup commands, add MOTD
1 parent 3d26352 commit 8df1d17

12 files changed

Lines changed: 249 additions & 157 deletions

Essentials/AutoCommand.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using Torch;
8+
using Torch.API;
89
using Torch.Commands;
10+
using Torch.Server;
911

1012
namespace Essentials
1113
{
@@ -31,6 +33,9 @@ private void OnTimerChanged()
3133

3234
private void RunCommand(object state)
3335
{
36+
if (((TorchServer)TorchBase.Instance).State != ServerState.Running)
37+
return;
38+
3439
var autoCommand = (AutoCommand)state;
3540
TorchBase.Instance.Invoke(() =>
3641
{
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
[Category("cleanup")]
13+
public class CleanupModule : CommandModule
14+
{
15+
[Command("scan", "Find grids matching the given conditions: hastype, notype, hassubtype, nosubtype, blockslessthan, blocksgreaterthan, ownedby")]
16+
public void Scan()
17+
{
18+
var count = ScanConditions(Context.Args).Count();
19+
Context.Respond($"Found {count} grids matching the given conditions.");
20+
}
21+
22+
[Command("delete", "Delete grids matching the given conditions")]
23+
public void Delete()
24+
{
25+
var count = 0;
26+
foreach (var grid in ScanConditions(Context.Args))
27+
{
28+
grid.Close();
29+
count++;
30+
}
31+
32+
Context.Respond($"Deleted {count} grids matching the given conditions.");
33+
}
34+
35+
private IEnumerable<MyCubeGrid> ScanConditions(List<string> args)
36+
{
37+
var conditions = new List<Func<MyCubeGrid, bool>>();
38+
39+
for (var i = 0; i < args.Count; i += 2)
40+
{
41+
var arg = args[i];
42+
var parameter = args[i + 1];
43+
44+
switch (arg)
45+
{
46+
case "hastype":
47+
conditions.Add(g => g.HasBlockType(parameter));
48+
break;
49+
case "notype":
50+
conditions.Add(g => !g.HasBlockType(parameter));
51+
break;
52+
case "hassubtype":
53+
conditions.Add(g => g.HasBlockSubtype(parameter));
54+
break;
55+
case "nosubtype":
56+
conditions.Add(g => !g.HasBlockSubtype(parameter));
57+
break;
58+
case "blockslessthan":
59+
conditions.Add(g => BlocksLessThan(g, parameter));
60+
break;
61+
case "blocksgreaterthan":
62+
conditions.Add(g => BlocksGreaterThan(g, parameter));
63+
break;
64+
case "ownedby":
65+
conditions.Add(g => OwnedBy(g, parameter));
66+
break;
67+
}
68+
}
69+
70+
foreach (var grid in MyEntities.GetEntities().OfType<MyCubeGrid>())
71+
{
72+
if (conditions.TrueForAll(func => func(grid)))
73+
yield return grid;
74+
}
75+
}
76+
77+
private bool BlocksLessThan(MyCubeGrid grid, string str)
78+
{
79+
if (int.TryParse(str, out int count))
80+
return grid.BlocksCount < count;
81+
82+
return false;
83+
}
84+
85+
private bool BlocksGreaterThan(MyCubeGrid grid, string str)
86+
{
87+
if (int.TryParse(str, out int count))
88+
return grid.BlocksCount > count;
89+
90+
return false;
91+
}
92+
93+
private bool OwnedBy(MyCubeGrid grid, string str)
94+
{
95+
long identityId;
96+
97+
if (string.Compare(str, "nobody", StringComparison.InvariantCultureIgnoreCase) == 0)
98+
{
99+
return grid.BigOwners.Count == 0;
100+
}
101+
102+
if (string.Compare(str, "pirates", StringComparison.InvariantCultureIgnoreCase) == 0)
103+
{
104+
identityId = MyPirateAntennas.GetPiratesId();
105+
}
106+
else
107+
{
108+
var player = Utilities.GetPlayerByNameOrId(str);
109+
if (player == null)
110+
return false;
111+
112+
identityId = player.IdentityId;
113+
}
114+
115+
return grid.BigOwners.Contains(identityId);
116+
}
117+
}
118+
}

Essentials/Commands/DeleteModule.cs

Lines changed: 0 additions & 150 deletions
This file was deleted.

Essentials/Commands/EntityModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class EntityModule : CommandModule
2727
{
2828
[Command("refresh", "Resyncs all entities for the player running the command.")]
2929
[Permission(MyPromoteLevel.None)]
30-
public void Refresh2()
30+
public void Refresh()
3131
{
3232
if (Context.Player == null)
3333
return;

Essentials/Commands/PlayerModule.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace Essentials
1515
{
1616
public class PlayerModule : CommandModule
1717
{
18+
private EssentialsPlugin _plugin => (EssentialsPlugin)Context.Plugin;
19+
1820
[Command("say", "Say a message as the server.")]
1921
public void Say(string message)
2022
{
@@ -126,5 +128,12 @@ public void Unban(string playerName)
126128
Context.Respond("Player not found.");
127129
}
128130
}
131+
132+
[Command("motd", "Show the server's Message of the Day.")]
133+
[Permission(MyPromoteLevel.None)]
134+
public void Motd()
135+
{
136+
Context.Respond(_plugin.Config.Motd);
137+
}
129138
}
130139
}

Essentials/Essentials.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
4949
</PropertyGroup>
5050
<ItemGroup>
51+
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
52+
<HintPath>packages\NLog.4.4.11\lib\net45\NLog.dll</HintPath>
53+
</Reference>
5154
<Reference Include="PresentationCore" />
5255
<Reference Include="PresentationFramework" />
5356
<Reference Include="Sandbox.Common">
@@ -131,6 +134,8 @@
131134
<ItemGroup>
132135
<Compile Include="AutoCommand.cs" />
133136
<Compile Include="Commands\BlocksModule.cs" />
137+
<Compile Include="Commands\CleanupModule.cs" />
138+
<Compile Include="Commands\ScanModule.cs" />
134139
<Compile Include="Commands\VoxelModule.cs" />
135140
<Compile Include="Commands\WorldModule.cs" />
136141
<Compile Include="EssentialsConfig.cs" />
@@ -151,5 +156,8 @@
151156
<Generator>MSBuild:Compile</Generator>
152157
</Page>
153158
</ItemGroup>
159+
<ItemGroup>
160+
<None Include="packages.config" />
161+
</ItemGroup>
154162
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
155163
</Project>

Essentials/EssentialsConfig.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
namespace Essentials
1010
{
11-
public class EssentialsConfig
11+
public class EssentialsConfig : ViewModel
1212
{
13-
public MTObservableCollection<AutoCommand> AutoCommands { get; } = new MTObservableCollection<AutoCommand>();
13+
public ObservableList<AutoCommand> AutoCommands { get; } = new ObservableList<AutoCommand>();
14+
15+
private string _motd;
16+
public string Motd { get => _motd; set { _motd = value; OnPropertyChanged(); } }
1417
}
1518
}

Essentials/EssentialsControl.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
<local:EssentialsConfig/>
1010
</UserControl.DataContext>
1111
<StackPanel>
12+
<Label Content="Message of the Day"/>
13+
<TextBox Text="{Binding Motd}" AcceptsReturn="true" MinLines="5" TextWrapping="Wrap"/>
1214
<Label Content="AutoCommands"></Label>
15+
<Button Content="Add" Click="AddAutoCommand_OnClick"></Button>
1316
<DataGrid ItemsSource="{Binding AutoCommands, UpdateSourceTrigger=PropertyChanged}" KeyDown="UIElement_OnKeyDown"/>
17+
<Button Content="Save Config" Click="SaveConfig_OnClick"/>
1418
</StackPanel>
1519
</UserControl>

Essentials/EssentialsControl.xaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,15 @@ private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
4545
Plugin.Config.AutoCommands.Remove(item);
4646
}
4747
}
48+
49+
private void SaveConfig_OnClick(object sender, RoutedEventArgs e)
50+
{
51+
Plugin.Save();
52+
}
53+
54+
private void AddAutoCommand_OnClick(object sender, RoutedEventArgs e)
55+
{
56+
Plugin.Config.AutoCommands.Add(new AutoCommand());
57+
}
4858
}
4959
}

0 commit comments

Comments
 (0)