From 13baea71918ee7c209c450156317e36d6c65fdfe Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 21 Jun 2015 17:36:42 -0400 Subject: [PATCH 1/3] Partially implement chests --- TrueCraft.API/TrueCraft.API.csproj | 7 +- TrueCraft.API/Windows/IWindow.cs | 1 + TrueCraft.API/packages.config | 4 + TrueCraft.Core/Logic/BlockProvider.cs | 2 +- TrueCraft.Core/Logic/Blocks/ChestBlock.cs | 97 ++++++++++++++ TrueCraft.Core/Logic/Blocks/WallSignBlock.cs | 19 +++ TrueCraft.Core/TrueCraft.Core.csproj | 1 + TrueCraft.Core/Windows/ChestWindow.cs | 126 +++++++++++++++++++ TrueCraft.Core/Windows/Window.cs | 4 +- TrueCraft/RemoteClient.cs | 2 +- TrueCraft/TrueCraft.csproj | 6 +- TrueCraft/packages.config | 2 +- 12 files changed, 263 insertions(+), 8 deletions(-) create mode 100644 TrueCraft.API/packages.config create mode 100644 TrueCraft.Core/Windows/ChestWindow.cs diff --git a/TrueCraft.API/TrueCraft.API.csproj b/TrueCraft.API/TrueCraft.API.csproj index 4000c40..52f909a 100644 --- a/TrueCraft.API/TrueCraft.API.csproj +++ b/TrueCraft.API/TrueCraft.API.csproj @@ -31,7 +31,7 @@ - ..\packages\YamlDotNet.3.5.1\lib\net35\YamlDotNet.dll + ..\packages\YamlDotNet.3.6.0\lib\net35\YamlDotNet.dll @@ -113,4 +113,7 @@ fNbt - \ No newline at end of file + + + + diff --git a/TrueCraft.API/Windows/IWindow.cs b/TrueCraft.API/Windows/IWindow.cs index 97ad635..b96c188 100644 --- a/TrueCraft.API/Windows/IWindow.cs +++ b/TrueCraft.API/Windows/IWindow.cs @@ -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; } diff --git a/TrueCraft.API/packages.config b/TrueCraft.API/packages.config new file mode 100644 index 0000000..c965a8b --- /dev/null +++ b/TrueCraft.API/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/TrueCraft.Core/Logic/BlockProvider.cs b/TrueCraft.Core/Logic/BlockProvider.cs index a01ba02..bfeefbe 100644 --- a/TrueCraft.Core/Logic/BlockProvider.cs +++ b/TrueCraft.Core/Logic/BlockProvider.cs @@ -99,7 +99,7 @@ namespace TrueCraft.Core.Logic coordinates += MathHelper.BlockFaceToCoordinates(face); var old = world.GetBlockData(coordinates); byte[] overwritable = - { + { AirBlock.BlockID, WaterBlock.BlockID, StationaryWaterBlock.BlockID, diff --git a/TrueCraft.Core/Logic/Blocks/ChestBlock.cs b/TrueCraft.Core/Logic/Blocks/ChestBlock.cs index 80a955a..022211c 100644 --- a/TrueCraft.Core/Logic/Blocks/ChestBlock.cs +++ b/TrueCraft.Core/Logic/Blocks/ChestBlock.cs @@ -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; + } } } \ No newline at end of file diff --git a/TrueCraft.Core/Logic/Blocks/WallSignBlock.cs b/TrueCraft.Core/Logic/Blocks/WallSignBlock.cs index d28ec0d..bffa96b 100644 --- a/TrueCraft.Core/Logic/Blocks/WallSignBlock.cs +++ b/TrueCraft.Core/Logic/Blocks/WallSignBlock.cs @@ -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 + } + }); + } } } \ No newline at end of file diff --git a/TrueCraft.Core/TrueCraft.Core.csproj b/TrueCraft.Core/TrueCraft.Core.csproj index d43f54c..22f6ffd 100644 --- a/TrueCraft.Core/TrueCraft.Core.csproj +++ b/TrueCraft.Core/TrueCraft.Core.csproj @@ -318,6 +318,7 @@ + diff --git a/TrueCraft.Core/Windows/ChestWindow.cs b/TrueCraft.Core/Windows/ChestWindow.cs new file mode 100644 index 0000000..173d806 --- /dev/null +++ b/TrueCraft.Core/Windows/ChestWindow.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/TrueCraft.Core/Windows/Window.cs b/TrueCraft.Core/Windows/Window.cs index ea54bd7..d56a3b0 100644 --- a/TrueCraft.Core/Windows/Window.cs +++ b/TrueCraft.Core/Windows/Window.cs @@ -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 diff --git a/TrueCraft/RemoteClient.cs b/TrueCraft/RemoteClient.cs index 227655c..ece4749 100644 --- a/TrueCraft/RemoteClient.cs +++ b/TrueCraft/RemoteClient.cs @@ -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())); } diff --git a/TrueCraft/TrueCraft.csproj b/TrueCraft/TrueCraft.csproj index ad320bb..affb812 100644 --- a/TrueCraft/TrueCraft.csproj +++ b/TrueCraft/TrueCraft.csproj @@ -14,6 +14,8 @@ false bin\Debug 4 + true + false false @@ -25,8 +27,8 @@ ..\lib\Ionic.Zip.Reduced.dll - - ..\packages\YamlDotNet.3.5.1\lib\net35\YamlDotNet.dll + + ..\packages\YamlDotNet.3.6.0\lib\net35\YamlDotNet.dll diff --git a/TrueCraft/packages.config b/TrueCraft/packages.config index 49d63fd..c965a8b 100644 --- a/TrueCraft/packages.config +++ b/TrueCraft/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file -- 2.47.2 From ef0a24bce2eb1544132b0c7ced30dd0977bef163 Mon Sep 17 00:00:00 2001 From: William Moorehouse Date: Sun, 21 Jun 2015 18:27:09 -0400 Subject: [PATCH 2/3] Added cobweb renderer --- .../Rendering/Blocks/CobwebRenderer.cs | 40 +++++++++++++++++++ TrueCraft.Client/TrueCraft.Client.csproj | 3 +- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 TrueCraft.Client/Rendering/Blocks/CobwebRenderer.cs diff --git a/TrueCraft.Client/Rendering/Blocks/CobwebRenderer.cs b/TrueCraft.Client/Rendering/Blocks/CobwebRenderer.cs new file mode 100644 index 0000000..da1a62a --- /dev/null +++ b/TrueCraft.Client/Rendering/Blocks/CobwebRenderer.cs @@ -0,0 +1,40 @@ +using System; +using TrueCraft.Core.Logic.Blocks; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using TrueCraft.API.Logic; + +namespace TrueCraft.Client.Rendering.Blocks +{ + public class CobwebRenderer : FlatQuadRenderer + { + static CobwebRenderer() + { + BlockRenderer.RegisterRenderer(CobwebBlock.BlockID, new CobwebRenderer()); + } + + protected Vector2 CobwebTextureMap { get { return new Vector2(11, 0); } } + protected Vector2[] CobwebTexture; + + public CobwebRenderer() + { + CobwebTexture = new[] + { + CobwebTextureMap + Vector2.UnitX + Vector2.UnitY, + CobwebTextureMap + Vector2.UnitY, + CobwebTextureMap, + CobwebTextureMap + Vector2.UnitX, + }; + for (int i = 0; i < Texture.Length; i++) + { + CobwebTexture[i] *= new Vector2(16f / 256f); + } + } + + public override VertexPositionNormalColorTexture[] Render(BlockDescriptor descriptor, Vector3 offset, + Tuple textureMap, int indiciesOffset, out int[] indicies) + { + return RenderQuads(descriptor, offset, CobwebTexture, indiciesOffset, out indicies, Color.White); + } + } +} diff --git a/TrueCraft.Client/TrueCraft.Client.csproj b/TrueCraft.Client/TrueCraft.Client.csproj index 6c8e685..acf12a3 100644 --- a/TrueCraft.Client/TrueCraft.Client.csproj +++ b/TrueCraft.Client/TrueCraft.Client.csproj @@ -67,6 +67,7 @@ + @@ -178,4 +179,4 @@ PreserveNewest - + \ No newline at end of file -- 2.47.2 From 88ed21639e98b063d9ef8df53dec385c15cb20e8 Mon Sep 17 00:00:00 2001 From: William Moorehouse Date: Sun, 21 Jun 2015 18:36:02 -0400 Subject: [PATCH 3/3] Simplified cobweb renderer --- .../Rendering/Blocks/CobwebRenderer.cs | 24 +------------------ 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/TrueCraft.Client/Rendering/Blocks/CobwebRenderer.cs b/TrueCraft.Client/Rendering/Blocks/CobwebRenderer.cs index da1a62a..5f70848 100644 --- a/TrueCraft.Client/Rendering/Blocks/CobwebRenderer.cs +++ b/TrueCraft.Client/Rendering/Blocks/CobwebRenderer.cs @@ -13,28 +13,6 @@ namespace TrueCraft.Client.Rendering.Blocks BlockRenderer.RegisterRenderer(CobwebBlock.BlockID, new CobwebRenderer()); } - protected Vector2 CobwebTextureMap { get { return new Vector2(11, 0); } } - protected Vector2[] CobwebTexture; - - public CobwebRenderer() - { - CobwebTexture = new[] - { - CobwebTextureMap + Vector2.UnitX + Vector2.UnitY, - CobwebTextureMap + Vector2.UnitY, - CobwebTextureMap, - CobwebTextureMap + Vector2.UnitX, - }; - for (int i = 0; i < Texture.Length; i++) - { - CobwebTexture[i] *= new Vector2(16f / 256f); - } - } - - public override VertexPositionNormalColorTexture[] Render(BlockDescriptor descriptor, Vector3 offset, - Tuple textureMap, int indiciesOffset, out int[] indicies) - { - return RenderQuads(descriptor, offset, CobwebTexture, indiciesOffset, out indicies, Color.White); - } + protected override Vector2 TextureMap { get { return new Vector2(11, 0); } } } } -- 2.47.2