/xshutdown [time] -- Shutsdown the server after the specified time (i.e. 2h or 2s or 5m or 1d) (not supported: 2h1m)
The console can also use it again as just /xshutdown to cancel a shutdown.
Requirements:
To compile this, you need to download an extra dll and put it into your MCDzienny folder.
Link: Microsoft.VisualBasic.dll
The Code:
- Code: Select all
// References: "Microsoft.VisualBasic.dll"
using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections.Generic;
using System.Timers;
namespace MCDzienny
{
public class CmdXShutdown : Command
{
public override string name { get { return "xshutdown"; } }
public override string shortcut { get { return "xshut"; } }
public override string type { get { return "mod"; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return (Group.Find("owner") == null ? (Group.Find("superwhore") == null ? LevelPermission.Nobody : Group.Find("superwhore").Permission) : Group.Find("owner").Permission); } }
private Timer shutdownTimer = new Timer();
private bool shutting = false;
private void OnTick(object sender, ElapsedEventArgs e)
{
this.shutdownTimer.Enabled = false;
Player.GlobalMessage("Server shutting down.");
Server.s.Log("Server shutting down.", false);
Bypass("shutdown", "Shutdown");
}
public override void Init()
{
shutdownTimer.Elapsed += this.OnTick;
}
public override void Use(Player p, string message)
{
if(shutting) {
if(p != null) { Player.SendMessage(p, "Only the console can cancel shutdowns, sorry. :/"); return; }
shutting = false;
this.shutdownTimer.Enabled = false;
Player.SendMessage(null, "Shutdown cancelled.");
return;
}
ShutdownTime time;
if(!ShutdownTime.TryParse(message, out time)) {
this.Help(p);
return;
}
shutting = true;
this.shutdownTimer.Interval = time.GetMilliseconds();
this.shutdownTimer.Enabled = true;
string timestr = time.Quantity.ToString() + " " + (time.Quantity == 1 ? time.Type.ToString().Substring(0, time.Type.ToString().Length - 1) : time.Type.ToString());
if(p != null) {
Player.SendMessage(null, p.name + " has scheduled the server for shutdown in " + timestr);
}
Player.SendMessage(p, "Server scheduled for shutdown in " + timestr);
}
private void Bypass(string name, string command)
{
object obj = (object)Command.all.Find(name);
NewLateBinding.LateCall(obj, null, command, null, null, null, null, true);
}
public override void Help(Player p)
{
Player.SendMessage(p, "/xshutdown [time] -- Shutsdown the server after the specified time (i.e. 2h or 2s or 5m or 1d) (not supported: 2h1m)");
Player.SendMessage(p, "/xshutdown -- when used again will cancel the shutdown (Console Only)");
}
}
public class ShutdownTime
{
private static bool ContainsOtherThan(List<char> these, string source, List<char> mustcontainthese)
{
if(source == "") { return true; }
source = source.ToLower();
if(mustcontainthese.Count > 0) {
for(int x = 0; x < mustcontainthese.Count; x++) {
if(!source.Contains(mustcontainthese[x].ToString())) { return true; }
}
}
if(these.Count > 0) {
for(int x = 0; x < source.Length; x++) {
if(!these.Contains(source[x])) { return true; }
}
}
return false;
}
private static bool IsNumeric(object obj) { double d; return double.TryParse(obj.ToString(), out d); }
public static ShutdownTime Parse(string input)
{
ShutdownTime ss = new ShutdownTime();
int quan;
TimeType tt;
if(input == "") { input = "default"; }
if(input.Length < 2) {
throw new ArgumentException("Invalid input.");
}
if(input == "default") { input = "20s"; }
List<char> t = new List<char>();
t.Add('h');
t.Add('s');
t.Add('m');
t.Add('d');
for(int x = 0; x <= 9; x++) { t.Add(Convert.ToChar(x.ToString())); }
if(ContainsOtherThan(t, input, new List<char>())) {
throw new ArgumentException("Invalid input.");
}
if(IsNumeric(input[input.Length - 1])) {
throw new ArgumentException("Invalid input.");
}
if(!IsNumeric(input.Substring(0, input.Length - 1))) { throw new ArgumentException("Invalid input."); }
quan = Convert.ToInt32(input.Substring(0, input.Length - 1));
Dictionary<char, TimeType> convert = new Dictionary<char, TimeType>();
convert.Add('h', TimeType.Hours);
convert.Add('s', TimeType.Seconds);
convert.Add('m', TimeType.Minutes);
convert.Add('d', TimeType.Days);
if(!convert.TryGetValue(input[input.Length - 1], out tt)) { throw new ArgumentException("Invalid input."); }
if(quan == 0) { throw new ArgumentException("Invalid input."); }
ss.Quantity = quan;
ss.Type = tt;
return ss;
}
public static bool TryParse(string input, out ShutdownTime time)
{
try {
time = ShutdownTime.Parse(input);
return true;
} catch {
time = default(ShutdownTime);
return false;
}
}
public enum TimeType
{
Seconds,
Minutes,
Hours,
Days
}
public int Quantity = 20;
public TimeType Type = TimeType.Seconds;
public int GetMilliseconds()
{
switch(Type)
{
case TimeType.Seconds:
return this.Quantity * 1000;
case TimeType.Minutes:
return this.Quantity * 60000;
case TimeType.Hours:
return this.Quantity * 3600000;
case TimeType.Days:
return this.Quantity * 86400000;
}
return this.Quantity;
}
}
}