Delay command should work in milliseconds. Fixes #475.

This commit is contained in:
UnknownShadow200 2017-07-09 23:36:38 +10:00
parent ebab87ca9e
commit c5b642f1e1
14 changed files with 64 additions and 50 deletions

View File

@ -54,7 +54,7 @@ namespace MCGalaxy.Commands {
/// <summary> Attempts to parse the given argument as an timespan in short form. </summary>
public static bool GetTimespan(Player p, string input, ref TimeSpan span,
string action, char defUnit) {
string action, string defUnit) {
span = TimeSpan.Zero;
try {
span = input.ParseShort(defUnit);

View File

@ -158,7 +158,7 @@ namespace MCGalaxy.Commands.Maintenance {
}
TimeSpan span = TimeSpan.Zero;
if (!CommandParser.GetTimespan(p, args[2], ref span, "set time spent to", 'm')) return;
if (!CommandParser.GetTimespan(p, args[2], ref span, "set time spent to", "m")) return;
if (who != null) {
setter(span);

View File

@ -45,7 +45,7 @@ namespace MCGalaxy.Commands.Moderation {
} else {
if (args.Length < 2) { Help(p); return; }
TimeSpan duration = TimeSpan.Zero;
if (!CommandParser.GetTimespan(p, args[1], ref duration, "freeze for", 'm')) return;
if (!CommandParser.GetTimespan(p, args[1], ref duration, "freeze for", "m")) return;
string reason = args.Length > 2 ? args[2] : "";
reason = ModActionCmd.ExpandReason(p, reason);

View File

@ -42,7 +42,7 @@ namespace MCGalaxy.Commands.Moderation {
string[] parts = message.SplitSpaces();
if (parts.Length >= 2) {
if (!CommandParser.GetTimespan(p, parts[1], ref delta, "highlight the past", 's')) return;
if (!CommandParser.GetTimespan(p, parts[1], ref delta, "highlight the past", "s")) return;
} else if (ParseTimespan(parts[0], out delta)) {
parts[0] = p.name;
} else {
@ -99,7 +99,7 @@ namespace MCGalaxy.Commands.Moderation {
static bool ParseTimespan(string input, out TimeSpan delta) {
delta = TimeSpan.Zero;
try { delta = input.ParseShort('s'); return true;
try { delta = input.ParseShort("s"); return true;
} catch (ArgumentException) { return false;
} catch (FormatException) { return false;
}

View File

@ -44,7 +44,7 @@ namespace MCGalaxy.Commands.Moderation {
if (args.Length < 2) { Help(p); return; }
TimeSpan duration = TimeSpan.Zero;
if (!CommandParser.GetTimespan(p, args[1], ref duration, "mute for", 's')) return;
if (!CommandParser.GetTimespan(p, args[1], ref duration, "mute for", "s")) return;
string reason = args.Length > 2 ? args[2] : "";
reason = ModActionCmd.ExpandReason(p, reason);

View File

@ -47,7 +47,7 @@ namespace MCGalaxy.Commands.Moderation {
}
TimeSpan span = TimeSpan.FromHours(1);
if (args.Length > 1 && !CommandParser.GetTimespan(p, args[1], ref span, "temp ban for", 'm')) return;
if (args.Length > 1 && !CommandParser.GetTimespan(p, args[1], ref span, "temp ban for", "m")) return;
if (span.TotalSeconds < 1) { Player.Message(p, "Cannot temp ban someone for less than a second."); return; }
reason = ModActionCmd.ExpandReason(p, reason);

View File

@ -58,7 +58,7 @@ namespace MCGalaxy.Commands.Moderation {
Group newRank = Matcher.FindRanks(p, args[1]);
if (newRank == null) return;
TimeSpan duration = TimeSpan.Zero;
if (!CommandParser.GetTimespan(p, args[2], ref duration, "temp rank for", 'h')) return;
if (!CommandParser.GetTimespan(p, args[2], ref duration, "temp rank for", "h")) return;
if (Server.tempRanks.Contains(target)) {
Player.Message(p, "&cThe player already has a temporary rank assigned!"); return;

View File

@ -198,9 +198,9 @@ namespace MCGalaxy.Commands.World {
BlockProps.Save("core", scope, i=> true);
Level[] loaded = LevelInfo.Loaded.Items;
foreach (Level lvl in loaded) {
foreach (Level lvl in loaded) {
lvl.BlockProps[block.Index] = Block.Props[block.Index];
lvl.UpdateBlockHandler(block);
lvl.BlockProps[block.Index] = BlockDefinition.GlobalProps[block.Index];
}
} else if (scope == BlockDefinition.GlobalProps) {
Level[] loaded = LevelInfo.Loaded.Items;

View File

@ -203,7 +203,7 @@ namespace MCGalaxy.Commands.Building {
}
static void GetCoreNames(List<string> names, Level lvl) {
BlockProps[] props = lvl == null ? lvl.BlockProps : Block.Props;
BlockProps[] props = lvl != null ? lvl.BlockProps : Block.Props;
for (int i = Block.air; i < Block.Count; i++) {
ExtBlock block = ExtBlock.FromIndex(i);
if (block.BlockID == Block.custom_block) continue;

View File

@ -206,7 +206,7 @@ namespace MCGalaxy.Commands.Building {
}
static void GetCoreNames(List<string> names, Level lvl) {
BlockProps[] props = lvl == null ? lvl.BlockProps : Block.Props;
BlockProps[] props = lvl != null ? lvl.BlockProps : Block.Props;
for (int i = Block.air; i < Block.Count; i++) {
ExtBlock block = ExtBlock.FromIndex(i);
if (block.BlockID == Block.custom_block) continue;

View File

@ -115,7 +115,7 @@ namespace MCGalaxy.Commands.Building {
if (timespan.CaselessEq("all")) {
return TimeSpan.FromSeconds(canAll ? int.MaxValue : p.group.MaxUndo);
} else if (!CommandParser.GetTimespan(p, timespan, ref delta, "undo the past", 's')) {
} else if (!CommandParser.GetTimespan(p, timespan, ref delta, "undo the past", "s")) {
return TimeSpan.MinValue;
}

View File

@ -28,7 +28,7 @@ namespace MCGalaxy.Commands.World {
public override void Use(Player p, string message) {
TimeSpan duration = TimeSpan.Zero;
if (!CommandParser.GetTimespan(p, message, ref duration, "wait for", 's')) return;
if (!CommandParser.GetTimespan(p, message, ref duration, "wait for", "ms")) return;
if (duration.TotalSeconds > 60) {
Player.Message(p, "Can only wait for a minute at most."); return;
@ -48,8 +48,9 @@ namespace MCGalaxy.Commands.World {
public override void Help(Player p) {
Player.Message(p, "%T/delay [timespan]");
Player.Message(p, "%HWaits for a certain amount of time.");
Player.Message(p, "%HThis is mainly designed for use in %T/mb%H, to run a command after a certain delay.");
Player.Message(p, "%H e.g. %T/mb air /delay 10 |/help me");
Player.Message(p, "%HMain use is to run a command after a certain delay in a %T/mb");
Player.Message(p, "%H e.g. %T/mb air /delay 1000ms |/help me %Hruns %T/help me " +
"%H1000 milliseconds (1 second) after the mb is clicked");
}
}
}

View File

@ -39,7 +39,7 @@ namespace MCGalaxy.Network {
req.UserAgent = Server.SoftwareNameVersioned;
return (WebRequest)req;
}
}
}
static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount) {
IPAddress localIP = null;

View File

@ -36,27 +36,6 @@ namespace MCGalaxy {
return negate ? "-" + time : time;
}
public static TimeSpan ParseShort(this string value, char defUnit) {
int num = 0;
long amount = 0, total = 0;
foreach (char c in value) {
if (c == ' ') continue;
if (c >= '0' && c <= '9') {
num = checked(num * 10); num += (c - '0');
continue;
}
amount = GetTicks(num, c);
total = checked(total + amount);
num = 0;
}
amount = GetTicks(num, defUnit);
total = checked(total + amount);
return TimeSpan.FromTicks(total);
}
static void Add(ref string time, int amount, char suffix, bool spaces) {
if (amount == 0) return;
@ -65,19 +44,53 @@ namespace MCGalaxy {
else
time = time + (spaces ? " " : "") + amount + suffix;
}
static long GetTicks(int num, char unit) {
if (unit == 's' || unit == 'S')
return num * TimeSpan.TicksPerSecond;
if (unit == 'm' || unit == 'M')
return num * TimeSpan.TicksPerMinute;
if (unit == 'h' || unit == 'H')
return num * TimeSpan.TicksPerHour;
if (unit == 'd' || unit == 'D')
return num * TimeSpan.TicksPerDay;
if (unit == 'w' || unit == 'W')
return num * TimeSpan.TicksPerDay * 7;
throw new FormatException(unit.ToString());
public static TimeSpan ParseShort(this string value, string defaultUnit) {
int num = 0;
long amount = 0, total = 0;
for (int i = 0; i < value.Length; i++) {
char c = value[i];
if (c == ' ') continue;
if (c >= '0' && c <= '9') {
num = checked(num * 10); num += (c - '0');
continue;
}
amount = GetTicks(num, GetUnit(value, i));
total = checked(total + amount);
num = 0;
}
amount = GetTicks(num, defaultUnit);
total = checked(total + amount);
return TimeSpan.FromTicks(total);
}
static long GetTicks(int num, string unit) {
if (unit == "s" || unit == "S") return num * TimeSpan.TicksPerSecond;
if (unit == "m" || unit == "M") return num * TimeSpan.TicksPerMinute;
if (unit == "h" || unit == "H") return num * TimeSpan.TicksPerHour;
if (unit == "d" || unit == "D") return num * TimeSpan.TicksPerDay;
if (unit == "w" || unit == "W") return num * TimeSpan.TicksPerDay * 7;
if (unit == "ms" || unit == "MS") return num * TimeSpan.TicksPerMillisecond;
throw new FormatException(unit);
}
static string GetUnit(string value, int i) {
string unit = "";
// Find all alphabetical chars
for (; i < value.Length; i++) {
char c = value[i];
if (c == ' ') continue;
if (c >= '0' && c <= '9') break;
unit += value[i];
}
return unit;
}
}
}