From 2bf9593bbf74b83bcb86dbdd7f26e255708febf5 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 12 Jun 2021 08:09:16 +1000 Subject: [PATCH] Tidy up /location code --- MCGalaxy/Commands/Moderation/CmdLocation.cs | 22 +++++++++++-------- MCGalaxy/Commands/building/CmdMessageBlock.cs | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/MCGalaxy/Commands/Moderation/CmdLocation.cs b/MCGalaxy/Commands/Moderation/CmdLocation.cs index f07bd895b..9dbf8ca86 100644 --- a/MCGalaxy/Commands/Moderation/CmdLocation.cs +++ b/MCGalaxy/Commands/Moderation/CmdLocation.cs @@ -43,19 +43,23 @@ namespace MCGalaxy.Commands.Moderation { p.Message("&WPlayer has an internal IP, cannot trace"); return; } - string json, region = null, country = null; - using (WebClient client = HttpUtil.CreateWebClient()) { - json = client.DownloadString("http://ipinfo.io/" + ip + "/geo"); + string json; + try { + WebRequest req = HttpUtil.CreateRequest("http://ipinfo.io/" + ip + "/geo"); + WebResponse res = req.GetResponse(); + json = HttpUtil.GetResponseText(res); + } catch (Exception ex) { + HttpUtil.DisposeErrorResponse(ex); + throw; } JsonReader reader = new JsonReader(json); - reader.OnMember = (obj, key, value) => { - if (key == "region") region = (string)value; - if (key == "country") country = (string)value; - }; + JsonObject obj = (JsonObject)reader.Parse(); + if (obj == null) { p.Message("&WError parsing GeoIP info"); return; } - reader.Parse(); - if (reader.Failed) { p.Message("&WError parsing GeoIP info"); return; } + object region = null, country = null; + obj.TryGetValue("region", out region); + obj.TryGetValue("country", out country); string suffix = HasExtraPerm(p, data.Rank, 1) ? "&b{1}&S/&b{2}" : "&b{2}"; string nick = name == null ? ip : "of " + p.FormatNick(name); diff --git a/MCGalaxy/Commands/building/CmdMessageBlock.cs b/MCGalaxy/Commands/building/CmdMessageBlock.cs index 310ef78ba..3741d5053 100644 --- a/MCGalaxy/Commands/building/CmdMessageBlock.cs +++ b/MCGalaxy/Commands/building/CmdMessageBlock.cs @@ -153,7 +153,7 @@ namespace MCGalaxy.Commands.Building { public override void Help(Player p) { p.Message("&T/MB [block] [message]"); p.Message("&HPlaces a message in your next block."); - List names = SupportedBlocks(p); + List names = SupportedBlocks(p); p.Message("&H Supported blocks: &S{0}", names.Join()); p.Message("&H Use | to separate commands, e.g. /say 1 |/say 2"); p.Message("&H Note: \"@p\" is a placeholder for player who clicked.");