Minecraft-Console-Client/MinecraftClient/Inventory/Item.cs

133 lines
4.1 KiB
C#
Raw Normal View History

2020-03-26 15:01:42 +08:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
2020-03-26 15:01:42 +08:00
namespace MinecraftClient.Inventory
{
2020-03-28 15:01:08 +01:00
/// <summary>
/// Represents an item inside a Container
/// </summary>
2020-03-26 15:01:42 +08:00
public class Item
{
2020-03-28 15:01:08 +01:00
/// <summary>
/// Item Type
2020-03-28 15:01:08 +01:00
/// </summary>
public ItemType Type;
2020-03-28 15:01:08 +01:00
/// <summary>
/// Item Count
/// </summary>
2020-03-26 15:01:42 +08:00
public int Count;
2020-03-28 15:01:08 +01:00
/// <summary>
/// Item Metadata
/// </summary>
public Dictionary<string, object>? NBT;
2020-03-26 15:01:42 +08:00
2020-03-28 15:01:08 +01:00
/// <summary>
/// Create an item with ItemType, Count and Metadata
2020-03-28 15:01:08 +01:00
/// </summary>
/// <param name="itemType">Type of the item</param>
/// <param name="count">Item Count</param>
/// <param name="nbt">Item Metadata</param>
public Item(ItemType itemType, int count, Dictionary<string, object>? nbt)
2020-03-26 15:01:42 +08:00
{
Type = itemType;
Count = count;
NBT = nbt;
2020-03-26 15:01:42 +08:00
}
2020-03-28 15:01:08 +01:00
/// <summary>
/// Check if the item slot is empty
2020-03-28 15:01:08 +01:00
/// </summary>
/// <returns>TRUE if the item is empty</returns>
public bool IsEmpty
2020-03-26 15:01:42 +08:00
{
get
{
return Type == ItemType.Air || Count == 0;
}
2020-03-26 15:01:42 +08:00
}
/// <summary>
/// Retrieve item display name from NBT properties. NULL if no display name is defined.
/// </summary>
public string? DisplayName
{
get
{
if (NBT != null && NBT.ContainsKey("display"))
{
if (NBT["display"] is Dictionary<string, object> 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;
}
}
/// <summary>
/// Retrieve item lores from NBT properties. Returns null if no lores is defined.
/// </summary>
public string[]? Lores
{
get
{
List<string> lores = new();
if (NBT != null && NBT.ContainsKey("display"))
{
if (NBT["display"] is Dictionary<string, object> 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;
}
}
/// <summary>
/// Retrieve item damage from NBT properties. Returns 0 if no damage is defined.
/// </summary>
public int Damage
{
get
{
if (NBT != null && NBT.ContainsKey("Damage"))
{
object damage = NBT["Damage"];
if (damage != null)
{
return int.Parse(damage.ToString() ?? string.Empty);
}
}
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();
}
2020-03-26 15:01:42 +08:00
}
}