public void NBTIOTests_WriteRawFileTest() { ListTag list = new ListTag("list", NBTTagType.INT); list.Add(new IntTag(12345)); list.Add(new IntTag(67890)); CompoundTag subTag = new CompoundTag(); subTag.PutBool("bool", true); subTag.PutByte("byte", 123); CompoundTag tag = new CompoundTag(); tag.PutBool("bool", true); tag.PutByte("byte", 123); tag.PutByteArray("byteArray", ArrayUtils.CreateArray <byte>(200)); tag.PutShort("short", 12345); tag.PutInt("int", 12345678); tag.PutIntArray("intArray", ArrayUtils.CreateArray <int>(200)); tag.PutLong("long", 123456789123456); tag.PutLongArray("longArray", ArrayUtils.CreateArray <long>(200)); tag.PutFloat("float", 12.3456f); tag.PutDouble("double", 12.3456789); tag.PutList(list); tag.PutCompound("com", subTag); NBTIO.WriteRawFile(Environment.CurrentDirectory + "\\test.nbt", tag); }
public void NBTIOTests_Test3() { this.namedTag = new CompoundTag(); this.namedTag.PutList(new ListTag("Attributes", NBTTagType.COMPOUND)); this.namedTag.PutList(new ListTag("Pos", NBTTagType.FLOAT)); this.namedTag.PutList(new ListTag("Rotation", NBTTagType.FLOAT)); this.namedTag.PutInt("PlayerGameMode", 0); this.namedTag.PutInt("PlayerLevel", 0); this.namedTag.PutFloat("PlayerLevelProgress", 0f); if (!this.namedTag.Exist("Inventory")) { ListTag initItems = new ListTag("Inventory", NBTTagType.COMPOUND); for (int i = 0; i < 36; ++i) { initItems.Add(NBTIO.WriteItem(Item.Get(0), i)); } this.namedTag.PutList(initItems); } ListTag items = this.namedTag.GetList("Inventory"); for (int i = 0; i < 36; ++i) { Item item = NBTIO.ReadItem((CompoundTag)items[i]); } NBTIO.WriteGZIPFile(Environment.CurrentDirectory + "\\test3.nbt", this.namedTag); Console.WriteLine(NBTIO.ReadGZIPFile(Environment.CurrentDirectory + "\\test3.nbt")); }
public PlayerInventory(Player player) : base(player, 36) { if (!player.NamedTag.Exist("Inventory")) { ListTag initItems = new ListTag("Inventory", NBTTagType.COMPOUND); for (int i = 0; i < this.Size; ++i) { initItems.Add(NBTIO.WriteItem(Item.Get(0, 0, 0), i)); } player.NamedTag.PutList(initItems); } ListTag items = player.NamedTag.GetList("Inventory"); for (int i = 0; i < this.Size; ++i) { Item item = NBTIO.ReadItem((CompoundTag)items[i]); this.SetItem(i, item, false); } this.PlayerCursorInventory = new PlayerCursorInventory(player); this.PlayerEnderChestInventory = new PlayerEnderChestInventory(player); this.CraftingGridInventory = new CraftingGridInventory(player); }
public ChunkData Read(int x, int z) { const int width = 32; const int depth = 32; using (FileStream regionFile = File.OpenRead(RegionPath)) { byte[] buffer = new byte[8192]; regionFile.Read(buffer, 0, 8192); int xi = (x % width); if (xi < 0) { xi += 32; } int zi = (z % depth); if (zi < 0) { zi += 32; } int tableOffset = (xi + zi * width) * 4; regionFile.Seek(tableOffset, SeekOrigin.Begin); byte[] offsetBuffer = new byte[4]; regionFile.Read(offsetBuffer, 0, 3); Array.Reverse(offsetBuffer); int offset = BitConverter.ToInt32(offsetBuffer, 0) << 4; byte[] bytes = BitConverter.GetBytes(offset >> 4); Array.Reverse(bytes); if (offset != 0 && offsetBuffer[0] != bytes[0] && offsetBuffer[1] != bytes[1] && offsetBuffer[2] != bytes[2]) { throw new FormatException(); } int length = regionFile.ReadByte(); if (offset == 0 || length == 0) { return(null); } regionFile.Seek(offset, SeekOrigin.Begin); byte[] waste = new byte[4]; regionFile.Read(waste, 0, 4); int compressionMode = regionFile.ReadByte(); if (compressionMode != 0x02) { throw new FormatException(); } CompoundTag tag = NBTIO.ReadZLIBFile(new BinaryReader(regionFile).ReadBytes((int)(regionFile.Length - regionFile.Position)), NBTEndian.BIG_ENDIAN); return(new ChunkData(RegionPosition, new Tuple <int, int>(x, z), tag)); } }
public void Save() { CompoundTag tag = new CompoundTag(); tag.PutCompound("Data", this.Data); tag = new CompoundTag().PutCompound("", tag); NBTIO.WriteGZIPFile(this.FolderPath, tag, NBTEndian.BIG_ENDIAN); }
public void Create(World world) { CompoundTag tag = new CompoundTag(""); tag.PutCompound("Data", this.CreateData(world)); NBTIO.WriteGZIPFile($"{Server.ExecutePath}\\worlds\\{world.Name}\\level.dat", tag); }
public void SpawnTo(Player player) { BlockEntityDataPacket pk = new BlockEntityDataPacket { Position = new BlockCoordinate3D((int)this.X, (int)this.Y, (int)this.Z), Namedtag = NBTIO.WriteTag(this.SpawnCompound(), NBTEndian.LITTLE_ENDIAN, true) }; player.SendPacket(pk); }
public void Save() { if (this.HasSpawned) { this.SaveNBT(); string path = $"{Server.ExecutePath}\\players\\{this.Name}.dat"; NBTIO.WriteGZIPFile(path, this.NamedTag, NBTEndian.BIG_ENDIAN); } }
public override void SaveNBT() { ListTag inventory = new ListTag("EnderChestInventory", NBTTagType.COMPOUND); for (int i = 0; i < this.Size; ++i) { inventory.Add(NBTIO.WriteItem(this.GetItem(i), i)); } this.Player.NamedTag.PutList(inventory); }
public override CompoundTag SaveNBT() { CompoundTag nbt = base.SaveNBT(); nbt.PutShort("Age", this.Age); nbt.PutShort("PickupDelay", this.PickupDelay); nbt.PutString("Owner", this.Owner); nbt.PutCompound("Item", NBTIO.WriteItem(this.Item)); return(nbt); }
protected override void EntityInit(CompoundTag nbt) { base.EntityInit(nbt); this.Age = nbt.GetShort("Age"); this.PickupDelay = nbt.GetShort("PickupDelay"); this.Owner = nbt.GetString("Owner"); this.Item = NBTIO.ReadItem(nbt.GetCompound("Item")); this.SetFlag(Entity.DATA_FLAGS, Entity.DATA_FLAG_IMMOBILE, true); }
public virtual CompoundTag SaveNBT() { CompoundTag nbt = new CompoundTag(); ListTag list = new ListTag(this.Name, NBTTagType.COMPOUND); for (int i = 0; i < this.Size; ++i) { list.Add(NBTIO.WriteItem(this.GetItem(i), i)); } nbt.PutList(list); return(nbt); }
public static Item Get(int id, int damage, int count, byte[] nbt) { Item item = Item.Get(id, damage, count); CompoundTag tag = new CompoundTag(); if (nbt != null && nbt.Length > 0) { tag = NBTIO.ReadTag(nbt); } item.NamedTag = tag; return(item); }
public BlockEntityChest(Position position, CompoundTag nbt = null) : base(position, nbt) { this.Inventory = new ChestInventory(this); if (!this.NamedTag.Exist("items")) { this.NamedTag.PutList(new ListTag("items", NBTTagType.COMPOUND)); } ListTag items = this.NamedTag.GetList("items"); for (int i = 0; i < items.Count; ++i) { this.Inventory.SetItem(i, NBTIO.ReadItem((CompoundTag)items[i])); } }
public void NBTIOTests_ReadRawFileTest() { CompoundTag tag = NBTIO.ReadRawFile(Environment.CurrentDirectory + "\\test.nbt"); tag.GetBool("bool"); tag.GetByte("byte"); tag.GetByteArray("byteArray"); tag.GetShort("short"); tag.GetInt("int"); tag.GetIntArray("intArray"); tag.GetLong("long"); tag.GetLongArray("longArray"); tag.GetFloat("float"); tag.GetDouble("double"); tag.GetList("list"); tag.GetCompound("com"); Console.WriteLine(tag); }
public void NBTIOTests_Test4() { /*this.namedTag = new CompoundTag(); * Item item = Item.Get(10); * item.SetCustomName("Test"); * this.namedTag.PutCompound("Item", NBTIO.WriteItem(item)); * * byte[] buffer = NBTIO.WriteTag(this.namedTag); * Console.WriteLine(NBTIO.ReadTag(buffer));*/ string tags = "0a0000090400656e63680a01000000020200696412000203006c766c01000000"; byte[] t = tags.Chunks(2).Select(x => Convert.ToByte(new string(x.ToArray()), 16)).ToArray(); CompoundTag com = NBTIO.ReadTag(t); Console.WriteLine(com); Console.WriteLine(""); Console.WriteLine(NBTIO.ReadTag(NBTIO.WriteTag(com))); }
public CompoundTag GetNamedTag() { if (!this.HasTags) { return(new CompoundTag()); } if (this.cachedNBT == null) { this.cachedNBT = NBTIO.ReadTag(this.tags); } if (this.cachedNBT != null) { this.cachedNBT.Name = ""; } return(this.cachedNBT); }
public virtual void LoadNBT(CompoundTag nbt) { if (!nbt.Exist(this.Name)) { ListTag list = new ListTag(this.Name, NBTTagType.COMPOUND); for (int i = 0; i < this.Size; ++i) { list.Add(NBTIO.WriteItem(Item.Get(0, 0, 0))); } nbt.PutList(list); } ListTag items = nbt.GetList(this.Name); for (int i = 0; i < this.Size; ++i) { Item item = NBTIO.ReadItem((CompoundTag)items[i]); this.SetItem(i, item, false); } }
private void LoadData() { string path = $"{Server.ExecutePath}\\players\\{this.Name}.dat"; if (!File.Exists(path)) { PlayerCreateDataEventArgs playerCreateDataEvent = new PlayerCreateDataEventArgs(this); PlayerEvents.OnPlayerCreateData(playerCreateDataEvent); this.RegisterData(); } else { this.NamedTag = NBTIO.ReadGZIPFile(path, NBTEndian.BIG_ENDIAN); } this.Inventory = new PlayerInventory(this); this.gameMode = GameModeExtention.FromIndex(this.NamedTag.GetInt("PlayerGameMode")); }
public EntityOffhandInventory(EntityLiving holder) : base(holder) { if (!holder.NamedTag.Exist("Offhand")) { ListTag newTag = new ListTag("Offhand", NBTTagType.COMPOUND); for (int i = 0; i < this.Size; ++i) { newTag.Add(NBTIO.WriteItem(Item.Get(0, 0, 0))); } holder.NamedTag.PutList(newTag); } ListTag items = holder.NamedTag.GetList("Offhand"); for (int i = 0; i < this.Size; ++i) { Item item = NBTIO.ReadItem((CompoundTag)items[i]); this.SetItem(i, item, false); } }
public ChestInventory(BlockEntityChest holder) : base(holder) { if (!holder.NamedTag.Exist("Items")) { ListTag initItems = new ListTag("Items", NBTTagType.COMPOUND); for (int i = 0; i < this.Size; ++i) { initItems.Add(NBTIO.WriteItem(Item.Get(0, 0, 0), i)); } holder.NamedTag.PutList(initItems); } ListTag items = holder.NamedTag.GetList("Items"); for (int i = 0; i < this.Size; ++i) { Item item = NBTIO.ReadItem((CompoundTag)items[i]); this.SetItem(i, item, false); } }
public EntityArmorInventory(EntityLiving entity) : base(entity) { if (!entity.NamedTag.Exist("Armor")) { ListTag newTag = new ListTag("Armor", NBTTagType.COMPOUND); for (int i = 0; i < this.Size; ++i) { newTag.Add(NBTIO.WriteItem(Item.Get(0, 0, 0), i)); } entity.NamedTag.PutList(newTag); } ListTag items = entity.NamedTag.GetList("Armor"); for (int i = 0; i < this.Size; ++i) { Item item = NBTIO.ReadItem((CompoundTag)items[i]); this.SetItem(i, item, false); } }
public CompoundTag GetOfflinePlayerData(string xuid) { string path = $"{PlayerDataPath}/{xuid}.dat"; CompoundTag nbt; if (!File.Exists(path)) { World world = World.GetMainWorld(); Position pos = world.GetWorldSpawn(); nbt = new CompoundTag() .PutLong("firstPlayed", DateTime.Now.ToBinary()) .PutLong("lastPlayed", DateTime.Now.ToBinary()) .PutList(new ListTag("Pos", NBTTagType.FLOAT) .Add(new FloatTag("", pos.X)) .Add(new FloatTag("", pos.Y)) .Add(new FloatTag("", pos.Z))) .PutList(new ListTag("Motion", NBTTagType.FLOAT) .Add(new FloatTag("", 0)) .Add(new FloatTag("", 0)) .Add(new FloatTag("", 0))) .PutList(new ListTag("Rotation", NBTTagType.FLOAT) .Add(new FloatTag("", 0)) .Add(new FloatTag("", 0))) .PutString("World", pos.World.Name) .PutInt("Dimension", DimensionIDs.OverWorld) .PutInt("PlayerGameType", Instance.ServerProperty.GameMode.GetIndex()) .PutInt("SpawnX", world.SpawnX) .PutInt("SpawnY", world.SpawnY) .PutInt("SpawnZ", world.SpawnZ) .PutInt("Score", 0); this.SaveOfflinePlayerData(xuid, nbt); } else { nbt = NBTIO.ReadGZIPFile(path, NBTEndian.BIG_ENDIAN); } return(nbt); }
public byte[] GetBytes() { using (BinaryStream stream = new BinaryStream()) { int sendChunk = 16; for (int i = 15; i >= 0; i--) { if (this.SubChunks[i].IsEnpty) { sendChunk = i; } else { break; } } stream.WriteByte((byte)sendChunk); for (int i = 0; i < sendChunk; ++i) { stream.WriteBytes(this.SubChunks[i].GetBytes()); } byte[] b1 = new byte[512]; Buffer.BlockCopy(this.HeightMap, 0, b1, 0, 512); stream.WriteBytes(b1); stream.WriteBytes(this.Biomes); stream.WriteByte(0); stream.WriteSVarInt(0); //TODO Extra Data foreach (KeyValuePair <BlockCoordinate3D, BlockEntity> blockEntity in this.BlockEntities) { BlockEntitySpawnable s = blockEntity.Value as BlockEntitySpawnable; stream.WriteBytes(NBTIO.WriteTag(s?.SpawnCompound(), NBTEndian.LITTLE_ENDIAN, true)); } return(stream.ToArray()); } }
public PlayerEnderChestInventory(Player player) : base(null) { this.Player = player; if (!player.NamedTag.Exist("EnderChestInventory")) { ListTag newTag = new ListTag("EnderChestInventory", NBTTagType.COMPOUND); for (int i = 0; i < this.Size; ++i) { newTag.Add(NBTIO.WriteItem(Item.Get(0, 0, 0))); } player.NamedTag.PutList(newTag); } ListTag items = player.NamedTag.GetList("EnderChestInventory"); for (int i = 0; i < this.Size; ++i) { Item item = NBTIO.ReadItem((CompoundTag)items[i]); this.SetItem(i, item, false); } }
public Chunk GetChunk(int chunkX, int chunkZ) { int regionX = chunkX >> 5; int regionZ = chunkZ >> 5; string key = $"{regionX}:{regionZ}"; lock (this.Files) { RegionFile file = null; if (!this.Files.ContainsKey(key)) { file = new RegionFile(this.WorldName, regionX, regionZ); this.Files.Add(key, file); Chunk chunk = new Chunk(chunkX, chunkZ); return(chunk); } else { file = this.Files[key]; if (file.IsFileCreated) { byte[] data = file.GetChunkBytes(chunkX, chunkZ); if (data == null) { Chunk chunk = new Chunk(chunkX, chunkZ); return(chunk); } return(this.ChunkFormat.NBTDeserialize(NBTIO.ReadZLIBFile(data, NBT.Data.NBTEndian.BIG_ENDIAN))); } else { Chunk chunk = new Chunk(chunkX, chunkZ); return(chunk); } } } }
public void WriteItem(Item item) { if (item == null || item.ID == 0) { this.WriteSVarInt(0); return; } this.WriteSVarInt(item.ID); int auxValue = ((item.Damage & 0x7fff) << 8) | (item.Count & 0xff); this.WriteSVarInt(auxValue); byte[] nbt = new byte[0]; if (item.NamedTag.Count != 0) { CompoundTag tag = (CompoundTag)item.NamedTag.Clone(); tag.Name = ""; nbt = NBTIO.WriteTag(tag); } this.WriteLShort((ushort)nbt.Length); this.WriteBytes(nbt); string[] canPlaceOn = item.CanPlaceOn; this.WriteSVarInt(canPlaceOn.Length); for (int i = 0; i < canPlaceOn.Length; ++i) { this.WriteString(canPlaceOn[i]); } string[] canDestroy = item.CanDestroy; this.WriteSVarInt(canDestroy.Length); for (int i = 0; i < canDestroy.Length; ++i) { this.WriteString(canDestroy[i]); } }
public Chunk GetChunk(int chunkX, int chunkZ) { int width = 32; int depth = 32; int rx = chunkX >> 5; int rz = chunkZ >> 5; Tuple <int, int> regionPos = new Tuple <int, int>(rx, rz); Tuple <int, int> chunkPos = new Tuple <int, int>(chunkX, chunkZ); if (this._chunks.ContainsKey(chunkPos)) { return(this._chunks[chunkPos]); } string filePath = Path.Combine(this.WorldPath, $@"region/r.{rx}.{rz}.mca"); if (!File.Exists(filePath)) { return(new Chunk(this.World, chunkX, chunkZ)); } if (!_files.ContainsKey(regionPos)) { this._files[regionPos] = File.OpenRead(filePath); } FileStream regionFile = this._files[regionPos]; byte[] buffer = new byte[8192]; regionFile.Read(buffer, 0, 8192); int xi = (chunkX % width); if (xi < 0) { xi += 32; } int zi = (chunkZ % depth); if (zi < 0) { zi += 32; } int tableOffset = (xi + zi * width) * 4; regionFile.Seek(tableOffset, SeekOrigin.Begin); byte[] offsetBuffer = new byte[4]; regionFile.Read(offsetBuffer, 0, 3); Array.Reverse(offsetBuffer); int offset = BitConverter.ToInt32(offsetBuffer, 0) << 4; byte[] bytes = BitConverter.GetBytes(offset >> 4); Array.Reverse(bytes); if (offset != 0 && offsetBuffer[0] != bytes[0] && offsetBuffer[1] != bytes[1] && offsetBuffer[2] != bytes[2]) { throw new FormatException(); } int length = regionFile.ReadByte(); if (offset == 0 || length == 0) { return(new Chunk(this.World, chunkX, chunkZ)); } regionFile.Seek(offset, SeekOrigin.Begin); byte[] waste = new byte[4]; regionFile.Read(waste, 0, 4); int compressionMode = regionFile.ReadByte(); if (compressionMode != 0x02) { throw new FormatException(); } CompoundTag tag = NBTIO.ReadZLIBFile(new BinaryReader(regionFile).ReadBytes((int)(regionFile.Length - regionFile.Position))); return(this.ChunkFormat.NBTDeserialize(tag)); }
public void Write(string filePath, ChunkData[] datas) { const int width = 32; const int depth = 32; File.Copy(RegionPath, filePath); using (var regionFile = File.Open(filePath, FileMode.Open)) { byte[] buffer = new byte[8192]; regionFile.Read(buffer, 0, buffer.Length); for (int i = 0; i < datas.Length; i++) { ChunkData data = datas[i]; int xi = (data.ChunkOffset.Item1 % width); if (xi < 0) { xi += 32; } int zi = (data.ChunkOffset.Item2 % depth); if (zi < 0) { zi += 32; } int tableOffset = (xi + zi * width) * 4; regionFile.Seek(tableOffset, SeekOrigin.Begin); byte[] offsetBuffer = new byte[4]; regionFile.Read(offsetBuffer, 0, 3); Array.Reverse(offsetBuffer); int offset = BitConverter.ToInt32(offsetBuffer, 0) << 4; byte sectorCount = (byte)regionFile.ReadByte(); byte[] nbtBuf = NBTIO.WriteZLIBFile(new CompoundTag().PutCompound("", data.Data), NBTEndian.BIG_ENDIAN); int nbtLength = nbtBuf.Length; byte nbtSectorCount = (byte)Math.Ceiling(nbtLength / 4096d); if (offset == 0 || sectorCount == 0 || nbtSectorCount > sectorCount) { regionFile.Seek(0, SeekOrigin.End); offset = (int)((int)regionFile.Position & 0xfffffff0); regionFile.Seek(tableOffset, SeekOrigin.Begin); byte[] bytes = BitConverter.GetBytes(offset >> 4); Array.Reverse(bytes); regionFile.Write(bytes, 0, 3); regionFile.WriteByte(nbtSectorCount); } byte[] lenghtBytes = BitConverter.GetBytes(nbtLength + 1); Array.Reverse(lenghtBytes); regionFile.Seek(offset, SeekOrigin.Begin); regionFile.Write(lenghtBytes, 0, 4); regionFile.WriteByte(0x02); regionFile.Write(nbtBuf, 0, nbtBuf.Length); int reminder; Math.DivRem(nbtLength + 4, 4096, out reminder); byte[] padding = new byte[4096 - reminder]; if (padding.Length > 0) { regionFile.Write(padding, 0, padding.Length); } } } }
public void Load(World world) { CompoundTag tag = NBTIO.ReadGZIPFile($"{Server.ExecutePath}\\worlds\\{world.Name}\\level.dat"); CompoundTag data = tag.GetCompound("Data"); }