Finish implementing inventory interactions

This commit is contained in:
Drew DeVault 2015-01-26 00:45:55 -07:00
parent 3a4d1c7107
commit affcb43617
5 changed files with 115 additions and 9 deletions

View File

@ -30,8 +30,11 @@ namespace TrueCraft.Core.Networking.Packets
WindowID = stream.ReadInt8();
SlotIndex = stream.ReadInt16();
ItemID = stream.ReadInt16();
Count = stream.ReadInt8();
Metadata = stream.ReadInt16();
if (ItemID != -1)
{
Count = stream.ReadInt8();
Metadata = stream.ReadInt16();
}
}
public void WritePacket(IMinecraftStream stream)
@ -39,8 +42,11 @@ namespace TrueCraft.Core.Networking.Packets
stream.WriteInt8(WindowID);
stream.WriteInt16(SlotIndex);
stream.WriteInt16(ItemID);
stream.WriteInt8(Count);
stream.WriteInt16(Metadata);
if (ItemID != -1)
{
stream.WriteInt8(Count);
stream.WriteInt16(Metadata);
}
}
}
}

View File

@ -22,6 +22,7 @@ namespace TrueCraft.Commands
Commands.Add(new PingCommand());
Commands.Add(new GiveCommand());
Commands.Add(new HelpCommand());
Commands.Add(new ResendInvCommand());
}
/// <summary>

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrueCraft.Core.Windows;
using TrueCraft.API;
using TrueCraft.API.Networking;
using TrueCraft.Core.Networking.Packets;
namespace TrueCraft.Commands
{
public class ResendInvCommand : Command
{
public override string Name
{
get { return "reinv"; }
}
public override string Description
{
get { return "Resends your inventory to the selected client."; }
}
public override string[] Aliases
{
get { return new string[0]; }
}
public override void Handle(IRemoteClient Client, string Alias, string[] Arguments)
{
if (Arguments.Length != 0)
{
Help(Client, Alias, Arguments);
return;
}
Client.QueuePacket(new WindowItemsPacket(0, Client.Inventory.GetSlots()));
}
public override void Help(IRemoteClient Client, string Alias, string[] Arguments)
{
Client.SendMessage("/reinv: Resends your inventory.");
}
}
}

View File

@ -62,7 +62,7 @@ namespace TrueCraft.Handlers
{
if (use)
{
// Temporary
// Temporary: just place the damn thing
position += MathHelper.BlockFaceToCoordinates(packet.Face);
client.World.SetBlockID(position, (byte)slot.Id);
client.World.SetMetadata(position, (byte)slot.Metadata);
@ -88,21 +88,75 @@ namespace TrueCraft.Handlers
var window = client.CurrentWindow;
if (packet.SlotIndex >= window.Length || packet.SlotIndex < 0)
return;
ItemStack existing = window[packet.SlotIndex];
ItemStack held = client.ItemStaging;
if (client.ItemStaging.Empty) // Picking up something
{
if (packet.Shift)
{
Console.WriteLine("Moving to alternate");
window.MoveToAlternateArea(packet.SlotIndex);
}
else
{
client.ItemStaging = window[packet.SlotIndex];
if (packet.RightClick)
{
sbyte mod = (sbyte)(existing.Count % 2);
existing.Count /= 2;
held = existing;
held.Count += mod;
client.ItemStaging = held;
window[packet.SlotIndex] = existing;
}
else
{
client.ItemStaging = window[packet.SlotIndex];
window[packet.SlotIndex] = ItemStack.EmptyStack;
}
}
}
else // Setting something down
{
if (existing.Empty) // Replace empty slot
{
if (packet.RightClick)
{
var newItem = (ItemStack)client.ItemStaging.Clone();
newItem.Count = 1;
held.Count--;
window[packet.SlotIndex] = newItem;
client.ItemStaging = held;
}
else
{
window[packet.SlotIndex] = client.ItemStaging;
client.ItemStaging = ItemStack.EmptyStack;
}
}
else
{
if (existing.CanMerge(client.ItemStaging)) // Merge items
{
// TODO: Consider the maximum stack size
if (packet.RightClick)
{
existing.Count++;
held.Count--;
window[packet.SlotIndex] = existing;
client.ItemStaging = held;
}
else
{
existing.Count += client.ItemStaging.Count;
window[packet.SlotIndex] = existing;
client.ItemStaging = ItemStack.EmptyStack;
}
}
else // Swap items
{
window[packet.SlotIndex] = client.ItemStaging;
client.ItemStaging = existing;
}
}
}
}

View File

@ -56,6 +56,7 @@
<Compile Include="Entities\ObjectEntity.cs" />
<Compile Include="Entities\PlayerEntity.cs" />
<Compile Include="Handlers\InteractionHandlers.cs" />
<Compile Include="Commands\DebugCommands.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
@ -73,4 +74,4 @@
</ProjectReference>
</ItemGroup>
<ItemGroup />
</Project>
</Project>