Trim & Improve the help message

This commit is contained in:
BruceChen 2022-09-08 17:19:13 +08:00
parent 65bcd83330
commit ac3f346f14
3 changed files with 186 additions and 201 deletions

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using MinecraftClient.Inventory; using MinecraftClient.Inventory;
namespace MinecraftClient.Commands namespace MinecraftClient.Commands
@ -11,54 +12,61 @@ namespace MinecraftClient.Commands
public override string CmdUsage { get { return GetBasicUsage(); } } public override string CmdUsage { get { return GetBasicUsage(); } }
public override string CmdDesc { get { return "cmd.inventory.desc"; } } public override string CmdDesc { get { return "cmd.inventory.desc"; } }
public override string Run(McClient handler, string command, Dictionary<string, object> localVars) public override string Run(McClient handler, string command, Dictionary<string, object>? localVars)
{ {
if (handler.GetInventoryEnabled()) if (handler.GetInventoryEnabled())
{ {
string[] args = getArgs(command); string[] args = getArgs(command);
if (args.Length >= 1) if (args.Length >= 1)
{
try
{ {
int inventoryId; int inventoryId;
if (args[0].ToLower() == "creativegive") if (args[0].ToLower() == "creativegive")
{ {
if (args.Length >= 4) if (args.Length >= 4)
{ {
int slot = int.Parse(args[1]); if (!int.TryParse(args[1], out int slot))
ItemType itemType = ItemType.Stone; return GetCmdDescTranslated();
if (Enum.TryParse(args[2], true, out itemType))
if (Enum.TryParse(args[2], true, out ItemType itemType))
{ {
if (handler.GetGamemode() == 1) if (handler.GetGamemode() == 1)
{ {
int count = int.Parse(args[3]); if (!int.TryParse(args[3], out int count))
return GetCmdDescTranslated();
if (handler.DoCreativeGive(slot, itemType, count, null)) if (handler.DoCreativeGive(slot, itemType, count, null))
return Translations.Get("cmd.inventory.creative_done", itemType, count, slot); return Translations.Get("cmd.inventory.creative_done", itemType, count, slot);
else return Translations.Get("cmd.inventory.creative_fail"); else
} return Translations.Get("cmd.inventory.creative_fail");
else return Translations.Get("cmd.inventory.need_creative"); }
else
return Translations.Get("cmd.inventory.need_creative");
} }
else else
{
return GetCmdDescTranslated(); return GetCmdDescTranslated();
} }
} else
else return GetCmdDescTranslated(); return GetCmdDescTranslated();
} }
else if (args[0].ToLower() == "creativedelete") else if (args[0].ToLower() == "creativedelete")
{ {
if (args.Length >= 2) if (args.Length >= 2)
{ {
int slot = int.Parse(args[1]); if (!int.TryParse(args[1], out int slot))
return GetCmdDescTranslated();
if (handler.GetGamemode() == 1) if (handler.GetGamemode() == 1)
{ {
if (handler.DoCreativeGive(slot, ItemType.Null, 0, null)) if (handler.DoCreativeGive(slot, ItemType.Null, 0, null))
return Translations.Get("cmd.inventory.creative_delete", slot); return Translations.Get("cmd.inventory.creative_delete", slot);
else return Translations.Get("cmd.inventory.creative_fail"); else
return Translations.Get("cmd.inventory.creative_fail");
} }
else return Translations.Get("cmd.inventory.need_creative"); else
return Translations.Get("cmd.inventory.need_creative");
} }
else return GetCmdDescTranslated(); else
return GetCmdDescTranslated();
} }
else if (args[0].ToLower().StartsWith("p")) else if (args[0].ToLower().StartsWith("p"))
{ {
@ -71,184 +79,158 @@ namespace MinecraftClient.Commands
availableIds.Remove(0); // remove player inventory ID from list availableIds.Remove(0); // remove player inventory ID from list
if (availableIds.Count > 0) if (availableIds.Count > 0)
inventoryId = availableIds.Max(); // use foreground container inventoryId = availableIds.Max(); // use foreground container
else return Translations.Get("cmd.inventory.container_not_found"); else
return Translations.Get("cmd.inventory.container_not_found");
} }
else if (args[0].ToLower() == "help") else if (args[0].ToLower() == "help")
{ {
if (args.Length >= 2) if (args.Length >= 2)
{
return GetSubCommandHelp(args[1]); return GetSubCommandHelp(args[1]);
else
return GetHelp();
} }
else return GetHelp(); else if (!int.TryParse(args[0], out inventoryId))
} return GetCmdDescTranslated();
else inventoryId = int.Parse(args[0]);
string action = args.Length > 1 Container? inventory = handler.GetInventory(inventoryId);
? args[1].ToLower()
: "list";
switch (action)
{
case "close":
if (handler.CloseInventory(inventoryId))
return Translations.Get("cmd.inventory.close", inventoryId);
else return Translations.Get("cmd.inventory.close_fail", inventoryId);
case "list":
Container inventory = handler.GetInventory(inventoryId);
if (inventory == null) if (inventory == null)
return Translations.Get("cmd.inventory.not_exist", inventoryId); return Translations.Get("cmd.inventory.not_exist", inventoryId);
SortedDictionary<int, Item> itemsSorted = new SortedDictionary<int, Item>(inventory.Items);
List<string> response = new List<string>(); string action = args.Length > 1 ? args[1].ToLower() : "list";
response.Add(Translations.Get("cmd.inventory.inventory") + " #" + inventoryId + " - " + inventory.Title + "§8"); if (action == "close")
{
if (handler.CloseInventory(inventoryId))
return Translations.Get("cmd.inventory.close", inventoryId);
else
return Translations.Get("cmd.inventory.close_fail", inventoryId);
}
else if (action == "list")
{
StringBuilder response = new();
response.Append(Translations.Get("cmd.inventory.inventory"));
response.AppendLine(String.Format(" #{0} - {1}§8", inventoryId, inventory.Title));
string asciiArt = inventory.Type.GetAsciiArt(); string asciiArt = inventory.Type.GetAsciiArt();
if (asciiArt != null && Settings.DisplayInventoryLayout) if (asciiArt != null && Settings.DisplayInventoryLayout)
response.Add(asciiArt); response.AppendLine(asciiArt);
int selectedHotbar = handler.GetCurrentSlot() + 1; int selectedHotbar = handler.GetCurrentSlot() + 1;
foreach (KeyValuePair<int, Item> item in itemsSorted) foreach ((int itemId, Item item) in new SortedDictionary<int, Item>(inventory.Items))
{ {
int hotbar; bool isHotbar = inventory.IsHotbar(itemId, out int hotbar);
bool isHotbar = inventory.IsHotbar(item.Key, out hotbar);
string hotbarString = isHotbar ? (hotbar + 1).ToString() : " "; string hotbarString = isHotbar ? (hotbar + 1).ToString() : " ";
if ((hotbar + 1) == selectedHotbar) if ((hotbar + 1) == selectedHotbar)
hotbarString = ">" + hotbarString; hotbarString = ">" + hotbarString;
response.Add(String.Format("{0,2} | #{1,-2}: {2}", hotbarString, item.Key, item.Value.ToString())); response.AppendLine(String.Format("{0,2} | #{1,-2}: {2}", hotbarString, itemId, item.ToString()));
} }
if (inventoryId == 0) if (inventoryId == 0)
response.Add(Translations.Get("cmd.inventory.hotbar", (handler.GetCurrentSlot() + 1))); response.AppendLine(Translations.Get("cmd.inventory.hotbar", (handler.GetCurrentSlot() + 1)));
return String.Join("\n", response.ToArray());
case "click": response.Remove(response.Length - 1, 1); // Remove last '\n'
if (args.Length >= 3) return response.ToString();
}
else if (action == "click" && args.Length >= 3)
{ {
try if (!int.TryParse(args[2], out int slot))
{ return GetCmdDescTranslated();
int slot = int.Parse(args[2]);
WindowActionType actionType = WindowActionType.LeftClick; WindowActionType actionType = WindowActionType.LeftClick;
string keyName = "cmd.inventory.left"; string keyName = "cmd.inventory.left";
if (args.Length >= 4) if (args.Length >= 4)
{ {
string b = args[3]; string b = args[3];
if (b.ToLower()[0] == 'r') if (b.ToLower()[0] == 'r')
{ (actionType, keyName) = (WindowActionType.RightClick, "cmd.inventory.right");
actionType = WindowActionType.RightClick; else if (b.ToLower()[0] == 'm')
keyName = "cmd.inventory.right"; (actionType, keyName) = (WindowActionType.MiddleClick, "cmd.inventory.middle");
}
if (b.ToLower()[0] == 'm')
{
actionType = WindowActionType.MiddleClick;
keyName = "cmd.inventory.middle";
}
} }
handler.DoWindowAction(inventoryId, slot, actionType); handler.DoWindowAction(inventoryId, slot, actionType);
return Translations.Get("cmd.inventory.clicking", Translations.Get(keyName), slot, inventoryId); return Translations.Get("cmd.inventory.clicking", Translations.Get(keyName), slot, inventoryId);
} }
catch (FormatException) { return GetCmdDescTranslated(); } else if (action == "shiftclick" && args.Length >= 3)
}
else return CmdUsage;
case "shiftclick":
if (args.Length >= 3)
{ {
try if (!int.TryParse(args[2], out int slot))
{ return GetCmdDescTranslated();
int slot = int.Parse(args[2]);
handler.DoWindowAction(inventoryId, slot, WindowActionType.ShiftClick); if (!handler.DoWindowAction(inventoryId, slot, WindowActionType.ShiftClick))
return Translations.Get("cmd.inventory.shiftclick_fail");
return Translations.Get("cmd.inventory.shiftclick", slot, inventoryId); return Translations.Get("cmd.inventory.shiftclick", slot, inventoryId);
} }
catch (FormatException) { return GetCmdDescTranslated(); } else if (action == "drop" && args.Length >= 3)
}
else return CmdUsage;
case "drop":
if (args.Length >= 3)
{ {
try if (!int.TryParse(args[2], out int slot))
{ return GetCmdDescTranslated();
int slot = int.Parse(args[2]);
// check item exist // check item exist
if (!handler.GetInventory(inventoryId).Items.ContainsKey(slot)) if (!inventory.Items.ContainsKey(slot))
return Translations.Get("cmd.inventory.no_item", slot); return Translations.Get("cmd.inventory.no_item", slot);
WindowActionType actionType = WindowActionType.DropItem; WindowActionType actionType = WindowActionType.DropItem;
if (args.Length >= 4) if (args.Length >= 4 && args[3].ToLower() == "all")
{
if (args[3].ToLower() == "all")
{
actionType = WindowActionType.DropItemStack; actionType = WindowActionType.DropItemStack;
}
}
if (handler.DoWindowAction(inventoryId, slot, actionType)) if (handler.DoWindowAction(inventoryId, slot, actionType))
{ {
if (actionType == WindowActionType.DropItemStack) if (actionType == WindowActionType.DropItemStack)
return Translations.Get("cmd.inventory.drop_stack", slot); return Translations.Get("cmd.inventory.drop_stack", slot);
else return Translations.Get("cmd.inventory.drop", slot); else
return Translations.Get("cmd.inventory.drop", slot);
} }
else else
{
return "Failed"; return "Failed";
} }
} else
catch (FormatException) { return GetCmdDescTranslated(); }
}
else return GetCmdDescTranslated();
default:
return GetCmdDescTranslated(); return GetCmdDescTranslated();
} }
}
catch (FormatException) { return GetCmdDescTranslated(); }
}
else else
{ {
Dictionary<int, Container> inventories = handler.GetInventories(); StringBuilder response = new();
List<string> response = new List<string>(); response.AppendLine(Translations.Get("cmd.inventory.inventories")).Append(':');
response.Add(Translations.Get("cmd.inventory.inventories") + ":"); foreach ((int invId, Container inv) in handler.GetInventories())
foreach (KeyValuePair<int, Container> inventory in inventories) response.AppendLine(String.Format(" #{0}: {1}§8", invId, inv.Title));
{ response.Append(CmdUsage);
response.Add(String.Format(" #{0}: {1}", inventory.Key, inventory.Value.Title + "§8")); return response.ToString();
}
response.Add(CmdUsage);
return String.Join("\n", response);
} }
} }
else return Translations.Get("extra.inventory_required"); else
return Translations.Get("extra.inventory_required");
} }
#region Methods for commands help #region Methods for commands help
private string GetCommandDesc()
{
return GetBasicUsage() + " Type \"/inventory help\" for more help";
}
private string GetAvailableActions() private static string GetAvailableActions()
{ {
return Translations.Get("cmd.inventory.help.available") + ": list, close, click, drop, creativegive, creativedelete."; return Translations.Get("cmd.inventory.help.available") + ": list, close, click, drop, creativegive, creativedelete.";
} }
private string GetBasicUsage() private static string GetBasicUsage()
{ {
return Translations.Get("cmd.inventory.help.basic") + ": /inventory <player|container|<id>> <action>."; return Translations.Get("cmd.inventory.help.basic") + ": /inventory <player|container|<id>> <action>.";
} }
private string GetHelp() private static string GetHelp()
{ {
return Translations.Get("cmd.inventory.help.help", GetAvailableActions()); return Translations.Get("cmd.inventory.help.help", GetAvailableActions());
} }
private string GetSubCommandHelp(string cmd) private static string GetSubCommandHelp(string cmd)
{ {
switch (cmd) string usageStr = ' ' + Translations.Get("cmd.inventory.help.usage") + ": ";
return cmd switch
{ {
case "list": "list" => Translations.Get("cmd.inventory.help.list") + usageStr + "/inventory <player|container|<id>> list",
return Translations.Get("cmd.inventory.help.list") + ' ' + Translations.Get("cmd.inventory.help.usage") + ": /inventory <player|container|<id>> list"; "close" => Translations.Get("cmd.inventory.help.close") + usageStr + "/inventory <player|container|<id>> close",
case "close": "click" => Translations.Get("cmd.inventory.help.click") + usageStr + "/inventory <player|container|<id>> click <slot> [left|right|middle]\nDefault is left click",
return Translations.Get("cmd.inventory.help.close") + ' ' + Translations.Get("cmd.inventory.help.usage") + ": /inventory <player|container|<id>> close"; "shiftclick" => Translations.Get("cmd.inventory.help.shiftclick") + usageStr + "/inventory <player|container|<id>> shiftclick <slot>",
case "click": "drop" => Translations.Get("cmd.inventory.help.drop") + usageStr + "/inventory <player|container|<id>> drop <slot> [all]\nAll means drop full stack",
return Translations.Get("cmd.inventory.help.click") + ' ' + Translations.Get("cmd.inventory.help.usage") + ": /inventory <player|container|<id>> click <slot> [left|right|middle]. \nDefault is left click"; "creativegive" => Translations.Get("cmd.inventory.help.creativegive") + usageStr + "/inventory creativegive <slot> <itemtype> <amount>",
case "drop": "creativedelete" => Translations.Get("cmd.inventory.help.creativedelete") + usageStr + "/inventory creativedelete <slot>",
return Translations.Get("cmd.inventory.help.drop") + ' ' + Translations.Get("cmd.inventory.help.usage") + ": /inventory <player|container|<id>> drop <slot> [all]. \nAll means drop full stack"; "help" => GetHelp(),
case "creativegive": _ => Translations.Get("cmd.inventory.help.unknown") + GetAvailableActions(),
return Translations.Get("cmd.inventory.help.creativegive") + ' ' + Translations.Get("cmd.inventory.help.usage") + ": /inventory creativegive <slot> <itemtype> <amount>"; };
case "creativedelete":
return Translations.Get("cmd.inventory.help.creativedelete") + ' ' + Translations.Get("cmd.inventory.help.usage") + ": /inventory creativedelete <slot>";
case "help":
return GetHelp();
default:
return Translations.Get("cmd.inventory.help.unknown") + GetAvailableActions();
}
} }
#endregion #endregion
} }

View file

@ -1820,6 +1820,7 @@ namespace MinecraftClient
} }
break; break;
case ContainerType.Lectern: case ContainerType.Lectern:
return false;
break; break;
case ContainerType.Loom: case ContainerType.Loom:
if (slotId >= 0 && slotId <= 3) if (slotId >= 0 && slotId <= 3)
@ -1941,9 +1942,9 @@ namespace MinecraftClient
} }
} }
break; break;
default:
// TODO: Define more container type here // TODO: Define more container type here
goto case ContainerType.Generic_9x3; default:
return false;
} }
// Cursor have item or not doesn't matter // Cursor have item or not doesn't matter

View file

@ -297,6 +297,7 @@ cmd.inventory.right=Right
cmd.inventory.middle=Middle cmd.inventory.middle=Middle
cmd.inventory.clicking={0} clicking slot {1} in window #{2} cmd.inventory.clicking={0} clicking slot {1} in window #{2}
cmd.inventory.shiftclick=Shift clicking slot {0} in window #{1} cmd.inventory.shiftclick=Shift clicking slot {0} in window #{1}
cmd.inventory.shiftclick_fail=Shift click failed, this may be because this container type is not supported
cmd.inventory.no_item=No item in slot #{0} cmd.inventory.no_item=No item in slot #{0}
cmd.inventory.drop=Dropped 1 item from slot #{0} cmd.inventory.drop=Dropped 1 item from slot #{0}
cmd.inventory.drop_stack=Dropped whole item stack from slot #{0} cmd.inventory.drop_stack=Dropped whole item stack from slot #{0}
@ -308,6 +309,7 @@ cmd.inventory.help.usage=Usage
cmd.inventory.help.list=List your inventory. cmd.inventory.help.list=List your inventory.
cmd.inventory.help.close=Close an opened container. cmd.inventory.help.close=Close an opened container.
cmd.inventory.help.click=Click on an item. cmd.inventory.help.click=Click on an item.
cmd.inventory.help.shiftclick=Shift click an item.
cmd.inventory.help.drop=Drop an item from inventory. cmd.inventory.help.drop=Drop an item from inventory.
cmd.inventory.help.creativegive=Give item in creative mode. cmd.inventory.help.creativegive=Give item in creative mode.
cmd.inventory.help.creativedelete=Clear slot in creative mode. cmd.inventory.help.creativedelete=Clear slot in creative mode.