Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
46 changes: 22 additions & 24 deletions Essentials/AutoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,10 @@ public string ScheduledTime
{
_scheduledTime = TimeSpan.Parse(value);
OnPropertyChanged();
if (_scheduledTime != TimeSpan.Zero)
{
Interval = TimeSpan.Zero.ToString();
_nextRun = DateTime.Now.Date + _scheduledTime;
if (_nextRun < DateTime.Now)
_nextRun += TimeSpan.FromDays(1);
}
if (CommandTrigger != Trigger.Scheduled) return;
_nextRun = DateTime.Now.Date + _scheduledTime;
if (_nextRun < DateTime.Now)
_nextRun += TimeSpan.FromDays(1);
}
}

Expand All @@ -78,11 +75,12 @@ public string Interval
{
_interval = TimeSpan.Parse(value);
OnPropertyChanged();
if (_interval != TimeSpan.Zero)
if (CommandTrigger == Trigger.Timed)
{
ScheduledTime = TimeSpan.Zero.ToString(); //I hate myself for this
//ScheduledTime = TimeSpan.Zero.ToString(); //I hate myself for this **FIXED!!!***
_nextRun = DateTime.Now + _interval;
}

}
}

Expand Down Expand Up @@ -122,13 +120,14 @@ public void Update()
if (DateTime.Now < _nextRun)
return;

//double cast here as I'm unsure how casting directly between enum types will work
if (DayOfWeek != DayOfWeek.All && DateTime.Now.DayOfWeek != (System.DayOfWeek)(int)DayOfWeek)
{
//adding one day because I can't be bothered to calculate exact interval
_nextRun += TimeSpan.FromDays(1);
return;
}
if(CommandTrigger == Trigger.Scheduled && Interval == TimeSpan.Zero.ToString())
if (DayOfWeek != DayOfWeek.All && DateTime.Now.DayOfWeek != (System.DayOfWeek)(int)DayOfWeek)
{
//adding one day because I can't be bothered to calculate exact interval
_nextRun += TimeSpan.FromDays(1);
return;
}


if (Steps.Count <= 0)
return;
Expand All @@ -139,14 +138,13 @@ public void Update()
_currentStep++;
_nextRun += step.DelaySpan;

if (_currentStep >= Steps.Count)
{
_currentStep = 0;
if (_scheduledTime != TimeSpan.Zero)
_nextRun = DateTime.Now.Date + _scheduledTime + TimeSpan.FromDays(1);
else
_nextRun = DateTime.Now + _interval;
}
if (_currentStep < Steps.Count) return;
_currentStep = 0;
if(CommandTrigger == Trigger.Scheduled && Interval == TimeSpan.Zero.ToString())
_nextRun = DateTime.Now.Date + _scheduledTime + TimeSpan.FromDays(1);
else if((CommandTrigger != Trigger.Disabled || CommandTrigger != Trigger.Vote) && Interval != TimeSpan.Zero.ToString())

_nextRun = DateTime.Now + _interval;
}


Expand Down
52 changes: 19 additions & 33 deletions Essentials/AutoCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NLog;
using Torch;
using Torch.API;
using Torch.Server.ViewModels;
using Sandbox.Game.World;
using Sandbox.Game.Multiplayer;
using Sandbox.Game.Entities;
Expand All @@ -16,6 +17,8 @@ namespace Essentials
{
public class AutoCommands : IDisposable
{
protected EntityTreeViewModel Tree { get; }

private static AutoCommands _instance;
public static AutoCommands Instance => _instance ?? (_instance = new AutoCommands());
private static readonly Logger Log = LogManager.GetLogger("Essentials");
Expand All @@ -29,58 +32,41 @@ public void Start()
_timer.Start();
}

public void RunOnStart()
private bool CanRun(AutoCommand command)
{
foreach (var command in EssentialsPlugin.Instance.Config.AutoCommands)
switch (command.CommandTrigger)
{
if (command.CommandTrigger != Trigger.OnStart)
return;
else if(command.CommandTrigger == Trigger.OnStart)
{
case Trigger.Disabled:
break;
case Trigger.OnStart:
var a = TimeSpan.Parse(command.Interval);
var b = ((ITorchServer)TorchBase.Instance).ElapsedPlayTime;
if ((a - b).TotalSeconds <= 1 && (a - b).TotalSeconds > 0)
command.RunNow();
}
}
}
private bool CanRun(AutoCommand command)
{
switch (command.CommandTrigger)
{
default:
return false;
return true;
break;
case Trigger.Vote:
break;
case Trigger.Timed:
return true;
case Trigger.Scheduled:
return true;
case Trigger.GridCount:
int gridCount = 0;
foreach (var e in MyEntities.GetEntities())
{
if (e is IMyCubeGrid)
gridCount++;
}
if (gridCount >= command.TriggerCount)
return true;
else return false;
return Tree.Grids.Count >= command.TriggerCount;
case Trigger.PlayerCount:
if (MySession.Static.Players.GetOnlinePlayerCount() >= command.TriggerCount)
return true;
else return false;
return MySession.Static.Players.GetOnlinePlayerCount() >= command.TriggerCount;
case Trigger.SimSpeed:
if (Math.Min(Sync.ServerSimulationRatio, 1) <= command.TriggerRatio)
return true;
else
return false;
return Math.Min(Sync.ServerSimulationRatio, 1) <= command.TriggerRatio;

default:
throw new Exception("fuck it");
}

return false;
}


private void TimerElapsed(object sender, ElapsedEventArgs e)
{
RunOnStart();
foreach (var command in EssentialsPlugin.Instance.Config.AutoCommands)
{
if(!CanRun(command))
Expand Down
5 changes: 5 additions & 0 deletions Essentials/EssentialsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ public EssentialsConfig()
[Display(Name = "MotdURL", Description = "Sets a URL to show to players when they connect. Opens in the steam overlay, if enabled.")]
public string MotdUrl { get => _motdUrl; set => SetValue(ref _motdUrl, value); }

private bool _newUserMotdUrl;
[Display(Name = "Url for New Users Only", Description = "MOTD URL for new users only")]
public bool NewUserMotdUrl{get => _newUserMotdUrl;set => SetValue(ref _newUserMotdUrl, value);}

private bool _stopShips;
[Display(Name = "Stop entities on start", Description = "Stop all entities in the world when the server starts.")]
public bool StopShipsOnStart { get => _stopShips; set => SetValue(ref _stopShips, value); }


private bool _utilityShowPosition;

[Display(Name = "Grid list show position",Description = "Show users the position of all grids they own in the grids list command.")]
Expand Down
11 changes: 10 additions & 1 deletion Essentials/EssentialsPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ private void MotdOnce(IPlayer player)
public void SendMotd(MyPlayer player)
{
long playerId = player.Identity.IdentityId;
if (!string.IsNullOrEmpty(Config.MotdUrl))

if (!string.IsNullOrEmpty(Config.MotdUrl) && !Config.NewUserMotdUrl)
{
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
MyVisualScriptLogicProvider.OpenSteamOverlay(Config.MotdUrl, playerId);
Expand All @@ -233,10 +234,18 @@ public void SendMotd(MyPlayer player)
bool newUser = !Config.KnownSteamIds.Contains(id);
if (newUser)
Config.KnownSteamIds.Add(id);
if (!string.IsNullOrEmpty(Config.MotdUrl) && newUser && Config.NewUserMotdUrl)
{
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
MyVisualScriptLogicProvider.OpenSteamOverlay(Config.MotdUrl, playerId);
else
MyVisualScriptLogicProvider.OpenSteamOverlay($"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}", playerId);
}

if (newUser && !string.IsNullOrEmpty(Config.NewUserMotd))
{
ModCommunication.SendMessageTo(new DialogMessage(MySession.Static.Name, "New User Message Of The Day", Config.NewUserMotd.Replace("%player%", name)), id);

}
else if (!string.IsNullOrEmpty(Config.Motd))
{
Expand Down