Skip to content

Commit f3e341e

Browse files
authored
Merge pull request #199 from N1Ran/master
Some needed updates
2 parents 2b3d43e + 54cf02f commit f3e341e

4 files changed

Lines changed: 146 additions & 30 deletions

File tree

‎Essentials/AutoCommand.cs‎

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,6 @@ public string Name
5757
set => SetValue(ref _name, value);
5858
}
5959

60-
//[Display(Name = "Scheduled Time", GroupName = "Schedule", Description = "Sets a time of day for this command to be run. Format is HH:MM:SS. MUST use 24 hour format! Will be reset to zero if Interval is set.")]
61-
[Display(Visible = false)]
62-
public string ScheduledTime
63-
{
64-
get => _scheduledTime.ToString();
65-
set
66-
{
67-
_scheduledTime = TimeSpan.Parse(value);
68-
OnPropertyChanged();
69-
}
70-
}
71-
7260
[Display(Order = 2, Description = "Sets an interval/Time for this command to be repeated. Format is HH:MM:SS.")]
7361
public string Interval
7462
{
@@ -79,7 +67,6 @@ public string Interval
7967
OnPropertyChanged();
8068
if (CommandTrigger == Trigger.Timed)
8169
{
82-
//ScheduledTime = TimeSpan.Zero.ToString(); //I hate myself for this **FIXED!!!***
8370
_nextRun = DateTime.Now + _interval;
8471
}
8572

@@ -157,6 +144,7 @@ public void Update()
157144

158145
if (_currentStep < Steps.Count) return;
159146
_currentStep = 0;
147+
_cTokenSource?.Dispose();
160148
_nextRun = _trigger == Trigger.Scheduled
161149
? DateTime.Now.Date + _interval + TimeSpan.FromDays(1)
162150
: _nextRun = DateTime.Now + _interval;
@@ -204,26 +192,54 @@ public override string ToString()
204192
}
205193
}
206194

195+
private CancellationTokenSource _cTokenSource;
207196

208197
/// <summary>
209198
/// Runs the command and all steps immediately, in a new thread
210199
/// </summary>
211-
internal void RunNow()
200+
internal async void RunNow()
212201
{
213-
Task.Run(() =>
202+
_cTokenSource = new CancellationTokenSource();
203+
var token = _cTokenSource.Token;
204+
205+
await Task.Run(() =>
214206
{
215-
foreach (var step in Steps)
207+
while (!token.IsCancellationRequested)
216208
{
217-
step.RunStep();
218-
Thread.Sleep(step.DelaySpan);
209+
foreach (var step in Steps)
210+
{
211+
if (token.IsCancellationRequested)
212+
{
213+
break;
214+
}
215+
step.RunStep();
216+
Thread.Sleep(step.DelaySpan);
217+
}
219218
}
220-
});
219+
}, token);
220+
221+
_cTokenSource.Dispose();
222+
}
223+
224+
internal void Cancel()
225+
{
226+
Log.Info($"Cancelling autocommand {_name}");
227+
_cTokenSource?.Cancel();
228+
_currentStep = 0;
229+
_nextRun = _trigger == Trigger.Scheduled
230+
? DateTime.Now.Date + _interval + TimeSpan.FromDays(1)
231+
: _nextRun = DateTime.Now + _interval;
221232
}
222233

223234
public override string ToString()
224235
{
225236
return $"{Name} : {_trigger.ToString()} : {Steps.Count}";
226237
}
238+
239+
internal bool IsRunning()
240+
{
241+
return _currentStep > 0 || _cTokenSource != null && _cTokenSource?.IsCancellationRequested == false;
242+
}
227243
}
228244

229245
public enum Gtl
@@ -233,7 +249,6 @@ public enum Gtl
233249
Equal
234250
}
235251

236-
//TODO Remove Scheduled
237252
public enum Trigger
238253
{
239254
Disabled,

‎Essentials/AutoCommands.cs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ public void Start()
4040

4141
private bool CanRun(AutoCommand command)
4242
{
43+
if (MySession.Static?.Ready == false) return false;
44+
4345
switch (command.CommandTrigger)
4446
{
4547
case Trigger.Disabled:
4648
return false;
4749
case Trigger.OnStart:
48-
if (command.Completed || MySession.Static?.Ready == false)break;
50+
if (command.Completed)break;
4951
command.Completed = true;
5052
command.RunNow();
51-
5253
break;
5354
case Trigger.Vote:
5455
break;
@@ -73,11 +74,11 @@ private bool CanRun(AutoCommand command)
7374

7475
if (command.Compare == GreaterThan)
7576
{
76-
return MySession.Static.Players.GetOnlinePlayerCount() > command.TriggerCount;
77+
return MySession.Static?.Players.GetOnlinePlayerCount() > command.TriggerCount;
7778
}
7879
else if (command.Compare == LessThan)
7980
{
80-
return MySession.Static.Players.GetOnlinePlayerCount() < command.TriggerCount;
81+
return MySession.Static?.Players.GetOnlinePlayerCount() < command.TriggerCount;
8182
}
8283
break;
8384

‎Essentials/Commands/AdminModule.cs‎

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,119 @@ public void ListPlayers()
7878
}
7979

8080

81-
[Command("runauto", "Runs the auto command with the given name immediately")]
81+
[Command("runauto", "Runs the auto command with the given commandName immediately")]
8282
[Permission(MyPromoteLevel.Admin)]
8383
public void RunAuto(string name)
8484
{
8585
var command = EssentialsPlugin.Instance.Config.AutoCommands.FirstOrDefault(c => c.Name.Equals(name));
8686
if (command == null)
8787
{
88-
Context.Respond($"Couldn't find an auto command with the name {name}");
88+
Context.Respond($"Couldn't find an auto command with the commandName {name}");
8989
return;
9090
}
91-
91+
Context.Respond($"Running the autocommand {command.Name}");
9292
command.RunNow();
9393
}
9494

95+
[Command("cancelauto", "Cancels the auto command with the given commandName immediately")]
96+
[Permission(MyPromoteLevel.Admin)]
97+
public void EndAuto(string commandName)
98+
{
99+
var command = EssentialsPlugin.Instance.Config.AutoCommands.FirstOrDefault(c => c.Name.Equals(commandName));
100+
if (command == null)
101+
{
102+
Context.Respond($"Couldn't find an auto command with the commandName {commandName}");
103+
return;
104+
}
105+
Context.Respond($"AutoCommand {command.Name} cancelled");
106+
command.Cancel();
107+
}
108+
109+
[Command("cancelautobyindex", "Cancels the auto command with the given index immediately")]
110+
[Permission(MyPromoteLevel.Admin)]
111+
public void EndByOrder(int commandNumber = 0)
112+
{
113+
var commands = new List<AutoCommand>(EssentialsPlugin.Instance.Config.AutoCommands.Where(x=>x.IsRunning()));
114+
115+
if (commands.Count == 0)
116+
{
117+
Context.Respond("No active autocommand found");
118+
return;
119+
}
120+
121+
if (commandNumber < 1 || commandNumber > commands.Count)
122+
{
123+
Context.Respond($"{commandNumber} number is out of range");
124+
return;
125+
}
126+
var command = commands[commandNumber - 1];
127+
if (command == null)
128+
{
129+
Context.Respond($"Couldn't find an auto command from provided number");
130+
return;
131+
}
132+
Context.Respond($"AutoCommand {command.Name} cancelled");
133+
command.Cancel();
134+
}
135+
136+
[Command("listauto", "Lists all detected autocommands ")]
137+
[Permission(MyPromoteLevel.Admin)]
138+
public void ListAuto()
139+
{
140+
var commands = EssentialsPlugin.Instance.Config.AutoCommands;
141+
StringBuilder sb = new StringBuilder();
142+
int count = 1;
143+
144+
foreach (var command in commands)
145+
{
146+
sb.AppendLine((count++) + " " + command.Name);
147+
}
148+
149+
if (Context.Player == null)
150+
{
151+
Context.Respond("Current AutoCommands:");
152+
Context.Respond(sb.ToString());
153+
154+
}
155+
156+
else
157+
{
158+
ModCommunication.SendMessageTo(new DialogMessage("Current AutoCommands",$"Found {commands.Count} Commands",sb.ToString()), Context.Player.SteamUserId);
159+
}
160+
}
161+
162+
[Command("listrunningauto", "Lists all running autocommands ")]
163+
[Permission(MyPromoteLevel.Admin)]
164+
public void ListAutoRunning()
165+
{
166+
var commands = new List<AutoCommand>(EssentialsPlugin.Instance.Config.AutoCommands.Where(x=>x.IsRunning()));
167+
168+
if (commands.Count == 0)
169+
{
170+
Context.Respond("No active autocommand found");
171+
return;
172+
}
173+
StringBuilder sb = new StringBuilder();
174+
int count = 1;
175+
176+
foreach (var command in commands)
177+
{
178+
sb.AppendLine((count++) + " " + command.Name);
179+
}
180+
181+
if (Context.Player == null)
182+
{
183+
Context.Respond("Current AutoCommands:");
184+
Context.Respond(sb.ToString());
185+
186+
}
187+
188+
else
189+
{
190+
ModCommunication.SendMessageTo(new DialogMessage("Current AutoCommands",$"Found {commands.Count} Commands",sb.ToString()), Context.Player.SteamUserId);
191+
}
192+
}
193+
95194
[Command("set toolbar", "Makes your current toolbar the new default toolbar for new players.")]
96195
[Permission(MyPromoteLevel.Admin)]
97196
public void SetToolbar()

‎Essentials/Commands/BlocksModule.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void RemoveSubtype(string subtype)
9292
}
9393

9494
foreach (var x in toRemove)
95-
x.CubeGrid.RazeBlock(x.Position);
95+
x.CubeGrid?.RemoveBlock(x);
9696
Context.Respond($"Removed {toRemove.Count} blocks of subtype {subtype}.");
9797
}
9898

@@ -111,7 +111,7 @@ public void RemoveType(string type)
111111
}
112112

113113
foreach (var x in toRemove)
114-
x.CubeGrid.RazeBlock(x.Position);
114+
x.CubeGrid?.RemoveBlock(x);
115115
Context.Respond($"Removed {toRemove.Count} blocks of type {type}.");
116116
}
117117

@@ -211,7 +211,8 @@ public bool IsBlockTypeOf(MyFunctionalBlock block, BlockCategory category)
211211
case BlockCategory.Power:
212212
return block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_Reactor) ||
213213
block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_BatteryBlock) ||
214-
block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_SolarPanel);
214+
block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_SolarPanel) ||
215+
block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_FueledPowerProducer);
215216
case BlockCategory.Production:
216217
return block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_Assembler) ||
217218
block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_Refinery) ||

0 commit comments

Comments
 (0)