Improve inventory command output (#1582)

* Improve inventory command output

* Remove unused code

* Sort item list before printing

* Fix incorrect container slot count

* Update Container.IsHotbar method
This commit is contained in:
ReinforceZwei 2021-05-11 14:02:47 +08:00 committed by GitHub
parent 1cd7c098c3
commit 073458f5f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 38 deletions

View file

@ -78,29 +78,23 @@ namespace MinecraftClient.Commands
else return Translations.Get("cmd.inventory.close_fail", inventoryId); else return Translations.Get("cmd.inventory.close_fail", inventoryId);
case "list": case "list":
Container inventory = handler.GetInventory(inventoryId); 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>(); List<string> response = new List<string>();
response.Add(Translations.Get("cmd.inventory.inventory") + " #" + inventoryId + " - " + inventory.Title + "§8"); response.Add(Translations.Get("cmd.inventory.inventory") + " #" + inventoryId + " - " + inventory.Title + "§8");
foreach (KeyValuePair<int, Item> item in inventory.Items) int selectedHotbar = handler.GetCurrentSlot() + 1;
foreach (KeyValuePair<int, Item> item in itemsSorted)
{ {
string displayName = item.Value.DisplayName; int hotbar;
if (String.IsNullOrEmpty(displayName)) bool isHotbar = inventory.IsHotbar(item.Key, out hotbar);
{ string hotbarString = isHotbar ? (hotbar + 1).ToString() : " ";
if (item.Value.Damage != 0) if ((hotbar + 1) == selectedHotbar)
response.Add(String.Format(" #{0}: {1} x{2} | {3}: {4}", item.Key, item.Value.Type, item.Value.Count, Translations.Get("cmd.inventory.damage"), item.Value.Damage)); hotbarString = ">" + hotbarString;
else response.Add(String.Format("{0,2} | #{1,-2}: {2}", hotbarString, item.Key, item.Value.ToString()));
response.Add(String.Format(" #{0}: {1} x{2}", item.Key, item.Value.Type, item.Value.Count));
}
else
{
if (item.Value.Damage != 0)
response.Add(String.Format(" #{0}: {1} x{2} - {3}§8 | {4}: {5}", item.Key, item.Value.Type, item.Value.Count, displayName, Translations.Get("cmd.inventory.damage"), item.Value.Damage));
else
response.Add(String.Format(" #{0}: {1} x{2} - {3}§8", item.Key, item.Value.Type, item.Value.Count, displayName));
}
} }
if (inventoryId == 0) response.Add(Translations.Get("cmd.inventory.hotbar", (handler.GetCurrentSlot() + 1))); if (inventoryId == 0)
response.Add(Translations.Get("cmd.inventory.hotbar", (handler.GetCurrentSlot() + 1)));
return String.Join("\n", response.ToArray()); return String.Join("\n", response.ToArray());
case "click": case "click":
if (args.Length >= 3) if (args.Length >= 3)

View file

@ -191,5 +191,29 @@ namespace MinecraftClient.Inventory
} }
return result.ToArray(); return result.ToArray();
} }
/// <summary>
/// Check the given slot ID is a hotbar slot and give the hotbar number
/// </summary>
/// <param name="slotId">The slot ID to check</param>
/// <param name="hotbar">Zero-based, 0-8. -1 if not a hotbar</param>
/// <returns>True if given slot ID is a hotbar slot</returns>
public bool IsHotbar(int slotId, out int hotbar)
{
int hotbarStart = Type.SlotCount() - 9;
// Remove offhand slot
if (Type == ContainerType.PlayerInventory)
hotbarStart--;
if ((slotId >= hotbarStart) && (slotId <= hotbarStart + 9))
{
hotbar = slotId - hotbarStart;
return true;
}
else
{
hotbar = -1;
return false;
}
}
} }
} }

View file

@ -7,30 +7,35 @@ namespace MinecraftClient.Inventory
{ {
public static class ContainerTypeExtensions public static class ContainerTypeExtensions
{ {
/// <summary>
/// Get the slot count of the container
/// </summary>
/// <param name="c"></param>
/// <returns>Slot count of the container</returns>
public static int SlotCount(this ContainerType c) public static int SlotCount(this ContainerType c)
{ {
switch (c) switch (c)
{ {
case ContainerType.PlayerInventory: return 44; case ContainerType.PlayerInventory: return 46;
case ContainerType.Generic_9x3: return 62; case ContainerType.Generic_9x3: return 63;
case ContainerType.Generic_9x6: return 89; case ContainerType.Generic_9x6: return 90;
case ContainerType.Generic_3x3: return 44; case ContainerType.Generic_3x3: return 45;
case ContainerType.Crafting: return 45; case ContainerType.Crafting: return 46;
case ContainerType.BlastFurnace: return 38; case ContainerType.BlastFurnace: return 39;
case ContainerType.Furnace: return 38; case ContainerType.Furnace: return 39;
case ContainerType.Smoker: return 38; case ContainerType.Smoker: return 39;
case ContainerType.Enchantment: return 37; case ContainerType.Enchantment: return 38;
case ContainerType.BrewingStand: return 40; case ContainerType.BrewingStand: return 41;
case ContainerType.Merchant: return 38; case ContainerType.Merchant: return 39;
case ContainerType.Beacon: return 36; case ContainerType.Beacon: return 37;
case ContainerType.Anvil: return 38; case ContainerType.Anvil: return 39;
case ContainerType.Hopper: return 40; case ContainerType.Hopper: return 41;
case ContainerType.ShulkerBox: return 62; case ContainerType.ShulkerBox: return 63;
case ContainerType.Loom: return 39; case ContainerType.Loom: return 40;
case ContainerType.Stonecutter: return 37; case ContainerType.Stonecutter: return 38;
case ContainerType.Lectern: return 36; case ContainerType.Lectern: return 37;
case ContainerType.Cartography: return 38; case ContainerType.Cartography: return 39;
case ContainerType.Grindstone: return 38; case ContainerType.Grindstone: return 39;
case ContainerType.Unknown: return 0; case ContainerType.Unknown: return 0;
default: return 0; default: return 0;
} }

View file

@ -115,5 +115,22 @@ namespace MinecraftClient.Inventory
return 0; return 0;
} }
} }
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("x{0,-2} {1}", Count, Type.ToString());
string displayName = DisplayName;
if (!String.IsNullOrEmpty(displayName))
{
sb.AppendFormat(" - {0}§8", displayName);
}
int damage = Damage;
if (damage != 0)
{
sb.AppendFormat(" | {0}: {1}", Translations.Get("cmd.inventory.damage"), damage);
}
return sb.ToString();
}
} }
} }