// 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); }
public void ChangingListTagType() { var list = new NbtList(); // changing list type to an out-of-range type Assert.Throws<ArgumentOutOfRangeException>(() => list.ListType = (NbtTagType)200); // failing to add or insert a tag should not change ListType Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(-1, new NbtInt())); Assert.Throws<ArgumentException>(() => list.Add(new NbtInt("namedTagWhereUnnamedIsExpected"))); Assert.AreEqual(NbtTagType.Unknown, list.ListType); // changing the type of an empty list to "End" is allowed, see https://github.com/fragmer/fNbt/issues/12 Assert.DoesNotThrow(() => list.ListType = NbtTagType.End); Assert.AreEqual(list.ListType, NbtTagType.End); // changing the type of an empty list back to "Unknown" is allowed too! Assert.DoesNotThrow(() => list.ListType = NbtTagType.Unknown); Assert.AreEqual(list.ListType, NbtTagType.Unknown); // adding the first element should set the tag type list.Add(new NbtInt()); Assert.AreEqual(list.ListType, NbtTagType.Int); // setting correct type for a non-empty list Assert.DoesNotThrow(() => list.ListType = NbtTagType.Int); // changing list type to an incorrect type Assert.Throws<ArgumentException>(() => list.ListType = NbtTagType.Short); // after the list is cleared, we should once again be allowed to change its TagType list.Clear(); Assert.DoesNotThrow(() => list.ListType = NbtTagType.Short); }
public void ChangingListTagType() { var list = new NbtList(); // changing list type to an out-of-range type Assert.Throws <ArgumentOutOfRangeException>(() => list.ListType = (NbtTagType)200); // failing to add or insert a tag should not change ListType Assert.Throws <ArgumentOutOfRangeException>(() => list.Insert(-1, new NbtInt())); Assert.Throws <ArgumentException>(() => list.Add(new NbtInt("namedTagWhereUnnamedIsExpected"))); Assert.AreEqual(NbtTagType.Unknown, list.ListType); // changing the type of an empty list to "End" is allowed, see https://github.com/fragmer/fNbt/issues/12 Assert.DoesNotThrow(() => list.ListType = NbtTagType.End); Assert.AreEqual(list.ListType, NbtTagType.End); // changing the type of an empty list back to "Unknown" is allowed too! Assert.DoesNotThrow(() => list.ListType = NbtTagType.Unknown); Assert.AreEqual(list.ListType, NbtTagType.Unknown); // adding the first element should set the tag type list.Add(new NbtInt()); Assert.AreEqual(list.ListType, NbtTagType.Int); // setting correct type for a non-empty list Assert.DoesNotThrow(() => list.ListType = NbtTagType.Int); // changing list type to an incorrect type Assert.Throws <ArgumentException>(() => list.ListType = NbtTagType.Short); // after the list is cleared, we should once again be allowed to change its TagType list.Clear(); Assert.DoesNotThrow(() => list.ListType = NbtTagType.Short); }
public void FirstInsertTest() { NbtList list = new NbtList(); Assert.AreEqual(NbtTagType.Unknown, list.ListType); list.Insert(0, new NbtInt(123)); // Inserting a tag should set ListType Assert.AreEqual(NbtTagType.Int, list.ListType); }
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)); }
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)); }
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)); }
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 ) ); }