From ffa904260f11c89ef5decdd4da72ed7d6e391bc0 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 3 May 2015 12:44:05 -0600 Subject: [PATCH] Add TileEntities to Chunk objects --- TrueCraft.Core/TrueCraft.Core.csproj | 3 +- TrueCraft.Core/World/Chunk.cs | 53 +++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/TrueCraft.Core/TrueCraft.Core.csproj b/TrueCraft.Core/TrueCraft.Core.csproj index a36d666..198639b 100644 --- a/TrueCraft.Core/TrueCraft.Core.csproj +++ b/TrueCraft.Core/TrueCraft.Core.csproj @@ -7,6 +7,7 @@ Library TrueCraft.Core TrueCraft.Core + true true @@ -17,7 +18,6 @@ prompt 4 false - true full @@ -26,7 +26,6 @@ prompt 4 false - true diff --git a/TrueCraft.Core/World/Chunk.cs b/TrueCraft.Core/World/Chunk.cs index 92b60f6..f0762c1 100644 --- a/TrueCraft.Core/World/Chunk.cs +++ b/TrueCraft.Core/World/Chunk.cs @@ -1,8 +1,5 @@ using System; -using System.Linq; -using System.Runtime.Serialization; using System.Collections.Generic; -using System.Reflection; using fNbt; using fNbt.Serialization; using TrueCraft.API.World; @@ -35,6 +32,7 @@ namespace TrueCraft.Core.World public int X { get; set; } [TagName("zPos")] public int Z { get; set; } + public Dictionary TileEntities { get; set; } public Coordinates2D Coordinates { @@ -62,6 +60,7 @@ namespace TrueCraft.Core.World Biomes = new byte[Width * Depth]; HeightMap = new int[Width * Depth]; LastAccessed = DateTime.Now; + TileEntities = new Dictionary(); } public Chunk(Coordinates2D coordinates) : this() @@ -173,6 +172,27 @@ namespace TrueCraft.Core.World int index = coordinates.Y + (coordinates.Z * Height) + (coordinates.X * Height * Width); BlockLight[index] = value; } + + /// + /// Gets the tile entity for the given coordinates. May return null. + /// + public NbtCompound GetTileEntity(Coordinates3D coordinates) + { + LastAccessed = DateTime.Now; + if (TileEntities.ContainsKey(coordinates)) + return TileEntities[coordinates]; + return null; + } + + /// + /// Sets the tile entity at the given coordinates to the given value. + /// + public void SetTileEntity(Coordinates3D coordinates, NbtCompound value) + { + LastAccessed = DateTime.Now; + TileEntities[coordinates] = value; + IsModified = true; + } /// /// Gets the height of the specified column. @@ -216,7 +236,22 @@ namespace TrueCraft.Core.World chunk.Add(new NbtByteArray("Data", Metadata.Data)); chunk.Add(new NbtByteArray("SkyLight", SkyLight.Data)); chunk.Add(new NbtByteArray("BlockLight", BlockLight.Data)); - // TODO: Tile entities, entities + + var tiles = new NbtList("TileEntities", NbtTagType.Compound); + foreach (var kvp in TileEntities) + { + var c = new NbtCompound(); + c.Add(new NbtList("coordinates", new[] { + new NbtInt(kvp.Key.X), + new NbtInt(kvp.Key.Y), + new NbtInt(kvp.Key.Z) + })); + c.Add(new NbtList("value", new[] { kvp.Value })); + tiles.Add(c); + } + chunk.Add(tiles); + + // TODO: Entities return chunk; } @@ -238,6 +273,16 @@ namespace TrueCraft.Core.World BlockLight.Data = tag["BlockLight"].ByteArrayValue; SkyLight = new NibbleArray(); SkyLight.Data = tag["SkyLight"].ByteArrayValue; + + if (tag.Contains("TileEntities")) + { + foreach (var entity in tag["TileEntities"] as NbtList) + { + TileEntities[new Coordinates3D(entity["coordinates"][0].IntValue, + entity["coordinates"][1].IntValue, + entity["coordinates"][2].IntValue)] = entity["value"][0] as NbtCompound; + } + } // TODO: Tile entities, entities }