Decode and store inventory updates from server

This commit is contained in:
Drew DeVault 2015-09-30 08:36:30 -04:00
parent b0c41ffdcc
commit 8d8da15f5a
7 changed files with 69 additions and 6 deletions

View File

@ -9,7 +9,7 @@ using TrueCraft.API.World;
namespace TrueCraft.Client.Handlers namespace TrueCraft.Client.Handlers
{ {
internal static class ChunkHandler internal static class ChunkHandlers
{ {
public static void HandleBlockChange(IPacket _packet, MultiplayerClient client) public static void HandleBlockChange(IPacket _packet, MultiplayerClient client)
{ {

View File

@ -0,0 +1,41 @@
using System;
using TrueCraft.API.Networking;
using TrueCraft.Core.Networking.Packets;
using TrueCraft.API.Windows;
using TrueCraft.API;
namespace TrueCraft.Client.Handlers
{
internal static class InventoryHandlers
{
public static void HandleWindowItems(IPacket _packet, MultiplayerClient client)
{
var packet = (WindowItemsPacket)_packet;
if (packet.WindowID == 0)
client.Inventory.SetSlots(packet.Items);
else
{
// TODO
}
}
public static void HandleSetSlot(IPacket _packet, MultiplayerClient client)
{
var packet = (SetSlotPacket)_packet;
IWindow window = null;
if (packet.WindowID == 0)
window = client.Inventory;
else
{
// TODO
}
if (window != null)
{
if (packet.SlotIndex >= 0 && packet.SlotIndex < window.Length)
{
window[packet.SlotIndex] = new ItemStack(packet.ItemID, packet.Count, packet.Metadata);
}
}
}
}
}

View File

@ -17,9 +17,12 @@ namespace TrueCraft.Client.Handlers
client.RegisterPacketHandler(new SetPlayerPositionPacket().ID, HandlePositionAndLook); client.RegisterPacketHandler(new SetPlayerPositionPacket().ID, HandlePositionAndLook);
client.RegisterPacketHandler(new LoginResponsePacket().ID, HandleLoginResponse); client.RegisterPacketHandler(new LoginResponsePacket().ID, HandleLoginResponse);
client.RegisterPacketHandler(new ChunkPreamblePacket().ID, ChunkHandler.HandleChunkPreamble); client.RegisterPacketHandler(new ChunkPreamblePacket().ID, ChunkHandlers.HandleChunkPreamble);
client.RegisterPacketHandler(new ChunkDataPacket().ID, ChunkHandler.HandleChunkData); client.RegisterPacketHandler(new ChunkDataPacket().ID, ChunkHandlers.HandleChunkData);
client.RegisterPacketHandler(new BlockChangePacket().ID, ChunkHandler.HandleBlockChange); client.RegisterPacketHandler(new BlockChangePacket().ID, ChunkHandlers.HandleBlockChange);
client.RegisterPacketHandler(new WindowItemsPacket().ID, InventoryHandlers.HandleWindowItems);
client.RegisterPacketHandler(new SetSlotPacket().ID, InventoryHandlers.HandleSetSlot);
} }
public static void HandleChatMessage(IPacket _packet, MultiplayerClient client) public static void HandleChatMessage(IPacket _packet, MultiplayerClient client)

View File

@ -5,6 +5,7 @@ using TrueCraft.Client.Input;
using TrueCraft.Client.Rendering; using TrueCraft.Client.Rendering;
using TrueCraft.API; using TrueCraft.API;
using System; using System;
using System.Text;
namespace TrueCraft.Client.Modules namespace TrueCraft.Client.Modules
{ {
@ -83,7 +84,7 @@ namespace TrueCraft.Client.Modules
Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 1), Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 1),
string.Format("Standing at <{0:N2}, {1:N2}, {2:N2}>", string.Format("Standing at <{0:N2}, {1:N2}, {2:N2}>",
Game.Client.Position.X, Game.Client.Position.Y, Game.Client.Position.Z)); Game.Client.Position.X, Game.Client.Position.Y, Game.Client.Position.Z));
Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 2), Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 2),
string.Format(ChatColor.Gray + "Looking at {0} ({1})", Game.HighlightedBlock, string.Format(ChatColor.Gray + "Looking at {0} ({1})", Game.HighlightedBlock,
@ -92,6 +93,18 @@ namespace TrueCraft.Client.Modules
Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 3), Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 3),
string.Format(ChatColor.Gray + "{0} pending chunks", Game.ChunkModule.ChunkRenderer.PendingChunks)); string.Format(ChatColor.Gray + "{0} pending chunks", Game.ChunkModule.ChunkRenderer.PendingChunks));
var sb = new StringBuilder(ChatColor.DarkGray + "inv: ");
for (int i = 0; i < Game.Client.Inventory.Hotbar.Length; i++)
{
var provider = Game.ItemRepository.GetItemProvider(Game.Client.Inventory.Hotbar[i].ID);
if (provider != null)
sb.Append(provider.DisplayName + " ");
else
sb.Append("[empty]");
}
Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 4), sb.ToString());
SpriteBatch.End(); SpriteBatch.End();
} }

View File

@ -16,6 +16,7 @@ using System.IO;
using TrueCraft.Core; using TrueCraft.Core;
using TrueCraft.API.Physics; using TrueCraft.API.Physics;
using TrueCraft.Core.Physics; using TrueCraft.Core.Physics;
using TrueCraft.Core.Windows;
namespace TrueCraft.Client namespace TrueCraft.Client
{ {
@ -36,6 +37,7 @@ namespace TrueCraft.Client
public PhysicsEngine Physics { get; set; } public PhysicsEngine Physics { get; set; }
public bool LoggedIn { get; internal set; } public bool LoggedIn { get; internal set; }
public int EntityID { get; internal set; } public int EntityID { get; internal set; }
public InventoryWindow Inventory { get; set; }
public bool Connected public bool Connected
{ {
@ -66,6 +68,7 @@ namespace TrueCraft.Client
PacketHandlers = new PacketHandler[0x100]; PacketHandlers = new PacketHandler[0x100];
Handlers.PacketHandlers.RegisterHandlers(this); Handlers.PacketHandlers.RegisterHandlers(this);
World = new ReadOnlyWorld(); World = new ReadOnlyWorld();
Inventory = new InventoryWindow(null);
var repo = new BlockRepository(); var repo = new BlockRepository();
repo.DiscoverBlockProviders(); repo.DiscoverBlockProviders();
World.World.BlockRepository = repo; World.World.BlockRepository = repo;

View File

@ -101,7 +101,6 @@
<Compile Include="Handlers\PacketHandlers.cs" /> <Compile Include="Handlers\PacketHandlers.cs" />
<Compile Include="Events\ChatMessageEventArgs.cs" /> <Compile Include="Events\ChatMessageEventArgs.cs" />
<Compile Include="BMFont.cs" /> <Compile Include="BMFont.cs" />
<Compile Include="Handlers\ChunkHandler.cs" />
<Compile Include="ReadOnlyWorld.cs" /> <Compile Include="ReadOnlyWorld.cs" />
<Compile Include="Events\ChunkEventArgs.cs" /> <Compile Include="Events\ChunkEventArgs.cs" />
<Compile Include="Rendering\Mesh.cs" /> <Compile Include="Rendering\Mesh.cs" />
@ -135,6 +134,8 @@
<Compile Include="Modules\DebugInfoModule.cs" /> <Compile Include="Modules\DebugInfoModule.cs" />
<Compile Include="Modules\HUDModule.cs" /> <Compile Include="Modules\HUDModule.cs" />
<Compile Include="Rendering\Blocks\CactusRenderer.cs" /> <Compile Include="Rendering\Blocks\CactusRenderer.cs" />
<Compile Include="Handlers\InventoryHandlers.cs" />
<Compile Include="Handlers\ChunkHandlers.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup> <ItemGroup>

View File

@ -18,6 +18,8 @@ namespace TrueCraft.Core.Windows
private void HandleWindowChange(object sender, WindowChangeEventArgs e) private void HandleWindowChange(object sender, WindowChangeEventArgs e)
{ {
if (Repository == null)
return;
var current = Repository.GetRecipe(Bench); var current = Repository.GetRecipe(Bench);
if (e.SlotIndex == CraftingOutput) if (e.SlotIndex == CraftingOutput)
{ {