Partially implement chests

This commit is contained in:
Drew DeVault 2015-06-21 17:36:42 -04:00
parent 4a0b4c4865
commit 13baea7191
12 changed files with 263 additions and 8 deletions

View File

@ -31,7 +31,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="YamlDotNet">
<HintPath>..\packages\YamlDotNet.3.5.1\lib\net35\YamlDotNet.dll</HintPath>
<HintPath>..\packages\YamlDotNet.3.6.0\lib\net35\YamlDotNet.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
@ -113,4 +113,7 @@
<Name>fNbt</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>

View File

@ -11,6 +11,7 @@ namespace TrueCraft.API.Windows
string Name { get; }
sbyte Type { get; }
int Length { get; }
int MinecraftWasWrittenByFuckingIdiotsLength { get; }
ItemStack this[int index] { get; set; }
bool Empty { get; }

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="YamlDotNet" version="3.6.0" targetFramework="net40" />
</packages>

View File

@ -2,6 +2,10 @@ using System;
using TrueCraft.API;
using TrueCraft.Core.Logic.Items;
using TrueCraft.API.Logic;
using TrueCraft.API.World;
using TrueCraft.API.Networking;
using fNbt;
using TrueCraft.Core.Windows;
namespace TrueCraft.Core.Logic.Blocks
{
@ -60,5 +64,98 @@ namespace TrueCraft.Core.Logic.Blocks
{
get { return false; }
}
private static readonly Coordinates3D[] AdjacentBlocks =
{
Coordinates3D.North,
Coordinates3D.South,
Coordinates3D.West,
Coordinates3D.East
};
public override void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world, IRemoteClient user)
{
int adjacent = 0;
var coords = coordinates + MathHelper.BlockFaceToCoordinates(face);
Coordinates3D _ = Coordinates3D.Down;
// Check for adjacent chests. We can only allow one adjacent check block.
for (int i = 0; i < AdjacentBlocks.Length; i++)
{
if (world.GetBlockID(coords + AdjacentBlocks[i]) == ChestBlock.BlockID)
{
_ = coords + AdjacentBlocks[i];
adjacent++;
}
}
if (adjacent <= 1)
{
if (_ != Coordinates3D.Down)
{
// Confirm that adjacent chest is not a double chest
for (int i = 0; i < AdjacentBlocks.Length; i++)
{
if (world.GetBlockID(_ + AdjacentBlocks[i]) == ChestBlock.BlockID)
adjacent++;
}
}
if (adjacent <= 1)
base.ItemUsedOnBlock(coordinates, item, face, world, user);
}
}
public override void BlockPlaced(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user)
{
world.SetMetadata(descriptor.Coordinates, (byte)MathHelper.DirectionByRotationFlat(user.Entity.Yaw, true));
}
public override bool BlockRightClicked(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user)
{
bool unobstructed = true, isDouble = false;
Coordinates3D other = Coordinates3D.Down;
for (int i = 0; i < AdjacentBlocks.Length; i++)
{
if (world.GetBlockID(descriptor.Coordinates + AdjacentBlocks[i]) == ChestBlock.BlockID)
{
isDouble = true;
other = descriptor.Coordinates + AdjacentBlocks[i];
var _ = world.BlockRepository.GetBlockProvider(world.GetBlockID(
descriptor.Coordinates + AdjacentBlocks[i] + Coordinates3D.Up));
if (_.Opaque)
unobstructed = false;
}
}
if (world.BlockRepository.GetBlockProvider(world.GetBlockID(descriptor.Coordinates + Coordinates3D.Up)).Opaque)
unobstructed = false;
if (!unobstructed)
return false;
var entity = world.GetTileEntity(descriptor.Coordinates);
var window = new ChestWindow((InventoryWindow)user.Inventory, isDouble);
if (entity != null)
{
foreach (NbtCompound item in (NbtList)entity["Items"])
{
var stack = ItemStack.FromNbt(item);
window.ChestInventory[stack.Index] = stack;
}
}
if (isDouble)
{
entity = world.GetTileEntity(other);
if (entity != null)
{
foreach (NbtCompound item in (NbtList)entity["Items"])
{
var stack = ItemStack.FromNbt(item);
window.ChestInventory[stack.Index] = stack;
}
}
}
user.OpenWindow(window);
window.WindowChange += (sender, e) =>
{
// TODO: Update tile entity
}; // TODO: Memory leak here
return false;
}
}
}

View File

@ -4,6 +4,8 @@ using TrueCraft.API;
using TrueCraft.API.World;
using TrueCraft.API.Networking;
using TrueCraft.Core.Logic.Items;
using TrueCraft.Core.Networking.Packets;
using fNbt;
namespace TrueCraft.Core.Logic.Blocks
{
@ -37,5 +39,22 @@ namespace TrueCraft.Core.Logic.Blocks
{
return new[] { new ItemStack(SignItem.ItemID) };
}
public override void TileEntityLoadedForClient(BlockDescriptor descriptor, IWorld world, NbtCompound entity, IRemoteClient client)
{
client.QueuePacket(new UpdateSignPacket
{
X = descriptor.Coordinates.X,
Y = (short)descriptor.Coordinates.Y,
Z = descriptor.Coordinates.Z,
Text = new[]
{
entity["Text1"].StringValue,
entity["Text2"].StringValue,
entity["Text3"].StringValue,
entity["Text4"].StringValue
}
});
}
}
}

View File

@ -318,6 +318,7 @@
<Compile Include="TrueCraftUser.cs" />
<Compile Include="RuntimeInfo.cs" />
<Compile Include="Logic\Blocks\DandelionBlock.cs" />
<Compile Include="Windows\ChestWindow.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

View File

@ -0,0 +1,126 @@
using System;
using TrueCraft.API.Windows;
using TrueCraft.Core.Windows;
using TrueCraft.API;
namespace TrueCraft.Core.Windows
{
public class ChestWindow : Window
{
public ChestWindow(InventoryWindow inventory, bool doubleChest = false)
{
DoubleChest = doubleChest;
if (doubleChest)
{
WindowAreas = new[]
{
new WindowArea(ChestIndex, 54, 9, 3), // Chest
new WindowArea(DoubleMainIndex, 27, 9, 3), // Main inventory
new WindowArea(DoubleHotbarIndex, 9, 9, 1) // Hotbar
};
}
else
{
WindowAreas = new[]
{
new WindowArea(ChestIndex, 27, 9, 3), // Chest
new WindowArea(MainIndex, 27, 9, 3), // Main inventory
new WindowArea(HotbarIndex, 9, 9, 1) // Hotbar
};
}
inventory.MainInventory.CopyTo(MainInventory);
inventory.Hotbar.CopyTo(Hotbar);
Copying = false;
inventory.WindowChange += (sender, e) =>
{
if (Copying) return;
if ((e.SlotIndex >= InventoryWindow.MainIndex && e.SlotIndex < InventoryWindow.MainIndex + inventory.MainInventory.Length)
|| (e.SlotIndex >= InventoryWindow.HotbarIndex && e.SlotIndex < InventoryWindow.HotbarIndex + inventory.Hotbar.Length))
{
inventory.MainInventory.CopyTo(MainInventory);
inventory.Hotbar.CopyTo(Hotbar);
}
};
foreach (var area in WindowAreas)
area.WindowChange += (s, e) => OnWindowChange(new WindowChangeEventArgs(
(s as WindowArea).StartIndex + e.SlotIndex, e.Value));
}
public const int ChestIndex = 0;
public const int MainIndex = 27;
public const int HotbarIndex = 54;
public const int DoubleMainIndex = 54;
public const int DoubleHotbarIndex = 81;
public bool DoubleChest { get; set; }
public override IWindowArea[] WindowAreas { get; protected set; }
private bool Copying { get; set; }
public override string Name
{
get
{
if (DoubleChest)
return "Large Chest";
return "Chest";
}
}
public override sbyte Type
{
get
{
return 0;
}
}
public IWindowArea ChestInventory
{
get
{
return WindowAreas[0];
}
}
public IWindowArea MainInventory
{
get
{
return WindowAreas[1];
}
}
public IWindowArea Hotbar
{
get
{
return WindowAreas[2];
}
}
public override int MinecraftWasWrittenByFuckingIdiotsLength
{
get
{
return ChestInventory.Length;
}
}
public override void CopyToInventory(IWindow inventoryWindow)
{
var window = (InventoryWindow)inventoryWindow;
Copying = true;
MainInventory.CopyTo(window.MainInventory);
Hotbar.CopyTo(window.Hotbar);
Copying = false;
}
protected override IWindowArea GetLinkedArea(int index, ItemStack slot)
{
if (index == 0)
return Hotbar;
else
return ChestInventory;
}
}
}

View File

@ -70,7 +70,7 @@ namespace TrueCraft.Core.Windows
throw new IndexOutOfRangeException();
}
public int Length
public virtual int Length
{
get
{
@ -78,6 +78,8 @@ namespace TrueCraft.Core.Windows
}
}
public virtual int MinecraftWasWrittenByFuckingIdiotsLength { get { return Length; } }
public bool Empty
{
get

View File

@ -175,7 +175,7 @@ namespace TrueCraft
CurrentWindow = window;
window.ID = NextWindowID++;
if (NextWindowID < 0) NextWindowID = 1;
QueuePacket(new OpenWindowPacket(window.ID, window.Type, window.Name, (sbyte)window.Length));
QueuePacket(new OpenWindowPacket(window.ID, window.Type, window.Name, (sbyte)window.MinecraftWasWrittenByFuckingIdiotsLength));
QueuePacket(new WindowItemsPacket(window.ID, window.GetSlots()));
}

View File

@ -14,6 +14,8 @@
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<WarningLevel>4</WarningLevel>
<DebugSymbols>true</DebugSymbols>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>false</Optimize>
@ -25,8 +27,8 @@
<Reference Include="Ionic.Zip.Reduced">
<HintPath>..\lib\Ionic.Zip.Reduced.dll</HintPath>
</Reference>
<Reference Include="YamlDotNet, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\YamlDotNet.3.5.1\lib\net35\YamlDotNet.dll</HintPath>
<Reference Include="YamlDotNet">
<HintPath>..\packages\YamlDotNet.3.6.0\lib\net35\YamlDotNet.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="YamlDotNet" version="3.5.1" targetFramework="net4" userInstalled="true" />
<package id="YamlDotNet" version="3.6.0" targetFramework="net40" />
</packages>