Implement command completion suggestions.

This commit is contained in:
BruceChen 2022-12-06 15:50:17 +08:00
parent 5d2589b10f
commit 84cf749344
115 changed files with 4684 additions and 2695 deletions

View file

@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Brigadier.NET;
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
using MinecraftClient.Inventory;
namespace MinecraftClient.Commands
@ -14,270 +15,374 @@ namespace MinecraftClient.Commands
public override string CmdUsage { get { return GetBasicUsage(); } }
public override string CmdDesc { get { return Translations.cmd_inventory_desc; } }
public override void RegisterCommand(McClient handler, CommandDispatcher<CommandSource> dispatcher)
public override void RegisterCommand(McClient handler, CommandDispatcher<CmdResult> dispatcher)
{
dispatcher.Register(l => l.Literal("help")
.Then(l => l.Literal(CmdName)
.Executes(r => GetUsage(r.Source, string.Empty))
.Then(l => l.Literal("list")
.Executes(r => GetUsage(r.Source, "list")))
.Then(l => l.Literal("close")
.Executes(r => GetUsage(r.Source, "close")))
.Then(l => l.Literal("click")
.Executes(r => GetUsage(r.Source, "click")))
.Then(l => l.Literal("drop")
.Executes(r => GetUsage(r.Source, "drop")))
.Then(l => l.Literal("creativegive")
.Executes(r => GetUsage(r.Source, "creativegive")))
.Then(l => l.Literal("creativedelete")
.Executes(r => GetUsage(r.Source, "creativedelete")))
.Then(l => l.Literal("inventories")
.Executes(r => GetUsage(r.Source, "inventories")))
.Then(l => l.Literal("search")
.Executes(r => GetUsage(r.Source, "search")))
.Then(l => l.Literal("help")
.Executes(r => GetUsage(r.Source, "help")))
)
);
dispatcher.Register(l => l.Literal(CmdName)
.Executes(r => ListAllInventories(r.Source, handler))
.Then(l => l.Literal("creativegive")
.Then(l => l.Argument("Slot", Arguments.Integer(0, 89))
.Then(l => l.Argument("ItemType", MccArguments.ItemType())
.Then(l => l.Argument("Count", Arguments.Integer(min: 1))
.Executes(r => DoCreativeGive(r.Source, handler, Arguments.GetInteger(r, "Slot"), MccArguments.GetItemType(r, "ItemType"), Arguments.GetInteger(r, "Count")))))))
.Then(l => l.Literal("creativedelete")
.Then(l => l.Argument("Slot", Arguments.Integer(0, 89))
.Executes(r => DoCreativeDelete(r.Source, handler, Arguments.GetInteger(r, "Slot")))))
.Then(l => l.Literal("inventories")
.Executes(r => ListAvailableInventories(r.Source, handler)))
.Then(l => l.Literal("search")
.Then(l => l.Argument("ItemType", MccArguments.ItemType())
.Executes(r => SearchItem(r.Source, handler, MccArguments.GetItemType(r, "ItemType"), null))
.Then(l => l.Argument("Count", Arguments.Integer(0, 64))
.Executes(r => SearchItem(r.Source, handler, MccArguments.GetItemType(r, "ItemType"), Arguments.GetInteger(r, "Count"))))))
.Then(l => l.Argument("InventoryId", MccArguments.InventoryId())
.Then(l => l.Literal("close")
.Executes(r => DoCloseAction(r.Source, handler, Arguments.GetInteger(r, "InventoryId"))))
.Then(l => l.Literal("list")
.Executes(r => DoListAction(r.Source, handler, Arguments.GetInteger(r, "InventoryId"))))
.Then(l => l.Literal("click")
.Then(l => l.Argument("Slot", Arguments.Integer(0, 89))
.Executes(r => DoClickAction(r.Source, handler, Arguments.GetInteger(r, "InventoryId"), Arguments.GetInteger(r, "Slot"), WindowActionType.LeftClick))
.Then(l => l.Argument("Action", MccArguments.InventoryAction())
.Executes(r => DoClickAction(r.Source, handler, Arguments.GetInteger(r, "InventoryId"), Arguments.GetInteger(r, "Slot"), MccArguments.GetInventoryAction(r, "Action"))))))
.Then(l => l.Literal("drop")
.Then(l => l.Argument("Slot", Arguments.Integer(0, 89))
.Executes(r => DoDropAction(r.Source, handler, Arguments.GetInteger(r, "InventoryId"), Arguments.GetInteger(r, "Slot"), WindowActionType.DropItem))
.Then(l => l.Literal("all")
.Executes(r => DoDropAction(r.Source, handler, Arguments.GetInteger(r, "InventoryId"), Arguments.GetInteger(r, "Slot"), WindowActionType.DropItemStack))))))
.Then(l => l.Literal("player")
.Then(l => l.Literal("list")
.Executes(r => DoListAction(r.Source, handler, inventoryId: 0)))
.Then(l => l.Literal("click")
.Then(l => l.Argument("Slot", Arguments.Integer(0, 45))
.Executes(r => DoClickAction(r.Source, handler, inventoryId: 0, Arguments.GetInteger(r, "Slot"), WindowActionType.LeftClick))
.Then(l => l.Argument("Action", MccArguments.InventoryAction())
.Executes(r => DoClickAction(r.Source, handler, inventoryId: 0, Arguments.GetInteger(r, "Slot"), MccArguments.GetInventoryAction(r, "Action"))))))
.Then(l => l.Literal("drop")
.Then(l => l.Argument("Slot", Arguments.Integer(0, 45))
.Executes(r => DoDropAction(r.Source, handler, inventoryId: 0, Arguments.GetInteger(r, "Slot"), WindowActionType.DropItem))
.Then(l => l.Literal("all")
.Executes(r => DoDropAction(r.Source, handler, inventoryId: 0, Arguments.GetInteger(r, "Slot"), WindowActionType.DropItemStack))))))
.Then(l => l.Literal("container")
.Then(l => l.Literal("close")
.Executes(r => DoCloseAction(r.Source, handler, inventoryId: null)))
.Then(l => l.Literal("list")
.Executes(r => DoListAction(r.Source, handler, inventoryId: null)))
.Then(l => l.Literal("click")
.Then(l => l.Argument("Slot", Arguments.Integer(0, 89))
.Executes(r => DoClickAction(r.Source, handler, inventoryId: null, Arguments.GetInteger(r, "Slot"), WindowActionType.LeftClick))
.Then(l => l.Argument("Action", MccArguments.InventoryAction())
.Executes(r => DoClickAction(r.Source, handler, inventoryId: null, Arguments.GetInteger(r, "Slot"), MccArguments.GetInventoryAction(r, "Action"))))))
.Then(l => l.Literal("drop")
.Then(l => l.Argument("Slot", Arguments.Integer(0, 89))
.Executes(r => DoDropAction(r.Source, handler, inventoryId: null, Arguments.GetInteger(r, "Slot"), WindowActionType.DropItem))
.Then(l => l.Literal("all")
.Executes(r => DoDropAction(r.Source, handler, inventoryId: null, Arguments.GetInteger(r, "Slot"), WindowActionType.DropItemStack))))))
.Then(l => l.Literal("_help")
.Redirect(dispatcher.GetRoot().GetChild("help").GetChild(CmdName)))
);
}
public override string Run(McClient handler, string command, Dictionary<string, object>? localVars)
private int GetUsage(CmdResult r, string? cmd)
{
if (handler.GetInventoryEnabled())
string usageStr = ' ' + Translations.cmd_inventory_help_usage + ": ";
return r.SetAndReturn(cmd switch
{
string[] args = GetArgs(command);
if (args.Length >= 1)
{
int inventoryId;
if (args[0].ToLower() == "creativegive")
{
if (args.Length >= 4)
{
if (!int.TryParse(args[1], NumberStyles.Any, CultureInfo.CurrentCulture, out int slot))
return GetCmdDescTranslated();
#pragma warning disable format // @formatter:off
"list" => Translations.cmd_inventory_help_list + usageStr + "/inventory <player|container|<id>> list",
"close" => Translations.cmd_inventory_help_close + usageStr + "/inventory <player|container|<id>> close",
"click" => Translations.cmd_inventory_help_click + usageStr + "/inventory <player|container|<id>> click <slot> [left|right|middle|shift]\nDefault is left click",
"drop" => Translations.cmd_inventory_help_drop + usageStr + "/inventory <player|container|<id>> drop <slot> [all]\nAll means drop full stack",
"creativegive" => Translations.cmd_inventory_help_creativegive + usageStr + "/inventory creativegive <slot> <itemtype> <amount>",
"creativedelete" => Translations.cmd_inventory_help_creativedelete + usageStr + "/inventory creativedelete <slot>",
"inventories" => Translations.cmd_inventory_help_inventories + usageStr + "/inventory inventories",
"search" => Translations.cmd_inventory_help_search + usageStr + "/inventory search <item type> [count]",
"help" => GetCmdDescTranslated(),
"action" => Translations.cmd_inventory_help_unknown + GetAvailableActions(),
_ => GetCmdDescTranslated(),
#pragma warning restore format // @formatter:on
});
}
if (Enum.TryParse(args[2], true, out ItemType itemType))
{
if (handler.GetGamemode() == 1)
{
if (!int.TryParse(args[3], NumberStyles.Any, CultureInfo.CurrentCulture, out int count))
return GetCmdDescTranslated();
private int GetMaximumInventoryId(McClient handler)
{
List<int> availableIds = handler.GetInventories().Keys.ToList();
return availableIds.Max(); // use foreground container
}
if (handler.DoCreativeGive(slot, itemType, count, null))
return string.Format(Translations.cmd_inventory_creative_done, itemType, count, slot);
else
return Translations.cmd_inventory_creative_fail;
}
else
return Translations.cmd_inventory_need_creative;
}
else
return GetCmdDescTranslated();
}
else
return GetCmdDescTranslated();
}
else if (args[0].ToLower() == "creativedelete")
{
if (args.Length >= 2)
{
if (!int.TryParse(args[1], NumberStyles.Any, CultureInfo.CurrentCulture, out int slot))
return GetCmdDescTranslated();
private int ListAllInventories(CmdResult r, McClient handler)
{
if (!handler.GetInventoryEnabled())
{
handler.Log.Info(Translations.extra_inventory_required);
return -1;
}
StringBuilder response = new();
response.Append(Translations.cmd_inventory_inventories).Append(":\n");
foreach ((int invId, Container inv) in handler.GetInventories())
response.AppendLine(String.Format(" #{0}: {1}§8", invId, inv.Title));
response.Append(CmdUsage);
handler.Log.Info(response.ToString());
return r.SetAndReturn(CmdResult.Status.Done);
}
if (handler.GetGamemode() == 1)
{
if (handler.DoCreativeGive(slot, ItemType.Null, 0, null))
return string.Format(Translations.cmd_inventory_creative_delete, slot);
else
return Translations.cmd_inventory_creative_fail;
}
else
return Translations.cmd_inventory_need_creative;
}
else
return GetCmdDescTranslated();
}
else if (args[0].ToLower().StartsWith("p"))
{
// player inventory is always ID 0
inventoryId = 0;
}
else if (args[0].ToLower().StartsWith("c"))
{
List<int> availableIds = handler.GetInventories().Keys.ToList();
availableIds.Remove(0); // remove player inventory ID from list
if (availableIds.Count > 0)
inventoryId = availableIds.Max(); // use foreground container
else
return Translations.cmd_inventory_container_not_found;
}
else if (args[0].ToLower().StartsWith("inventories") || args[0].ToLower().StartsWith("i"))
{
Dictionary<int, Container> inventories = handler.GetInventories();
List<int> availableIds = inventories.Keys.ToList();
StringBuilder response = new();
response.AppendLine(Translations.cmd_inventory_inventories_available);
private int DoCreativeGive(CmdResult r, McClient handler, int slot, ItemType itemType, int count)
{
if (!handler.GetInventoryEnabled())
return r.SetAndReturn(CmdResult.Status.FailNeedInventory);
foreach (int id in availableIds)
response.AppendLine(String.Format(" #{0} - {1}§8", id, inventories[id].Title));
return response.ToString();
}
else if (args[0].ToLower().StartsWith("search") || args[0].ToLower().StartsWith("s"))
{
if (args.Length < 2)
return GetCmdDescTranslated();
if (!Enum.TryParse(args[1], true, out ItemType parsedItemType))
return GetCmdDescTranslated();
bool shouldUseItemCount = args.Length >= 3;
int itemCount = 0;
if (shouldUseItemCount && !int.TryParse(args[2], NumberStyles.Any, CultureInfo.CurrentCulture, out itemCount))
return GetCmdDescTranslated();
Dictionary<int, Container> inventories = handler.GetInventories();
Dictionary<int, List<Item>> foundItems = new();
List<Container> availableInventories = inventories.Values.ToList();
availableInventories.ForEach(inventory =>
{
inventory.Items.Values
.ToList()
.FindAll(item => item.Type == parsedItemType && (!shouldUseItemCount || item.Count == itemCount))
.ForEach(item =>
{
if (!foundItems.ContainsKey(inventory.ID))
{
foundItems.Add(inventory.ID, new List<Item>() { item });
return;
}
List<Item> invItems = foundItems[inventory.ID];
invItems.Add(item);
foundItems.Remove(inventory.ID);
foundItems.Add(inventory.ID, invItems);
});
});
if (foundItems.Count == 0)
return Translations.cmd_inventory_no_found_items;
StringBuilder response = new();
response.AppendLine(Translations.cmd_inventory_found_items + ":");
foreach ((int invId, List<Item> itemsList) in new SortedDictionary<int, List<Item>>(foundItems))
{
if (itemsList.Count > 0)
{
response.AppendLine(String.Format("{0} (#{1}):", inventories[invId].Title, invId));
foreach (Item item in itemsList)
response.AppendLine(String.Format("\t- {0}", item.ToFullString()));
response.AppendLine(" ");
}
}
return response.ToString();
}
else if (args[0].ToLower() == "help")
{
if (args.Length >= 2)
return GetSubCommandHelp(args[1]);
else
return GetHelp();
}
else if (!int.TryParse(args[0], NumberStyles.Any, CultureInfo.CurrentCulture, out inventoryId))
return GetCmdDescTranslated();
Container? inventory = handler.GetInventory(inventoryId);
if (inventory == null)
return string.Format(Translations.cmd_inventory_not_exist, inventoryId);
string action = args.Length > 1 ? args[1].ToLower() : "list";
if (action == "close")
{
if (handler.CloseInventory(inventoryId))
return string.Format(Translations.cmd_inventory_close, inventoryId);
else
return string.Format(Translations.cmd_inventory_close_fail, inventoryId);
}
else if (action == "list")
{
StringBuilder response = new();
response.Append(Translations.cmd_inventory_inventory);
response.AppendLine(String.Format(" #{0} - {1}§8", inventoryId, inventory.Title));
string? asciiArt = inventory.Type.GetAsciiArt();
if (asciiArt != null && Settings.Config.Main.Advanced.ShowInventoryLayout)
response.AppendLine(asciiArt);
int selectedHotbar = handler.GetCurrentSlot() + 1;
foreach ((int itemId, Item item) in new SortedDictionary<int, Item>(inventory.Items))
{
bool isHotbar = inventory.IsHotbar(itemId, out int hotbar);
string hotbarString = isHotbar ? (hotbar + 1).ToString() : " ";
if ((hotbar + 1) == selectedHotbar)
hotbarString = ">" + hotbarString;
response.AppendLine(String.Format("{0,2} | #{1,-2}: {2}", hotbarString, itemId, item.ToFullString()));
}
if (inventoryId == 0)
response.AppendLine(string.Format(Translations.cmd_inventory_hotbar, (handler.GetCurrentSlot() + 1)));
response.Remove(response.Length - 1, 1); // Remove last '\n'
return response.ToString();
}
else if (action == "click" && args.Length >= 3)
{
if (!int.TryParse(args[2], NumberStyles.Any, CultureInfo.CurrentCulture, out int slot))
return GetCmdDescTranslated();
WindowActionType actionType = WindowActionType.LeftClick;
string keyName = Translations.cmd_inventory_left;
if (args.Length >= 4)
{
string b = args[3];
if (b.ToLower()[0] == 'r')
(actionType, keyName) = (WindowActionType.RightClick, Translations.cmd_inventory_right);
else if (b.ToLower()[0] == 'm')
(actionType, keyName) = (WindowActionType.MiddleClick, Translations.cmd_inventory_middle);
}
handler.DoWindowAction(inventoryId, slot, actionType);
return string.Format(Translations.cmd_inventory_clicking, keyName, slot, inventoryId);
}
else if (action == "shiftclick" && args.Length >= 3)
{
if (!int.TryParse(args[2], NumberStyles.Any, CultureInfo.CurrentCulture, out int slot))
return GetCmdDescTranslated();
if (!handler.DoWindowAction(inventoryId, slot, WindowActionType.ShiftClick))
return Translations.cmd_inventory_shiftclick_fail;
return string.Format(Translations.cmd_inventory_shiftclick, slot, inventoryId);
}
else if (action == "drop" && args.Length >= 3)
{
if (!int.TryParse(args[2], NumberStyles.Any, CultureInfo.CurrentCulture, out int slot))
return GetCmdDescTranslated();
// check item exist
if (!inventory.Items.ContainsKey(slot))
return string.Format(Translations.cmd_inventory_no_item, slot);
WindowActionType actionType = WindowActionType.DropItem;
if (args.Length >= 4 && args[3].ToLower() == "all")
actionType = WindowActionType.DropItemStack;
if (handler.DoWindowAction(inventoryId, slot, actionType))
{
if (actionType == WindowActionType.DropItemStack)
return string.Format(Translations.cmd_inventory_drop_stack, slot);
else
return string.Format(Translations.cmd_inventory_drop, slot);
}
else
return "Failed";
}
else
return GetCmdDescTranslated();
}
if (handler.GetGamemode() == 1)
{
if (handler.DoCreativeGive(slot, itemType, count, null))
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.cmd_inventory_creative_done, itemType, count, slot));
else
{
StringBuilder response = new();
response.Append(Translations.cmd_inventory_inventories).Append(":\n");
foreach ((int invId, Container inv) in handler.GetInventories())
response.AppendLine(String.Format(" #{0}: {1}§8", invId, inv.Title));
response.Append(CmdUsage);
return response.ToString();
}
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_inventory_creative_fail);
}
else
return Translations.extra_inventory_required;
{
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_inventory_need_creative);
}
}
private int DoCreativeDelete(CmdResult r, McClient handler, int slot)
{
if (!handler.GetInventoryEnabled())
return r.SetAndReturn(CmdResult.Status.FailNeedInventory);
if (handler.GetGamemode() == 1)
{
if (handler.DoCreativeGive(slot, ItemType.Null, 0, null))
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.cmd_inventory_creative_delete, slot));
else
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_inventory_creative_fail);
}
else
{
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_inventory_need_creative);
}
}
private int ListAvailableInventories(CmdResult r, McClient handler)
{
if (!handler.GetInventoryEnabled())
return r.SetAndReturn(CmdResult.Status.FailNeedInventory);
Dictionary<int, Container> inventories = handler.GetInventories();
List<int> availableIds = inventories.Keys.ToList();
StringBuilder response = new();
response.AppendLine(Translations.cmd_inventory_inventories_available);
foreach (int id in availableIds)
response.AppendLine(String.Format(" #{0} - {1}§8", id, inventories[id].Title));
handler.Log.Info(response.ToString());
return r.SetAndReturn(CmdResult.Status.Done);
}
private int SearchItem(CmdResult r, McClient handler, ItemType itemType, int? itemCount)
{
if (!handler.GetInventoryEnabled())
return r.SetAndReturn(CmdResult.Status.FailNeedInventory);
Dictionary<int, Container> inventories = handler.GetInventories();
Dictionary<int, List<Item>> foundItems = new();
List<Container> availableInventories = inventories.Values.ToList();
availableInventories.ForEach(inventory =>
{
inventory.Items.Values
.ToList()
.FindAll(item => item.Type == itemType && (!itemCount.HasValue || item.Count == itemCount))
.ForEach(item =>
{
if (!foundItems.ContainsKey(inventory.ID))
{
foundItems.Add(inventory.ID, new List<Item>() { item });
return;
}
List<Item> invItems = foundItems[inventory.ID];
invItems.Add(item);
foundItems.Remove(inventory.ID);
foundItems.Add(inventory.ID, invItems);
});
});
if (foundItems.Count == 0)
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_inventory_no_found_items);
StringBuilder response = new();
response.AppendLine(Translations.cmd_inventory_found_items + ":");
foreach ((int invId, List<Item> itemsList) in new SortedDictionary<int, List<Item>>(foundItems))
{
if (itemsList.Count > 0)
{
response.AppendLine(String.Format("{0} (#{1}):", inventories[invId].Title, invId));
foreach (Item item in itemsList)
response.AppendLine(String.Format("\t- {0}", item.ToFullString()));
response.AppendLine(" ");
}
}
handler.Log.Info(response.ToString());
return r.SetAndReturn(CmdResult.Status.Done);
}
private int DoCloseAction(CmdResult r, McClient handler, int? inventoryId)
{
if (!handler.GetInventoryEnabled())
return r.SetAndReturn(CmdResult.Status.FailNeedInventory);
if (!inventoryId.HasValue)
{
inventoryId = GetMaximumInventoryId(handler);
if (inventoryId == 0)
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_inventory_container_not_found);
}
Container? inventory = handler.GetInventory(inventoryId.Value);
if (inventory == null)
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_inventory_not_exist, inventoryId));
if (handler.CloseInventory(inventoryId.Value))
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.cmd_inventory_close, inventoryId));
else
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_inventory_close_fail, inventoryId));
}
private int DoListAction(CmdResult r, McClient handler, int? inventoryId)
{
if (!handler.GetInventoryEnabled())
return r.SetAndReturn(CmdResult.Status.FailNeedInventory);
if (!inventoryId.HasValue)
{
inventoryId = GetMaximumInventoryId(handler);
if (inventoryId == 0)
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_inventory_container_not_found);
}
Container? inventory = handler.GetInventory(inventoryId.Value);
if (inventory == null)
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_inventory_not_exist, inventoryId));
StringBuilder response = new();
response.Append(Translations.cmd_inventory_inventory);
response.AppendLine(String.Format(" #{0} - {1}§8", inventoryId, inventory.Title));
string? asciiArt = inventory.Type.GetAsciiArt();
if (asciiArt != null && Settings.Config.Main.Advanced.ShowInventoryLayout)
response.AppendLine(asciiArt);
int selectedHotbar = handler.GetCurrentSlot() + 1;
foreach ((int itemId, Item item) in new SortedDictionary<int, Item>(inventory.Items))
{
bool isHotbar = inventory.IsHotbar(itemId, out int hotbar);
string hotbarString = isHotbar ? (hotbar + 1).ToString() : " ";
if ((hotbar + 1) == selectedHotbar)
hotbarString = ">" + hotbarString;
response.AppendLine(String.Format("{0,2} | #{1,-2}: {2}", hotbarString, itemId, item.ToFullString()));
}
if (inventoryId == 0)
response.AppendLine(string.Format(Translations.cmd_inventory_hotbar, (handler.GetCurrentSlot() + 1)));
response.Remove(response.Length - 1, 1); // Remove last '\n'
handler.Log.Info(response.ToString());
return r.SetAndReturn(CmdResult.Status.Done);
}
private int DoClickAction(CmdResult r, McClient handler, int? inventoryId, int slot, WindowActionType actionType)
{
if (!handler.GetInventoryEnabled())
return r.SetAndReturn(CmdResult.Status.FailNeedInventory);
if (!inventoryId.HasValue)
{
inventoryId = GetMaximumInventoryId(handler);
if (inventoryId == 0)
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_inventory_container_not_found);
}
Container? inventory = handler.GetInventory(inventoryId.Value);
if (inventory == null)
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_inventory_not_exist, inventoryId));
string keyName = actionType switch
{
WindowActionType.LeftClick => Translations.cmd_inventory_left,
WindowActionType.RightClick => Translations.cmd_inventory_right,
WindowActionType.MiddleClick => Translations.cmd_inventory_middle,
WindowActionType.ShiftClick => Translations.cmd_inventory_shiftclick,
_ => "unknown",
};
handler.Log.Info(string.Format(Translations.cmd_inventory_clicking, keyName, slot, inventoryId));
return r.SetAndReturn(handler.DoWindowAction(inventoryId.Value, slot, actionType));
}
private int DoDropAction(CmdResult r, McClient handler, int? inventoryId, int slot, WindowActionType actionType)
{
if (!handler.GetInventoryEnabled())
return r.SetAndReturn(CmdResult.Status.FailNeedInventory);
if (!inventoryId.HasValue)
{
inventoryId = GetMaximumInventoryId(handler);
if (inventoryId == 0)
return r.SetAndReturn(CmdResult.Status.Fail, Translations.cmd_inventory_container_not_found);
}
Container? inventory = handler.GetInventory(inventoryId.Value);
if (inventory == null)
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_inventory_not_exist, inventoryId));
// check item exist
if (!inventory.Items.ContainsKey(slot))
return r.SetAndReturn(CmdResult.Status.Fail, string.Format(Translations.cmd_inventory_no_item, slot));
if (handler.DoWindowAction(inventoryId.Value, slot, actionType))
{
if (actionType == WindowActionType.DropItemStack)
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.cmd_inventory_drop_stack, slot));
else
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.cmd_inventory_drop, slot));
}
else
{
return r.SetAndReturn(CmdResult.Status.Fail, "Drop Failed");
}
}
#region Methods for commands help
private static string GetAvailableActions()
@ -290,31 +395,6 @@ namespace MinecraftClient.Commands
return Translations.cmd_inventory_help_basic + ": /inventory <player|container|<id>> <action>.";
}
private static string GetHelp()
{
return string.Format(Translations.cmd_inventory_help_help, GetAvailableActions());
}
private static string GetSubCommandHelp(string cmd)
{
string usageStr = ' ' + Translations.cmd_inventory_help_usage + ": ";
return cmd switch
{
#pragma warning disable format // @formatter:off
"list" => Translations.cmd_inventory_help_list + usageStr + "/inventory <player|container|<id>> list",
"close" => Translations.cmd_inventory_help_close + usageStr + "/inventory <player|container|<id>> close",
"click" => Translations.cmd_inventory_help_click + usageStr + "/inventory <player|container|<id>> click <slot> [left|right|middle]\nDefault is left click",
"shiftclick" => Translations.cmd_inventory_help_shiftclick + usageStr + "/inventory <player|container|<id>> shiftclick <slot>",
"drop" => Translations.cmd_inventory_help_drop + usageStr + "/inventory <player|container|<id>> drop <slot> [all]\nAll means drop full stack",
"creativegive" => Translations.cmd_inventory_help_creativegive + usageStr + "/inventory creativegive <slot> <itemtype> <amount>",
"creativedelete" => Translations.cmd_inventory_help_creativedelete + usageStr + "/inventory creativedelete <slot>",
"inventories" => Translations.cmd_inventory_help_inventories + usageStr + "/inventory inventories",
"search" => Translations.cmd_inventory_help_search + usageStr + "/inventory search <item type> [count]",
"help" => GetHelp(),
_ => Translations.cmd_inventory_help_unknown + GetAvailableActions(),
#pragma warning restore format // @formatter:on
};
}
#endregion
}
}