Tooltip support & Bug fix

This commit is contained in:
BruceChen 2022-12-11 13:00:19 +08:00
parent 892999ac98
commit 5e11ed3896
40 changed files with 8409 additions and 1987 deletions

View file

@ -23,8 +23,27 @@ namespace MinecraftClient.CommandHandler.ArgumentType
public override Task<Suggestions> ListSuggestions<TSource>(CommandContext<TSource> context, SuggestionsBuilder builder)
{
foreach (var result in Enum.GetNames(typeof(EntityType)))
builder.Suggest(result);
foreach (EntityType result in Enum.GetValues<EntityType>())
{
string name = result.ToString();
string localName = Entity.GetTypeString(result);
bool same = true;
for (int i = 0, j = 0; i < name.Length; ++i, ++j)
{
while (j < localName.Length && localName[j] == ' ')
++j;
if (j >= localName.Length || name[i] != localName[j])
{
same = false;
break;
}
}
if (same)
builder.Suggest(name);
else
builder.Suggest(name, new SuggestionTooltip(localName));
}
return builder.BuildFuture();
}
}

View file

@ -0,0 +1,43 @@
using System;
using System.Threading.Tasks;
using Brigadier.NET;
using Brigadier.NET.ArgumentTypes;
using Brigadier.NET.Context;
using Brigadier.NET.Suggestion;
namespace MinecraftClient.CommandHandler.ArgumentType
{
public class HotbarSlotArgumentType : ArgumentType<int>
{
public override int Parse(IStringReader reader)
{
reader.SkipWhitespace();
return reader.ReadInt();
}
public override Task<Suggestions> ListSuggestions<TSource>(CommandContext<TSource> context, SuggestionsBuilder builder)
{
McClient? client = CmdResult.client;
if (client != null)
{
Inventory.Container? inventory = client.GetInventory(0);
if (inventory != null)
{
for (int i = 1; i <= 9; ++i)
{
if (inventory.Items.TryGetValue(i - 1 + 36, out Inventory.Item? item))
{
string slotStr = i.ToString();
if (slotStr.StartsWith(builder.RemainingLowerCase, StringComparison.InvariantCultureIgnoreCase))
{
string itemDesc = item.Count == 1 ? item.GetTypeString() : string.Format("{0}x{1}", item.Count, item.GetTypeString());
builder.Suggest(slotStr, new SuggestionTooltip(itemDesc));
}
}
}
}
}
return builder.BuildFuture();
}
}
}

View file

@ -25,7 +25,13 @@ namespace MinecraftClient.CommandHandler.ArgumentType
{
string invName = inv.Key.ToString();
if (invName.StartsWith(builder.RemainingLowerCase, StringComparison.InvariantCultureIgnoreCase))
builder.Suggest(invName);
{
string? invTitle = inv.Value.Title;
if (!string.IsNullOrWhiteSpace(invTitle))
builder.Suggest(invName, new SuggestionTooltip(invTitle));
else
builder.Suggest(invName);
}
}
}
return builder.BuildFuture();

View file

@ -0,0 +1,55 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Brigadier.NET;
using Brigadier.NET.ArgumentTypes;
using Brigadier.NET.Context;
using Brigadier.NET.Suggestion;
namespace MinecraftClient.CommandHandler.ArgumentType
{
public class InventorySlotArgumentType : ArgumentType<int>
{
public override int Parse(IStringReader reader)
{
reader.SkipWhitespace();
return reader.ReadInt();
}
public override Task<Suggestions> ListSuggestions<TSource>(CommandContext<TSource> context, SuggestionsBuilder builder)
{
McClient? client = CmdResult.client;
if (client != null && context.Nodes.Count >= 2)
{
string invName = context.Nodes[1].Range.Get(builder.Input);
if (!int.TryParse(invName, out int invId))
invId = invName switch
{
"creativegive" => 0,
"creativedelete" => 0,
"player" => 0,
"container" => client.GetInventories().Keys.ToList().Max(),
_ => -1,
};
Inventory.Container? inventory = client.GetInventory(invId);
if (inventory != null)
{
foreach ((int slot, Inventory.Item item) in inventory.Items)
{
if (item != null && item.Count > 0)
{
string slotStr = slot.ToString();
if (slotStr.StartsWith(builder.RemainingLowerCase, StringComparison.InvariantCultureIgnoreCase))
{
string itemDesc = item.Count == 1 ? item.GetTypeString() : string.Format("{0}x{1}", item.Count, item.GetTypeString());
builder.Suggest(slotStr, new SuggestionTooltip(itemDesc));
}
}
}
}
}
return builder.BuildFuture();
}
}
}

View file

@ -23,8 +23,29 @@ namespace MinecraftClient.CommandHandler.ArgumentType
public override Task<Suggestions> ListSuggestions<TSource>(CommandContext<TSource> context, SuggestionsBuilder builder)
{
foreach (var result in Enum.GetNames(typeof(ItemType)))
builder.Suggest(result);
foreach (ItemType result in Enum.GetValues<ItemType>())
{
if (result == ItemType.Unknown || result == ItemType.Null)
continue;
string name = result.ToString();
string localName = Item.GetTypeString(result);
bool same = true;
for (int i = 0, j = 0; i < name.Length; ++i, ++j)
{
while (j < localName.Length && localName[j] == ' ')
++j;
if (j >= localName.Length || name[i] != localName[j])
{
same = false;
break;
}
}
if (same)
builder.Suggest(name);
else
builder.Suggest(name, new SuggestionTooltip(localName));
}
return builder.BuildFuture();
}
}

View file

@ -71,6 +71,11 @@ namespace MinecraftClient.CommandHandler
return new InventoryActionArgumentType();
}
public static InventorySlotArgumentType InventorySlot()
{
return new InventorySlotArgumentType();
}
public static Inventory.WindowActionType GetInventoryAction<TSource>(CommandContext<TSource> context, string name)
{
return context.GetArgument<Inventory.WindowActionType>(name);
@ -100,5 +105,10 @@ namespace MinecraftClient.CommandHandler
{
return new MapBotMapIdArgumentType();
}
public static HotbarSlotArgumentType HotbarSlot()
{
return new HotbarSlotArgumentType();
}
}
}

View file

@ -18,19 +18,26 @@ namespace MinecraftClient.CommandHandler.Patch
public static string GetAllUsageString(this CommandDispatcher<CmdResult> commandDispatcher, string commandName, bool restricted)
{
char cmdChar = Settings.Config.Main.Advanced.InternalCmdChar.ToChar();
string[] usages = commandDispatcher.GetAllUsage(commandDispatcher.GetRoot().GetChild(commandName), new(), restricted);
StringBuilder sb = new();
sb.AppendLine("All Usages:");
foreach (var usage in usages)
try
{
sb.Append(cmdChar).Append(commandName).Append(' ');
if (usage.Length > 0 && usage[0] == '_')
sb.AppendLine(usage.Replace("_help -> ", $"_help -> {cmdChar}help "));
else
sb.AppendLine(usage);
string[] usages = commandDispatcher.GetAllUsage(commandDispatcher.GetRoot().GetChild(commandName), new(), restricted);
StringBuilder sb = new();
sb.AppendLine("All Usages:");
foreach (var usage in usages)
{
sb.Append(cmdChar).Append(commandName).Append(' ');
if (usage.Length > 0 && usage[0] == '_')
sb.AppendLine(usage.Replace("_help -> ", $"_help -> {cmdChar}help "));
else
sb.AppendLine(usage);
}
sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}
catch
{
return string.Empty;
}
sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}
}
}

View file

@ -0,0 +1,14 @@
using Brigadier.NET;
namespace MinecraftClient.CommandHandler
{
internal class SuggestionTooltip : IMessage
{
public SuggestionTooltip(string tooltip)
{
String = tooltip;
}
public string String { get; set; }
}
}