Example #1
0
 public static Rotation FromNbt(NbtList nbtTag)
 {
     Rotation rot = new Rotation(0f, 0f);
     rot.Yaw = nbtTag.Get<NbtFloat>(0).Value;
     rot.Pitch = nbtTag.Get<NbtFloat>(1).Value;
     return rot;
 }
Example #2
0
        public void SerializingEmpty()
        {
            // check saving/loading lists of all possible value types
            var testFile = new NbtFile(new NbtCompound("root")
            {
                new NbtList("emptyList", NbtTagType.End),
                new NbtList("listyList", NbtTagType.List)
                {
                    new NbtList(NbtTagType.End)
                }
            });

            byte[] buffer = testFile.SaveToBuffer(NbtCompression.None);

            testFile.LoadFromBuffer(buffer, 0, buffer.Length, NbtCompression.None);

            NbtList list1 = testFile.RootTag.Get <NbtList>("emptyList");

            Assert.AreEqual(list1.Count, 0);
            Assert.AreEqual(list1.ListType, NbtTagType.End);

            NbtList list2 = testFile.RootTag.Get <NbtList>("listyList");

            Assert.AreEqual(list2.Count, 1);
            Assert.AreEqual(list2.ListType, NbtTagType.List);
            Assert.AreEqual(list2.Get <NbtList>(0).Count, 0);
            Assert.AreEqual(list2.Get <NbtList>(0).ListType, NbtTagType.End);
        }
Example #3
0
        public static Rotation FromNbt(NbtList nbtTag)
        {
            Rotation rot = new Rotation(0f, 0f);

            rot.Yaw   = nbtTag.Get <NbtFloat>(0).Value;
            rot.Pitch = nbtTag.Get <NbtFloat>(1).Value;
            return(rot);
        }
Example #4
0
        public void GettersAndSetters()
        {
            var parent          = new NbtCompound("Parent");
            var child           = new NbtCompound("Child");
            var nestedChild     = new NbtCompound("NestedChild");
            var childList       = new NbtList("ChildList");
            var nestedChildList = new NbtList("NestedChildList");

            childList.Add(new NbtInt(1));
            var nestedInt = new NbtInt(1);

            nestedChildList.Add(nestedInt);
            parent.Add(child);
            parent.Add(childList);
            child.Add(nestedChild);
            child.Add(nestedChildList);

            // Accessing nested compound tags using indexers
            Assert.AreEqual(parent["Child"]["NestedChild"], nestedChild);
            Assert.AreEqual(parent["Child"]["NestedChildList"], nestedChildList);
            Assert.AreEqual(parent["Child"]["NestedChildList"][0], nestedInt);

            // Accessing nested compound tags using Get<T>
            Assert.AreEqual(parent.Get <NbtCompound>("Child").Get <NbtCompound>("NestedChild"), nestedChild);
            Assert.AreEqual(parent.Get <NbtCompound>("Child").Get <NbtList>("NestedChildList"), nestedChildList);
            Assert.AreEqual(parent.Get <NbtCompound>("Child").Get <NbtList>("NestedChildList")[0], nestedInt);

            // Accessing with Get<T> and an invalid given type
            Assert.Throws <InvalidCastException>(() => parent.Get <NbtInt>("Child"));

            // Trying to use integer indexers on non-NbtList tags
            Assert.Throws <InvalidOperationException>(() => parent[0]    = nestedInt);
            Assert.Throws <InvalidOperationException>(() => nestedInt[0] = nestedInt);

            // Trying to use string indexers on non-NbtCompound tags
            Assert.Throws <InvalidOperationException>(() => childList["test"] = nestedInt);
            Assert.Throws <InvalidOperationException>(() => nestedInt["test"] = nestedInt);

            // Trying to get a non-existent element by name
            Assert.IsNull(parent.Get <NbtTag>("NonExistentTag"));
            Assert.IsNull(parent["NonExistentTag"]);

            // Null indices on NbtCompound
            Assert.Throws <ArgumentNullException>(() => parent.Get <NbtTag>(null));
            Assert.Throws <ArgumentNullException>(() => parent[null] = new NbtInt(1));
            Assert.Throws <ArgumentNullException>(() => nestedInt    = (NbtInt)parent[null]);

            // Out-of-range indices on NbtList
            Assert.Throws <ArgumentOutOfRangeException>(() => nestedInt     = (NbtInt)childList[-1]);
            Assert.Throws <ArgumentOutOfRangeException>(() => childList[-1] = new NbtInt(1));
            Assert.Throws <ArgumentOutOfRangeException>(() => nestedInt     = childList.Get <NbtInt>(-1));
            Assert.Throws <ArgumentOutOfRangeException>(() => nestedInt     = (NbtInt)childList[childList.Count]);
            Assert.Throws <ArgumentOutOfRangeException>(() => nestedInt     = childList.Get <NbtInt>(childList.Count));
        }
Example #5
0
        public void Block(string file)
        {
            var myFile = new NbtFile();

            myFile.LoadFromFile(file);
            var     myCompTag = myFile.RootTag;
            NbtList entries   = myFile.RootTag.Get <NbtList>("blocks");

            // Y :
            for (int i = 0; i < size[1]; i++)
            {
                // Z :
                for (int j = 0; j < size[2]; j++)
                {
                    // X :
                    for (int k = 0; k < size[0]; k++)
                    {
                        for (int l = 0; l < count; l++)
                        {
                            NbtList pos = entries.Get <NbtCompound>(l).Get <NbtList>("pos");
                            if (pos.Get <NbtInt>(0).IntValue == k &&
                                pos.Get <NbtInt>(1).IntValue == i &&
                                pos.Get <NbtInt>(2).IntValue == j)
                            {
                                int state = entries.Get <NbtCompound>(l).Get <NbtInt>("state").IntValue;
                                //MessageBox.Show(state.ToString());
                                data[0] = k.ToString();
                                data[1] = i.ToString();
                                data[2] = j.ToString();
                                data[3] = palette[state][0];
                                data[4] = palette[state][1];
                                data[5] = palette[state][2];
                                data[6] = palette[state][3];
                                data[7] = palette[state][4];
                                data[8] = palette[state][5];
                                data[9] = palette[state][6];

                                block.AddRange(data);
                            }
                        }
                    }
                }
            }

            /*for (int i = 0; i < (count*4); i++)
             * {
             *  MessageBox.Show((string)block[i] + " :" + i);
             * }*/
        }
Example #6
0
        public void Palette(string file)
        {
            var myFile = new NbtFile();

            myFile.LoadFromFile(file);
            var     myCompTag = myFile.RootTag;
            NbtList block     = myFile.RootTag.Get <NbtList>("palette");

            for (int i = 0; i < block.Count; i++)
            {
                if (block.Get <NbtCompound>(i).Contains("Properties") == true)
                {
                    BlockStateConstraint bSC = new BlockStateConstraint();
                    palette.Add(bSC.BlockStates(file, i));
                }
                else
                {
                    palette.Add(new List <string> {
                        block.Get <NbtCompound>(i).Get <NbtString>("Name").StringValue, "null", "null", "null", "null", "null", "null"
                    });
                }
            }
        }
Example #7
0
        public void Size(string file)
        {
            var myFile = new NbtFile();

            myFile.LoadFromFile(file);
            var     myCompTag = myFile.RootTag;
            NbtList list      = myFile.RootTag.Get <NbtList>("size");

            for (int i = 0; i < 3; i++)
            {
                size[i] = list.Get <NbtInt>(i).IntValue;
                //MessageBox.Show(size[i].ToString());
            }
            count = size[0] * size[1] * size[2];
        }
Example #8
0
    public void readFromNbt(NbtCompound tag)
    {
        NbtList list = tag.getList("candidates");

        for (int i = 0; i < list.Count; i++)
        {
            this.candidates.Add(new Candidate(list.Get <NbtCompound>(i)));
        }

        if (tag.hasKey("trainee"))
        {
            this.trainee             = new Trainee(tag.getCompound("trainee"));
            this.traineeTrainingTime = tag.getFloat("trainingTime");
        }
    }
Example #9
0
        public void Serializing1()
        {
            // check the basics of saving/loading
            const NbtTagType expectedListType = NbtTagType.Int;
            const int        elements         = 10;

            // construct nbt file
            var writtenFile = new NbtFile(new NbtCompound("ListTypeTest"));
            var writtenList = new NbtList("Entities", null, expectedListType);

            for (int i = 0; i < elements; i++)
            {
                writtenList.Add(new NbtInt(i));
            }

            NbtCompound rootTag = (NbtCompound)writtenFile.RootTag;

            rootTag.Add(writtenList);

            // test saving
            byte[] data = writtenFile.SaveToBuffer(NbtCompression.None);

            // test loading
            var  readFile  = new NbtFile();
            long bytesRead = readFile.LoadFromBuffer(data, 0, data.Length, NbtCompression.None);

            Assert.AreEqual(bytesRead, data.Length);

            // check contents of loaded file
            Assert.NotNull(readFile.RootTag);
            Assert.IsInstanceOf <NbtList>(readFile.RootTag["Entities"]);
            var readList = (NbtList)readFile.RootTag["Entities"];

            Assert.AreEqual(writtenList.ListType, readList.ListType);
            Assert.AreEqual(readList.Count, writtenList.Count);

            // check .ToArray
            CollectionAssert.AreEquivalent(readList, readList.ToArray());
            CollectionAssert.AreEquivalent(readList, readList.ToArray <NbtInt>());

            // check contents of loaded list
            for (int i = 0; i < elements; i++)
            {
                Assert.AreEqual(readList.Get <NbtInt>(i).Value, writtenList.Get <NbtInt>(i).Value);
            }
        }
Example #10
0
    public void readFromNbt(NbtCompound tag)
    {
        this.aboveGroundMap = tag.getByteArray("aboveGroundMap");

        this.workerSpawnPoint = tag.getPosition("workerSpawnPoint");

        // Read Layers:
        NbtList layers = tag.getList("layers");

        for (int i = 0; i < layers.Count; i++)
        {
            Layer       layer         = new Layer(this.world, i);
            NbtCompound layerCompound = layers.Get <NbtCompound>(i);
            layer.readFromNbt(layerCompound);
            this.layers[layer.depth] = layer;

            // Call onCreate method for all the behaviors, now that all Cell's
            // have been loaded.
            for (int x = 0; x < this.world.mapSize; x++)
            {
                for (int y = 0; y < this.world.mapSize; y++)
                {
                    CellState state = layer.getCellState(x, y);
                    if (state.behavior != null)
                    {
                        state.behavior.onCreate(this.world, state, new Position(x, y, layer.depth));
                    }
                }
            }

            // After onCreate is called for all of the behaviors, let them read their
            // state from NBT.
            NbtList listBehaviorTags = layerCompound.getList("meta");
            foreach (NbtCompound behaviorTag in listBehaviorTags)
            {
                CellBehavior meta = layer.getCellState(behaviorTag.getInt("xPos"), behaviorTag.getInt("yPos")).behavior;
                if (meta != null && meta is IHasData)
                {
                    ((IHasData)meta).readFromNbt(behaviorTag);
                }
            }
        }
    }
Example #11
0
        public void Serializing()
        {
            string           fileName         = Path.Combine(TempDir, "NbtListType.nbt");
            const NbtTagType expectedListType = NbtTagType.Int;
            const int        elements         = 10;

            // construct nbt file
            NbtFile writtenFile = new NbtFile(new NbtCompound("ListTypeTest"));
            NbtList writtenList = new NbtList("Entities", null, expectedListType);

            for (int i = 0; i < elements; i++)
            {
                writtenList.Add(new NbtInt(i));
            }
            writtenFile.RootTag.Add(writtenList);

            // test saving
            writtenFile.SaveToFile(fileName, NbtCompression.GZip);

            // test loading
            NbtFile readFile = new NbtFile(fileName);

            // check contents of loaded file
            Assert.NotNull(readFile.RootTag);
            Assert.IsInstanceOf <NbtList>(readFile.RootTag["Entities"]);
            NbtList readList = (NbtList)readFile.RootTag["Entities"];

            Assert.AreEqual(readList.ListType, writtenList.ListType);
            Assert.AreEqual(readList.Count, writtenList.Count);

            // check .ToArray
            CollectionAssert.AreEquivalent(readList, readList.ToArray());
            CollectionAssert.AreEquivalent(readList, readList.ToArray <NbtInt>());

            // check contents of loaded list
            for (int i = 0; i < elements; i++)
            {
                Assert.AreEqual(readList.Get <NbtInt>(i).Value, writtenList.Get <NbtInt>(i).Value);
            }
        }
Example #12
0
        public void GettersAndSetters()
        {
            // construct a document for us to test.
            var nestedChild     = new NbtCompound("NestedChild");
            var nestedInt       = new NbtInt(1);
            var nestedChildList = new NbtList("NestedChildList")
            {
                nestedInt
            };
            var child = new NbtCompound("Child")
            {
                nestedChild,
                nestedChildList
            };
            var childList = new NbtList("ChildList")
            {
                new NbtInt(1)
            };
            var parent = new NbtCompound("Parent")
            {
                child,
                childList
            };

            // Accessing nested compound tags using indexers
            Assert.AreEqual(nestedChild, parent["Child"]["NestedChild"]);
            Assert.AreEqual(nestedChildList, parent["Child"]["NestedChildList"]);
            Assert.AreEqual(nestedInt, parent["Child"]["NestedChildList"][0]);

            // Accessing nested compound tags using Get and Get<T>
            Assert.Throws <ArgumentNullException>(() => parent.Get <NbtCompound>(null));
            Assert.IsNull(parent.Get <NbtCompound>("NonExistingChild"));
            Assert.AreEqual(nestedChild, parent.Get <NbtCompound>("Child").Get <NbtCompound>("NestedChild"));
            Assert.AreEqual(nestedChildList, parent.Get <NbtCompound>("Child").Get <NbtList>("NestedChildList"));
            Assert.AreEqual(nestedInt, parent.Get <NbtCompound>("Child").Get <NbtList>("NestedChildList")[0]);
            Assert.Throws <ArgumentNullException>(() => parent.Get(null));
            Assert.IsNull(parent.Get("NonExistingChild"));
            Assert.AreEqual(nestedChild, (parent.Get("Child") as NbtCompound).Get("NestedChild"));
            Assert.AreEqual(nestedChildList, (parent.Get("Child") as NbtCompound).Get("NestedChildList"));
            Assert.AreEqual(nestedInt, (parent.Get("Child") as NbtCompound).Get("NestedChildList")[0]);

            // Accessing with Get<T> and an invalid given type
            Assert.Throws <InvalidCastException>(() => parent.Get <NbtInt>("Child"));

            // Using TryGet and TryGet<T>
            NbtTag dummyTag;

            Assert.Throws <ArgumentNullException>(() => parent.TryGet(null, out dummyTag));
            Assert.IsFalse(parent.TryGet("NonExistingChild", out dummyTag));
            Assert.IsTrue(parent.TryGet("Child", out dummyTag));
            NbtCompound dummyCompoundTag;

            Assert.Throws <ArgumentNullException>(() => parent.TryGet(null, out dummyCompoundTag));
            Assert.IsFalse(parent.TryGet("NonExistingChild", out dummyCompoundTag));
            Assert.IsTrue(parent.TryGet("Child", out dummyCompoundTag));

            // Trying to use integer indexers on non-NbtList tags
            Assert.Throws <InvalidOperationException>(() => parent[0]    = nestedInt);
            Assert.Throws <InvalidOperationException>(() => nestedInt[0] = nestedInt);

            // Trying to use string indexers on non-NbtCompound tags
            Assert.Throws <InvalidOperationException>(() => childList["test"] = nestedInt);
            Assert.Throws <InvalidOperationException>(() => nestedInt["test"] = nestedInt);

            // Trying to get a non-existent element by name
            Assert.IsNull(parent.Get <NbtTag>("NonExistentTag"));
            Assert.IsNull(parent["NonExistentTag"]);

            // Null indices on NbtCompound
            Assert.Throws <ArgumentNullException>(() => parent.Get <NbtTag>(null));
            Assert.Throws <ArgumentNullException>(() => parent[null] = new NbtInt(1));
            Assert.Throws <ArgumentNullException>(() => nestedInt    = (NbtInt)parent[null]);

            // Out-of-range indices on NbtList
            Assert.Throws <ArgumentOutOfRangeException>(() => nestedInt     = (NbtInt)childList[-1]);
            Assert.Throws <ArgumentOutOfRangeException>(() => childList[-1] = new NbtInt(1));
            Assert.Throws <ArgumentOutOfRangeException>(() => nestedInt     = childList.Get <NbtInt>(-1));
            Assert.Throws <ArgumentOutOfRangeException>(() => nestedInt     = (NbtInt)childList[childList.Count]);
            Assert.Throws <ArgumentOutOfRangeException>(() => nestedInt     = childList.Get <NbtInt>(childList.Count));

            // Using setter correctly
            parent["NewChild"] = new NbtByte("NewChild");

            // Using setter incorrectly
            object dummyObject;

            Assert.Throws <ArgumentNullException>(() => parent["Child"] = null);
            Assert.NotNull(parent["Child"]);
            Assert.Throws <ArgumentException>(() => parent["Child"]     = new NbtByte("NotChild"));
            Assert.Throws <InvalidOperationException>(() => dummyObject = parent[0]);
            Assert.Throws <InvalidOperationException>(() => parent[0]   = new NbtByte("NewerChild"));

            // Try adding tag to self
            var selfTest = new NbtCompound("SelfTest");

            Assert.Throws <ArgumentException>(() => selfTest["SelfTest"] = selfTest);

            // Try adding a tag that already has a parent
            Assert.Throws <ArgumentException>(() => selfTest[child.Name] = child);
        }
Example #13
0
        public void Serializing1()
        {
            // check the basics of saving/loading
            const NbtTagType expectedListType = NbtTagType.Int;
            const int elements = 10;

            // construct nbt file
            var writtenFile = new NbtFile(new NbtCompound("ListTypeTest"));
            var writtenList = new NbtList("Entities", null, expectedListType);
            for (int i = 0; i < elements; i++) {
                writtenList.Add(new NbtInt(i));
            }
            writtenFile.RootTag.Add(writtenList);

            // test saving
            byte[] data = writtenFile.SaveToBuffer(NbtCompression.None);

            // test loading
            var readFile = new NbtFile();
            long bytesRead = readFile.LoadFromBuffer(data, 0, data.Length, NbtCompression.None);
            Assert.AreEqual(bytesRead, data.Length);

            // check contents of loaded file
            Assert.NotNull(readFile.RootTag);
            Assert.IsInstanceOf<NbtList>(readFile.RootTag["Entities"]);
            var readList = (NbtList)readFile.RootTag["Entities"];
            Assert.AreEqual(writtenList.ListType, readList.ListType);
            Assert.AreEqual(readList.Count, writtenList.Count);

            // check .ToArray
            CollectionAssert.AreEquivalent(readList, readList.ToArray());
            CollectionAssert.AreEquivalent(readList, readList.ToArray<NbtInt>());

            // check contents of loaded list
            for (int i = 0; i < elements; i++) {
                Assert.AreEqual(readList.Get<NbtInt>(i).Value, writtenList.Get<NbtInt>(i).Value);
            }
        }
Example #14
0
        public void GettersAndSetters()
        {
            // construct a document for us to test.
            var nestedChild = new NbtCompound("NestedChild");
            var nestedInt = new NbtInt(1);
            var nestedChildList = new NbtList("NestedChildList") {
                nestedInt
            };
            var child = new NbtCompound("Child") {
                nestedChild,
                nestedChildList
            };
            var childList = new NbtList("ChildList") {
                new NbtInt(1)
            };
            var parent = new NbtCompound("Parent") {
                child,
                childList
            };

            // Accessing nested compound tags using indexers
            Assert.AreEqual(parent["Child"]["NestedChild"], nestedChild);
            Assert.AreEqual(parent["Child"]["NestedChildList"], nestedChildList);
            Assert.AreEqual(parent["Child"]["NestedChildList"][0], nestedInt);

            // Accessing nested compound tags using Get and Get<T>
            Assert.AreEqual(parent.Get<NbtCompound>("Child").Get<NbtCompound>("NestedChild"), nestedChild);
            Assert.AreEqual(parent.Get<NbtCompound>("Child").Get<NbtList>("NestedChildList"), nestedChildList);
            Assert.AreEqual(parent.Get<NbtCompound>("Child").Get<NbtList>("NestedChildList")[0], nestedInt);
            Assert.AreEqual((parent.Get("Child") as NbtCompound).Get("NestedChild"), nestedChild);
            Assert.AreEqual((parent.Get("Child") as NbtCompound).Get("NestedChildList"), nestedChildList);
            Assert.AreEqual((parent.Get("Child") as NbtCompound).Get("NestedChildList")[0], nestedInt);

            // Accessing with Get<T> and an invalid given type
            Assert.Throws<InvalidCastException>(() => parent.Get<NbtInt>("Child"));

            // Using TryGet and TryGet<T>
            NbtTag dummyTag;
            Assert.IsTrue(parent.TryGet("Child", out dummyTag));
            NbtCompound dummyCompoundTag;
            Assert.IsTrue(parent.TryGet("Child", out dummyCompoundTag));

            // Trying to use integer indexers on non-NbtList tags
            Assert.Throws<InvalidOperationException>(() => parent[0] = nestedInt);
            Assert.Throws<InvalidOperationException>(() => nestedInt[0] = nestedInt);

            // Trying to use string indexers on non-NbtCompound tags
            Assert.Throws<InvalidOperationException>(() => childList["test"] = nestedInt);
            Assert.Throws<InvalidOperationException>(() => nestedInt["test"] = nestedInt);

            // Trying to get a non-existent element by name
            Assert.IsNull(parent.Get<NbtTag>("NonExistentTag"));
            Assert.IsNull(parent["NonExistentTag"]);

            // Null indices on NbtCompound
            Assert.Throws<ArgumentNullException>(() => parent.Get<NbtTag>(null));
            Assert.Throws<ArgumentNullException>(() => parent[null] = new NbtInt(1));
            Assert.Throws<ArgumentNullException>(() => nestedInt = (NbtInt)parent[null]);

            // Out-of-range indices on NbtList
            Assert.Throws<ArgumentOutOfRangeException>(() => nestedInt = (NbtInt)childList[-1]);
            Assert.Throws<ArgumentOutOfRangeException>(() => childList[-1] = new NbtInt(1));
            Assert.Throws<ArgumentOutOfRangeException>(() => nestedInt = childList.Get<NbtInt>(-1));
            Assert.Throws<ArgumentOutOfRangeException>(() => nestedInt = (NbtInt)childList[childList.Count]);
            Assert.Throws<ArgumentOutOfRangeException>(() => nestedInt = childList.Get<NbtInt>(childList.Count));

            // Using setter correctly
            parent["NewChild"] = new NbtByte("NewChild");

            // Using setter incorrectly
            object dummyObject;
            Assert.Throws<ArgumentNullException>(() => parent["Child"] = null);
            Assert.NotNull(parent["Child"]);
            Assert.Throws<ArgumentException>(() => parent["Child"] = new NbtByte("NotChild"));
            Assert.Throws<InvalidOperationException>(() => dummyObject = parent[0]);
            Assert.Throws<InvalidOperationException>(() => parent[0] = new NbtByte("NewerChild"));

            // Try adding tag to self
            var selfTest = new NbtCompound("SelfTest");
            Assert.Throws<ArgumentException>(() => selfTest["SelfTest"] = selfTest);

            // Try adding a tag that already has a parent
            Assert.Throws<ArgumentException>(() => selfTest[child.Name] = child);
        }
Example #15
0
        public void Serializing()
        {
            string fileName = Path.Combine( TempDir, "NbtListType.nbt" );
            const NbtTagType expectedListType = NbtTagType.Int;
            const int elements = 10;

            // construct nbt file
            NbtFile writtenFile = new NbtFile( new NbtCompound( "ListTypeTest" ) );
            NbtList writtenList = new NbtList( "Entities", null, expectedListType );
            for( int i = 0; i < elements; i++ ) {
                writtenList.Add( new NbtInt( i ) );
            }
            writtenFile.RootTag.Add( writtenList );

            // test saving
            writtenFile.SaveToFile( fileName, NbtCompression.GZip );

            // test loading
            NbtFile readFile = new NbtFile( fileName );

            // check contents of loaded file
            Assert.NotNull( readFile.RootTag );
            Assert.IsInstanceOf<NbtList>( readFile.RootTag["Entities"] );
            NbtList readList = (NbtList)readFile.RootTag["Entities"];
            Assert.AreEqual( readList.ListType, writtenList.ListType );
            Assert.AreEqual( readList.Count, writtenList.Count );

            // check .ToArray
            CollectionAssert.AreEquivalent( readList, readList.ToArray() );
            CollectionAssert.AreEquivalent( readList, readList.ToArray<NbtInt>() );

            // check contents of loaded list
            for( int i = 0; i < elements; i++ ) {
                Assert.AreEqual( readList.Get<NbtInt>( i ).Value, writtenList.Get<NbtInt>( i ).Value );
            }
        }
Example #16
0
        static void Main(string[] args)
        {
            List <NbtFile>   structures          = new List <NbtFile>();
            List <string>    blockPalette        = new List <string>();
            List <Structure> processedStructures = new List <Structure>();

            if (!Directory.Exists("input"))
            {
                Console.WriteLine("input folder not found");
                return;
            }

            string[] files = Directory.GetFiles("input", "*.nbt");

            if (files.Length == 0)
            {
                return;
            }

            foreach (string f in files)
            {
                structures.Add(new NbtFile(f));
            }

            foreach (NbtFile structure in structures)
            {
                NbtList palette = structure.RootTag.Get <NbtList>("palette");

                foreach (NbtCompound b in palette)
                {
                    blockPalette.Add(b.Get <NbtString>("Name").Value);
                }
            }

            if (!Directory.Exists("output"))
            {
                Directory.CreateDirectory("output");
            }

            if (File.Exists("output/blockPalette.txt"))
            {
                File.Delete("output/blockPalette.txt");
            }

            blockPalette = blockPalette.Distinct().ToList();

            File.WriteAllLines("output/blockPalette.txt", blockPalette);


            foreach (NbtFile structure in structures)
            {
                NbtList size = structure.RootTag.Get <NbtList>("size");

                NbtList palette = structure.RootTag.Get <NbtList>("palette");

                NbtList blocks = structure.RootTag.Get <NbtList>("blocks");

                Structure struc = new Structure(size.Get <NbtInt>(0).Value, size.Get <NbtInt>(1).Value, size.Get <NbtInt>(2).Value);

                foreach (NbtCompound b in blocks)
                {
                    int     state = b.Get <NbtInt>("state").Value;
                    NbtList pos   = b.Get <NbtList>("pos");

                    /*for (int a = 0; a < blockPalette.Count; a++)
                     * {*/
                    string name = palette.Get <NbtCompound>(state).Get <NbtString>("Name").Value;

                    /*if (blockPalette[a] == name)
                     * {*/
                    struc.blocks.Add(new Block(name.Replace("minecraft:", ""), pos.Get <NbtInt>(0).Value, pos.Get <NbtInt>(1).Value, pos.Get <NbtInt>(2).Value));

                    /*}
                     * }*/
                }

                processedStructures.Add(struc);
            }

            JsonSerializerOptions o = new JsonSerializerOptions();

            o.WriteIndented = true;

            string json = JsonSerializer.Serialize(processedStructures, o);

            if (File.Exists("output/blocks.json"))
            {
                File.Delete("output/blocks.json");
            }

            File.WriteAllText("output/blocks.json", json);
        }
Example #17
-1
        public void GettersAndSetters()
        {
            NbtCompound parent = new NbtCompound( "Parent" );
            NbtCompound child = new NbtCompound( "Child" );
            NbtCompound nestedChild = new NbtCompound( "NestedChild" );
            NbtList childList = new NbtList( "ChildList" );
            NbtList nestedChildList = new NbtList( "NestedChildList" );
            NbtInt testInt = new NbtInt( 1 );
            childList.Add( testInt );
            nestedChildList.Add( testInt );
            parent.Add( child );
            parent.Add( childList );
            child.Add( nestedChild );
            child.Add( nestedChildList );

            // Accessing nested compound tags using indexers
            Assert.AreEqual( parent["Child"]["NestedChild"], nestedChild );
            Assert.AreEqual( parent["Child"]["NestedChildList"], nestedChildList );
            Assert.AreEqual( parent["Child"]["NestedChildList"][0], testInt );

            // Accessing nested compound tags using Get<T>
            Assert.AreEqual( parent.Get<NbtCompound>( "Child" ).Get<NbtCompound>( "NestedChild" ), nestedChild );
            Assert.AreEqual( parent.Get<NbtCompound>( "Child" ).Get<NbtList>( "NestedChildList" ), nestedChildList );
            Assert.AreEqual( parent.Get<NbtCompound>( "Child" ).Get<NbtList>( "NestedChildList" )[0], testInt );

            // Accessing with Get<T> and an invalid given type
            Assert.Throws<InvalidCastException>( () => parent.Get<NbtInt>( "Child" ) );

            // Trying to use integer indexers on non-NbtList tags
            Assert.Throws<InvalidOperationException>( () => parent[0] = testInt );
            Assert.Throws<InvalidOperationException>( () => testInt[0] = testInt );

            // Trying to use string indexers on non-NbtCompound tags
            Assert.Throws<InvalidOperationException>( () => childList["test"] = testInt );
            Assert.Throws<InvalidOperationException>( () => testInt["test"] = testInt );

            // Trying to get a non-existent element by name
            Assert.IsNull( parent.Get<NbtTag>( "NonExistentTag" ) );
            Assert.IsNull( parent["NonExistentTag"] );

            // Null indices on NbtCompound
            Assert.Throws<ArgumentNullException>( () => parent.Get<NbtTag>( null ) );
            Assert.Throws<ArgumentNullException>( () => parent[null] = testInt );
            Assert.Throws<ArgumentNullException>( () => testInt = (NbtInt)parent[null] );

            // Out-of-range indices on NbtList
            Assert.Throws<ArgumentOutOfRangeException>( () => testInt = (NbtInt)childList[-1] );
            Assert.Throws<ArgumentOutOfRangeException>( () => childList[-1] = testInt );
            Assert.Throws<ArgumentOutOfRangeException>( () => testInt = childList.Get<NbtInt>( -1 ) );
            Assert.Throws<ArgumentOutOfRangeException>( () => testInt = (NbtInt)childList[childList.Count] );
            Assert.Throws<ArgumentOutOfRangeException>( () => testInt = childList.Get<NbtInt>( childList.Count ) );
        }