public NBTFileReader(String path)
        {
            this.path = path;

            FileStream stream = null;
               // GZipStream gzip = null;
            BinaryReader reader = null;

            try
            {

                stream = new FileStream(this.path, FileMode.Open, FileAccess.Read);
               // gzip = new GZipStream(stream, CompressionMode.Decompress);
                reader = new BinaryReader(stream);

                //file must start with a tag compound, whose tag type is 10
                byte tagType = reader.ReadByte();

                if (tagType != TagCompound.TagType)
                {
                    throw new InvalidTagException("Tag type at the beginning of the file must be 10");
                }
                else
                {
                    compound = new TagCompound();
                    compound.read(reader);
                }
            }
            catch (InvalidTagException)
            {
                throw;
            }
            catch (IOException exception)
            {
                throw new FailedReadException(exception.Message, exception);
            }
            catch (Exception exception)
            {
                throw new FailedReadException(exception.Message, exception);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

              /*  if (gzip != null)
                {
                    gzip.Close();
                }*/

                if (stream != null)
                {
                    stream.Close();
                }
            }
        }
        public static void write(String path, TagCompound tag)
        {
            FileStream stream = null;
            //GZipStream gzip = null;
            BinaryWriter writer = null;

            try
            {
                stream = new FileStream(path, FileMode.Create, FileAccess.Write);
               // gzip = new GZipStream(stream, CompressionMode.Compress);
                writer = new BinaryWriter(stream);
                tag.write(writer);
            }
            catch (IOException exception)
            {
                throw new FailedWriteException("An IOException occurred while writing NBT file", exception);
            }
            catch (Exception exception)
            {
                throw new FailedWriteException("An exception occurred while writing NBT file", exception);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }

              /*  if (gzip != null)
                {
                    gzip.Close();
                }*/

                if (stream != null)
                {
                    stream.Close();
                }
            }
        }
        private bool loadlevelFile()
        {
            this.path = saveTo + "/level.dat"; ;
            try
            {
                 lreader = new NBTFileReader(path);
                 lroot = lreader.compound;

                hasStrongholdEndPortal = (TagByte)lroot.getNode(@"\Data\hasStrongholdEndPortal");
                hasStronghold = (TagByte)lroot.getNode(@"\Data\hasStronghold");
                hardcore = (TagByte)lroot.getNode(@"\Data\hardcore");
                thundering = (TagByte)lroot.getNode(@"\Data\thundering");
                raining = (TagByte)lroot.getNode(@"\Data\raining");
                hasBeenInCreative = (TagByte)lroot.getNode(@"\Data\hasBeenInCreative");
                spawnBonusChest = (TagByte)lroot.getNode(@"\Data\spawnBonusChest");
                MapFeatures = (TagByte)lroot.getNode(@"\Data\MapFeatures");
                hasBeenInCreative = (TagByte)lroot.getNode(@"\Data\hasBeenInCreative");

                LSpawnX = (TagInt)lroot.getNode(@"\Data\SpawnX");
                LSpawnY = (TagInt)lroot.getNode(@"\Data\SpawnY");
                LSpawnZ = (TagInt)lroot.getNode(@"\Data\SpawnZ");

                StrongholdX = (TagInt)lroot.getNode(@"\Data\StrongholdX");
                StrongholdY = (TagInt)lroot.getNode(@"\Data\StrongholdY");
                StrongholdZ = (TagInt)lroot.getNode(@"\Data\StrongholdZ");

                StrongholdEndPortalX = (TagInt)lroot.getNode(@"\Data\StrongholdEndPortalX");
                StrongholdEndPortalZ = (TagInt)lroot.getNode(@"\Data\StrongholdEndPortalZ");

                RandomSeed = (TagLong)lroot.getNode(@"\Data\RandomSeed");
                LastPlayed = (TagLong)lroot.getNode(@"\Data\LastPlayed");
                Time = (TagLong)lroot.getNode(@"\Data\Time");

                GameType = (TagInt)lroot.getNode(@"\Data\GameType");
                rainTime = (TagInt)lroot.getNode(@"\Data\rainTime");
                thunderTime = (TagInt)lroot.getNode(@"\Data\thunderTime");

            }
            catch (FailedReadException exception)
            {
                if (exception.InnerException != null)
                {
                    MessageBox.Show(exception.InnerException.Message);
                }
                return false;
            }

            if (this.hasStrongholdEndPortal.ToString() == "1")
            { this.chkhasStrongholdEndPortal.Checked = true; }
            else { this.chkhasStrongholdEndPortal.Checked = false; }

            if (this.hasStronghold.ToString() == "1")
            { this.chkhasStronghold.Checked = true; }
            else { this.chkhasStronghold.Checked = false; }

            if (this.hardcore.ToString() == "1")
            { this.chkhardcore.Checked = true; }
            else { this.chkhardcore.Checked = false; }

            if (this.thundering.ToString() == "1")
            { this.chkthundering.Checked = true; }
            else { this.chkthundering.Checked = false; }

            if (this.raining.ToString() == "1")
            { this.chkraining.Checked = true; }
            else { this.chkraining.Checked = false; }

            if (this.hasBeenInCreative.ToString() == "1")
            { this.chkhasBeenInCreative.Checked = true; }
            else { this.chkhasBeenInCreative.Checked = false; }

            if (this.spawnBonusChest.ToString() == "1")
            { this.chkspawnBonusChest.Checked = true; }
            else { this.chkspawnBonusChest.Checked = false; }

            if (this.MapFeatures.ToString() == "1")
            { this.chkMapFeatures.Checked = true; }
            else { this.chkMapFeatures.Checked = false; }

            if (this.hasBeenInCreative.ToString() == "1")
            { this.chkhasBeenInCreative.Checked = true; }
            else { this.chkhasBeenInCreative.Checked = false; }

            this.txtLSpawnX.Text = this.LSpawnX.ToString();
            this.txtLSpawnY.Text = this.LSpawnY.ToString();
            this.txtLSpawnZ.Text = this.LSpawnZ.ToString();

            this.txtStrongholdX.Text = this.StrongholdX.ToString();
            this.txtStrongholdY.Text = this.StrongholdY.ToString();
            this.txtStrongholdZ.Text = this.StrongholdZ.ToString();

            this.txtStrongholdEndPortalX.Text = this.StrongholdEndPortalX.ToString();
            this.txtStrongholdEndPortalZ.Text = this.StrongholdEndPortalZ.ToString();

            this.txtRandomSeed.Text = this.RandomSeed.ToString();
            this.txtLastPlayed.Text = this.LastPlayed.ToString();
            this.txtTime.Text = this.Time.ToString();

            this.txtGameType.Text = this.GameType.ToString();
            this.txtrainTime.Text = this.rainTime.ToString();
            this.txtthunderTime.Text = this.thunderTime.ToString();
            return true;
        }
 private bool loadFile(string path)
 {
     this.path = path;
     try
     {
         this.reader = new NBTFileReader(path);
         this.root = this.reader.compound;
         if (!((((this.loadColorsArray() && this.loadScale()) && (this.loadDimension() && this.loadWidth())) && (this.loadHeight() && this.loadXCenter())) && this.loadZCenter()))
         {
             return false;
         }
     }
     catch (FailedReadException exception)
     {
         if (exception.InnerException != null)
         {
             MessageBox.Show(exception.InnerException.Message);
         }
         return false;
     }
      this.txtScale.Text = this.scale.ToString();
      this.txtDimension.Text = this.dimension.ToString();
      this.txtWidth.Text = this.width.ToString();
      this.txtHeight.Text = this.height.ToString();
      this.txtXCenter.Text = this.xCenter.ToString();
      this.txtZCenter.Text = this.zCenter.ToString();
     //this.lblImage.Visible = false;
     return true;
 }
 public void defaultTagValues(bool resetColors)
 {
     this.root = new TagCompound();
     TagCompound tag = new TagCompound("data");
     this.root.addTag(tag);
       this.scale = new TagByte("scale", 0);
       this.dimension = new TagInt("dimension", 0);
       this.width = new TagShort("width", 0x80);
       this.height = new TagShort("height", 0x80);
       this.xCenter = new TagInt("xCenter", 0x2710);
       this.zCenter = new TagInt("zCenter", 0x2710);
     if (resetColors)
     {
         byte[] bytes = new byte[0x4000];
         for (int i = 0; i < 0x4000; i++)
         {
             bytes[i] = 0;
         }
         this.colours = new TagByteArray("colors", 0x4000, bytes);
     }
      tag.addTag(this.scale);
      tag.addTag(this.dimension);
      tag.addTag(this.width);
      tag.addTag(this.height);
      tag.addTag(this.xCenter);
      tag.addTag(this.zCenter);
     tag.addTag(this.colours);
 }
 public void clearTags()
 {
     this.root = null;
     this.colours = null;
     /*  this.scale = null;
       this.dimension = null;
       this.width = null;
       this.height = null;
       this.xCenter = null;
       this.zCenter = null;
       this.txtScale.Text = "";
       this.txtDimension.Text = "";
       this.txtWidth.Text = "";
       this.txtHeight.Text = "";
       this.txtXCenter.Text = "";
       this.txtZCenter.Text = "";*/
     // this.Text = "Map Item Editor";
 }
 void Save(string file)
 {
     try
     {
         Page page = (Page)tabControl.SelectedTab;
         FileInfo info = new FileInfo(file);
         if (info.Exists && page.file != info.FullName)
         {
             string str;
             if (info.Name == "level.dat")
                 str = "Are you sure you want to overwrite " + info.Directory.Name + "?";
             else str = "Are you sure you want to overwrite '" + info.Name + "'?";
             DialogResult result = MessageBox.Show(str, "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
             if (result != DialogResult.Yes) return;
         }
         page.file = info.FullName;
         NbtTag root, tag;
         if (info.Exists)
         {
             root = NbtTag.Load(page.file);
             if (info.Extension.ToLower() == ".dat")
                 CreateBackup(file);
             tag = root;
         }
         else
         {
             if (info.Extension.ToLower() == ".dat")
             {
                 MessageBox.Show("You can't create a new Minecraft level/player file.\n" +
                                 "Select an existing one instead.", "Error",
                                 MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return;
             } root = NbtTag.CreateCompound("Inventory", NbtTag.CreateList(NbtTagType.Compound));
             tag = root;
         } if (tag.Type == NbtTagType.Compound && tag.Contains("Data")) { tag = tag["Data"]; }
         if (tag.Type == NbtTagType.Compound && tag.Contains("Player")) { tag = tag["Player"]; }
         if (tag.Type != NbtTagType.Compound || !tag.Contains("Inventory")) { throw new Exception("Can't find Inventory tag."); }
         Inventory.Save(tag, page.slots);
         root.Save(page.file);
         if (info.Name == "level.dat") { page.Text = info.Directory.Name; }
         else { page.Text = info.Name; }
         Text = "INVedit - " + page.Text;
         page.changed = false;
         btnReload.Enabled = true;
     }
     catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
 }
        private bool loadplayerFile()
        {
            this.path = saveTo + "/" + domainUpDown1.Text; ;
            try
            {
                 preader = new NBTFileReader(path);
                 proot = preader.compound;

                Health = (TagShort)proot.getNode(@"\Health");
                DeathTime = (TagShort)proot.getNode(@"\DeathTime");
                AttackTime = (TagShort)proot.getNode(@"\AttackTime");
                HurtTime = (TagShort)proot.getNode(@"\HurtTime");
                PDimension = (TagInt)proot.getNode(@"\Dimension");
                Sleeping = (TagByte)proot.getNode(@"\Sleeping");
                invulnerable = (TagByte)proot.getNode(@"\abilities\invulnerable");
                instabuild = (TagByte)proot.getNode(@"\abilities\instabuild");
                flying = (TagByte)proot.getNode(@"\abilities\flying");
                mayfly = (TagByte)proot.getNode(@"\abilities\mayfly");

                SleepTimer = (TagShort)proot.getNode(@"\SleepTimer");
                SpawnX = (TagInt)proot.getNode(@"\SpawnX");
                SpawnY = (TagInt)proot.getNode(@"\SpawnY");
                SpawnZ = (TagInt)proot.getNode(@"\SpawnZ");
                foodLevel = (TagInt)proot.getNode(@"\foodLevel");
                foodTickTimer = (TagInt)proot.getNode(@"\foodTickTimer");
                foodExhaustionLevel = (TagFloat)proot.getNode(@"\foodExhaustionLevel");
                foodSaturationLevel = (TagFloat)proot.getNode(@"\foodSaturationLevel");
                XpP = (TagFloat)proot.getNode(@"\XpP");
                XpLevel = (TagInt)proot.getNode(@"\XpLevel");
                XpTotal = (TagInt)proot.getNode(@"\XpTotal");
                /*  this.reader = new NBTFileReader(path);
                  this.root = this.reader.compound;
                  if (!((((this.loadColorsArray()))))) //&& this.loadScale()) && (this.loadDimension() && this.loadWidth())) && (this.loadHeight() && this.loadXCenter())) && this.loadZCenter()))
                  {
                      return false;
                  }*/
            }
            catch (FailedReadException exception)
            {
                if (exception.InnerException != null)
                {
                    MessageBox.Show(exception.InnerException.Message);
                }
                return false;
            }
            this.txtHealth.Text = this.Health.ToString();
            // this.txtDeathTime.Text = this.DeathTime.ToString();
            // this.txtAttackTime.Text = this.AttackTime.ToString();
            // this.txtHurtTime.Text = this.HurtTime.ToString();
            this.txtPDimension.Text = this.PDimension.ToString();
            if (this.Sleeping.ToString() == "1")
            { this.chkSleeping.Checked = true; }
            else { this.chkSleeping.Checked = false; }

            if (this.instabuild.ToString() == "1")
            { this.chkInstabuild.Checked = true; }
            else { this.chkInstabuild.Checked = false; }

            if (this.invulnerable.ToString() == "1")
            { this.chkInvulnerable.Checked = true; }
            else { this.chkInvulnerable.Checked = false; }

            if (this.mayfly.ToString() == "1")
            { this.chkMayfly.Checked = true; }
            else { this.chkMayfly.Checked = false; }

            if (this.flying.ToString() == "1")
            { this.chkFlying.Checked = true; }
            else { this.chkFlying.Checked = false; }

            // this.txtSleepTimer.Text = this.SleepTimer.ToString();
            this.txtSpawnX.Text = this.SpawnX.ToString();
            this.txtSpawnY.Text = this.SpawnY.ToString();
            this.txtSpawnZ.Text = this.SpawnZ.ToString();
            this.txtFoodLevel.Text = this.foodLevel.ToString();
            this.txtFoodTickTimer.Text = this.foodTickTimer.ToString();
            this.txtFoodExhaustionLevel.Text = this.foodExhaustionLevel.ToString();
            this.txtFoodSaturationLevel.Text = this.foodSaturationLevel.ToString();
            this.txtXpP.Text = this.XpP.ToString();
            this.txtXpLevel.Text = this.XpLevel.ToString();
            this.txtXpTotal.Text = this.XpTotal.ToString();
            return true;
        }
        public static List<Tag> readData(BinaryReader reader)
        {
            bool completed = false;
            List<Tag> compound = new List<Tag>();

            while (!completed)
            {
                byte tagType = reader.ReadByte();

                //Console.WriteLine("TagType: " + Util.getTypeName(tagType));

                switch (tagType)
                {
                    case 0:
                        completed = true;
                        break;
                    case 1:
                        TagByte tagByte = new TagByte();
                        tagByte.read(reader);
                        compound.Add(tagByte);
                        break;
                    case 2:
                        TagShort tagShort = new TagShort();
                        tagShort.read(reader);
                        compound.Add(tagShort);
                        break;
                    case 3:
                        TagInt tagInt = new TagInt();
                        tagInt.read(reader);
                        compound.Add(tagInt);
                        break;
                    case 4:
                        TagLong tagLong = new TagLong();
                        tagLong.read(reader);
                        compound.Add(tagLong);
                        break;
                    case 5:
                        TagFloat tagFloat = new TagFloat();
                        tagFloat.read(reader);
                        compound.Add(tagFloat);
                        break;
                    case 6:
                        TagDouble tagDouble = new TagDouble();
                        tagDouble.read(reader);
                        compound.Add(tagDouble);
                        break;
                    case 7:
                        TagByteArray tagByteArray = new TagByteArray();
                        tagByteArray.read(reader);
                        compound.Add(tagByteArray);
                        break;
                    case 8:
                        TagString tagString = new TagString();
                        tagString.read(reader);
                        compound.Add(tagString);
                        break;
                    case 9:
                        TagList tagList = new TagList();
                        tagList.read(reader);
                        compound.Add(tagList);
                        break;
                    case 10:
                        TagCompound tagCompound = new TagCompound();
                        tagCompound.read(reader);
                        compound.Add(tagCompound);
                        break;
                    default:
                        throw new InvalidTagException("An invalid tag type: " + tagType + ", was found while parsing a TAG_Compound");
                }
            }

            return compound;
        }
        public static List <Tag> readData(BinaryReader reader)
        {
            bool       completed = false;
            List <Tag> compound  = new List <Tag>();

            while (!completed)
            {
                byte tagType = reader.ReadByte();

                //Console.WriteLine("TagType: " + Util.getTypeName(tagType));

                switch (tagType)
                {
                case 0:
                    completed = true;
                    break;

                case 1:
                    TagByte tagByte = new TagByte();
                    tagByte.read(reader);
                    compound.Add(tagByte);
                    break;

                case 2:
                    TagShort tagShort = new TagShort();
                    tagShort.read(reader);
                    compound.Add(tagShort);
                    break;

                case 3:
                    TagInt tagInt = new TagInt();
                    tagInt.read(reader);
                    compound.Add(tagInt);
                    break;

                case 4:
                    TagLong tagLong = new TagLong();
                    tagLong.read(reader);
                    compound.Add(tagLong);
                    break;

                case 5:
                    TagFloat tagFloat = new TagFloat();
                    tagFloat.read(reader);
                    compound.Add(tagFloat);
                    break;

                case 6:
                    TagDouble tagDouble = new TagDouble();
                    tagDouble.read(reader);
                    compound.Add(tagDouble);
                    break;

                case 7:
                    TagByteArray tagByteArray = new TagByteArray();
                    tagByteArray.read(reader);
                    compound.Add(tagByteArray);
                    break;

                case 8:
                    TagString tagString = new TagString();
                    tagString.read(reader);
                    compound.Add(tagString);
                    break;

                case 9:
                    TagList tagList = new TagList();
                    tagList.read(reader);
                    compound.Add(tagList);
                    break;

                case 10:
                    TagCompound tagCompound = new TagCompound();
                    tagCompound.read(reader);
                    compound.Add(tagCompound);
                    break;

                default:
                    throw new InvalidTagException("An invalid tag type: " + tagType + ", was found while parsing a TAG_Compound");
                }
            }

            return(compound);
        }
        public override void read(BinaryReader reader)
        {
            try
            {
                this.list = new List<object>();
                this.name = Util.readString(reader);
                this.tagId = reader.ReadSByte();
                int length = 0;

                if (BitConverter.IsLittleEndian)
                {
                    length = Util.readIntegerBigEndian(reader);
                }
                else
                {
                    length = reader.ReadInt32();
                }

                for (int a = 0; a < length; a++)
                {
                    switch (tagId)
                    {
                        case 1:
                            list.Add(TagByte.readData(reader));
                            break;
                        case 2:
                            list.Add(TagShort.readData(reader));
                            break;
                        case 3:
                            list.Add(TagInt.readData(reader));
                            break;
                        case 4:
                            list.Add(TagLong.readData(reader));
                            break;
                        case 5:
                            list.Add(TagFloat.readData(reader));
                            break;
                        case 6:
                            list.Add(TagDouble.readData(reader));
                            break;
                        case 7:
                            list.Add(TagByteArray.readData(reader));
                            break;
                        case 8:
                            list.Add(TagString.readData(reader));
                            break;
                        case 9:
                            TagList newList = new TagList();
                            newList.read(reader);
                            list.Add(newList);
                            break;
                        case 10:
                            List<Tag> newTagList = TagCompound.readData(reader);
                            TagCompound compound = new TagCompound("", newTagList);
                            list.Add(compound);
                            break;
                    }
                }
            }
            catch (IOException exception)
            {
                throw new FailedReadException(exception.Message, exception);
            }
        }