From c5b642f1e1f6b75b4c4786d2787ce049505e6628 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 9 Jul 2017 23:36:38 +1000 Subject: [PATCH] Delay command should work in milliseconds. Fixes #475. --- MCGalaxy/Commands/CommandParser.cs | 2 +- .../Commands/Maintenance/CmdPlayerEdit.cs | 2 +- MCGalaxy/Commands/Moderation/CmdFreeze.cs | 2 +- MCGalaxy/Commands/Moderation/CmdHighlight.cs | 4 +- MCGalaxy/Commands/Moderation/CmdMute.cs | 2 +- MCGalaxy/Commands/Moderation/CmdTempBan.cs | 2 +- MCGalaxy/Commands/Moderation/CmdTempRank.cs | 2 +- MCGalaxy/Commands/World/CmdBlockProperties.cs | 4 +- MCGalaxy/Commands/building/CmdMessageBlock.cs | 2 +- MCGalaxy/Commands/building/CmdPortal.cs | 2 +- MCGalaxy/Commands/building/CmdUndo.cs | 2 +- MCGalaxy/Commands/other/CmdDelay.cs | 7 +- MCGalaxy/Network/Utils/HttpUtil.cs | 2 +- MCGalaxy/util/Extensions/TimeExts.cs | 79 +++++++++++-------- 14 files changed, 64 insertions(+), 50 deletions(-) diff --git a/MCGalaxy/Commands/CommandParser.cs b/MCGalaxy/Commands/CommandParser.cs index c9e05b3bc..495afb5e0 100644 --- a/MCGalaxy/Commands/CommandParser.cs +++ b/MCGalaxy/Commands/CommandParser.cs @@ -54,7 +54,7 @@ namespace MCGalaxy.Commands { /// Attempts to parse the given argument as an timespan in short form. 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); diff --git a/MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs b/MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs index d61031c9b..c538926ce 100644 --- a/MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs +++ b/MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs @@ -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); diff --git a/MCGalaxy/Commands/Moderation/CmdFreeze.cs b/MCGalaxy/Commands/Moderation/CmdFreeze.cs index f4fbb58b0..a7d4f44dc 100644 --- a/MCGalaxy/Commands/Moderation/CmdFreeze.cs +++ b/MCGalaxy/Commands/Moderation/CmdFreeze.cs @@ -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); diff --git a/MCGalaxy/Commands/Moderation/CmdHighlight.cs b/MCGalaxy/Commands/Moderation/CmdHighlight.cs index b88e36672..afb28dd6b 100644 --- a/MCGalaxy/Commands/Moderation/CmdHighlight.cs +++ b/MCGalaxy/Commands/Moderation/CmdHighlight.cs @@ -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; } diff --git a/MCGalaxy/Commands/Moderation/CmdMute.cs b/MCGalaxy/Commands/Moderation/CmdMute.cs index 4bb2162e4..e0672f54e 100644 --- a/MCGalaxy/Commands/Moderation/CmdMute.cs +++ b/MCGalaxy/Commands/Moderation/CmdMute.cs @@ -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); diff --git a/MCGalaxy/Commands/Moderation/CmdTempBan.cs b/MCGalaxy/Commands/Moderation/CmdTempBan.cs index 8c93528d1..23d3f73f2 100644 --- a/MCGalaxy/Commands/Moderation/CmdTempBan.cs +++ b/MCGalaxy/Commands/Moderation/CmdTempBan.cs @@ -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); diff --git a/MCGalaxy/Commands/Moderation/CmdTempRank.cs b/MCGalaxy/Commands/Moderation/CmdTempRank.cs index 035e29047..efbbbc8b5 100644 --- a/MCGalaxy/Commands/Moderation/CmdTempRank.cs +++ b/MCGalaxy/Commands/Moderation/CmdTempRank.cs @@ -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; diff --git a/MCGalaxy/Commands/World/CmdBlockProperties.cs b/MCGalaxy/Commands/World/CmdBlockProperties.cs index 76a3ef1d2..4b0948c66 100644 --- a/MCGalaxy/Commands/World/CmdBlockProperties.cs +++ b/MCGalaxy/Commands/World/CmdBlockProperties.cs @@ -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; diff --git a/MCGalaxy/Commands/building/CmdMessageBlock.cs b/MCGalaxy/Commands/building/CmdMessageBlock.cs index 4415b3512..19a721a9a 100644 --- a/MCGalaxy/Commands/building/CmdMessageBlock.cs +++ b/MCGalaxy/Commands/building/CmdMessageBlock.cs @@ -203,7 +203,7 @@ namespace MCGalaxy.Commands.Building { } static void GetCoreNames(List 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; diff --git a/MCGalaxy/Commands/building/CmdPortal.cs b/MCGalaxy/Commands/building/CmdPortal.cs index f263e3b2a..7edfb1dc2 100644 --- a/MCGalaxy/Commands/building/CmdPortal.cs +++ b/MCGalaxy/Commands/building/CmdPortal.cs @@ -206,7 +206,7 @@ namespace MCGalaxy.Commands.Building { } static void GetCoreNames(List 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; diff --git a/MCGalaxy/Commands/building/CmdUndo.cs b/MCGalaxy/Commands/building/CmdUndo.cs index 5d8109689..40cb8c334 100644 --- a/MCGalaxy/Commands/building/CmdUndo.cs +++ b/MCGalaxy/Commands/building/CmdUndo.cs @@ -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; } diff --git a/MCGalaxy/Commands/other/CmdDelay.cs b/MCGalaxy/Commands/other/CmdDelay.cs index 84ce81fb0..afa23b790 100644 --- a/MCGalaxy/Commands/other/CmdDelay.cs +++ b/MCGalaxy/Commands/other/CmdDelay.cs @@ -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"); } } } diff --git a/MCGalaxy/Network/Utils/HttpUtil.cs b/MCGalaxy/Network/Utils/HttpUtil.cs index e3790e368..1c33be7dc 100644 --- a/MCGalaxy/Network/Utils/HttpUtil.cs +++ b/MCGalaxy/Network/Utils/HttpUtil.cs @@ -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; diff --git a/MCGalaxy/util/Extensions/TimeExts.cs b/MCGalaxy/util/Extensions/TimeExts.cs index 48288f8ee..56e5fce80 100644 --- a/MCGalaxy/util/Extensions/TimeExts.cs +++ b/MCGalaxy/util/Extensions/TimeExts.cs @@ -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; } } }