From 5c1c4cc93787eb38a814a4d3bba31f96083e03b5 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 13 Jul 2016 09:54:27 +1000 Subject: [PATCH] Simplify /binfo output and also use relative instead of absolute time. --- Commands/Information/CmdAbout.cs | 109 ++++++++++++++----------------- util/TimeUtils.cs | 20 +++--- 2 files changed, 61 insertions(+), 68 deletions(-) diff --git a/Commands/Information/CmdAbout.cs b/Commands/Information/CmdAbout.cs index 994a74e55..c64fcd4a0 100644 --- a/Commands/Information/CmdAbout.cs +++ b/Commands/Information/CmdAbout.cs @@ -1,29 +1,27 @@ /* - Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) - - Dual-licensed under the Educational Community License, Version 2.0 and - the GNU General Public License, Version 3 (the "Licenses"); you may - not use this file except in compliance with the Licenses. You may - obtain a copy of the Licenses at - - http://www.opensource.org/licenses/ecl2.php - http://www.gnu.org/licenses/gpl-3.0.html - - Unless required by applicable law or agreed to in writing, - software distributed under the Licenses are distributed on an "AS IS" - BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - or implied. See the Licenses for the specific language governing - permissions and limitations under the Licenses. -*/ + Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ using System; using System.Collections.Generic; using System.Data; using MCGalaxy.SQL; -namespace MCGalaxy.Commands -{ - public sealed class CmdAbout : Command - { +namespace MCGalaxy.Commands { + public sealed class CmdAbout : Command { public override string name { get { return "about"; } } public override string shortcut { get { return "b"; } } public override string type { get { return CommandTypes.Information; } } @@ -32,75 +30,66 @@ namespace MCGalaxy.Commands public CmdAbout() { } public override void Use(Player p, string message) { - if (Player.IsSuper(p)) { MessageInGameOnly(p); return; } + if (Player.IsSuper(p)) { MessageInGameOnly(p); return; } Player.Message(p, "Break/build a block to display information."); p.ClearBlockchange(); p.Blockchange += PlacedBlock; - } + } void PlacedBlock(Player p, ushort x, ushort y, ushort z, byte type, byte extType) { if (!p.staticCommands) p.ClearBlockchange(); byte b = p.level.GetTile(x, y, z); - if (b == Block.Zero) { Player.Message(p, "Invalid Block(" + x + "," + y + "," + z + ")!"); return; } - p.SendBlockchange(x, y, z, b); + if (b == Block.Zero) { Player.Message(p, "Invalid Block ({0}, {1}, {2}).", x, y, z); return; } + p.RevertBlock(x, y, z); + byte id = b; if (b == Block.custom_block) - id = p.level.GetExtTile(x, y, z); - - string message = "Block (" + x + "," + y + "," + z + "): "; - message += "&f" + id + " = " + Block.Name(b); - Player.Message(p, message + "%S."); - - //safe against SQL injections because no user input is given here - DataTable Blocks = Database.fillData("SELECT * FROM `Block" + p.level.name + "` WHERE X=" + (int)x + " AND Y=" + (int)y + " AND Z=" + (int)z); - string Username, TimePerformed, BlockUsed; - bool Deleted, foundOne = false; + id = p.level.GetExtTile(x, y, z); + Player.Message(p, "Block ({0}, {1}, {2}): &f{3} = {4}%S.", x, y, z, id, Block.Name(b)); + DateTime now = DateTime.Now; + bool foundOne = false; + //safe against SQL injections because no user input is given here + DataTable Blocks = Database.fillData("SELECT * FROM `Block" + p.level.name + "` WHERE X=" + (int)x + " AND Y=" + (int)y + " AND Z=" + (int)z); for (int i = 0; i < Blocks.Rows.Count; i++) { foundOne = true; DataRow row = Blocks.Rows[i]; - Username = row["Username"].ToString().Trim(); - TimePerformed = DateTime.Parse(row["TimePerformed"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"); - BlockUsed = Block.Name(Convert.ToByte(row["Type"])); - Deleted = Convert.ToBoolean(row["Deleted"]); - - if (!Deleted) - Player.Message(p, "&3Created by " + Server.FindColor(Username) + Username + "%S, using &3" + BlockUsed); - else - Player.Message(p, "&4Destroyed by " + Server.FindColor(Username) + Username + "%S, using &3" + BlockUsed); - Player.Message(p, "Date and time modified: &2" + TimePerformed); + string user = row["Username"].ToString().Trim(); + DateTime time = DateTime.Parse(row["TimePerformed"].ToString()); + byte block = Convert.ToByte(row["Type"]); + bool deleted = Convert.ToBoolean(row["Deleted"]); + Output(p, user, block, deleted, now - time); } + Blocks.Dispose(); int bpIndex = p.level.PosToInt(x, y, z); List inCache = p.level.blockCache.FindAll(bP => bP.index == bpIndex); for (int i = 0; i < inCache.Count; i++) { foundOne = true; - Deleted = (inCache[i].flags & 1) != 0; - Username = inCache[i].name.Trim(); + string user = inCache[i].name.Trim(); DateTime time = Server.StartTimeLocal.AddSeconds(inCache[i].flags >> 2); - TimePerformed = time.ToString("yyyy-MM-dd HH:mm:ss"); - byte inBlock = (inCache[i].flags & 2) != 0 ? Block.custom_block : inCache[i].rawType; - BlockUsed = Block.Name(inBlock); - - if (!Deleted) - Player.Message(p, "&3Created by " + Server.FindColor(Username) + Username + "%S, using &3" + BlockUsed); - else - Player.Message(p, "&4Destroyed by " + Server.FindColor(Username) + Username + "%S, using &3" + BlockUsed); - Player.Message(p, "Date and time modified: &2" + TimePerformed); + byte block = (inCache[i].flags & 2) != 0 ? Block.custom_block : inCache[i].rawType; + bool deleted = (inCache[i].flags & 1) != 0; + Output(p, user, block, deleted, now - time); } if (!foundOne) - Player.Message(p, "This block has not been modified since the map was cleared."); - - Blocks.Dispose(); - + Player.Message(p, "No block change records found for this block."); GC.Collect(); GC.WaitForPendingFinalizers(); } + static void Output(Player p, string user, byte block, bool deleted, TimeSpan delta) { + string bName = Block.Name(block); + user = Server.FindColor(user) + user; + + Player.Message(p, "{0} ago {1} {2}", delta.Shorten(true, false), user, + deleted ? "&4deleted%S (using " + bName + ")" : "&3placed%S " + bName); + } + public override void Help(Player p) { Player.Message(p, "%T/about"); - Player.Message(p, "%HDisplays information about a block."); + Player.Message(p, "%HOutputs the change/edit history for a block."); } } } diff --git a/util/TimeUtils.cs b/util/TimeUtils.cs index 328f1ebc4..6aee7c37b 100644 --- a/util/TimeUtils.cs +++ b/util/TimeUtils.cs @@ -21,15 +21,16 @@ using System.Linq; namespace MCGalaxy { public static class TimeUtils { - public static string Shorten(this TimeSpan value, bool seconds = false) { + public static string Shorten(this TimeSpan value, + bool seconds = false, bool spaces = true) { string time = ""; bool negate = value.TotalSeconds < 0; if (negate) value = -value; - Add(ref time, value.Days, 'd'); - Add(ref time, value.Hours, 'h'); - Add(ref time, value.Minutes, 'm'); - if (seconds) Add(ref time, value.Seconds, 's'); + Add(ref time, value.Days, 'd', spaces); + Add(ref time, value.Hours, 'h', spaces); + Add(ref time, value.Minutes, 'm', spaces); + if (seconds) Add(ref time, value.Seconds, 's', spaces); if (time == "") time = seconds ? "0s" : "0m"; return negate ? "-" + time : time; @@ -83,10 +84,13 @@ namespace MCGalaxy { int.Parse(parts[2]), int.Parse(parts[3])); } - static void Add(ref string time, int amount, char suffix) { + static void Add(ref string time, int amount, char suffix, bool spaces) { if (amount == 0) return; - if (time == "") time = "" + amount + suffix; - else time = time + " " + amount + suffix; + + if (time == "") + time = "" + amount + suffix; + else + time = time + (spaces ? " " : "") + amount + suffix; } static long GetTicks(int num, char unit) {