Started to implement inventory handling

This commit is contained in:
xPenguinx 2019-05-25 17:45:59 -04:00 committed by ORelio
parent 76def0d56b
commit 5895a4161f
9 changed files with 148 additions and 1 deletions

View file

@ -47,6 +47,8 @@ namespace MinecraftClient
private string username; private string username;
private string uuid; private string uuid;
private string sessionid; private string sessionid;
private Inventory inventory;
public int GetServerPort() { return port; } public int GetServerPort() { return port; }
public string GetServerHost() { return host; } public string GetServerHost() { return host; }
@ -589,6 +591,15 @@ namespace MinecraftClient
} }
} }
/// <summary>
/// When an inventory is opened
/// </summary>
/// <param name="inventory">Location to reach</param>
public void onInventoryOpen(Inventory inventory)
{
//TODO: Handle Inventory
}
/// <summary> /// <summary>
/// When connection has been lost /// When connection has been lost
/// </summary> /// </summary>

View file

@ -251,6 +251,9 @@
<Compile Include="Commands\List.cs" /> <Compile Include="Commands\List.cs" />
<Compile Include="Mapping\Location.cs" /> <Compile Include="Mapping\Location.cs" />
<Compile Include="WinAPI\WindowsVersion.cs" /> <Compile Include="WinAPI\WindowsVersion.cs" />
<Compile Include="Protocol\Inventory.cs" />
<Compile Include="Protocol\InventoryType.cs" />
<Compile Include="Protocol\Item.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0,Profile=Client"> <BootstrapperPackage Include=".NETFramework,Version=v4.0,Profile=Client">

View file

@ -10,7 +10,7 @@ namespace MinecraftClient.Protocol.Handlers
/// </summary> /// </summary>
enum PacketIncomingType enum PacketIncomingType
{ {
KeepAlive, KeepAlive,
JoinGame, JoinGame,
ChatMessage, ChatMessage,
Respawn, Respawn,
@ -26,6 +26,9 @@ namespace MinecraftClient.Protocol.Handlers
KickPacket, KickPacket,
NetworkCompressionTreshold, NetworkCompressionTreshold,
ResourcePackSend, ResourcePackSend,
OpenWindow,
WindowItems,
SetSlot,
UnknownPacket UnknownPacket
} }
} }

View file

@ -464,6 +464,41 @@ namespace MinecraftClient.Protocol.Handlers
if (protocolversion >= MC18Version && protocolversion < MC19Version) if (protocolversion >= MC18Version && protocolversion < MC19Version)
compression_treshold = dataTypes.ReadNextVarInt(packetData); compression_treshold = dataTypes.ReadNextVarInt(packetData);
break; break;
case PacketIncomingType.OpenWindow:
if (protocolversion < MC1141Version)
{
byte windowID = dataTypes.ReadNextByte(packetData);
string type = dataTypes.ReadNextString(packetData).Replace("minecraft:", "").ToUpper();
InventoryType inventoryType = (InventoryType)Enum.Parse(typeof(InventoryType), type);
string title = dataTypes.ReadNextString(packetData);
byte slots = dataTypes.ReadNextByte(packetData);
Inventory inventory = new Inventory(windowID, inventoryType, title, slots);
handler.onInventoryOpen(inventory);
}
break;
case PacketIncomingType.WindowItems:
if (protocolversion < MC1141Version)
{
byte id = dataTypes.ReadNextByte(packetData);
short elements = dataTypes.ReadNextShort(packetData);
for (int i = 0; i < elements; i++)
{
short itemID = dataTypes.ReadNextShort(packetData);
if (itemID == -1) continue;
byte itemCount = dataTypes.ReadNextByte(packetData);
short itemDamage = dataTypes.ReadNextShort(packetData);
Item item = new Item(itemID, itemCount, itemDamage, 0);
//TODO: Add to the dictionary for the inventory its in using the id
if (packetData.ToArray().Count() > 0)
{
dataTypes.ReadNextNbt(packetData);
}
}
}
break;
case PacketIncomingType.ResourcePackSend: case PacketIncomingType.ResourcePackSend:
string url = dataTypes.ReadNextString(packetData); string url = dataTypes.ReadNextString(packetData);
string hash = dataTypes.ReadNextString(packetData); string hash = dataTypes.ReadNextString(packetData);

View file

@ -38,6 +38,9 @@ namespace MinecraftClient.Protocol.Handlers
case 0x40: return PacketIncomingType.KickPacket; case 0x40: return PacketIncomingType.KickPacket;
case 0x46: return PacketIncomingType.NetworkCompressionTreshold; case 0x46: return PacketIncomingType.NetworkCompressionTreshold;
case 0x48: return PacketIncomingType.ResourcePackSend; case 0x48: return PacketIncomingType.ResourcePackSend;
case 0x2D: return PacketIncomingType.OpenWindow;
case 0x30: return PacketIncomingType.WindowItems;
case 0x2F: return PacketIncomingType.SetSlot;
default: return PacketIncomingType.UnknownPacket; default: return PacketIncomingType.UnknownPacket;
} }
} }
@ -59,6 +62,9 @@ namespace MinecraftClient.Protocol.Handlers
case 0x0E: return PacketIncomingType.TabCompleteResult; case 0x0E: return PacketIncomingType.TabCompleteResult;
case 0x18: return PacketIncomingType.PluginMessage; case 0x18: return PacketIncomingType.PluginMessage;
case 0x1A: return PacketIncomingType.KickPacket; case 0x1A: return PacketIncomingType.KickPacket;
case 0x13: return PacketIncomingType.OpenWindow;
case 0x14: return PacketIncomingType.WindowItems;
case 0x16: return PacketIncomingType.SetSlot;
//NetworkCompressionTreshold removed in 1.9 //NetworkCompressionTreshold removed in 1.9
case 0x32: return PacketIncomingType.ResourcePackSend; case 0x32: return PacketIncomingType.ResourcePackSend;
default: return PacketIncomingType.UnknownPacket; default: return PacketIncomingType.UnknownPacket;
@ -81,7 +87,10 @@ namespace MinecraftClient.Protocol.Handlers
case 0x0E: return PacketIncomingType.TabCompleteResult; case 0x0E: return PacketIncomingType.TabCompleteResult;
case 0x18: return PacketIncomingType.PluginMessage; case 0x18: return PacketIncomingType.PluginMessage;
case 0x1A: return PacketIncomingType.KickPacket; case 0x1A: return PacketIncomingType.KickPacket;
case 0x13: return PacketIncomingType.OpenWindow;
case 0x33: return PacketIncomingType.ResourcePackSend; case 0x33: return PacketIncomingType.ResourcePackSend;
case 0x14: return PacketIncomingType.WindowItems;
case 0x16: return PacketIncomingType.SetSlot;
default: return PacketIncomingType.UnknownPacket; default: return PacketIncomingType.UnknownPacket;
} }
} }
@ -102,6 +111,9 @@ namespace MinecraftClient.Protocol.Handlers
case 0x0E: return PacketIncomingType.TabCompleteResult; case 0x0E: return PacketIncomingType.TabCompleteResult;
case 0x18: return PacketIncomingType.PluginMessage; case 0x18: return PacketIncomingType.PluginMessage;
case 0x1A: return PacketIncomingType.KickPacket; case 0x1A: return PacketIncomingType.KickPacket;
case 0x13: return PacketIncomingType.OpenWindow;
case 0x14: return PacketIncomingType.WindowItems;
case 0x16: return PacketIncomingType.SetSlot;
case 0x34: return PacketIncomingType.ResourcePackSend; case 0x34: return PacketIncomingType.ResourcePackSend;
default: return PacketIncomingType.UnknownPacket; default: return PacketIncomingType.UnknownPacket;
} }
@ -124,6 +136,9 @@ namespace MinecraftClient.Protocol.Handlers
case 0x19: return PacketIncomingType.PluginMessage; case 0x19: return PacketIncomingType.PluginMessage;
case 0x1B: return PacketIncomingType.KickPacket; case 0x1B: return PacketIncomingType.KickPacket;
case 0x37: return PacketIncomingType.ResourcePackSend; case 0x37: return PacketIncomingType.ResourcePackSend;
case 0x14: return PacketIncomingType.OpenWindow;
case 0x15: return PacketIncomingType.WindowItems;
case 0x17: return PacketIncomingType.SetSlot;
default: return PacketIncomingType.UnknownPacket; default: return PacketIncomingType.UnknownPacket;
} }
} }
@ -145,6 +160,9 @@ namespace MinecraftClient.Protocol.Handlers
case 0x18: return PacketIncomingType.PluginMessage; case 0x18: return PacketIncomingType.PluginMessage;
case 0x1A: return PacketIncomingType.KickPacket; case 0x1A: return PacketIncomingType.KickPacket;
case 0x39: return PacketIncomingType.ResourcePackSend; case 0x39: return PacketIncomingType.ResourcePackSend;
case 0x2E: return PacketIncomingType.OpenWindow;
case 0x14: return PacketIncomingType.WindowItems;
case 0x16: return PacketIncomingType.SetSlot;
default: return PacketIncomingType.UnknownPacket; default: return PacketIncomingType.UnknownPacket;
} }
} }

View file

@ -41,6 +41,12 @@ namespace MinecraftClient.Protocol
/// <param name="isJson">TRUE if the text is JSON-Encoded</param> /// <param name="isJson">TRUE if the text is JSON-Encoded</param>
void OnTextReceived(string text, bool isJson); void OnTextReceived(string text, bool isJson);
/// <summary>
/// This method is called when an inventory is opened
/// </summary>
/// <param name="inventory">Inventory that was opened</param>
void onInventoryOpen(Inventory inventory);
/// <summary> /// <summary>
/// Called when the player respawns, which happens on login, respawn and world change. /// Called when the player respawns, which happens on login, respawn and world change.
/// </summary> /// </summary>

View file

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
namespace MinecraftClient.Protocol
{
public class Inventory
{
public byte id { get; set; }
public InventoryType type { get; set; }
public string title { get; set; }
public byte slots { get; set; }
public Dictionary<Item, int> items { get; set; }
public Inventory(byte id, InventoryType type, string title, byte slots)
{
this.id = id;
this.type = type;
this.title = title;
this.slots = slots;
}
}
}

View file

@ -0,0 +1,21 @@
using System;
namespace MinecraftClient.Protocol
{
public enum InventoryType
{
CONTAINER,
CHEST,
CRAFTING_TABLE,
FURNACE,
DISPENSER,
ENCHANTING_TABLE,
BREWING_STAND,
VILLAGER,
BEACON,
ANVIL,
HOPPER,
DROPPER,
SHULKER_BOX,
ENTITYHORSE
}
}

View file

@ -0,0 +1,27 @@
using System;
namespace MinecraftClient.Protocol
{
public class Item
{
public int id;
public int count;
public int damage;
public byte nbtData;
public Item(int id, int count)
{
this.id = id;
this.count = count;
this.damage = 0;
}
public Item(int id, int damage, int count, byte nbtData)
{
this.id = id;
this.count = count;
this.damage = damage;
this.nbtData = nbtData;
}
}
}