Example #1
0
        // returns whether there was enough room to fit the chests
        protected bool PutChestsInInventory(NbtList invtag, IEnumerable <long> mapids)
        {
            // add to chests one by one
            var slots   = GetFreeSlots(invtag);
            int total   = mapids.Count();
            int current = 0;

            foreach (var slot in slots)
            {
                var chestcontents = mapids.Skip(current).Take(27);
                var chest         = CreateChest(chestcontents);
                chest.Add(new NbtByte("Slot", slot));
                // bedrock-specific lines, replace existing item in this slot which should only be air
                var existingitem = invtag.Where(x => x["Slot"].ByteValue == slot).FirstOrDefault();
                if (existingitem != null)
                {
                    invtag.Insert(invtag.IndexOf(existingitem), chest);
                    invtag.Remove(existingitem);
                }
                else
                {
                    invtag.ListType = NbtTagType.Compound;
                    invtag.Add(chest);
                }
                current += 27;
                if (current >= total)
                {
                    return(true);
                }
            }
            return(false);
        }
Example #2
0
        public void ManipulatingList()
        {
            var sameTags = new NbtTag[]
            {
                new NbtInt(0),
                new NbtInt(1),
                new NbtInt(2)
            };

            var list = new NbtList("Test1", sameTags);

            // testing enumerator
            var j = 0;

            foreach (var tag in list)
            {
                Assert.AreEqual(tag, sameTags[j++]);
            }

            // adding an item of correct type
            list.Add(new NbtInt(3));
            list.Insert(3, new NbtInt(4));

            // adding an item of wrong type
            Assert.Throws <ArgumentException>(() => list.Add(new NbtString()));
            Assert.Throws <ArgumentException>(() => list.Insert(3, new NbtString()));

            // testing array contents
            for (var i = 0; i < sameTags.Length; i++)
            {
                Assert.AreSame(sameTags[i], list[i]);
                Assert.AreEqual(((NbtInt)list[i]).Value, i);
            }

            // test removal
            Assert.IsFalse(list.Remove(new NbtInt(5)));
            Assert.IsTrue(list.Remove(sameTags[0]));
            list.RemoveAt(0);
            Assert.Throws <ArgumentOutOfRangeException>(() => list.RemoveAt(10));
        }
Example #3
0
        public void ManipulatingList()
        {
            var sameTags = new NbtTag[] {
                new NbtInt(0),
                new NbtInt(1),
                new NbtInt(2)
            };

            var list = new NbtList("Test1", sameTags);

            // testing enumerator, indexer, Contains, and IndexOf
            int j = 0;

            foreach (NbtTag tag in list)
            {
                Assert.IsTrue(list.Contains(sameTags[j]));
                Assert.AreEqual(sameTags[j], tag);
                Assert.AreEqual(j, list.IndexOf(tag));
                j++;
            }

            // adding an item of correct type
            list.Add(new NbtInt(3));
            list.Insert(3, new NbtInt(4));

            // adding an item of wrong type
            Assert.Throws <ArgumentException>(() => list.Add(new NbtString()));
            Assert.Throws <ArgumentException>(() => list.Insert(3, new NbtString()));
            Assert.Throws <ArgumentNullException>(() => list.Insert(3, null));

            // testing array contents
            for (int i = 0; i < sameTags.Length; i++)
            {
                Assert.AreSame(sameTags[i], list[i]);
                Assert.AreEqual(i, ((NbtInt)list[i]).Value);
            }

            // test removal
            Assert.IsFalse(list.Remove(new NbtInt(5)));
            Assert.IsTrue(list.Remove(sameTags[0]));
            Assert.Throws <ArgumentNullException>(() => list.Remove(null));
            list.RemoveAt(0);
            Assert.Throws <ArgumentOutOfRangeException>(() => list.RemoveAt(10));

            // Test some failure scenarios for Add:
            // adding a list to itself
            var loopList = new NbtList();

            Assert.AreEqual(NbtTagType.Unknown, loopList.ListType);
            Assert.Throws <ArgumentException>(() => loopList.Add(loopList));

            // adding same tag to multiple lists
            Assert.Throws <ArgumentException>(() => loopList.Add(list[0]));
            Assert.Throws <ArgumentException>(() => loopList.Insert(0, list[0]));

            // adding null tag
            Assert.Throws <ArgumentNullException>(() => loopList.Add(null));

            // make sure that all those failed adds didn't affect the tag
            Assert.AreEqual(0, loopList.Count);
            Assert.AreEqual(NbtTagType.Unknown, loopList.ListType);

            // try creating a list with invalid tag type
            Assert.Throws <ArgumentOutOfRangeException>(() => new NbtList((NbtTagType)200));
        }
Example #4
0
        public void ManipulatingList()
        {
            var sameTags = new NbtTag[] {
                new NbtInt(0),
                new NbtInt(1),
                new NbtInt(2)
            };

            var list = new NbtList("Test1", sameTags);

            // testing enumerator, indexer, Contains, and IndexOf
            int j = 0;
            foreach (NbtTag tag in list) {
                Assert.IsTrue(list.Contains(sameTags[j]));
                Assert.AreEqual(sameTags[j], tag);
                Assert.AreEqual(j, list.IndexOf(tag));
                j++;
            }

            // adding an item of correct type
            list.Add(new NbtInt(3));
            list.Insert(3, new NbtInt(4));

            // adding an item of wrong type
            Assert.Throws<ArgumentException>(() => list.Add(new NbtString()));
            Assert.Throws<ArgumentException>(() => list.Insert(3, new NbtString()));
            Assert.Throws<ArgumentNullException>(() => list.Insert(3, null));

            // testing array contents
            for (int i = 0; i < sameTags.Length; i++) {
                Assert.AreSame(sameTags[i], list[i]);
                Assert.AreEqual(i, ((NbtInt)list[i]).Value);
            }

            // test removal
            Assert.IsFalse(list.Remove(new NbtInt(5)));
            Assert.IsTrue(list.Remove(sameTags[0]));
            Assert.Throws<ArgumentNullException>(() => list.Remove(null));
            list.RemoveAt(0);
            Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(10));

            // Test some failure scenarios for Add:
            // adding a list to itself
            var loopList = new NbtList();
            Assert.AreEqual(NbtTagType.Unknown, loopList.ListType);
            Assert.Throws<ArgumentException>(() => loopList.Add(loopList));

            // adding same tag to multiple lists
            Assert.Throws<ArgumentException>(() => loopList.Add(list[0]));
            Assert.Throws<ArgumentException>(() => loopList.Insert(0, list[0]));

            // adding null tag
            Assert.Throws<ArgumentNullException>(() => loopList.Add(null));

            // make sure that all those failed adds didn't affect the tag
            Assert.AreEqual(0, loopList.Count);
            Assert.AreEqual(NbtTagType.Unknown, loopList.ListType);

            // try creating a list with invalid tag type
            Assert.Throws<ArgumentOutOfRangeException>(() => new NbtList((NbtTagType)200));
        }
Example #5
0
        public void ManipulatingList()
        {
            var sameTags = new NbtTag[] {
                new NbtInt( 0 ),
                new NbtInt( 1 ),
                new NbtInt( 2 )
            };

            NbtList list = new NbtList( "Test1", sameTags );

            // testing enumerator
            int j = 0;
            foreach( NbtTag tag in list ) {
                Assert.AreEqual( tag, sameTags[j++] );
            }

            // adding an item of correct type
            list.Add( new NbtInt( 3 ) );
            list.Insert( 3, new NbtInt( 4 ) );

            // adding an item of wrong type
            Assert.Throws<ArgumentException>( () => list.Add( new NbtString() ) );
            Assert.Throws<ArgumentException>( () => list.Insert( 3, new NbtString() ) );

            // testing array contents
            for( int i = 0; i < sameTags.Length; i++ ) {
                Assert.AreSame( sameTags[i], list[i] );
                Assert.AreEqual( ( (NbtInt)list[i] ).Value, i );
            }

            // test removal
            Assert.IsFalse( list.Remove( new NbtInt( 5 ) ) );
            Assert.IsTrue( list.Remove( sameTags[0] ) );
            list.RemoveAt( 0 );
            Assert.Throws<ArgumentOutOfRangeException>( () => list.RemoveAt( 10 ) );
        }