using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace MinecraftClient.Inventory
{
///
/// Represents an item inside a Container
///
public class Item
{
///
/// Item Type
///
public ItemType Type;
///
/// Item Count
///
public int Count;
///
/// Item Metadata
///
public Dictionary? NBT;
///
/// Create an item with ItemType, Count and Metadata
///
/// Type of the item
/// Item Count
/// Item Metadata
public Item(ItemType itemType, int count, Dictionary? nbt)
{
Type = itemType;
Count = count;
NBT = nbt;
}
///
/// Check if the item slot is empty
///
/// TRUE if the item is empty
public bool IsEmpty
{
get
{
return Type == ItemType.Air || Count == 0;
}
}
///
/// Retrieve item display name from NBT properties. NULL if no display name is defined.
///
public string? DisplayName
{
get
{
if (NBT != null && NBT.ContainsKey("display"))
{
if (NBT["display"] is Dictionary displayProperties && displayProperties.ContainsKey("Name"))
{
string? displayName = displayProperties["Name"] as string;
if (!String.IsNullOrEmpty(displayName))
return MinecraftClient.Protocol.ChatParser.ParseText(displayProperties["Name"].ToString() ?? string.Empty);
}
}
return null;
}
}
///
/// Retrieve item lores from NBT properties. Returns null if no lores is defined.
///
public string[]? Lores
{
get
{
List lores = new();
if (NBT != null && NBT.ContainsKey("display"))
{
if (NBT["display"] is Dictionary displayProperties && displayProperties.ContainsKey("Lore"))
{
object[] displayName = (object[])displayProperties["Lore"];
lores.AddRange(from string st in displayName
let str = MinecraftClient.Protocol.ChatParser.ParseText(st.ToString())
select str);
return lores.ToArray();
}
}
return null;
}
}
///
/// Retrieve item damage from NBT properties. Returns 0 if no damage is defined.
///
public int Damage
{
get
{
if (NBT != null && NBT.ContainsKey("Damage"))
{
object damage = NBT["Damage"];
if (damage != null)
{
return int.Parse(damage.ToString() ?? string.Empty, NumberStyles.Any, CultureInfo.CurrentCulture);
}
}
return 0;
}
}
public override string ToString()
{
StringBuilder sb = new();
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();
}
}
}