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

@ -191,5 +191,29 @@ namespace MinecraftClient.Inventory
}
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
{
/// <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)
{
switch (c)
{
case ContainerType.PlayerInventory: return 44;
case ContainerType.Generic_9x3: return 62;
case ContainerType.Generic_9x6: return 89;
case ContainerType.Generic_3x3: return 44;
case ContainerType.Crafting: return 45;
case ContainerType.BlastFurnace: return 38;
case ContainerType.Furnace: return 38;
case ContainerType.Smoker: return 38;
case ContainerType.Enchantment: return 37;
case ContainerType.BrewingStand: return 40;
case ContainerType.Merchant: return 38;
case ContainerType.Beacon: return 36;
case ContainerType.Anvil: return 38;
case ContainerType.Hopper: return 40;
case ContainerType.ShulkerBox: return 62;
case ContainerType.Loom: return 39;
case ContainerType.Stonecutter: return 37;
case ContainerType.Lectern: return 36;
case ContainerType.Cartography: return 38;
case ContainerType.Grindstone: return 38;
case ContainerType.PlayerInventory: return 46;
case ContainerType.Generic_9x3: return 63;
case ContainerType.Generic_9x6: return 90;
case ContainerType.Generic_3x3: return 45;
case ContainerType.Crafting: return 46;
case ContainerType.BlastFurnace: return 39;
case ContainerType.Furnace: return 39;
case ContainerType.Smoker: return 39;
case ContainerType.Enchantment: return 38;
case ContainerType.BrewingStand: return 41;
case ContainerType.Merchant: return 39;
case ContainerType.Beacon: return 37;
case ContainerType.Anvil: return 39;
case ContainerType.Hopper: return 41;
case ContainerType.ShulkerBox: return 63;
case ContainerType.Loom: return 40;
case ContainerType.Stonecutter: return 38;
case ContainerType.Lectern: return 37;
case ContainerType.Cartography: return 39;
case ContainerType.Grindstone: return 39;
case ContainerType.Unknown: return 0;
default: return 0;
}

View file

@ -115,5 +115,22 @@ namespace MinecraftClient.Inventory
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();
}
}
}