public override int TryReadPacket(byte[] buffer, int length) { int offset = 1; int x, z; short y; short dataLength; byte[] data; if (!DataUtility.TryReadInt32(buffer, ref offset, length, out x)) return -1; if (!DataUtility.TryReadInt16(buffer, ref offset, length, out y)) return -1; if (!DataUtility.TryReadInt32(buffer, ref offset, length, out z)) return -1; if (!DataUtility.TryReadByte(buffer, ref offset, length, out Action)) return -1; if (!DataUtility.TryReadInt16(buffer, ref offset, length, out dataLength)) return -1; if (dataLength != 0) { if (!DataUtility.TryReadArray(buffer, ref offset, length, out data, dataLength)) return -1; Data = new NbtFile(); Data.LoadFile(new MemoryStream(data), true); } Position = new Vector3(x, y, z); return offset; }
public static string Get(string world_file, bool realspawn) { try { byte[] XOR_KEY = { 108, 111, 108, 32, 101, 97, 115, 116, 101, 114, 32, 101, 103, 103 }; NbtFile nbt = new NbtFile(world_file, true); nbt.LoadFile(); byte[] randSeed = BitConverter.GetBytes(nbt.RootTag.Tags[0].Query<NbtLong>("/Data/RandomSeed").Value); byte[] time = BitConverter.GetBytes(nbt.RootTag.Tags[0].Query<NbtLong>("/Data/Time").Value); byte[] playerX, playerY, playerZ, rotationX, rotationY; if (!realspawn) { playerX = BitConverter.GetBytes((int)Math.Floor(nbt.RootTag.Tags[0].Query<NbtDouble>("/Data/Player/Pos/0").Value)); playerY = BitConverter.GetBytes((int)Math.Floor(nbt.RootTag.Tags[0].Query<NbtDouble>("/Data/Player/Pos/1").Value)); playerZ = BitConverter.GetBytes((int)Math.Floor(nbt.RootTag.Tags[0].Query<NbtDouble>("/Data/Player/Pos/2").Value)); rotationX = BitConverter.GetBytes(nbt.RootTag.Tags[0].Query<NbtFloat>("/Data/Player/Rotation/0").Value); rotationY = BitConverter.GetBytes(nbt.RootTag.Tags[0].Query<NbtFloat>("/Data/Player/Rotation/1").Value); } else { playerX = BitConverter.GetBytes(nbt.RootTag.Tags[0].Query<NbtInt>("/Data/SpawnX").Value); playerY = BitConverter.GetBytes(nbt.RootTag.Tags[0].Query<NbtInt>("/Data/SpawnY").Value + 2); playerZ = BitConverter.GetBytes(nbt.RootTag.Tags[0].Query<NbtInt>("/Data/SpawnZ").Value); rotationX = BitConverter.GetBytes(0f); rotationY = BitConverter.GetBytes(0f); } uint size = (sizeof(long) * 2) + (sizeof(int) * 3) + (sizeof(float) * 2); byte[] data = new byte[size]; int end = 0; Array.Copy(randSeed, 0, data, end, randSeed.Length); end += randSeed.Length; Array.Copy(time, 0, data, end, time.Length); end += time.Length; Array.Copy(playerX, 0, data, end, playerX.Length); end += playerX.Length; Array.Copy(playerY, 0, data, end, playerY.Length); end += playerY.Length; Array.Copy(playerZ, 0, data, end, playerZ.Length); end += playerZ.Length; Array.Copy(rotationX, 0, data, end, rotationX.Length); end += rotationX.Length; Array.Copy(rotationY, 0, data, end, rotationY.Length); for (int i = 0; i < data.Length; i++) { int mod = i % XOR_KEY.Length; data[i] = (byte)((int)data[i] ^ (int)XOR_KEY[mod]); } return "[" + Convert.ToBase64String(data) + "]"; } catch (Exception ex) { return "ERROR: " + ex.Message; } }
private void Form1_Load(object sender, EventArgs e) { NbtFile test = new NbtFile("C:\\Users\\Paul Bruner\\Desktop\\Ol Drive\\alu-new-sixteen-bits.schematic",true); test.LoadFile(); redBmp = new redstoneBmp(); }
private void UpdatePosition() { NbtFile PlayerDat = new NbtFile(this.Server.ServerDirectory + "\\world\\players\\" + this.Username + ".dat"); PlayerDat.LoadFile(); this._position.x = PlayerDat.Query<LibNbt.Tags.NbtDouble>("//Pos/0").Value; this._position.y = PlayerDat.Query<LibNbt.Tags.NbtDouble>("//Pos/1").Value; this._position.z = PlayerDat.Query<LibNbt.Tags.NbtDouble>("//Pos/2").Value; PlayerDat.Dispose(); }
public static void test(Player player, Command cmd) { if (!File.Exists("C:/users/jb509/desktop/1.schematic")) { player.Message("Nop"); return; } NbtFile file = new NbtFile("C:/users/jb509/desktop/1.schematic"); file.RootTag = new NbtCompound("Schematic"); file.LoadFile(); bool notClassic = false; short width = file.RootTag.Query<NbtShort>("/Schematic/Width").Value; short height = file.RootTag.Query<NbtShort>("/Schematic/Height").Value; short length = file.RootTag.Query<NbtShort>("/Schematic/Length").Value; Byte[] blocks = file.RootTag.Query<NbtByteArray>("/Schematic/Blocks").Value; Vector3I pos = player.Position.ToBlockCoords(); int i = 0; player.Message("&SDrawing Schematic ({0}x{1}x{2})", length, width, height); for (int x = pos.X; x < width + pos.X; x++) { for (int y = pos.Y; y < length + pos.Y; y++) { for (int z = pos.Z; z < height + pos.Z; z++) { if (Enum.Parse(typeof(Block), ((Block)blocks[i]).ToString(), true) != null) { if (!notClassic && blocks[i] > 49) { notClassic = true; player.Message("&WSchematic used is not designed for Minecraft Classic;" + " Converting all unsupported blocks with air"); } if (blocks[i] < 50) { player.WorldMap.QueueUpdate(new BlockUpdate(null, (short)x, (short)y, (short)z, (Block)blocks[i])); } } i++; } } } file.Dispose(); }
private void LoadFromFile(string directory) { NbtFile file = new NbtFile(); using (var stream = File.Open(Path.Combine(LevelDirectory, "level.dat"), FileMode.Open)) file.LoadFile(stream, true); var data = file.RootTag.Get<NbtCompound>("Data"); var serializer = new NbtSerializer(typeof(SavedLevel)); SavedLevel level = (SavedLevel)serializer.Deserialize(data); Name = level.LevelName; Time = level.Time; GameMode = (GameMode)level.GameMode; MapFeatures = level.MapFeatures; Seed = level.Seed; // Find world generator string generatorName = level.GeneratorName; WorldGenerator = GetGenerator(generatorName); WorldGenerator.Seed = Seed; GeneratorOptions = level.GeneratorOptions; WorldGenerator.Initialize(this); SpawnPoint = level.SpawnPoint; World = new World(this, WorldGenerator, Path.Combine(directory, "region")); }
protected bool OpenChunk(int x, int y, out NbtFile file) { var realPath = GetChunkPath(x, y); if (!File.Exists(realPath)) { file = null; return false; } var nbt = new NbtFile(); nbt.LoadFile(realPath, true); file = nbt; return true; }
private void button8_Click_1(object sender, EventArgs e) { NbtFile PlayerDat = new NbtFile(YAMS.Core.RootFolder + "\\servers\\1\\world\\players\\bigolslabomeat.dat"); PlayerDat.LoadFile(); Vector _position; _position.x = PlayerDat.Query<LibNbt.Tags.NbtDouble>("//Pos/0").Value; _position.y = PlayerDat.Query<LibNbt.Tags.NbtDouble>("//Pos/1").Value; _position.z = PlayerDat.Query<LibNbt.Tags.NbtDouble>("//Pos/2").Value; PlayerDat.Dispose(); MessageBox.Show("x:" + _position.x.ToString() + " y:" + _position.y.ToString() + " z:" + _position.z.ToString()); }
public void RemoveTileEntity(TileEntity e) { long CX = e.Pos.X / 16; long CY = e.Pos.Y / 16; string f = GetChunkFilename((int)CX, (int)CY); try { mChunk = new NbtFile(f); mChunk.LoadFile(); NbtCompound level = (NbtCompound)mChunk.RootTag["Level"]; NbtList TileEntities = (NbtList)level["TileEntities"]; int found = -1; for (int i = 0; i < TileEntities.Tags.Count; i++) { TileEntity te = new TileEntity((NbtCompound)TileEntities[i]); if (te.Pos == e.Pos) { found = i; } } if (found > -1) TileEntities.Tags.RemoveAt(found); level["TileEntities"] = TileEntities; mChunk.RootTag["Level"] = level; mChunk.SaveFile(f); } catch (Exception) { } }
public void SetTileEntity(TileEntity e) { long CX=e.Pos.X/16; long CY=e.Pos.Y/16; string f = GetChunkFilename((int)CX, (int)CY); try { mChunk = new NbtFile(f); mChunk.LoadFile(); NbtCompound level = (NbtCompound)mChunk.RootTag["Level"]; NbtList tents = (NbtList)level["TileEntities"]; int found = -1; for(int i = 0;i<tents.Tags.Count;i++) { TileEntity te = new TileEntity((NbtCompound)tents[i]); if (te.Pos == e.Pos) { found = i; } } if (found > -1) tents[found] = e.ToNBT(); else tents.Tags.Add(e.ToNBT()); level["TileEntities"] = tents; mChunk.RootTag["Level"] = level; mChunk.SaveFile(f); } catch (Exception) { } }
private Chunk _LoadChunk(int x, int z) { byte[] CurrentBlocks; /* if (CurrentBlock.X == x && CurrentBlock.Z == z) { Console.WriteLine("Already loaded...!"); return null; } */ //x = (x < 0) ? x - 2 : x; //z = (z < 0) ? z - 2 : z; CurrentBlock.X = x; CurrentBlock.Z = z; string derp; Chunk c = new Chunk(this); c.Loading = true; c.Filename = GetChunkFilename(x, z); c.CreationDate = File.GetCreationTime(c.Filename); c.Creator = "?"; c.Size = ChunkScale; if (!File.Exists(c.Filename)) { Console.WriteLine("! {0}", c.Filename); return null; } //try { chunk = new NbtFile(c.Filename); chunk.LoadFile(); NbtCompound level = (NbtCompound)chunk.RootTag["Level"]; c.Position = new Vector3i( level.Get<NbtInt>("xPos").Value, level.Get<NbtInt>("zPos").Value, 0); if ((int)c.Position.X != x || (int)c.Position.Y != z) { throw new Exception(string.Format("Chunk pos is wrong. {0}!={1}", c.Filename, c.Position)); } NbtList TileEntities = (NbtList)level["TileEntities"]; if (TileEntities.Tags.Count > 0) { //Console.WriteLine("*** Found TileEntities."); LoadTileEnts(ref c, (int)x, (int)z, TileEntities); } NbtList Entities = (NbtList)level["Entities"]; if (Entities.Tags.Count > 0) { //Console.WriteLine("*** Found Entities."); LoadEnts(ref c, (int)x, (int)z, Entities); } // Blocks c.Blocks = DecompressBlocks(level.Get<NbtByteArray>("Blocks").Value); c.BlockLight = DecompressLightmap(level.Get<NbtByteArray>("BlockLight").Value); c.SkyLight = DecompressLightmap(level.Get<NbtByteArray>("SkyLight").Value); c.Data = DecompressDatamap(level.Get<NbtByteArray>("Data").Value); c.Loading = false; c.UpdateOverview(); string ci = string.Format("{0},{1}", x, z); if (Chunks.ContainsKey(ci)) return Chunks[ci]; Chunks.Add(ci, c); /* @TODO: Make Pig spawner converter. for (int Z = 0; Z < ChunkScale.X; Z++) { for (int Y = 0; Y < ChunkScale.Y; Y++) { for (int X = 0; X < ChunkScale.X; X++) { long index = X + (Z * ChunkY + Y) * ChunkZ; byte b = CurrentChunks[index]; if (b == Blocks.Find("Mob spawner").ID) { MobSpawner ms = new MobSpawner(X + (int)(x * ChunkScale.X), Y + (int)(z * ChunkScale.Y), Z, "Pig", 20); ms.id = "MobSpawner"; ms.UUID = Guid.NewGuid(); _TileEntities.Add(ms.UUID, ms); c++; } } } } */ //if (c>0) Console.WriteLine("*** {0} spawners found.", c); //Console.WriteLine("Loaded {0} bytes from chunk {1}.", CurrentChunks.Length, c.Filename); return c; } /*catch (Exception e) { string err = string.Format(" *** ERROR: Chunk {0},{1} ({2}) failed to load:\n\n{3}", x, z, c.Filename, e); Console.WriteLine(err); if (CorruptChunk != null) CorruptChunk(err, c.Filename); return null; }*/ }
public void ForEachChunk(Chunk.ChunkModifierDelegate cmd) { string[] f = Directory.GetFiles(mFolder,"c*.*.dat",SearchOption.AllDirectories); int Complete=0; foreach (string file in f) { if (ForEachProgress != null) ForEachProgress(f.Length, Complete++); //Console.WriteLine(Path.GetExtension(file)); if (Path.GetExtension(file) == "dat") continue; if (file.EndsWith(".genlock")) continue; NbtFile nf = new NbtFile(file); try { nf.LoadFile(); NbtCompound Level = (NbtCompound)nf.RootTag["Level"]; long x = (Level["xPos"] as NbtInt).Value; long y = (Level["zPos"] as NbtInt).Value; nf.Dispose(); cmd(x, y); } catch (Exception e) { if (CorruptChunk != null) { Vector2i pos = GetChunkCoordsFromFile(file); CorruptChunk(pos.X,pos.Y,"[" + Complete.ToString() + "]" + e.ToString(), file); } // continue; } } // This MUST be done. ForEachProgress = null; }
public void TEST002_SaveMap() { if (Directory.Exists("002")) Directory.Delete("002", true); Directory.CreateDirectory("002"); // Saves map here. InfdevHandler mh = new InfdevHandler(); mh.Save("002/level.dat"); mh.SetDimension(0); Chunk cnkA = mh.NewChunk(0, 0); cnkA.Blocks[0, 0, 0] = 0x01; cnkA.Blocks[0, 0, 1] = 0x02; cnkA.Blocks[0, 0, 2] = 0x03; cnkA.Blocks[0, 0, 3] = 0x04; cnkA.Save(); FileInfo fA = new FileInfo(cnkA.Filename); Assert.Greater(fA.Length, 0, "System writing zero-length chunks."); Chunk cnkB = mh.GetChunk(0, 0); Assert.AreEqual(cnkB.Blocks[0, 0, 0], 0x01); Assert.AreEqual(cnkB.Blocks[0, 0, 1], 0x02); Assert.AreEqual(cnkB.Blocks[0, 0, 2], 0x03); Assert.AreEqual(cnkB.Blocks[0, 0, 3], 0x04); Console.WriteLine("Opening {0}...", cnkB.Filename); NbtFile chunkFile = new NbtFile(cnkB.Filename); chunkFile.LoadFile(); NbtCompound level = chunkFile.RootTag.Get<NbtCompound>("Level"); // Listed from a Minecraft-generated chunk. string[] reqdEntries = new string[]{ "xPos", "zPos", "LastUpdate", "Blocks", "Data", "SkyLight", "BlockLight", "HeightMap", "TerrainPopulated", "Entities", "TileEntities" }; foreach (string entry in reqdEntries) { if (level.Get(entry)==null) { Console.WriteLine(chunkFile.RootTag.ToString()); Assert.Fail(string.Format("\"{0}\" is not included", entry)); } else { Console.WriteLine(" * Has {0}...", entry); } } Directory.Delete("002", true); }
public PlayerEntity LoadPlayer(string name) { PlayerEntity entity = new PlayerEntity(); if (!File.Exists(Path.Combine(LevelDirectory, "players", name + ".dat"))) { // Return default player entity entity.Position = SpawnPoint; entity.SpawnPoint = SpawnPoint; entity.Position += new Vector3(0, PlayerEntity.Height, 0); entity.GameMode = GameMode; return entity; } NbtFile file = new NbtFile(); using (Stream stream = File.Open(Path.Combine(LevelDirectory, "players", name + ".dat"), FileMode.Open)) file.LoadFile(stream, true); var data = file.RootTag; entity.OnGround = data.Get<NbtByte>("OnGround").Value == 1; entity.Air = data.Get<NbtShort>("Air").Value; entity.Health = data.Get<NbtShort>("Health").Value; Dimension dimension = (Dimension)data.Get<NbtInt>("Dimension").Value; // TODO entity.Food = (short)data.Get<NbtInt>("foodLevel").Value; entity.XpLevel = data.Get<NbtInt>("XpLevel").Value; entity.XpTotal = data.Get<NbtInt>("XpTotal").Value; // TODO: Set velocity based on fall distance entity.FoodExhaustion = data.Get<NbtFloat>("foodExhaustionLevel").Value; entity.FoodSaturation = data.Get<NbtFloat>("foodSaturationLevel").Value; entity.XpProgress = data.Get<NbtFloat>("XpP").Value; var equipment = data.Get<NbtList>("Equipment"); var inventory = data.Get<NbtList>("Inventory"); var motion = data.Get<NbtList>("Motion"); var pos = data.Get<NbtList>("Pos"); var rotation = data.Get<NbtList>("Rotation"); var abilities = data.Get<NbtCompound>("abilities"); // Appears to be unused, is overriden by the inventory contents // foreach (var item in equipment.Tags) foreach (var item in inventory.Tags) { var slot = Slot.FromNbt((NbtCompound)item); slot.Index = DataSlotToNetworkSlot(slot.Index); entity.Inventory[slot.Index] = slot; } entity.Velocity = new Vector3( ((NbtDouble)motion.Tags[0]).Value, ((NbtDouble)motion.Tags[1]).Value, ((NbtDouble)motion.Tags[2]).Value); entity.Position = new Vector3( ((NbtDouble)pos.Tags[0]).Value, ((NbtDouble)pos.Tags[1]).Value, ((NbtDouble)pos.Tags[2]).Value); if (data.Get<NbtInt>("SpawnX") != null) { entity.SpawnPoint = new Vector3( data.Get<NbtInt>("SpawnX").Value, data.Get<NbtInt>("SpawnY").Value, data.Get<NbtInt>("SpawnZ").Value); } entity.Yaw = ((NbtFloat)rotation.Tags[0]).Value; entity.Pitch = ((NbtFloat)rotation.Tags[1]).Value; // TODO: Abilities return entity; }
public NbtHolder(string filename) { this.path = filename; File = new NbtFile(filename); File.LoadFile(); }
private void LoadFromFile(string directory) { NbtFile file = new NbtFile(); using (var stream = File.Open(Path.Combine(LevelDirectory, "level.dat"), FileMode.Open)) file.LoadFile(stream, true); // TODO: Gracefully handle missing tags var data = file.RootTag.Get<NbtCompound>("Data"); Name = data.Get<NbtString>("LevelName").Value; Time = data.Get<NbtLong>("Time").Value; GameMode = (GameMode)data.Get<NbtInt>("GameType").Value; MapFeatures = data.Get<NbtByte>("MapFeatures").Value == 1; Seed = data.Get<NbtLong>("RandomSeed").Value; // Find world generator string generatorName = data.Get<NbtString>("generatorName").Value; WorldGenerator = GetGenerator(generatorName); WorldGenerator.Seed = Seed; int x, y, z; x = data.Get<NbtInt>("SpawnX").Value; y = data.Get<NbtInt>("SpawnY").Value; z = data.Get<NbtInt>("SpawnZ").Value; SpawnPoint = new Vector3(x, y, z); World = new World(WorldGenerator, Path.Combine(directory, "region")); }
internal override Vector2i _GetChunkCoordsFromFile(string file) { Vector2i r = new Vector2i(0, 0); NbtFile f = new NbtFile(file); try { f.LoadFile(); } catch (Exception e) { if (CorruptChunk != null) CorruptChunk(0,0,e.ToString(), file); return null; } NbtCompound Level = (NbtCompound)f.RootTag["Level"]; Vector2i t = new Vector2i(0, 0); r.X = (Level["xPos"] as NbtInt).Value; r.Y = (Level["zPos"] as NbtInt).Value; f.Dispose(); return r; }
public void RemoveEntity(Entity e) { Guid ID = e.UUID; if (_Entities.ContainsKey(ID)) _Entities.Remove(ID); int CX = (int)e.Pos.X / 16; int CY = (int)e.Pos.Y / 16; e.Pos.X = (int)e.Pos.X % 16; e.Pos.Y = (int)e.Pos.Y % 16; string f = GetChunkFilename(CX, CY); if (!File.Exists(f)) { Console.WriteLine("! {0}", f); return; } try { chunk = new NbtFile(f); chunk.LoadFile(); NbtCompound level = (NbtCompound)chunk.RootTag["Level"]; NbtList ents = (NbtList)level["Entities"]; if (e.OrigPos != null) { NbtCompound dc = null; foreach (NbtCompound c in ents.Tags) { Entity ent = new Entity(c); if (e.Pos == ent.Pos) { dc = c; break; } } if (dc != null) ents.Tags.Remove(dc); } level["Entities"] = ents; chunk.RootTag["Level"] = level; chunk.SaveFile(f); } catch (Exception) { } }
public static Blocks Load(string filename) { NbtFile f = new NbtFile(); f.LoadFile(filename); NbtCompound root = f.RootTag; NbtTag nBlocks = root["Blocks"]; NbtTag nData = root["Data"]; NbtTag nWidth = root["Width"]; NbtTag nLength = root["Length"]; NbtTag nHeight = root["Height"]; byte[] blocks = ((NbtByteArray)nBlocks).Value; byte[] extra = ((NbtByteArray)nData).Value; int X = (int)((NbtShort)nWidth).Value; int Y = (int)((NbtShort)nLength).Value; int Z = (int)((NbtShort)nHeight).Value; Blocks b = new Blocks(X, Y, Z); //bool sch = filename.EndsWith(".schematic"); for (int i = 0; i < blocks.Length; i++) switch (blocks[i]) { case 0: case 6: case 37: case 38: case 39: case 40: case 51: case 59: case 63: case 65: case 66: case 68: case 78: case 83: b[i] = Block.AIR; break; case 55: b[i] = Block.WIRE; break; case 75: // Off b[i] = new Block(BlockType.TORCH, PlaceTorch(extra[i]), 0, 0, 0); break; case 76: // Off b[i] = new Block(BlockType.TORCH, PlaceTorch(extra[i]), 16, 0, 0); // System.Console.WriteLine("Loc: {2} Torch Dir: {0} Byte: {1}", b[i].Place.ToString(), extra[i] & 0x7,i); break; case 69: b[i] = new Block(BlockType.LEVER, PlaceTorch(extra[i]),(extra[i] & 0x8) == 1 ? 16 : 0, 0, 0); // System.Console.WriteLine("Lever Dir: {0} Byte: {1}", b[i].Place.ToString(), extra[i] & 0x7); break; case 70: case 72: b[i] = Block.PREASUREPAD; break; case 77: b[i] = Block.BUTTON; b[i].Place = PlaceButton(extra[i]); break; case 64: // doors not working yet case 71: break; case 93: // skip repeaters for now break; default: b[i] = Block.BLOCK; break; } return b; }
/// <summary> /// Retrieves the requested chunk from the region, or /// generates it if a world generator is provided. /// </summary> /// <param name="position">The position of the requested local chunk coordinates.</param> public Chunk GetChunk(Vector3 position) { // TODO: This could use some refactoring lock (Chunks) { if (!Chunks.ContainsKey(position)) { if (regionFile != null) { // Search the stream for that region lock (regionFile) { var chunkData = GetChunkFromTable(position); if (chunkData == null) { if (WorldGenerator == null) throw new ArgumentException("The requested chunk is not loaded.", "position"); Chunks.Add(position, WorldGenerator.GenerateChunk(position, this)); return Chunks[position]; } regionFile.Seek(chunkData.Item1, SeekOrigin.Begin); int length = DataUtility.ReadInt32(regionFile); int compressionMode = regionFile.ReadByte(); switch (compressionMode) { case 1: // gzip break; case 2: // zlib byte[] compressed = new byte[length]; regionFile.Read(compressed, 0, compressed.Length); byte[] uncompressed = ZlibStream.UncompressBuffer(compressed); MemoryStream memoryStream = new MemoryStream(uncompressed); NbtFile nbt = new NbtFile(); nbt.LoadFile(memoryStream, false); var chunk = Chunk.FromNbt(position, nbt); chunk.ParentRegion = this; Chunks.Add(position, chunk); break; default: throw new InvalidDataException("Invalid compression scheme provided by region file."); } } } else if (WorldGenerator == null) throw new ArgumentException("The requested chunk is not loaded.", "position"); else Chunks.Add(position, WorldGenerator.GenerateChunk(position, this)); } return Chunks[position]; } }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog.Filter = "Schematic Files .schematic | *.schematic"; openFileDialog.Title = "Select a Schematic"; openFileDialog.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal); if (openFileDialog.ShowDialog() == DialogResult.OK) { NbtFile test = new NbtFile(openFileDialog.FileName, true); test.LoadFile(); AssertNbtBigFile(test); paintIt = true; this.Width = rXmax * 20; this.Height = rYmax * 20; this.Refresh(); Console.WriteLine("CLICK!"); } }
public SlotData ReadSlotData() { var slotData = new SlotData(ReadInt16()); if (slotData.ItemID == -1) return null; slotData.ItemCount = ReadByte(); slotData.ItemDamage = ReadInt16(); var size = ReadInt16(); if (size == -1) return slotData; var file = new NbtFile(); file.LoadFile(_stream, true); var list = file.Query<NbtCompound>("").Query<NbtList>("ench"); slotData.ItemEnchantments = list; return slotData; }
public Vector2i GetChunkCoordsFromFile(string file,bool test) { Vector2i r = new Vector2i(0, 0); // cX.Y.dat // X.Y.dat string[] peices =Path.GetFileName(file.Substring(1)).Split('.'); long X; long Y; Radix.Decode(peices[0],36,out X); Radix.Decode(peices[1],36,out Y); r.X = (int)X; r.Y = (int)Y; if (test) { NbtFile f = new NbtFile(file); try { f.LoadFile(); } catch (Exception e) { if (CorruptChunk != null) CorruptChunk(X,Y,e.ToString(), file); return null; } NbtCompound Level = (NbtCompound)f.RootTag["Level"]; Vector2i t = new Vector2i(0, 0); t.X = (Level["xPos"] as NbtInt).Value; t.Y = (Level["zPos"] as NbtInt).Value; f.Dispose(); if (t != r) throw new Exception("Test failed."); } return r; }
public void SaveChunk(Chunk cnk) { NbtFile c = new NbtFile(cnk.Filename); c.LoadFile(); //Console.WriteLine("Saving "+f); NbtCompound Level = (NbtCompound)c.RootTag["Level"]; //string ci = string.Format("{0},{1}", x, z); Level.Tags.Remove(Level["Blocks"]); // BLOCKS ///////////////////////////////////////////////////// byte[] blocks = new byte[ChunkX * ChunkY * ChunkZ]; for (int X = 0; X < ChunkX; X++) { for (int Y = 0; Y < ChunkY; Y++) { for (int Z = 0; Z < ChunkZ; Z++) { blocks[GetBlockIndex(X, Y, Z)] = cnk.Blocks[X, Y, Z]; } } } Level.Tags.Add(new NbtByteArray("Blocks", blocks)); blocks = null; // LIGHTING /////////////////////////////////////////////////// // TODO: Whatever is going on in here is crashing Minecraft now. byte[] lighting = CompressLightmap(cnk.SkyLight); Level.Tags.Add(new NbtByteArray("SkyLight", lighting)); lighting = CompressLightmap(cnk.BlockLight); Level.Tags.Add(new NbtByteArray("BlockLight", lighting)); lighting = CompressDatamap(cnk.Data); Level.Tags.Add(new NbtByteArray("Data", lighting)); // HEIGHTMAP (Needed for lighting). byte[] hm = new byte[256]; for (int x = 0; x < 16; x++) { for (int y = 0; y < 16; y++) { hm[y + (x * 16)] = (byte)cnk.HeightMap[x, y]; } } NbtList ents = new NbtList("Entities"); // ENTITIES /////////////////////////////////////////////////// foreach (KeyValuePair<Guid, Entity> ent in cnk.Entities) { ents.Tags.Add(ent.Value.ToNBT()); } Level.Tags.Add(ents); NbtList tents = new NbtList("TileEntities"); // TILE ENTITIES ////////////////////////////////////////////// foreach (KeyValuePair<Guid, TileEntity> tent in cnk.TileEntities) { ents.Tags.Add(tent.Value.ToNBT()); } Level.Tags.Add(tents); c.RootTag["Level"] = Level; //try //{ c.SaveFile(cnk.Filename); //} //catch (Exception e) //{ // Console.WriteLine(e); //} }
public static void Init() { if (File.Exists(".luf")) { foreach (string f in File.ReadAllLines(".luf")) { if(File.Exists(f)) LastUsedFiles.Push(f); } } if (File.Exists(".settings")) { try { NbtFile f = new NbtFile(".settings"); f.LoadFile(); ShowGridLines = f.RootTag.Get<NbtByte>("GridLines").Value == 0x01 ? true : false; ShowChunks = f.RootTag.Get<NbtByte>("ShowChunks").Value == 0x01 ? true : false; ShowMapIcons = f.RootTag.Get<NbtByte>("ShowMapIcons").Value == 0x01 ? true : false; ShowWaterDepth = f.RootTag.Get<NbtByte>("ShowWaterDepth").Value == 0x01 ? true : false; f.Dispose(); } catch (Exception) { Save(); } } }
public void Load(string filename) { try { mChunks.Clear(); //CurrentBlock = new Vector3i(0, 0, 0); Filename = filename; mFolder = Path.GetDirectoryName(filename); mRoot = new NbtFile(filename); mRoot.LoadFile(); } catch (Exception e) { Console.WriteLine(e); System.Windows.Forms.DialogResult dr = System.Windows.Forms.MessageBox.Show("Your level.dat is broken. Copying over level.dat_old...","ERROR",System.Windows.Forms.MessageBoxButtons.OKCancel); if (dr == System.Windows.Forms.DialogResult.OK) { File.Copy(Path.ChangeExtension(filename, "dat_old"), filename); Load(filename); } else return; } LoadChunk(0, 0); }
public override void Load(string Folder) { string f = Path.Combine(Folder, "DefaultMapGenerator.dat"); if (!File.Exists(f)) return; NbtFile nf = new NbtFile(f); nf.LoadFile(f); GenerateCaves = nf.Query<NbtByte>("/DefaultMapGenerator/GenerateCaves").Value == 0x01 ? true : false; GenerateDungeons = nf.Query<NbtByte>("/DefaultMapGenerator/GenerateDungeons").Value == 0x01 ? true : false; GenerateOres = nf.Query<NbtByte>("/DefaultMapGenerator/GenerateOres").Value == 0x01 ? true : false; GenerateWater = nf.Query<NbtByte>("/DefaultMapGenerator/GenerateWater").Value == 0x01 ? true : false; HellMode = nf.Query<NbtByte>("/DefaultMapGenerator/HellMode").Value == 0x01 ? true : false; GenerateTrees = nf.Query<NbtByte>("/DefaultMapGenerator/GenerateTrees").Value == 0x01 ? true : false; Frequency = nf.Query<NbtDouble>("/DefaultMapGenerator/Frequency").Value; NoiseQuality = (NoiseQuality) nf.Query<NbtByte>("/DefaultMapGenerator/NoiseQuality").Value; OctaveCount = nf.Query<NbtInt>("/DefaultMapGenerator/OctaveCount").Value; Lacunarity = nf.Query<NbtDouble>("/DefaultMapGenerator/Lacunarity").Value; Persistance = nf.Query<NbtDouble>("/DefaultMapGenerator/Persistance").Value; ContinentNoiseFrequency = nf.Query<NbtDouble>("/DefaultMapGenerator/ContinentNoiseFrequency").Value; CaveThreshold = nf.Query<NbtDouble>("/DefaultMapGenerator/CaveThreshold").Value; }
private Chunk _LoadChunk(int x, int z) { //CurrentBlock.X = x; //CurrentBlock.Z = z; Chunk c = new Chunk(this); c.Loading = true; c.Filename = GetChunkFilename(x, z); c.CreationDate = File.GetCreationTime(c.Filename); c.Creator = "?"; c.Size = ChunkScale; if (!File.Exists(c.Filename)) { Console.WriteLine("! {0}", c.Filename); return null; } #if !DEBUG || CATCH_CHUNK_ERRORS try { #endif mChunk = new NbtFile(c.Filename); mChunk.LoadFile(); NbtCompound level = (NbtCompound)mChunk.RootTag["Level"]; c.Position = new Vector3i( level.Get<NbtInt>("xPos").Value, level.Get<NbtInt>("zPos").Value, 0); if ((int)c.Position.X != x || (int)c.Position.Y != z) { throw new Exception(string.Format("Chunk pos is wrong. {0}!={1}", c.Filename, c.Position)); } NbtList TileEntities = (NbtList)level["TileEntities"]; if (TileEntities.Tags.Count > 0) { //Console.WriteLine("*** Found TileEntities."); LoadTileEnts(ref c, (int)x, (int)z, TileEntities); } NbtList Entities = (NbtList)level["Entities"]; Console.WriteLine("*** Found {0} Entities.",Entities.Tags.Count); if (Entities.Tags.Count > 0) { LoadEnts(ref c, (int)x, (int)z, Entities); } // Blocks c.Blocks = DecompressBlocks(level.Get<NbtByteArray>("Blocks").Value); c.BlockLight = DecompressLightmap(level.Get<NbtByteArray>("BlockLight").Value); c.SkyLight = DecompressLightmap(level.Get<NbtByteArray>("SkyLight").Value); c.Data = DecompressDatamap(level.Get<NbtByteArray>("Data").Value); c.Loading = false; c.UpdateOverview(); string ci = string.Format("{0},{1}", x, z); if (mChunks.ContainsKey(ci)) return mChunks[ci]; mChunks.Add(ci, c); //TODO: Make Pig spawner converter. for (int Z = 0; Z < ChunkScale.X; Z++) { for (int Y = 0; Y < ChunkScale.Y; Y++) { for (int X = 0; X < ChunkScale.X; X++) { byte b = c.Blocks[X, Y, Z]; if (b == 52) { MobSpawner ms = new MobSpawner(); ms.Pos=new Vector3i(X, Y, Z); ms.EntityId = "Pig"; ms.UUID = Guid.NewGuid(); mTileEntities.Add(ms.UUID, ms); //c++; } } } } //if (c>0) Console.WriteLine("*** {0} spawners found.", c); //Console.WriteLine("Loaded {0} bytes from chunk {1}.", CurrentChunks.Length, c.Filename); return c; #if !DEBUG || CATCH_CHUNK_ERRORS } catch (Exception e) { string err = string.Format(" *** ERROR: Chunk {0},{1} ({2}) failed to load:\n\n{3}", x, z, c.Filename, e); Console.WriteLine(err); if (CorruptChunk != null) CorruptChunk(x,z,err, c.Filename); return null; } #endif }