2020-03-26 15:01:42 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2022-10-04 11:53:07 +08:00
|
|
|
|
using System.Globalization;
|
2022-10-06 14:53:05 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
2022-12-06 15:50:17 +08:00
|
|
|
|
using MinecraftClient.Protocol.Message;
|
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>
|
2020-03-28 23:30:17 +01:00
|
|
|
|
/// Item Type
|
2020-03-28 15:01:08 +01:00
|
|
|
|
/// </summary>
|
2020-03-28 23:30:17 +01:00
|
|
|
|
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
|
|
|
|
|
2024-03-12 11:15:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Item Count
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int Data;
|
|
|
|
|
|
|
2020-03-28 15:01:08 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Item Metadata
|
|
|
|
|
|
/// </summary>
|
2022-08-15 23:55:44 +08:00
|
|
|
|
public Dictionary<string, object>? NBT;
|
2020-03-26 15:01:42 +08:00
|
|
|
|
|
2020-03-28 15:01:08 +01:00
|
|
|
|
/// <summary>
|
2020-08-17 23:08:50 +08:00
|
|
|
|
/// Create an item with ItemType, Count and Metadata
|
2020-03-28 15:01:08 +01:00
|
|
|
|
/// </summary>
|
2020-08-17 23:08:50 +08:00
|
|
|
|
/// <param name="itemType">Type of the item</param>
|
|
|
|
|
|
/// <param name="count">Item Count</param>
|
2020-06-20 21:30:23 +02:00
|
|
|
|
/// <param name="nbt">Item Metadata</param>
|
2022-08-15 23:55:44 +08:00
|
|
|
|
public Item(ItemType itemType, int count, Dictionary<string, object>? nbt)
|
2020-03-26 15:01:42 +08:00
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
Type = itemType;
|
|
|
|
|
|
Count = count;
|
|
|
|
|
|
NBT = nbt;
|
2020-03-26 15:01:42 +08:00
|
|
|
|
}
|
2024-03-12 11:15:05 +01:00
|
|
|
|
|
|
|
|
|
|
public Item(ItemType itemType, int count, int data, Dictionary<string, object>? nbt) : this(itemType, count, nbt)
|
|
|
|
|
|
{
|
|
|
|
|
|
Data = data;
|
|
|
|
|
|
}
|
2020-03-28 15:01:08 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-03-29 18:41:26 +02:00
|
|
|
|
/// Check if the item slot is empty
|
2020-03-28 15:01:08 +01:00
|
|
|
|
/// </summary>
|
2020-03-29 18:41:26 +02:00
|
|
|
|
/// <returns>TRUE if the item is empty</returns>
|
|
|
|
|
|
public bool IsEmpty
|
2020-03-26 15:01:42 +08:00
|
|
|
|
{
|
2023-04-30 15:18:33 +02:00
|
|
|
|
get { return Type == ItemType.Air || Count == 0; }
|
2020-03-26 15:01:42 +08:00
|
|
|
|
}
|
2020-04-01 22:05:44 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Retrieve item display name from NBT properties. NULL if no display name is defined.
|
|
|
|
|
|
/// </summary>
|
2022-10-02 18:31:08 +08:00
|
|
|
|
public string? DisplayName
|
2020-04-01 22:05:44 +02:00
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (NBT != null && NBT.ContainsKey("display"))
|
|
|
|
|
|
{
|
2023-04-30 15:18:33 +02:00
|
|
|
|
if (NBT["display"] is Dictionary<string, object> displayProperties &&
|
|
|
|
|
|
displayProperties.ContainsKey("Name"))
|
2020-04-01 22:05:44 +02:00
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
string? displayName = displayProperties["Name"] as string;
|
2020-04-01 22:05:44 +02:00
|
|
|
|
if (!String.IsNullOrEmpty(displayName))
|
2022-12-06 15:50:17 +08:00
|
|
|
|
return ChatParser.ParseText(displayProperties["Name"].ToString() ?? string.Empty);
|
2020-04-01 22:05:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-30 15:18:33 +02:00
|
|
|
|
|
2020-04-01 22:05:44 +02:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-10-02 18:31:08 +08:00
|
|
|
|
|
2020-08-26 21:58:45 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Retrieve item lores from NBT properties. Returns null if no lores is defined.
|
|
|
|
|
|
/// </summary>
|
2022-10-02 18:31:08 +08:00
|
|
|
|
public string[]? Lores
|
2020-08-26 21:58:45 +05:00
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
List<string> lores = new();
|
2020-08-26 21:58:45 +05:00
|
|
|
|
if (NBT != null && NBT.ContainsKey("display"))
|
|
|
|
|
|
{
|
2023-04-30 15:18:33 +02:00
|
|
|
|
if (NBT["display"] is Dictionary<string, object> displayProperties &&
|
|
|
|
|
|
displayProperties.ContainsKey("Lore"))
|
2020-08-26 21:58:45 +05:00
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
object[] displayName = (object[])displayProperties["Lore"];
|
|
|
|
|
|
lores.AddRange(from string st in displayName
|
2023-04-30 15:18:33 +02:00
|
|
|
|
let str = ChatParser.ParseText(st.ToString())
|
|
|
|
|
|
select str);
|
2020-08-26 21:58:45 +05:00
|
|
|
|
return lores.ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-30 15:18:33 +02:00
|
|
|
|
|
2020-08-26 21:58:45 +05:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-10-02 18:31:08 +08:00
|
|
|
|
|
2020-08-17 15:27:15 +05:00
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
2023-04-30 15:18:33 +02:00
|
|
|
|
return int.Parse(damage.ToString() ?? string.Empty, NumberStyles.Any,
|
|
|
|
|
|
CultureInfo.CurrentCulture);
|
2020-08-17 15:27:15 +05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-30 15:18:33 +02:00
|
|
|
|
|
2020-08-17 15:27:15 +05:00
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-05-11 14:02:47 +08:00
|
|
|
|
|
2022-10-21 19:05:49 +08:00
|
|
|
|
public static string GetTypeString(ItemType type)
|
2022-10-17 17:42:00 +08:00
|
|
|
|
{
|
2022-10-21 19:05:49 +08:00
|
|
|
|
string type_str = type.ToString();
|
|
|
|
|
|
string type_renamed = type_str.ToUnderscoreCase();
|
2022-12-06 15:50:17 +08:00
|
|
|
|
string? res1 = ChatParser.TranslateString("item.minecraft." + type_renamed);
|
2022-10-17 17:42:00 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(res1))
|
|
|
|
|
|
return res1;
|
2022-12-06 15:50:17 +08:00
|
|
|
|
string? res2 = ChatParser.TranslateString("block.minecraft." + type_renamed);
|
2022-10-17 17:42:00 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(res2))
|
|
|
|
|
|
return res2;
|
2022-10-21 19:05:49 +08:00
|
|
|
|
return type_str;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetTypeString()
|
|
|
|
|
|
{
|
|
|
|
|
|
return GetTypeString(Type);
|
2022-10-17 17:42:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string ToFullString()
|
|
|
|
|
|
{
|
|
|
|
|
|
StringBuilder sb = new();
|
|
|
|
|
|
sb.Append(ToString());
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-04-30 15:18:33 +02:00
|
|
|
|
if (NBT != null && (NBT.TryGetValue("Enchantments", out object? enchantments) ||
|
|
|
|
|
|
NBT.TryGetValue("StoredEnchantments", out enchantments)))
|
2022-10-17 17:42:00 +08:00
|
|
|
|
{
|
|
|
|
|
|
foreach (Dictionary<string, object> enchantment in (object[])enchantments)
|
|
|
|
|
|
{
|
|
|
|
|
|
short level = (short)enchantment["lvl"];
|
|
|
|
|
|
string id = ((string)enchantment["id"]).Replace(':', '.');
|
|
|
|
|
|
sb.AppendFormat(" | {0} {1}",
|
2023-04-30 15:18:33 +02:00
|
|
|
|
ChatParser.TranslateString("enchantment." + id) ?? id,
|
|
|
|
|
|
ChatParser.TranslateString("enchantment.level." + level) ?? level.ToString());
|
2022-10-17 17:42:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-30 15:18:33 +02:00
|
|
|
|
|
|
|
|
|
|
if (Lores != null && Lores.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var lore in Lores)
|
|
|
|
|
|
sb.AppendFormat(" | {0}", lore);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
2022-10-17 17:42:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-11 14:02:47 +08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
StringBuilder sb = new();
|
|
|
|
|
|
|
2022-10-17 20:00:33 +08:00
|
|
|
|
sb.AppendFormat("x{0,-2} {1}", Count, GetTypeString());
|
2022-10-02 18:31:08 +08:00
|
|
|
|
|
|
|
|
|
|
string? displayName = DisplayName;
|
2021-05-11 14:02:47 +08:00
|
|
|
|
if (!String.IsNullOrEmpty(displayName))
|
|
|
|
|
|
sb.AppendFormat(" - {0}§8", displayName);
|
2022-10-02 18:31:08 +08:00
|
|
|
|
|
2021-05-11 14:02:47 +08:00
|
|
|
|
int damage = Damage;
|
|
|
|
|
|
if (damage != 0)
|
2022-10-28 11:13:20 +08:00
|
|
|
|
sb.AppendFormat(" | {0}: {1}", Translations.cmd_inventory_damage, damage);
|
2022-10-02 18:31:08 +08:00
|
|
|
|
|
2021-05-11 14:02:47 +08:00
|
|
|
|
return sb.ToString();
|
|
|
|
|
|
}
|
2020-03-26 15:01:42 +08:00
|
|
|
|
}
|
2023-04-30 15:18:33 +02:00
|
|
|
|
}
|