mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-11-07 17:36:07 +00:00
Show links embedded in chat
Allows clicking if supported by terminal, or at least copy and paste to web browser. Suggestion by brkmrr9 in #207
This commit is contained in:
parent
9cd983c50d
commit
1180c06b1f
6 changed files with 59 additions and 28 deletions
|
|
@ -15,11 +15,11 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
/// The main function to convert text from MC 1.6+ JSON to MC 1.5.2 formatted text
|
||||
/// </summary>
|
||||
/// <param name="json">JSON serialized text</param>
|
||||
/// <param name="links">Optional container for links from JSON serialized text</param>
|
||||
/// <returns>Returns the translated text</returns>
|
||||
|
||||
public static string ParseText(string json)
|
||||
public static string ParseText(string json, List<string> links = null)
|
||||
{
|
||||
return JSONData2String(Json.ParseJson(json), "");
|
||||
return JSONData2String(Json.ParseJson(json), "", links);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -27,8 +27,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
/// </summary>
|
||||
/// <param name="colorname">Color Name</param>
|
||||
/// <returns>Color code</returns>
|
||||
|
||||
private static string color2tag(string colorname)
|
||||
private static string Color2tag(string colorname)
|
||||
{
|
||||
switch (colorname.ToLower())
|
||||
{
|
||||
|
|
@ -54,12 +53,24 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rules for text translation
|
||||
/// Specify whether translation rules have been loaded
|
||||
/// </summary>
|
||||
private static bool RulesInitialized = false;
|
||||
|
||||
private static bool init = false;
|
||||
/// <summary>
|
||||
/// Set of translation rules for formatting text
|
||||
/// </summary>
|
||||
private static Dictionary<string, string> TranslationRules = new Dictionary<string, string>();
|
||||
public static void InitTranslations() { if (!init) { InitRules(); init = true; } }
|
||||
|
||||
/// <summary>
|
||||
/// Initialize translation rules.
|
||||
/// Necessary for properly printing some chat messages.
|
||||
/// </summary>
|
||||
public static void InitTranslations() { if (!RulesInitialized) { InitRules(); RulesInitialized = true; } }
|
||||
|
||||
/// <summary>
|
||||
/// Internal rule initialization method. Looks for local rule file or download it from Mojang asset servers.
|
||||
/// </summary>
|
||||
private static void InitRules()
|
||||
{
|
||||
//Small default dictionnary of translation rules
|
||||
|
|
@ -85,11 +96,11 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
ConsoleIO.WriteLine("Downloading '" + Settings.Language + ".lang' from Mojang servers...");
|
||||
try
|
||||
{
|
||||
string assets_index = downloadString(Settings.TranslationsFile_Website_Index);
|
||||
string assets_index = DownloadString(Settings.TranslationsFile_Website_Index);
|
||||
string[] tmp = assets_index.Split(new string[] { "minecraft/lang/" + Settings.Language + ".lang" }, StringSplitOptions.None);
|
||||
tmp = tmp[1].Split(new string[] { "hash\": \"" }, StringSplitOptions.None);
|
||||
string hash = tmp[1].Split('"')[0]; //Translations file identifier on Mojang's servers
|
||||
System.IO.File.WriteAllText(Language_File, downloadString(Settings.TranslationsFile_Website_Download + '/' + hash.Substring(0, 2) + '/' + hash));
|
||||
System.IO.File.WriteAllText(Language_File, DownloadString(Settings.TranslationsFile_Website_Download + '/' + hash.Substring(0, 2) + '/' + hash));
|
||||
ConsoleIO.WriteLine("Done. File saved as '" + Language_File + '\'');
|
||||
}
|
||||
catch
|
||||
|
|
@ -140,10 +151,9 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
/// <param name="rulename">Name of the rule, chosen by the server</param>
|
||||
/// <param name="using_data">Data to be used in the rule</param>
|
||||
/// <returns>Returns the formatted text according to the given data</returns>
|
||||
|
||||
private static string TranslateString(string rulename, List<string> using_data)
|
||||
{
|
||||
if (!init) { InitRules(); init = true; }
|
||||
if (!RulesInitialized) { InitRules(); RulesInitialized = true; }
|
||||
if (TranslationRules.ContainsKey(rulename))
|
||||
{
|
||||
int using_idx = 0;
|
||||
|
|
@ -192,9 +202,9 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
/// </summary>
|
||||
/// <param name="data">JSON object to convert</param>
|
||||
/// <param name="colorcode">Allow parent color code to affect child elements (set to "" for function init)</param>
|
||||
/// <param name="links">Container for links from JSON serialized text</param>
|
||||
/// <returns>returns the Minecraft-formatted string</returns>
|
||||
|
||||
private static string JSONData2String(Json.JSONData data, string colorcode)
|
||||
private static string JSONData2String(Json.JSONData data, string colorcode, List<string> links)
|
||||
{
|
||||
string extra_result = "";
|
||||
switch (data.Type)
|
||||
|
|
@ -202,17 +212,28 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
case Json.JSONData.DataType.Object:
|
||||
if (data.Properties.ContainsKey("color"))
|
||||
{
|
||||
colorcode = color2tag(JSONData2String(data.Properties["color"], ""));
|
||||
colorcode = Color2tag(JSONData2String(data.Properties["color"], "", links));
|
||||
}
|
||||
if (data.Properties.ContainsKey("clickEvent") && links != null)
|
||||
{
|
||||
Json.JSONData clickEvent = data.Properties["clickEvent"];
|
||||
if (clickEvent.Properties.ContainsKey("action")
|
||||
&& clickEvent.Properties.ContainsKey("value")
|
||||
&& clickEvent.Properties["action"].StringValue == "open_url"
|
||||
&& !String.IsNullOrEmpty(clickEvent.Properties["value"].StringValue))
|
||||
{
|
||||
links.Add(clickEvent.Properties["value"].StringValue);
|
||||
}
|
||||
}
|
||||
if (data.Properties.ContainsKey("extra"))
|
||||
{
|
||||
Json.JSONData[] extras = data.Properties["extra"].DataArray.ToArray();
|
||||
foreach (Json.JSONData item in extras)
|
||||
extra_result = extra_result + JSONData2String(item, colorcode) + "§r";
|
||||
extra_result = extra_result + JSONData2String(item, colorcode, links) + "§r";
|
||||
}
|
||||
if (data.Properties.ContainsKey("text"))
|
||||
{
|
||||
return colorcode + JSONData2String(data.Properties["text"], colorcode) + extra_result;
|
||||
return colorcode + JSONData2String(data.Properties["text"], colorcode, links) + extra_result;
|
||||
}
|
||||
else if (data.Properties.ContainsKey("translate"))
|
||||
{
|
||||
|
|
@ -224,10 +245,10 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
Json.JSONData[] array = data.Properties["with"].DataArray.ToArray();
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
using_data.Add(JSONData2String(array[i], colorcode));
|
||||
using_data.Add(JSONData2String(array[i], colorcode, links));
|
||||
}
|
||||
}
|
||||
return colorcode + TranslateString(JSONData2String(data.Properties["translate"], ""), using_data) + extra_result;
|
||||
return colorcode + TranslateString(JSONData2String(data.Properties["translate"], "", links), using_data) + extra_result;
|
||||
}
|
||||
else return extra_result;
|
||||
|
||||
|
|
@ -235,7 +256,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
string result = "";
|
||||
foreach (Json.JSONData item in data.DataArray)
|
||||
{
|
||||
result += JSONData2String(item, colorcode);
|
||||
result += JSONData2String(item, colorcode, links);
|
||||
}
|
||||
return result;
|
||||
|
||||
|
|
@ -251,8 +272,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
/// </summary>
|
||||
/// <param name="url">URL of resource</param>
|
||||
/// <returns>Returns resource data if success, otherwise a WebException is raised</returns>
|
||||
|
||||
private static string downloadString(string url)
|
||||
private static string DownloadString(string url)
|
||||
{
|
||||
System.Net.HttpWebRequest myRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
|
||||
myRequest.Method = "GET";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue