/// <summary> /// Builds an Entity subtree from the current data. /// </summary> /// <returns>The root node of an Entity subtree representing the current data.</returns> public TagNode BuildTree() { TagNodeCompound tree = new TagNodeCompound(); TagNodeList pos = new TagNodeList(TagType.TAG_DOUBLE); pos.Add(new TagNodeDouble(_pos.X)); pos.Add(new TagNodeDouble(_pos.Y)); pos.Add(new TagNodeDouble(_pos.Z)); tree["Pos"] = pos; TagNodeList motion = new TagNodeList(TagType.TAG_DOUBLE); motion.Add(new TagNodeDouble(_motion.X)); motion.Add(new TagNodeDouble(_motion.Y)); motion.Add(new TagNodeDouble(_motion.Z)); tree["Motion"] = motion; TagNodeList rotation = new TagNodeList(TagType.TAG_FLOAT); rotation.Add(new TagNodeFloat((float)_rotation.Yaw)); rotation.Add(new TagNodeFloat((float)_rotation.Pitch)); tree["Rotation"] = rotation; tree["FallDistance"] = new TagNodeFloat(_fallDistance); tree["Fire"] = new TagNodeShort(_fire); tree["Air"] = new TagNodeShort(_air); tree["OnGround"] = new TagNodeByte(_onGround); return(tree); }
public void SetTileTickValue(int x, int y, int z, int tickValue) { BlockKey key = (TranslateCoordinates != null) ? TranslateCoordinates(x, y, z) : new BlockKey(x, y, z); TagNodeCompound te; if (!_tileTickTable.TryGetValue(key, out te)) { TileTick tt = new TileTick() { ID = _blocks[x, y, z], Ticks = tickValue, X = key.x, Y = key.y, Z = key.z, }; te = tt.BuildTree() as TagNodeCompound; _tileTicks.Add(te); _tileTickTable[key] = te; } else { te["t"].ToTagInt().Data = tickValue; } }
public void SetTileEntity(int x, int y, int z, TileEntity te) { BlockInfoEx info = BlockInfo.BlockTable[_blocks[x, y, z]] as BlockInfoEx; if (info == null) { throw new InvalidOperationException("The given block is of a type that does not support TileEntities."); } if (te.GetType() != TileEntityFactory.Lookup(info.TileEntityName)) { throw new ArgumentException("The TileEntity type is not valid for this block.", "te"); } BlockKey key = (TranslateCoordinates != null) ? TranslateCoordinates(x, y, z) : new BlockKey(x, y, z); TagNodeCompound oldte; if (_tileEntityTable.TryGetValue(key, out oldte)) { _tileEntities.Remove(oldte); } te.X = key.x; te.Y = key.y; te.Z = key.z; TagNodeCompound tree = te.BuildTree() as TagNodeCompound; _tileEntities.Add(tree); _tileEntityTable[key] = tree; }
public TagNode BuildTree() { TagNodeCompound level = _tree.Root["Level"] as TagNodeCompound; TagNodeCompound levelCopy = new TagNodeCompound(); foreach (KeyValuePair <string, TagNode> node in level) { levelCopy.Add(node.Key, node.Value); } TagNodeList sections = new TagNodeList(TagType.TAG_COMPOUND); for (int i = 0; i < _sections.Length; i++) { if (ShouldIncludeSection(_sections[i])) { sections.Add(_sections[i].BuildTree()); } } levelCopy["Sections"] = sections; if (_tileTicks.Count == 0) { levelCopy.Remove("TileTicks"); } return(levelCopy); }
/// <inheritdoc/> public TagNode BuildTree() { TagNodeCompound tree = new TagNodeCompound(); tree["id"] = new TagNodeString(_id); tree["Count"] = new TagNodeByte(_count); tree["Damage"] = new TagNodeShort(_damage); if (_enchantments.Count > 0) { TagNodeList enchList = new TagNodeList(TagType.TAG_COMPOUND); foreach (Enchantment e in _enchantments) { enchList.Add(e.BuildTree()); } TagNodeCompound tagtree = new TagNodeCompound(); tagtree["ench"] = enchList; if (_source != null && _source.ContainsKey("tag")) { tagtree.MergeFrom(_source["tag"].ToTagCompound()); } tree["tag"] = tagtree; } if (_source != null) { tree.MergeFrom(_source); } return(tree); }
public override TagNode BuildTree() { TagNodeCompound tree = base.BuildTree() as TagNodeCompound; tree["AttackTime"] = new TagNodeShort(_attackTime); tree["DeathTime"] = new TagNodeShort(_deathTime); tree["Health"] = new TagNodeShort(_health); tree["HurtTime"] = new TagNodeShort(_hurtTime); tree["CanPickUpLoot"] = new TagNodeByte(_canPickUpLoot); tree["PersistenceRequired"] = new TagNodeByte(_persistenceRequired); tree["CustomName"] = new TagNodeString(_customName); tree["CustomNameVisible"] = new TagNodeByte(_customNameVisible); if (_activeEffects != null) { TagNodeList effects = new TagNodeList(TagType.TAG_COMPOUND); foreach (ActiveEffects effect in _activeEffects) { if (!effect.IsValid) { continue; } TagNodeCompound ae = new TagNodeCompound(); ae["Id"] = new TagNodeByte((byte)effect.Id); ae["Amplifier"] = new TagNodeByte((byte)effect.Amplifier); ae["Duration"] = new TagNodeInt(effect.Duration); ae["Ambient"] = new TagNodeByte(effect.Ambient ? (byte)1 : (byte)0); effects.Add(ae); } tree["ActiveEffects"] = effects; } if (_equipment != null) { TagNodeList equipment = new TagNodeList(TagType.TAG_COMPOUND); foreach (Item item in _equipment) { equipment.Add(item.BuildTree()); } tree["Equipment"] = equipment; } if (_dropChances != null) { TagNodeList dropChances = new TagNodeList(TagType.TAG_FLOAT); foreach (float dc in _dropChances) { dropChances.Add(new TagNodeFloat(dc)); } tree["DropChances"] = dropChances; } return(tree); }
/// <summary> /// Updates the chunk's global world coordinates. /// </summary> /// <param name="x">Global X-coordinate.</param> /// <param name="z">Global Z-coordinate.</param> public virtual void SetLocation(int x, int z) { int diffx = (x - _cx) * XDIM; int diffz = (z - _cz) * ZDIM; // Update chunk position _cx = x; _cz = z; _tree.Root["Level"].ToTagCompound()["xPos"].ToTagInt().Data = x; _tree.Root["Level"].ToTagCompound()["zPos"].ToTagInt().Data = z; // Update tile entity coordinates List <TileEntity> tileEntites = new List <TileEntity>(); foreach (TagNodeCompound tag in _tileEntities) { TileEntity te = TileEntityFactory.Create(tag); if (te == null) { te = TileEntity.FromTreeSafe(tag); } if (te != null) { te.MoveBy(diffx, 0, diffz); tileEntites.Add(te); } } _tileEntities.Clear(); foreach (TileEntity te in tileEntites) { _tileEntities.Add(te.BuildTree()); } // Update entity coordinates List <TypedEntity> entities = new List <TypedEntity>(); foreach (TypedEntity entity in _entityManager) { entity.MoveBy(diffx, 0, diffz); entities.Add(entity); } _entities.Clear(); foreach (TypedEntity entity in entities) { _entityManager.Add(entity); } }
public bool AppendTag(TagNode tag) { if (_tag.ValueType != tag.GetTagType()) { return(false); } _tag.Add(tag); SetModified(); return(true); }
/// <inheritdoc/> public TagNode BuildTree() { TagNodeList list = new TagNodeList(TagType.TAG_COMPOUND); foreach (KeyValuePair <int, Item> item in _items) { TagNodeCompound itemtree = item.Value.BuildTree() as TagNodeCompound; itemtree["Slot"] = new TagNodeByte((byte)item.Key); list.Add(itemtree); } return(list); }
public static TagNodeList GetEnchantmentNBT(List <Enchantment> enchantments) { TagNodeCompound compound; TagNodeList list = new TagNodeList(TagType.TAG_COMPOUND); foreach (Enchantment ench in enchantments.ToArray()) { compound = new TagNodeCompound(); compound.Add("id", new TagNodeShort(ench.id)); compound.Add("lvl", new TagNodeShort(ench.level)); list.Add(compound); } return(list); }
public override TagNodeList GetNBTData() { TagNodeCompound comp; Item item; TagNodeList list = new TagNodeList(TagType.TAG_COMPOUND); for (int i = 0; i < Size; i++) { item = items[i]; if (item != null && item.id != -1) { comp = item.GetNBTData(); comp.Add("Slot", new TagNodeByte((byte)i)); list.Add(comp); } } return(list); }
public override TagNodeList GetNBTData() { TagNodeCompound comp; Item item; TagNodeList list = new TagNodeList(TagType.TAG_COMPOUND); for (int i = 0; i < Size; i++) { item = items[i]; if (item != null && item.id != -1) { comp = item.GetNBTData(); comp.Add("Slot", new TagNodeByte((byte)i)); list.Add(comp); } } return list; }
public static byte[] GetEnchantmentNBTData(List<Enchantment> enchantments) { if (enchantments.Count < 1) return new byte[0]; NbtTree nbt = new NbtTree(); TagNodeList list = new TagNodeList(TagType.TAG_COMPOUND); TagNodeCompound compound; foreach (Enchantment ench in enchantments.ToArray()) { compound = new TagNodeCompound(); compound.Add("id", new TagNodeShort(ench.id)); compound.Add("lvl", new TagNodeShort(ench.level)); list.Add(compound); } nbt.Root.Add("ench", list); using (MemoryStream ms = new MemoryStream()) { nbt.WriteTo(ms); return ms.ToArray().Compress(Ionic.Zlib.CompressionLevel.BestCompression, CompressionType.GZip); } }
/// <summary> /// Constructs a default <see cref="TagNodeList"/> satisfying the constraints of this node. /// </summary> /// <returns>A <see cref="TagNodeList"/> with a sensible default value. If a length is specified, default child <see cref="TagNode"/> objects of the necessary type will be created and added to the <see cref="TagNodeList"/>.</returns> public override TagNode BuildDefaultTree () { if (_length == 0) { return new TagNodeList(_type); } TagNodeList list = new TagNodeList(_type); for (int i = 0; i < _length; i++) { list.Add(_subschema.BuildDefaultTree()); } return list; }
/// <summary> /// Exports the <see cref="Schematic"/> object to a schematic file. /// </summary> /// <param name="path">The path to write out the schematic file to.</param> public void Export(string path) { int xdim = _blockset.XDim; int ydim = _blockset.YDim; int zdim = _blockset.ZDim; byte[] blockData = new byte[xdim * ydim * zdim]; byte[] dataData = new byte[xdim * ydim * zdim]; YZXByteArray schemaBlocks = new YZXByteArray(_blockset.XDim, _blockset.YDim, _blockset.ZDim, blockData); YZXByteArray schemaData = new YZXByteArray(_blockset.XDim, _blockset.YDim, _blockset.ZDim, dataData); TagNodeList entities = new TagNodeList(TagType.TAG_COMPOUND); TagNodeList tileEntities = new TagNodeList(TagType.TAG_COMPOUND); for (int x = 0; x < xdim; x++) { for (int z = 0; z < zdim; z++) { for (int y = 0; y < ydim; y++) { AlphaBlock block = _blockset.GetBlock(x, y, z); schemaBlocks[x, y, z] = (byte)block.ID; schemaData[x, y, z] = (byte)block.Data; TileEntity te = block.GetTileEntity(); if (te != null) { te.X = x; te.Y = y; te.Z = z; tileEntities.Add(te.BuildTree()); } } } } foreach (EntityTyped e in _entityset) { entities.Add(e.BuildTree()); } TagNodeCompound schematic = new TagNodeCompound(); schematic["Width"] = new TagNodeShort((short)xdim); schematic["Length"] = new TagNodeShort((short)zdim); schematic["Height"] = new TagNodeShort((short)ydim); schematic["Entities"] = entities; schematic["TileEntities"] = tileEntities; schematic["Materials"] = new TagNodeString("Alpha"); schematic["Blocks"] = new TagNodeByteArray(blockData); schematic["Data"] = new TagNodeByteArray(dataData); NBTFile schematicFile = new NBTFile(path); Stream nbtStream = schematicFile.GetDataOutputStream(); if (nbtStream == null) { return; } NbtTree tree = new NbtTree(schematic, "Schematic"); tree.WriteTo(nbtStream); nbtStream.Close(); }
public static TagNodeList GetEnchantmentNBT(List<Enchantment> enchantments) { TagNodeCompound compound; TagNodeList list = new TagNodeList(TagType.TAG_COMPOUND); foreach (Enchantment ench in enchantments.ToArray()) { compound = new TagNodeCompound(); compound.Add("id", new TagNodeShort(ench.id)); compound.Add("lvl", new TagNodeShort(ench.level)); list.Add(compound); } return list; }
/// <summary> /// Exports the <see cref="Schematic"/> object to a schematic file. /// </summary> /// <param name="path">The path to write out the schematic file to.</param> public void Export(string path) { int xdim = _blockset.XDim; int ydim = _blockset.YDim; int zdim = _blockset.ZDim; byte[] blockData = new byte[xdim * ydim * zdim]; byte[] dataData = new byte[xdim * ydim * zdim]; YZXByteArray schemaBlocks = new YZXByteArray(_blockset.XDim, _blockset.YDim, _blockset.ZDim, blockData); YZXByteArray schemaData = new YZXByteArray(_blockset.XDim, _blockset.YDim, _blockset.ZDim, dataData); TagNodeList entities = new TagNodeList(TagType.TAG_COMPOUND); TagNodeList tileEntities = new TagNodeList(TagType.TAG_COMPOUND); for (int x = 0; x < xdim; x++) { for (int z = 0; z < zdim; z++) { for (int y = 0; y < ydim; y++) { AlphaBlock block = _blockset.GetBlock(x, y, z); schemaBlocks[x, y, z] = (byte)block.ID; schemaData[x, y, z] = (byte)block.Data; TileEntity te = block.GetTileEntity(); if (te != null) { te.X = x; te.Y = y; te.Z = z; tileEntities.Add(te.BuildTree()); } } } } foreach (TypedEntity e in _entityset) { entities.Add(e.BuildTree()); } TagNodeCompound schematic = new TagNodeCompound(); schematic["Width"] = new TagNodeShort((short)xdim); schematic["Length"] = new TagNodeShort((short)zdim); schematic["Height"] = new TagNodeShort((short)ydim); schematic["Entities"] = entities; schematic["TileEntities"] = tileEntities; schematic["Materials"] = new TagNodeString("Alpha"); schematic["Blocks"] = new TagNodeByteArray(blockData); schematic["Data"] = new TagNodeByteArray(dataData); NBTFile schematicFile = new NBTFile(path); Stream nbtStream = schematicFile.GetDataOutputStream(); if (nbtStream == null) { return; } NbtTree tree = new NbtTree(schematic, "Schematic"); tree.WriteTo(nbtStream); nbtStream.Close(); }
public void Save(World w) { try { string path = CreatePath(w, x, z, true); string file = CreatePath(w, x, z); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } NbtTree nbt = new NbtTree(); nbt.Root.Add("Generated", new TagNodeByte((byte)(generated ? 1 : 0))); nbt.Root.Add("Populated", new TagNodeByte((byte)(populated ? 1 : 0))); nbt.Root.Add("Blocks", new TagNodeByteArray(blocks)); nbt.Root.Add("Meta", new TagNodeByteArray(meta)); nbt.Root.Add("BlockLight", new TagNodeByteArray(Light)); nbt.Root.Add("SkyLight", new TagNodeByteArray(SkyL)); nbt.Root.Add("HeightMap", new TagNodeByteArray(heightMap)); nbt.Root.Add("HeightMapPrec", new TagNodeByteArray(precipitationHeightMap.ToByteArray())); TagNodeList nbtList = new TagNodeList(TagType.TAG_COMPOUND); TagNodeCompound nbtCompound; lock (extra) foreach (KeyValuePair <int, ushort> kvp in extra) { nbtCompound = new TagNodeCompound(); nbtCompound.Add("Pos", new TagNodeInt(kvp.Key)); nbtCompound.Add("Value", new TagNodeShort((short)kvp.Value)); nbtList.Add(nbtCompound); } nbt.Root.Add("Extra", nbtList); nbtList = new TagNodeList(TagType.TAG_COMPOUND); List <Physics.Check> physChecks = w.physics.GetChunkChecks(x, z); foreach (Physics.Check check in physChecks) { nbtCompound = new TagNodeCompound(); nbtCompound.Add("Pos", new TagNodeList(TagType.TAG_INT) { new TagNodeInt(check.x), new TagNodeInt(check.y), new TagNodeInt(check.z) }); nbtCompound.Add("Meta", new TagNodeByte(check.meta)); nbtCompound.Add("Time", new TagNodeShort(check.time)); nbtList.Add(nbtCompound); } nbt.Root.Add("Physics", nbtList); nbtList = new TagNodeList(TagType.TAG_COMPOUND); List <Entity> entities = Entities; TagNodeCompound nbtCompound2; foreach (Entity e in entities) { if (e.isPlayer) { continue; } nbtCompound = new TagNodeCompound(); nbtCompound.Add("Motion", new TagNodeList(TagType.TAG_DOUBLE) { new TagNodeDouble(e.velocity[0]), new TagNodeDouble(e.velocity[1]), new TagNodeDouble(e.velocity[2]) }); nbtCompound.Add("Pos", new TagNodeList(TagType.TAG_DOUBLE) { new TagNodeDouble(e.pos.x), new TagNodeDouble(e.pos.y), new TagNodeDouble(e.pos.z) }); nbtCompound.Add("Rotation", new TagNodeList(TagType.TAG_FLOAT) { new TagNodeFloat(e.rot[0]), new TagNodeFloat(e.rot[1]) }); nbtCompound.Add("Type", new TagNodeByte((byte)e.Type)); nbtCompound.Add("Age", new TagNodeInt(e.age)); nbtCompound.Add("OnGround", new TagNodeByte(e.onground)); nbtCompound.Add("Health", new TagNodeShort(e.Health)); nbtCompound2 = new TagNodeCompound(); switch (e.Type) { case EntityType.AI: nbtCompound2.Add("Type", new TagNodeByte(e.ai.type)); break; case EntityType.Object: nbtCompound2.Add("Type", new TagNodeByte(e.obj.type)); break; case EntityType.Item: nbtCompound2.Add("ID", new TagNodeShort(e.I.id)); nbtCompound2.Add("Count", new TagNodeByte(e.I.count)); nbtCompound2.Add("Meta", new TagNodeShort(e.I.meta)); break; } nbtCompound.Add("Data", nbtCompound2); nbtList.Add(nbtCompound); } nbt.Root.Add("Entities", nbtList); nbtList = new TagNodeList(TagType.TAG_COMPOUND); foreach (Container c in GetContainers(w)) { nbtCompound = new TagNodeCompound(); nbtCompound.Add("Type", new TagNodeByte((byte)c.Type)); nbtCompound.Add("Pos", new TagNodeList(TagType.TAG_INT) { new TagNodeInt((int)c.Pos.x), new TagNodeInt((int)c.Pos.y), new TagNodeInt((int)c.Pos.z) }); nbtCompound.Add("Items", c.GetNBTData()); nbtList.Add(nbtCompound); //Console.WriteLine("SAVED CONTAINER @ " + (int)c.Pos.x + "," + (int)c.Pos.y + "," + (int)c.Pos.z + " @ " + x + "," + z); } nbt.Root.Add("Containers", nbtList); try { using (MemoryStream ms = new MemoryStream()) { nbt.WriteTo(ms); byte[] bytes = ms.ToArray().Compress(CompressionLevel.BestCompression, CompressionType.GZip); using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write)) fs.Write(bytes, 0, bytes.Length); } } catch (Exception ex) { Logger.LogToFile("Error saving chunk at " + x + "," + z + "!"); Logger.LogErrorToFile(ex); } this._dirty = false; //Console.WriteLine("SAVED " + x + " " + z); } catch (Exception ex) { Logger.LogError(ex); } }
private INBTTag readTagPlod(TagNodeType type, string name) { /* There are 3 types of nodes in a NBT file, namely TAG_LIST, TAG_COMPOUND and the base TAG_TYPEs. * * The difference between the data types and TAG_LIST and TAG_COMPOUND is both TAG_LIST and TAG_COMPOUND requires * additional reading methodology to effectively read the whole file. * * First, on a TAG_LIST container type, the nodes are sequentially read WITHOUT the name tag because virtually, it is a * custom data type array. * * Unlike TAG_LISTs, a TAG_COMPOUND container type requires the nodes to be read again by readTagHead() for n times * (listing will only stop if it were to see a TAG_END node) because this container type contains heterogeneous * mix of primitive data types. * * Lastly, if it is a base type data node, it will be directly read by the Read(BinaryReader, TagNodeType) method. * * In a nutshell, this method will read the value (payload) of a node depending on the type of the node. */ // check the tag type of the node switch (type) { // type is a TAG_LIST case TagNodeType.TAG_LIST: { // get the common TAG_TYPE of the list byte _tagType = NBTReader.Read(this._bRead, TagNodeType.TAG_BYTE); // then get the total number of items stored in that list int _tagCout = NBTReader.Read(this._bRead, TagNodeType.TAG_INT); // after getting those values, create a TagNodeList (basically a List) that will // hold the succeeding tag values. TagNodeList _assetsList = new TagNodeList(name, (TagNodeType)_tagType); // loop-it according to the total count of the list for (int i = 0; i < _tagCout; i++) { // read the data then immediately add it on the list _assetsList.Add((INBTTag)readTagPlod((TagNodeType)_tagType, "")); } // finally, return _assetsList to the parent method return _assetsList; } // type is a TAG_COMPOUND case TagNodeType.TAG_COMPOUND: { // create a TagNodeList (basically a Dictionary) that will hold the succeeding tag values. TagNodeListNamed _assetsMaps = new TagNodeListNamed(name); // yes, this is an intentional infinite loop >:) do { // read a tag node INBTTag _nodeMap = readTagHead(); // if tag node is not TAG_END, meaning there is more to add if (_nodeMap.Type != TagNodeType.TAG_END) { // add the _nodeMap into the list _assetsMaps.Add(_nodeMap.Name, _nodeMap); } // otherwise else { // break the loop *\o/* break; } } while (true); // return the list containing the newly read nodes to the parent method return _assetsMaps; } // tag is a primitive data type default: { // read the node according to the type of the node (the method Read() will handle the payload processing) return new TagNode(type, name, Read(this._bRead, type)); } } }
private void BuildNBTTree() { int elements2 = XDIM * ZDIM; _sections = new AnvilSection[16]; TagNodeList sections = new TagNodeList(TagType.TAG_COMPOUND); for (int i = 0; i < _sections.Length; i++) { _sections[i] = new AnvilSection(i); sections.Add(_sections[i].BuildTree()); } FusedDataArray3[] blocksBA = new FusedDataArray3[_sections.Length]; YZXNibbleArray[] dataBA = new YZXNibbleArray[_sections.Length]; YZXNibbleArray[] skyLightBA = new YZXNibbleArray[_sections.Length]; YZXNibbleArray[] blockLightBA = new YZXNibbleArray[_sections.Length]; for (int i = 0; i < _sections.Length; i++) { blocksBA[i] = new FusedDataArray3(_sections[i].AddBlocks, _sections[i].Blocks); dataBA[i] = _sections[i].Data; skyLightBA[i] = _sections[i].SkyLight; blockLightBA[i] = _sections[i].BlockLight; } _blocks = new CompositeDataArray3(blocksBA); _data = new CompositeDataArray3(dataBA); _skyLight = new CompositeDataArray3(skyLightBA); _blockLight = new CompositeDataArray3(blockLightBA); TagNodeIntArray heightMap = new TagNodeIntArray(new int[elements2]); _heightMap = new ZXIntArray(XDIM, ZDIM, heightMap); TagNodeByteArray biomes = new TagNodeByteArray(new byte[elements2]); _biomes = new ZXByteArray(XDIM, ZDIM, biomes); for (int x = 0; x < XDIM; x++) { for (int z = 0; z < ZDIM; z++) { _biomes[x, z] = BiomeType.Default; } } _entities = new TagNodeList(TagType.TAG_COMPOUND); _tileEntities = new TagNodeList(TagType.TAG_COMPOUND); _tileTicks = new TagNodeList(TagType.TAG_COMPOUND); TagNodeCompound level = new TagNodeCompound(); level.Add("Sections", sections); level.Add("HeightMap", heightMap); level.Add("Biomes", biomes); level.Add("Entities", _entities); level.Add("TileEntities", _tileEntities); level.Add("TileTicks", _tileTicks); level.Add("LastUpdate", new TagNodeLong(Timestamp())); level.Add("xPos", new TagNodeInt(_cx)); level.Add("zPos", new TagNodeInt(_cz)); level.Add("TerrainPopulated", new TagNodeByte()); _tree = new NbtTree(); _tree.Root.Add("Level", level); _blockManager = new AlphaBlockCollection(_blocks, _data, _blockLight, _skyLight, _heightMap, _tileEntities); _entityManager = new EntityCollection(_entities); }
public void Save(World w) { try { string path = CreatePath(w, x, z, true); string file = CreatePath(w, x, z); if (!Directory.Exists(path)) Directory.CreateDirectory(path); NbtTree nbt = new NbtTree(); nbt.Root.Add("Generated", new TagNodeByte((byte)(generated ? 1 : 0))); nbt.Root.Add("Populated", new TagNodeByte((byte)(populated ? 1 : 0))); nbt.Root.Add("Blocks", new TagNodeByteArray(blocks)); nbt.Root.Add("Meta", new TagNodeByteArray(meta)); nbt.Root.Add("BlockLight", new TagNodeByteArray(Light)); nbt.Root.Add("SkyLight", new TagNodeByteArray(SkyL)); nbt.Root.Add("HeightMap", new TagNodeByteArray(heightMap)); nbt.Root.Add("HeightMapPrec", new TagNodeByteArray(precipitationHeightMap.ToByteArray())); TagNodeList nbtList = new TagNodeList(TagType.TAG_COMPOUND); TagNodeCompound nbtCompound; lock (extra) foreach (KeyValuePair<int, ushort> kvp in extra) { nbtCompound = new TagNodeCompound(); nbtCompound.Add("Pos", new TagNodeInt(kvp.Key)); nbtCompound.Add("Value", new TagNodeShort((short)kvp.Value)); nbtList.Add(nbtCompound); } nbt.Root.Add("Extra", nbtList); nbtList = new TagNodeList(TagType.TAG_COMPOUND); List<Physics.Check> physChecks = w.physics.GetChunkChecks(x, z); foreach (Physics.Check check in physChecks) { nbtCompound = new TagNodeCompound(); nbtCompound.Add("Pos", new TagNodeList(TagType.TAG_INT) { new TagNodeInt(check.x), new TagNodeInt(check.y), new TagNodeInt(check.z) }); nbtCompound.Add("Meta", new TagNodeByte(check.meta)); nbtCompound.Add("Time", new TagNodeShort(check.time)); nbtList.Add(nbtCompound); } nbt.Root.Add("Physics", nbtList); nbtList = new TagNodeList(TagType.TAG_COMPOUND); List<Entity> entities = Entities; TagNodeCompound nbtCompound2; foreach (Entity e in entities) { if (e.isPlayer) continue; nbtCompound = new TagNodeCompound(); nbtCompound.Add("Motion", new TagNodeList(TagType.TAG_DOUBLE) { new TagNodeDouble(e.velocity[0]), new TagNodeDouble(e.velocity[1]), new TagNodeDouble(e.velocity[2]) }); nbtCompound.Add("Pos", new TagNodeList(TagType.TAG_DOUBLE) { new TagNodeDouble(e.pos.x), new TagNodeDouble(e.pos.y), new TagNodeDouble(e.pos.z) }); nbtCompound.Add("Rotation", new TagNodeList(TagType.TAG_FLOAT) { new TagNodeFloat(e.rot[0]), new TagNodeFloat(e.rot[1]) }); nbtCompound.Add("Type", new TagNodeByte((byte)e.Type)); nbtCompound.Add("Age", new TagNodeInt(e.age)); nbtCompound.Add("OnGround", new TagNodeByte(e.onground)); nbtCompound.Add("Health", new TagNodeShort(e.Health)); nbtCompound2 = new TagNodeCompound(); switch (e.Type) { case EntityType.AI: nbtCompound2.Add("Type", new TagNodeByte(e.ai.type)); break; case EntityType.Object: nbtCompound2.Add("Type", new TagNodeByte(e.obj.type)); break; case EntityType.Item: nbtCompound2.Add("ID", new TagNodeShort(e.I.id)); nbtCompound2.Add("Count", new TagNodeByte(e.I.count)); nbtCompound2.Add("Meta", new TagNodeShort(e.I.meta)); break; } nbtCompound.Add("Data", nbtCompound2); nbtList.Add(nbtCompound); } nbt.Root.Add("Entities", nbtList); nbtList = new TagNodeList(TagType.TAG_COMPOUND); foreach (Container c in GetContainers(w)) { nbtCompound = new TagNodeCompound(); nbtCompound.Add("Type", new TagNodeByte((byte)c.Type)); nbtCompound.Add("Pos", new TagNodeList(TagType.TAG_INT) { new TagNodeInt((int)c.Pos.x), new TagNodeInt((int)c.Pos.y), new TagNodeInt((int)c.Pos.z) }); nbtCompound.Add("Items", c.GetNBTData()); nbtList.Add(nbtCompound); //Console.WriteLine("SAVED CONTAINER @ " + (int)c.Pos.x + "," + (int)c.Pos.y + "," + (int)c.Pos.z + " @ " + x + "," + z); } nbt.Root.Add("Containers", nbtList); try { using (MemoryStream ms = new MemoryStream()) { nbt.WriteTo(ms); byte[] bytes = ms.ToArray().Compress(CompressionLevel.BestCompression, CompressionType.GZip); using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write)) fs.Write(bytes, 0, bytes.Length); } } catch (Exception ex) { Logger.LogToFile("Error saving chunk at " + x + "," + z + "!"); Logger.LogErrorToFile(ex); } this._dirty = false; //Console.WriteLine("SAVED " + x + " " + z); } catch (Exception ex) { Logger.LogError(ex); } }
/// <inheritdoc/> public TagNode BuildTree() { TagNodeCompound tree = new TagNodeCompound(); tree["id"] = new TagNodeShort(_id); tree["Count"] = new TagNodeByte(_count); tree["Damage"] = new TagNodeShort(_damage); TagNodeCompound tagtree = new TagNodeCompound(); if (_enchantments.Count > 0) { TagNodeList enchList = new TagNodeList(TagType.TAG_COMPOUND); foreach (Enchantment e in _enchantments) { enchList.Add(e.BuildTree()); } tagtree["ench"] = enchList; if (_source != null && _source.ContainsKey("tag")) { tagtree.MergeFrom(_source["tag"].ToTagCompound()); } } if (_storedEnchantments.Count > 0) { TagNodeList storedEnchList = new TagNodeList(TagType.TAG_COMPOUND); foreach (Enchantment e in _storedEnchantments) { storedEnchList.Add(e.BuildTree()); } tagtree["StoredEnchantments"] = storedEnchList; if (_source != null && _source.ContainsKey("tag")) { tagtree.MergeFrom(_source["tag"].ToTagCompound()); } } if (_name != null || _lore != null || _color != 0) { TagNodeCompound displayTag = new TagNodeCompound(); if (_color != 0) { displayTag.Add("color", new TagNodeInt(_color)); } if (_name != null) { displayTag.Add("Name", new TagNodeString(_name)); } if (_lore != null) { List <TagNode> LoreList = new List <TagNode>(); string[] lores = _lore.Split('\n'); foreach (string lore in lores) { LoreList.Add(new TagNodeString(lore)); } displayTag.Add("Lore", new TagNodeList(TagType.TAG_STRING, LoreList)); } tagtree["display"] = displayTag; } if (tagtree.Count > 0) { tree["tag"] = tagtree; } if (_source != null) { tree.MergeFrom(_source); } return(tree); }