Add code documentation for inventories

This commit is contained in:
ORelio 2020-03-28 15:01:08 +01:00
parent 195e162c7d
commit 6929ae236a
6 changed files with 134 additions and 18 deletions

View file

@ -8,8 +8,8 @@ namespace MinecraftClient.Commands
{ {
class GetInventory : Command class GetInventory : Command
{ {
public override string CMDName { get { return "getinventory"; } } public override string CMDName { get { return "inventory"; } }
public override string CMDDesc { get { return "getinventory: Show your inventory."; } } public override string CMDDesc { get { return "inventory: Show your inventory."; } }
public override string Run(McTcpClient handler, string command, Dictionary<string, object> localVars) public override string Run(McTcpClient handler, string command, Dictionary<string, object> localVars)
{ {

View file

@ -5,50 +5,123 @@ using System.Text;
namespace MinecraftClient.Inventory namespace MinecraftClient.Inventory
{ {
/// <summary>
/// Represents a Minecraft inventory (player inventory, chest, etc.)
/// </summary>
public class Container public class Container
{ {
/// <summary>
/// ID of the container on the server
/// </summary>
public int ID; public int ID;
/// <summary>
/// Type of container
/// </summary>
public ContainerType Type; public ContainerType Type;
/// <summary>
/// title of container
/// </summary>
public string Title; public string Title;
/// <summary>
/// Container Items
/// </summary>
public Dictionary<int, Item> Items; public Dictionary<int, Item> Items;
/// <summary>
/// Create an empty container
/// </summary>
public Container() { } public Container() { }
/// <summary>
/// Create an empty container with ID, Type and Title
/// </summary>
/// <param name="id">Container ID</param>
/// <param name="type">Container Type</param>
/// <param name="title">Container Title</param>
public Container(int id, ContainerType type, string title) public Container(int id, ContainerType type, string title)
{ {
ID = id; ID = id;
Type = type; Type = type;
Title = title; Title = title;
Items = new Dictionary<int, Item>();
} }
public Container(int id, ContainerType type, string title,Dictionary<int,Item> items)
/// <summary>
/// Create a container with ID, Type, Title and Items
/// </summary>
/// <param name="id">Container ID</param>
/// <param name="type">Container Type</param>
/// <param name="title">Container Title</param>
/// <param name="items">Container Items (key: slot ID, value: item info)</param>
public Container(int id, ContainerType type, string title, Dictionary<int, Item> items)
{ {
ID = id; ID = id;
Type = type; Type = type;
Title = title; Title = title;
Items = items; Items = items;
} }
/// <summary>
/// Create an empty container with ID, Type and Title
/// </summary>
/// <param name="id">Container ID</param>
/// <param name="type">Container Type</param>
/// <param name="title">Container title</param>
public Container(int id, ContainerTypeOld type, string title) public Container(int id, ContainerTypeOld type, string title)
{ {
ID = id; ID = id;
Title = title; Title = title;
Type = ConvertType.ToNew(type); Type = ConvertType.ToNew(type);
Items = new Dictionary<int, Item>();
} }
/// <summary>
/// Create an empty container with ID, Type and Title
/// </summary>
/// <param name="id">Container ID</param>
/// <param name="typeID">Container Type</param>
/// <param name="title">Container Title</param>
public Container(int id, int typeID, string title) public Container(int id, int typeID, string title)
{ {
ID = id; ID = id;
Type = GetContainerType(typeID); Type = GetContainerType(typeID);
Title = title; Title = title;
Items = new Dictionary<int, Item>();
} }
// for player inventory because they dont have ID and title
/// <summary>
/// Create an empty container with Type
/// </summary>
/// <param name="type">Container Type</param>
public Container(ContainerType type) public Container(ContainerType type)
{ {
ID = -1;
Type = type; Type = type;
Title = null;
Items = new Dictionary<int, Item>();
} }
/// <summary>
/// Create an empty container with T^ype and Items
/// </summary>
/// <param name="type">Container Type</param>
/// <param name="items">Container Items (key: slot ID, value: item info)</param>
public Container(ContainerType type, Dictionary<int, Item> items) public Container(ContainerType type, Dictionary<int, Item> items)
{ {
ID = -1;
Type = type; Type = type;
Title = null;
Items = items; Items = items;
} }
/// <summary>
/// Get container type from Type ID
/// </summary>
/// <param name="typeID">Container Type ID</param>
/// <returns>Container Type</returns>
public static ContainerType GetContainerType(int typeID) public static ContainerType GetContainerType(int typeID)
{ {
// https://wiki.vg/Inventory didn't state the inventory ID, assume that list start with 0 // https://wiki.vg/Inventory didn't state the inventory ID, assume that list start with 0

View file

@ -5,26 +5,64 @@ using System.Text;
namespace MinecraftClient.Inventory namespace MinecraftClient.Inventory
{ {
/// <summary>
/// Represents an item inside a Container
/// </summary>
public class Item public class Item
{ {
/// <summary>
/// Item Type ID
/// </summary>
public int ID; public int ID;
/// <summary>
/// Item Count
/// </summary>
public int Count; public int Count;
public int SlotID = -1; // which slot is this item at, -1 = not specified
/// <summary>
/// Slot ID in the parent inventory (-1 means not specified)
/// </summary>
public int SlotID = -1;
/// <summary>
/// Item Metadata
/// </summary>
public Dictionary<string, object> NBT; public Dictionary<string, object> NBT;
public Item(int ID,int Count,int SlotID, Dictionary<string,object> NBT) /// <summary>
/// Create an item with Type ID, Count, Slot ID and Metadata
/// </summary>
/// <param name="ID">Item Type ID</param>
/// <param name="Count">Item Count</param>
/// <param name="SlotID">Item Slot ID in parent inventory</param>
/// <param name="NBT">Item Metadata</param>
public Item(int ID,int Count,int SlotID, Dictionary<string, object> NBT)
{ {
this.ID = ID; this.ID = ID;
this.Count = Count; this.Count = Count;
this.SlotID = SlotID; this.SlotID = SlotID;
this.NBT = NBT; this.NBT = NBT;
} }
/// <summary>
/// Create an item with Type ID, Count and Slot ID
/// </summary>
/// <param name="ID">Item Type ID</param>
/// <param name="Count">Item Count</param>
/// <param name="SlotID">Item Slot ID in parent inventory</param>
public Item(int ID, int Count, int SlotID) public Item(int ID, int Count, int SlotID)
{ {
this.ID = ID; this.ID = ID;
this.Count = Count; this.Count = Count;
this.SlotID = SlotID; this.SlotID = SlotID;
} }
/// <summary>
/// Create an item with Type ID and Count
/// </summary>
/// <param name="ID">Item Type ID</param>
/// <param name="Count">Item Count</param>
public Item(int ID, int Count) public Item(int ID, int Count)
{ {
this.ID = ID; this.ID = ID;

View file

@ -772,7 +772,13 @@ namespace MinecraftClient
{ {
inventories.Add(inventory); inventories.Add(inventory);
} }
ConsoleIO.WriteLine(inventory.Type.ToString());
if (Settings.DebugMessages)
{
ConsoleIO.WriteLineFormatted("§8An Inventory opened: " + inventory.Type + " - " + inventory.Title);
foreach (var item in inventory.Items)
ConsoleIO.WriteLineFormatted("§8 - Slot " + item.Key + ": " + item.Value.ID + " x" + item.Value.Count);
}
} }
/// <summary> /// <summary>
@ -797,12 +803,18 @@ namespace MinecraftClient
/// When received window items from server. /// When received window items from server.
/// </summary> /// </summary>
/// <param name="type">Inventory type</param> /// <param name="type">Inventory type</param>
/// <param name="itemList">Item list</param> /// <param name="itemList">Item list, key = slot ID, value = Item information</param>
public void OnWindowItems(int type, Dictionary<int, Inventory.Item> itemList) public void OnWindowItems(int type, Dictionary<int, Inventory.Item> itemList)
{ {
// 0 is player inventory // 0 is player inventory
if (type == 0) if (type == 0)
playerInventory.Items = itemList; playerInventory.Items = itemList;
if (Settings.DebugMessages)
{
ConsoleIO.WriteLineFormatted("§8Received Window of type " + type);
foreach (var item in itemList)
ConsoleIO.WriteLineFormatted("§8 - Slot " + item.Key + ": " + item.Value.ID + " x" + item.Value.Count);
}
} }
/// <summary> /// <summary>
@ -1017,10 +1029,7 @@ namespace MinecraftClient
/// <summary> /// <summary>
/// Get a dictionary of online player names and their corresponding UUID /// Get a dictionary of online player names and their corresponding UUID
/// </summary> /// </summary>
/// <returns> /// <returns>Dictionay of online players, key is UUID, value is Player name</returns>
/// dictionary of online player whereby
/// UUID represents the key
/// playername represents the value</returns>
public Dictionary<string, string> GetOnlinePlayersWithUUID() public Dictionary<string, string> GetOnlinePlayersWithUUID()
{ {
Dictionary<string, string> uuid2Player = new Dictionary<string, string>(); Dictionary<string, string> uuid2Player = new Dictionary<string, string>();

View file

@ -91,7 +91,7 @@
<Compile Include="Commands\ChangeSlot.cs" /> <Compile Include="Commands\ChangeSlot.cs" />
<Compile Include="Commands\Connect.cs" /> <Compile Include="Commands\Connect.cs" />
<Compile Include="Commands\Debug.cs" /> <Compile Include="Commands\Debug.cs" />
<Compile Include="Commands\GetInventory.cs" /> <Compile Include="Commands\Inventory.cs" />
<Compile Include="Commands\Look.cs" /> <Compile Include="Commands\Look.cs" />
<Compile Include="Commands\Move.cs" /> <Compile Include="Commands\Move.cs" />
<Compile Include="Commands\Exit.cs" /> <Compile Include="Commands\Exit.cs" />

View file

@ -504,9 +504,7 @@ namespace MinecraftClient.Protocol.Handlers
ContainerTypeOld inventoryType = (ContainerTypeOld)Enum.Parse(typeof(ContainerTypeOld), type); ContainerTypeOld inventoryType = (ContainerTypeOld)Enum.Parse(typeof(ContainerTypeOld), type);
string title = dataTypes.ReadNextString(packetData); string title = dataTypes.ReadNextString(packetData);
byte slots = dataTypes.ReadNextByte(packetData); byte slots = dataTypes.ReadNextByte(packetData);
Container inventory = new Container(windowID, inventoryType, title); Container inventory = new Container(windowID, inventoryType, title);
handler.OnInventoryOpen(inventory); handler.OnInventoryOpen(inventory);
} }
else else
@ -515,7 +513,6 @@ namespace MinecraftClient.Protocol.Handlers
int WindowType = dataTypes.ReadNextVarInt(packetData); int WindowType = dataTypes.ReadNextVarInt(packetData);
string title = dataTypes.ReadNextString(packetData); string title = dataTypes.ReadNextString(packetData);
Container inventory = new Container(WindowID, WindowType, title); Container inventory = new Container(WindowID, WindowType, title);
handler.OnInventoryOpen(inventory); handler.OnInventoryOpen(inventory);
} }
} }
@ -524,7 +521,6 @@ namespace MinecraftClient.Protocol.Handlers
if (handler.GetInventoryEnabled()) if (handler.GetInventoryEnabled())
{ {
byte windowID = dataTypes.ReadNextByte(packetData); byte windowID = dataTypes.ReadNextByte(packetData);
handler.OnInventoryClose(windowID); handler.OnInventoryClose(windowID);
} }
break; break;