Inventory handling

This commit is contained in:
ReinforceZwei 2020-03-26 15:01:42 +08:00 committed by ORelio
parent c870f080f2
commit bc449b404e
20 changed files with 538 additions and 44 deletions

View file

@ -10,9 +10,6 @@ namespace MinecraftClient.Protocol.Handlers
/// </summary>
enum PacketIncomingType
{
// modified by reinforce
SpawnEntity,
SpawnLivingEntity,
KeepAlive,
JoinGame,
ChatMessage,
@ -33,6 +30,9 @@ namespace MinecraftClient.Protocol.Handlers
OpenWindow,
WindowItems,
SetSlot,
SpawnEntity,
SpawnLivingEntity,
SpawnPlayer,
DestroyEntities,
SetCooldown,
EntityPosition,

View file

@ -22,6 +22,7 @@ namespace MinecraftClient.Protocol.Handlers
TeleportConfirm,
HeldItemChange,
InteractEntity,
UseItem
UseItem,
PlayerBlockPlacement
}
}

View file

@ -658,6 +658,14 @@ namespace MinecraftClient.Protocol.Handlers
{
return false;
}
public bool SendPlayerBlockPlacement(int hand, Location location, int face, float CursorX, float CursorY, float CursorZ, bool insideBlock)
{
return false;
}
public bool SendHeldItemChange(short slot)
{
return false;
}
/// <summary>
/// Send a plugin channel packet to the server.

View file

@ -497,7 +497,18 @@ namespace MinecraftClient.Protocol.Handlers
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);
// TODO:
MinecraftClient.Inventory.Container inventory = new MinecraftClient.Inventory.Container(windowID, inventoryType, title);
handler.OnInventoryOpen(inventory);
}
else
{
int WindowID = dataTypes.ReadNextVarInt(packetData);
int WindowType = dataTypes.ReadNextVarInt(packetData);
string title = dataTypes.ReadNextString(packetData);
MinecraftClient.Inventory.Container inventory = new MinecraftClient.Inventory.Container(WindowID, WindowType, title);
handler.OnInventoryOpen(inventory);
}
@ -536,7 +547,7 @@ namespace MinecraftClient.Protocol.Handlers
*/
byte id = dataTypes.ReadNextByte(packetData);
short elements = dataTypes.ReadNextShort(packetData);
Dictionary<int, Item> itemsList = new Dictionary<int, Item>(); // index is SlotID
Dictionary<int, MinecraftClient.Inventory.Item> itemsList = new Dictionary<int, MinecraftClient.Inventory.Item>(); // index is SlotID
for(int i = 0; i < elements; i++)
{
bool haveItem = dataTypes.ReadNextBool(packetData);
@ -546,13 +557,32 @@ namespace MinecraftClient.Protocol.Handlers
byte itemCount = dataTypes.ReadNextByte(packetData);
dataTypes.ReadNextNbt(packetData);
Item item = new Item(itemID, itemCount);
MinecraftClient.Inventory.Item item = new MinecraftClient.Inventory.Item(itemID, itemCount);
itemsList.Add(i, item);
}
}
handler.OnWindowItems(id, itemsList);
}
break;
case PacketIncomingType.SetSlot:
if(handler.GetInventoryEnabled())
{
byte WindowID = dataTypes.ReadNextByte(packetData);
short SlotID = dataTypes.ReadNextShort(packetData);
bool Present = dataTypes.ReadNextBool(packetData);
if (Present)
{
int ItemID = dataTypes.ReadNextVarInt(packetData);
byte Count = dataTypes.ReadNextByte(packetData);
Dictionary<string, object> NBT = dataTypes.ReadNextNbt(packetData);
handler.OnSetSlot(WindowID, SlotID, Present, ItemID, Count, NBT);
}
else
{
handler.OnSetSlot(WindowID, SlotID, Present);
}
}
break;
case PacketIncomingType.ResourcePackSend:
string url = dataTypes.ReadNextString(packetData);
string hash = dataTypes.ReadNextString(packetData);
@ -606,6 +636,22 @@ namespace MinecraftClient.Protocol.Handlers
handler.OnSpawnLivingEntity(EntityID, EntityType, EntityUUID, EntityLocation);
}
break;
case PacketIncomingType.SpawnPlayer:
if (handler.GetEntityHandlingEnabled())
{
int EntityID = dataTypes.ReadNextVarInt(packetData);
Guid UUID = dataTypes.ReadNextUUID(packetData);
double X = dataTypes.ReadNextDouble(packetData);
double Y = dataTypes.ReadNextDouble(packetData);
double Z = dataTypes.ReadNextDouble(packetData);
byte Yaw = dataTypes.ReadNextByte(packetData);
byte Pitch = dataTypes.ReadNextByte(packetData);
Location EntityLocation = new Location(X, Y, Z);
handler.OnSpawnPlayer(EntityID, UUID, EntityLocation, Yaw, Pitch);
}
break;
case PacketIncomingType.DestroyEntities:
if (handler.GetEntityHandlingEnabled())
{
@ -1185,8 +1231,7 @@ namespace MinecraftClient.Protocol.Handlers
}
return false;
}
// reinforce
/// <summary>
/// Send an Interact Entity Packet to server
/// </summary>
@ -1207,10 +1252,24 @@ namespace MinecraftClient.Protocol.Handlers
catch (System.IO.IOException) { return false; }
catch (ObjectDisposedException) { return false; }
}
// TODO: Interact at block location (e.g. chest)
// TODO: Interact at block location (e.g. chest minecart)
public bool SendInteractEntityPacket(int EntityID, int type, float X, float Y, float Z, int hand)
{
return false;
try
{
List<byte> fields = new List<byte>();
fields.AddRange(dataTypes.GetVarInt(EntityID));
fields.AddRange(dataTypes.GetVarInt(type));
fields.AddRange(dataTypes.GetFloat(X));
fields.AddRange(dataTypes.GetFloat(Y));
fields.AddRange(dataTypes.GetFloat(Z));
fields.AddRange(dataTypes.GetVarInt(hand));
SendPacket(PacketOutgoingType.InteractEntity, fields);
return true;
}
catch (SocketException) { return false; }
catch (System.IO.IOException) { return false; }
catch (ObjectDisposedException) { return false; }
}
public bool SendInteractEntityPacket(int EntityID, int type, float X, float Y, float Z)
{
@ -1230,5 +1289,42 @@ namespace MinecraftClient.Protocol.Handlers
catch (System.IO.IOException) { return false; }
catch (ObjectDisposedException) { return false; }
}
public bool SendPlayerBlockPlacement(int hand, Location location, int face, float CursorX, float CursorY, float CursorZ, bool insideBlock)
{
try
{
List<byte> packet = new List<byte>();
packet.AddRange(dataTypes.GetVarInt(hand));
packet.AddRange(dataTypes.GetLocation(location));
packet.AddRange(dataTypes.GetVarInt(face));
packet.AddRange(dataTypes.GetFloat(CursorX));
packet.AddRange(dataTypes.GetFloat(CursorY));
packet.AddRange(dataTypes.GetFloat(CursorZ));
packet.Add(Convert.ToByte(insideBlock ? 1 : 0));
SendPacket(PacketOutgoingType.PlayerBlockPlacement, packet);
return true;
}
catch (SocketException) { return false; }
catch (System.IO.IOException) { return false; }
catch (ObjectDisposedException) { return false; }
}
public bool SendHeldItemChange(short slot)
{
try
{
List<byte> packet = new List<byte>();
// short to byte (?
byte[] b = BitConverter.GetBytes(slot);
Array.Reverse(b);
packet.AddRange(b);
SendPacket(PacketOutgoingType.HeldItemChange, packet);
return true;
}
catch (SocketException) { return false; }
catch (System.IO.IOException) { return false; }
catch (ObjectDisposedException) { return false; }
}
}
}

View file

@ -48,6 +48,7 @@ namespace MinecraftClient.Protocol.Handlers
// Entity handling
case 0x0E: return PacketIncomingType.SpawnEntity; // for non-living entity
case 0x0F: return PacketIncomingType.SpawnLivingEntity; // for living entity
case 0x05: return PacketIncomingType.SpawnPlayer;
case 0x13: return PacketIncomingType.DestroyEntities;
case 0x15: return PacketIncomingType.EntityPosition;
case 0x17: return PacketIncomingType.EntityPositionAndRotation;
@ -86,6 +87,7 @@ namespace MinecraftClient.Protocol.Handlers
// Entity handling
case 0x00: return PacketIncomingType.SpawnEntity; // for non-living entity
case 0x03: return PacketIncomingType.SpawnLivingEntity; // for living entity
case 0x05: return PacketIncomingType.SpawnPlayer;
case 0x30: return PacketIncomingType.DestroyEntities;
case 0x25: return PacketIncomingType.EntityPosition;
case 0x26: return PacketIncomingType.EntityPositionAndRotation;
@ -122,6 +124,7 @@ namespace MinecraftClient.Protocol.Handlers
// Entity handling
case 0x00: return PacketIncomingType.SpawnEntity; // for non-living entity
case 0x03: return PacketIncomingType.SpawnLivingEntity; // for living entity
case 0x05: return PacketIncomingType.SpawnPlayer;
case 0x31: return PacketIncomingType.DestroyEntities;
case 0x26: return PacketIncomingType.EntityPosition;
case 0x27: return PacketIncomingType.EntityPositionAndRotation;
@ -158,6 +161,7 @@ namespace MinecraftClient.Protocol.Handlers
// Entity handling
case 0x00: return PacketIncomingType.SpawnEntity; // for non-living entity
case 0x03: return PacketIncomingType.SpawnLivingEntity; // for living entity
case 0x05: return PacketIncomingType.SpawnPlayer;
case 0x32: return PacketIncomingType.DestroyEntities;
case 0x26: return PacketIncomingType.EntityPosition;
case 0x27: return PacketIncomingType.EntityPositionAndRotation;
@ -194,6 +198,7 @@ namespace MinecraftClient.Protocol.Handlers
// Entity handling
case 0x00: return PacketIncomingType.SpawnEntity; // for non-living entity
case 0x03: return PacketIncomingType.SpawnLivingEntity; // for living entity
case 0x05: return PacketIncomingType.SpawnPlayer;
case 0x35: return PacketIncomingType.DestroyEntities;
case 0x28: return PacketIncomingType.EntityPosition;
case 0x29: return PacketIncomingType.EntityPositionAndRotation;
@ -229,6 +234,7 @@ namespace MinecraftClient.Protocol.Handlers
// Entity handling
case 0x00: return PacketIncomingType.SpawnEntity; // for non-living entity
case 0x03: return PacketIncomingType.SpawnLivingEntity; // for living entity
case 0x05: return PacketIncomingType.SpawnPlayer;
case 0x37: return PacketIncomingType.DestroyEntities;
case 0x28: return PacketIncomingType.EntityPosition;
case 0x29: return PacketIncomingType.EntityPositionAndRotation;
@ -264,6 +270,7 @@ namespace MinecraftClient.Protocol.Handlers
// Entity handling
case 0x00: return PacketIncomingType.SpawnEntity; // for non-living entity
case 0x03: return PacketIncomingType.SpawnLivingEntity; // for living entity
case 0x05: return PacketIncomingType.SpawnPlayer;
case 0x38: return PacketIncomingType.DestroyEntities;
case 0x29: return PacketIncomingType.EntityPosition;
case 0x2A: return PacketIncomingType.EntityPositionAndRotation;
@ -296,6 +303,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.TabComplete: return 0x14;
case PacketOutgoingType.PlayerPosition: return 0x04;
case PacketOutgoingType.PlayerPositionAndLook: return 0x06;
case PacketOutgoingType.HeldItemChange: return 0x17;
case PacketOutgoingType.InteractEntity: return 0x02;
case PacketOutgoingType.TeleportConfirm: throw new InvalidOperationException("Teleport confirm is not supported in protocol " + protocol);
}
@ -314,6 +322,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.PlayerPosition: return 0x0C;
case PacketOutgoingType.PlayerPositionAndLook: return 0x0D;
case PacketOutgoingType.TeleportConfirm: return 0x00;
case PacketOutgoingType.HeldItemChange: return 0x17;
case PacketOutgoingType.InteractEntity: return 0x0A;
}
}
@ -331,6 +340,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.PlayerPosition: return 0x0E;
case PacketOutgoingType.PlayerPositionAndLook: return 0x0F;
case PacketOutgoingType.TeleportConfirm: return 0x00;
case PacketOutgoingType.HeldItemChange: return 0x1A;
case PacketOutgoingType.InteractEntity: return 0x0B;
}
}
@ -348,6 +358,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.PlayerPosition: return 0x0D;
case PacketOutgoingType.PlayerPositionAndLook: return 0x0E;
case PacketOutgoingType.TeleportConfirm: return 0x00;
case PacketOutgoingType.HeldItemChange: return 0x1F;
case PacketOutgoingType.InteractEntity: return 0x0A;
}
}
@ -365,6 +376,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.PlayerPosition: return 0x10;
case PacketOutgoingType.PlayerPositionAndLook: return 0x11;
case PacketOutgoingType.TeleportConfirm: return 0x00;
case PacketOutgoingType.HeldItemChange: return 0x21;
case PacketOutgoingType.InteractEntity: return 0x0D;
}
}
@ -385,6 +397,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.HeldItemChange: return 0x23;
case PacketOutgoingType.InteractEntity: return 0x0E;
case PacketOutgoingType.UseItem: return 0x2D;
case PacketOutgoingType.PlayerBlockPlacement: return 0x2C;
}
}