Simplify /binfo output and also use relative instead of absolute time.

This commit is contained in:
UnknownShadow200 2016-07-13 09:54:27 +10:00
parent 8631316201
commit 5c1c4cc937
2 changed files with 61 additions and 68 deletions

View File

@ -1,29 +1,27 @@
/* /*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
Dual-licensed under the Educational Community License, Version 2.0 and Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing, Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS" software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using MCGalaxy.SQL; using MCGalaxy.SQL;
namespace MCGalaxy.Commands namespace MCGalaxy.Commands {
{ public sealed class CmdAbout : Command {
public sealed class CmdAbout : Command
{
public override string name { get { return "about"; } } public override string name { get { return "about"; } }
public override string shortcut { get { return "b"; } } public override string shortcut { get { return "b"; } }
public override string type { get { return CommandTypes.Information; } } public override string type { get { return CommandTypes.Information; } }
@ -32,75 +30,66 @@ namespace MCGalaxy.Commands
public CmdAbout() { } public CmdAbout() { }
public override void Use(Player p, string message) { 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."); Player.Message(p, "Break/build a block to display information.");
p.ClearBlockchange(); p.ClearBlockchange();
p.Blockchange += PlacedBlock; p.Blockchange += PlacedBlock;
} }
void PlacedBlock(Player p, ushort x, ushort y, ushort z, byte type, byte extType) { void PlacedBlock(Player p, ushort x, ushort y, ushort z, byte type, byte extType) {
if (!p.staticCommands) p.ClearBlockchange(); if (!p.staticCommands) p.ClearBlockchange();
byte b = p.level.GetTile(x, y, z); byte b = p.level.GetTile(x, y, z);
if (b == Block.Zero) { Player.Message(p, "Invalid Block(" + x + "," + y + "," + z + ")!"); return; } if (b == Block.Zero) { Player.Message(p, "Invalid Block ({0}, {1}, {2}).", x, y, z); return; }
p.SendBlockchange(x, y, z, b); p.RevertBlock(x, y, z);
byte id = b; byte id = b;
if (b == Block.custom_block) if (b == Block.custom_block)
id = p.level.GetExtTile(x, y, z); 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));
string message = "Block (" + x + "," + y + "," + z + "): "; DateTime now = DateTime.Now;
message += "&f" + id + " = " + Block.Name(b); bool foundOne = false;
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;
//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++) { for (int i = 0; i < Blocks.Rows.Count; i++) {
foundOne = true; foundOne = true;
DataRow row = Blocks.Rows[i]; DataRow row = Blocks.Rows[i];
Username = row["Username"].ToString().Trim(); string user = row["Username"].ToString().Trim();
TimePerformed = DateTime.Parse(row["TimePerformed"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"); DateTime time = DateTime.Parse(row["TimePerformed"].ToString());
BlockUsed = Block.Name(Convert.ToByte(row["Type"])); byte block = Convert.ToByte(row["Type"]);
Deleted = Convert.ToBoolean(row["Deleted"]); bool deleted = Convert.ToBoolean(row["Deleted"]);
Output(p, user, block, deleted, now - time);
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);
} }
Blocks.Dispose();
int bpIndex = p.level.PosToInt(x, y, z); int bpIndex = p.level.PosToInt(x, y, z);
List<Level.BlockPos> inCache = p.level.blockCache.FindAll(bP => bP.index == bpIndex); List<Level.BlockPos> inCache = p.level.blockCache.FindAll(bP => bP.index == bpIndex);
for (int i = 0; i < inCache.Count; i++) { for (int i = 0; i < inCache.Count; i++) {
foundOne = true; foundOne = true;
Deleted = (inCache[i].flags & 1) != 0; string user = inCache[i].name.Trim();
Username = inCache[i].name.Trim();
DateTime time = Server.StartTimeLocal.AddSeconds(inCache[i].flags >> 2); DateTime time = Server.StartTimeLocal.AddSeconds(inCache[i].flags >> 2);
TimePerformed = time.ToString("yyyy-MM-dd HH:mm:ss"); byte block = (inCache[i].flags & 2) != 0 ? Block.custom_block : inCache[i].rawType;
byte inBlock = (inCache[i].flags & 2) != 0 ? Block.custom_block : inCache[i].rawType; bool deleted = (inCache[i].flags & 1) != 0;
BlockUsed = Block.Name(inBlock); Output(p, user, block, deleted, now - time);
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);
} }
if (!foundOne) if (!foundOne)
Player.Message(p, "This block has not been modified since the map was cleared."); Player.Message(p, "No block change records found for this block.");
Blocks.Dispose();
GC.Collect(); GC.Collect();
GC.WaitForPendingFinalizers(); 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) { public override void Help(Player p) {
Player.Message(p, "%T/about"); Player.Message(p, "%T/about");
Player.Message(p, "%HDisplays information about a block."); Player.Message(p, "%HOutputs the change/edit history for a block.");
} }
} }
} }

View File

@ -21,15 +21,16 @@ using System.Linq;
namespace MCGalaxy { namespace MCGalaxy {
public static class TimeUtils { 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 = ""; string time = "";
bool negate = value.TotalSeconds < 0; bool negate = value.TotalSeconds < 0;
if (negate) value = -value; if (negate) value = -value;
Add(ref time, value.Days, 'd'); Add(ref time, value.Days, 'd', spaces);
Add(ref time, value.Hours, 'h'); Add(ref time, value.Hours, 'h', spaces);
Add(ref time, value.Minutes, 'm'); Add(ref time, value.Minutes, 'm', spaces);
if (seconds) Add(ref time, value.Seconds, 's'); if (seconds) Add(ref time, value.Seconds, 's', spaces);
if (time == "") time = seconds ? "0s" : "0m"; if (time == "") time = seconds ? "0s" : "0m";
return negate ? "-" + time : time; return negate ? "-" + time : time;
@ -83,10 +84,13 @@ namespace MCGalaxy {
int.Parse(parts[2]), int.Parse(parts[3])); 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 (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) { static long GetTicks(int num, char unit) {