public static double getDouble(this NbtCompound tag, string name, double defaultValue = 0) { NbtDouble tag1 = tag.Get <NbtDouble>(name); if (tag1 == null) { return(defaultValue); } else { return(tag1.Value); } }
public void NbtDoubleTest() { object dummy; NbtTag test = new NbtDouble( 0.4931287132182315 ); Assert.Throws<InvalidCastException>( () => dummy = test.ByteArrayValue ); Assert.Throws<InvalidCastException>( () => dummy = test.ByteValue ); Assert.AreEqual( 0.4931287132182315, test.DoubleValue ); Assert.AreEqual( (float)0.4931287132182315, test.FloatValue ); Assert.Throws<InvalidCastException>( () => dummy = test.IntArrayValue ); Assert.Throws<InvalidCastException>( () => dummy = test.IntValue ); Assert.Throws<InvalidCastException>( () => dummy = test.LongValue ); Assert.Throws<InvalidCastException>( () => dummy = test.ShortValue ); Assert.AreEqual( ( 0.4931287132182315 ).ToString( CultureInfo.InvariantCulture ), test.StringValue ); }
public void NbtDoubleTest() { object dummy; NbtTag test = new NbtDouble(0.4931287132182315); Assert.Throws <InvalidCastException>(() => dummy = test.ByteArrayValue); Assert.Throws <InvalidCastException>(() => dummy = test.ByteValue); Assert.AreEqual(0.4931287132182315, test.DoubleValue); Assert.AreEqual((float)0.4931287132182315, test.FloatValue); Assert.Throws <InvalidCastException>(() => dummy = test.IntArrayValue); Assert.Throws <InvalidCastException>(() => dummy = test.IntValue); Assert.Throws <InvalidCastException>(() => dummy = test.LongValue); Assert.Throws <InvalidCastException>(() => dummy = test.ShortValue); Assert.AreEqual(0.4931287132182315.ToString(CultureInfo.InvariantCulture), test.StringValue); }
public static string ToSnbt(this NbtDouble tag, SnbtOptions options) { string result; if (double.IsPositiveInfinity(tag.Value)) { result = "Infinity"; } else if (double.IsNegativeInfinity(tag.Value)) { result = "-Infinity"; } else if (double.IsNaN(tag.Value)) { result = "NaN"; } else { result = DataUtils.DoubleToString(tag.Value); } return(result + OptionalSuffix(options, DOUBLE_SUFFIX)); }
public void SetPropertyValue <T>(NbtTag tag, Expression <Func <T> > property, bool upperFirst = true) { var propertyInfo = ((MemberExpression)property.Body).Member as PropertyInfo; if (propertyInfo == null) { throw new ArgumentException("The lambda expression 'property' should point to a valid Property"); } NbtTag nbtTag = tag[propertyInfo.Name]; if (nbtTag == null) { nbtTag = tag[LowercaseFirst(propertyInfo.Name)]; } if (nbtTag == null) { if (propertyInfo.PropertyType == typeof(bool)) { nbtTag = new NbtByte(propertyInfo.Name); } else if (propertyInfo.PropertyType == typeof(byte)) { nbtTag = new NbtByte(LowercaseFirst(propertyInfo.Name)); } else if (propertyInfo.PropertyType == typeof(short)) { nbtTag = new NbtShort(LowercaseFirst(propertyInfo.Name)); } else if (propertyInfo.PropertyType == typeof(int)) { nbtTag = new NbtInt(LowercaseFirst(propertyInfo.Name)); } else if (propertyInfo.PropertyType == typeof(long)) { nbtTag = new NbtLong(LowercaseFirst(propertyInfo.Name)); } else if (propertyInfo.PropertyType == typeof(float)) { nbtTag = new NbtFloat(LowercaseFirst(propertyInfo.Name)); } else if (propertyInfo.PropertyType == typeof(double)) { nbtTag = new NbtDouble(LowercaseFirst(propertyInfo.Name)); } else if (propertyInfo.PropertyType == typeof(string)) { nbtTag = new NbtString(LowercaseFirst(propertyInfo.Name), ""); } else { return; } } var mex = property.Body as MemberExpression; var target = Expression.Lambda(mex.Expression).Compile().DynamicInvoke(); switch (nbtTag.TagType) { case NbtTagType.Unknown: break; case NbtTagType.End: break; case NbtTagType.Byte: if (propertyInfo.PropertyType == typeof(bool)) { tag[nbtTag.Name] = new NbtByte(nbtTag.Name, (byte)((bool)propertyInfo.GetValue(target) ? 1 : 0)); } else { tag[nbtTag.Name] = new NbtByte(nbtTag.Name, (byte)propertyInfo.GetValue(target)); } break; case NbtTagType.Short: tag[nbtTag.Name] = new NbtShort(nbtTag.Name, (short)propertyInfo.GetValue(target)); break; case NbtTagType.Int: if (propertyInfo.PropertyType == typeof(bool)) { tag[nbtTag.Name] = new NbtInt(nbtTag.Name, (bool)propertyInfo.GetValue(target) ? 1 : 0); } else { tag[nbtTag.Name] = new NbtInt(nbtTag.Name, (int)propertyInfo.GetValue(target)); } break; case NbtTagType.Long: tag[nbtTag.Name] = new NbtLong(nbtTag.Name, (long)propertyInfo.GetValue(target)); break; case NbtTagType.Float: tag[nbtTag.Name] = new NbtFloat(nbtTag.Name, (float)propertyInfo.GetValue(target)); break; case NbtTagType.Double: tag[nbtTag.Name] = new NbtDouble(nbtTag.Name, (double)propertyInfo.GetValue(target)); break; case NbtTagType.ByteArray: tag[nbtTag.Name] = new NbtByteArray(nbtTag.Name, (byte[])propertyInfo.GetValue(target)); break; case NbtTagType.String: tag[nbtTag.Name] = new NbtString(nbtTag.Name, (string)propertyInfo.GetValue(target) ?? ""); break; case NbtTagType.List: break; case NbtTagType.Compound: break; case NbtTagType.IntArray: tag[nbtTag.Name] = new NbtIntArray(nbtTag.Name, (int[])propertyInfo.GetValue(target)); break; default: throw new ArgumentOutOfRangeException(); } //return (T) propertyInfo.GetValue(target); }
public void CopyConstructorTest() { NbtByte byteTag = new NbtByte("byteTag", 1); NbtByte byteTagClone = (NbtByte)byteTag.Clone(); Assert.AreNotSame(byteTag, byteTagClone); Assert.AreEqual(byteTag.Name, byteTagClone.Name); Assert.AreEqual(byteTag.Value, byteTagClone.Value); Assert.Throws <ArgumentNullException>(() => new NbtByte((NbtByte)null)); NbtByteArray byteArrTag = new NbtByteArray("byteArrTag", new byte[] { 1, 2, 3, 4 }); NbtByteArray byteArrTagClone = (NbtByteArray)byteArrTag.Clone(); Assert.AreNotSame(byteArrTag, byteArrTagClone); Assert.AreEqual(byteArrTag.Name, byteArrTagClone.Name); Assert.AreNotSame(byteArrTag.Value, byteArrTagClone.Value); CollectionAssert.AreEqual(byteArrTag.Value, byteArrTagClone.Value); Assert.Throws <ArgumentNullException>(() => new NbtByteArray((NbtByteArray)null)); NbtCompound compTag = new NbtCompound("compTag", new NbtTag[] { new NbtByte("innerTag", 1) }); NbtCompound compTagClone = (NbtCompound)compTag.Clone(); Assert.AreNotSame(compTag, compTagClone); Assert.AreEqual(compTag.Name, compTagClone.Name); Assert.AreNotSame(compTag["innerTag"], compTagClone["innerTag"]); Assert.AreEqual(compTag["innerTag"].Name, compTagClone["innerTag"].Name); Assert.AreEqual(compTag["innerTag"].ByteValue, compTagClone["innerTag"].ByteValue); Assert.Throws <ArgumentNullException>(() => new NbtCompound((NbtCompound)null)); NbtDouble doubleTag = new NbtDouble("doubleTag", 1); NbtDouble doubleTagClone = (NbtDouble)doubleTag.Clone(); Assert.AreNotSame(doubleTag, doubleTagClone); Assert.AreEqual(doubleTag.Name, doubleTagClone.Name); Assert.AreEqual(doubleTag.Value, doubleTagClone.Value); Assert.Throws <ArgumentNullException>(() => new NbtDouble((NbtDouble)null)); NbtFloat floatTag = new NbtFloat("floatTag", 1); NbtFloat floatTagClone = (NbtFloat)floatTag.Clone(); Assert.AreNotSame(floatTag, floatTagClone); Assert.AreEqual(floatTag.Name, floatTagClone.Name); Assert.AreEqual(floatTag.Value, floatTagClone.Value); Assert.Throws <ArgumentNullException>(() => new NbtFloat((NbtFloat)null)); NbtInt intTag = new NbtInt("intTag", 1); NbtInt intTagClone = (NbtInt)intTag.Clone(); Assert.AreNotSame(intTag, intTagClone); Assert.AreEqual(intTag.Name, intTagClone.Name); Assert.AreEqual(intTag.Value, intTagClone.Value); Assert.Throws <ArgumentNullException>(() => new NbtInt((NbtInt)null)); NbtIntArray intArrTag = new NbtIntArray("intArrTag", new[] { 1, 2, 3, 4 }); NbtIntArray intArrTagClone = (NbtIntArray)intArrTag.Clone(); Assert.AreNotSame(intArrTag, intArrTagClone); Assert.AreEqual(intArrTag.Name, intArrTagClone.Name); Assert.AreNotSame(intArrTag.Value, intArrTagClone.Value); CollectionAssert.AreEqual(intArrTag.Value, intArrTagClone.Value); Assert.Throws <ArgumentNullException>(() => new NbtIntArray((NbtIntArray)null)); NbtList listTag = new NbtList("listTag", new NbtTag[] { new NbtByte(1) }); NbtList listTagClone = (NbtList)listTag.Clone(); Assert.AreNotSame(listTag, listTagClone); Assert.AreEqual(listTag.Name, listTagClone.Name); Assert.AreNotSame(listTag[0], listTagClone[0]); Assert.AreEqual(listTag[0].ByteValue, listTagClone[0].ByteValue); Assert.Throws <ArgumentNullException>(() => new NbtList((NbtList)null)); NbtLong longTag = new NbtLong("longTag", 1); NbtLong longTagClone = (NbtLong)longTag.Clone(); Assert.AreNotSame(longTag, longTagClone); Assert.AreEqual(longTag.Name, longTagClone.Name); Assert.AreEqual(longTag.Value, longTagClone.Value); Assert.Throws <ArgumentNullException>(() => new NbtLong((NbtLong)null)); NbtShort shortTag = new NbtShort("shortTag", 1); NbtShort shortTagClone = (NbtShort)shortTag.Clone(); Assert.AreNotSame(shortTag, shortTagClone); Assert.AreEqual(shortTag.Name, shortTagClone.Name); Assert.AreEqual(shortTag.Value, shortTagClone.Value); Assert.Throws <ArgumentNullException>(() => new NbtShort((NbtShort)null)); NbtString stringTag = new NbtString("stringTag", "foo"); NbtString stringTagClone = (NbtString)stringTag.Clone(); Assert.AreNotSame(stringTag, stringTagClone); Assert.AreEqual(stringTag.Name, stringTagClone.Name); Assert.AreEqual(stringTag.Value, stringTagClone.Value); Assert.Throws <ArgumentNullException>(() => new NbtString((NbtString)null)); }
public static NbtTag TreeToNBT(TreeNodeCollection tree) { NbtTag NBTTag = null; if (tree != null) { foreach (TreeNode node in tree) { string value = node.Text.Replace($"{node.Name}: ", string.Empty).Replace(".", ",");//TODO , or . by language try { switch (node.ImageKey) { case "buttonstring.png": { NBTTag = new NbtString(node.Name, value); break; } case "buttonint.png": { NBTTag = new NbtInt(node.Name, int.Parse(value)); break; } case "buttonbyte.png": { NBTTag = new NbtByte(node.Name, byte.Parse(value)); break; } case "buttonlong.png": { NBTTag = new NbtLong(node.Name, long.Parse(value)); break; } case "buttonshort.png": { NBTTag = new NbtShort(node.Name, short.Parse(value)); break; } case "buttonfloat.png": { NBTTag = new NbtFloat(node.Name, float.Parse(value)); break; } case "buttondouble.png": { NBTTag = new NbtDouble(node.Name, double.Parse(value)); break; } case "buttoncompound.png": { NBTTag = new NbtCompound(node.Name); foreach (object c in node.Nodes) { (new AlertForm(c.ToString())).ShowDialog(); if (c is TreeNode && ((TreeNode)c).Nodes.Count > 0) { ((NbtCompound)NBTTag).Add(TreeToNBT(((TreeNode)c).Nodes)); } else { (new AlertForm(c.GetType().ToString())).ShowDialog(); } } break; } case "buttonlist.png": { NBTTag = new NbtList(node.Name); foreach (object c in node.Nodes) { (new AlertForm(c.ToString())).ShowDialog(); if (c is TreeNode && ((TreeNode)c).Nodes.Count > 0) { ((NbtList)NBTTag).Add(TreeToNBT(((TreeNode)c).Nodes)); } else { (new AlertForm(c.GetType().ToString())).ShowDialog(); } } break; } case "buttonbytearray.png": { NBTTag = new NbtByteArray(node.Name, NodesToByteArray(node.Nodes)); (new AlertForm(NBTTag.ToString())).ShowDialog(); break; } case "buttonintarray.png": { NBTTag = new NbtIntArray(node.Name, NodesToIntArray(node.Nodes)); (new AlertForm(NBTTag.ToString())).ShowDialog(); break; } default: { throw new WarningException($"Warning: unhandeled node {node.ImageKey} can not be edited"); } } } catch (Exception exception) { if (exception is InvalidOperationException || exception is WarningException) { new AlertForm(exception.ToString()) { Text = "Warning!", Icon = System.Drawing.SystemIcons.Warning }.ShowDialog(); } else { new AlertForm(exception.ToString()) { Text = exception.GetType().ToString(), Icon = System.Drawing.SystemIcons.Error }.ShowDialog(); } } } } return(NBTTag); }
/** * BEFORE any digits, requiring backtracking. */ private NbtTag ParseNumber() { bool more = true; NbtTagType type = NbtTagType.Int; String literal = ""; while (more) { char next; try { next = NextChar(); } catch (Exception) { next = ' '; //just end the number } if (next <= '9' && next >= '0' || next == '-') { literal += next; } else if (next == '.') { literal += next; type = NbtTagType.Double; } else { more = false; switch (next) { case 'l': case 'L': type = NbtTagType.Long; break; case 'b': case 'B': type = NbtTagType.Byte; break; case 'f': case 'F': type = NbtTagType.Float; break; default: //End of number, put it back. Position--; break; } } } NbtTag ret; switch (type) { case NbtTagType.Byte: ret = new NbtByte(byte.Parse(literal)); break; case NbtTagType.Int: ret = new NbtInt(int.Parse(literal)); break; case NbtTagType.Long: ret = new NbtLong(long.Parse(literal)); break; case NbtTagType.Float: ret = new NbtFloat(float.Parse(literal)); break; case NbtTagType.Double: ret = new NbtDouble(double.Parse(literal)); break; default: ret = null; break; } return(ret); }
public static NbtTag Serialize <T>(this T obj, NbtTag tag = null) where T : new() { tag ??= new NbtCompound(string.Empty); if (obj == null) { throw new NullReferenceException(); } PropertyInfo[] properties = obj.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in properties) { var attribute = propertyInfo.GetCustomAttribute(typeof(JsonPropertyNameAttribute)) as JsonPropertyNameAttribute; string propertyName = attribute?.Name ?? propertyInfo.Name; NbtTag nbtTag = tag[propertyName] ?? tag[LowercaseFirst(propertyName)]; if (nbtTag == null) { if (propertyInfo.PropertyType == typeof(bool)) { nbtTag = new NbtByte(propertyName); } else if (propertyInfo.PropertyType == typeof(byte)) { nbtTag = new NbtByte(propertyName); } else if (propertyInfo.PropertyType == typeof(short)) { nbtTag = new NbtShort(propertyName); } else if (propertyInfo.PropertyType == typeof(int)) { nbtTag = new NbtInt(propertyName); } else if (propertyInfo.PropertyType == typeof(long)) { nbtTag = new NbtLong(propertyName); } else if (propertyInfo.PropertyType == typeof(float)) { nbtTag = new NbtFloat(propertyName); } else if (propertyInfo.PropertyType == typeof(double)) { nbtTag = new NbtDouble(propertyName); } else if (propertyInfo.PropertyType == typeof(string)) { nbtTag = new NbtString(propertyName, ""); } else { continue; } } //var mex = property.Body as MemberExpression; //var target = Expression.Lambda(mex.Expression).Compile().DynamicInvoke(); switch (nbtTag.TagType) { case NbtTagType.Unknown: break; case NbtTagType.End: break; case NbtTagType.Byte: if (propertyInfo.PropertyType == typeof(bool)) { tag[nbtTag.Name] = new NbtByte(nbtTag.Name, (byte)((bool)propertyInfo.GetValue(obj) ? 1 : 0)); } else { tag[nbtTag.Name] = new NbtByte(nbtTag.Name, (byte)propertyInfo.GetValue(obj)); } break; case NbtTagType.Short: tag[nbtTag.Name] = new NbtShort(nbtTag.Name, (short)propertyInfo.GetValue(obj)); break; case NbtTagType.Int: if (propertyInfo.PropertyType == typeof(bool)) { tag[nbtTag.Name] = new NbtInt(nbtTag.Name, (bool)propertyInfo.GetValue(obj) ? 1 : 0); } else { tag[nbtTag.Name] = new NbtInt(nbtTag.Name, (int)propertyInfo.GetValue(obj)); } break; case NbtTagType.Long: tag[nbtTag.Name] = new NbtLong(nbtTag.Name, (long)propertyInfo.GetValue(obj)); break; case NbtTagType.Float: tag[nbtTag.Name] = new NbtFloat(nbtTag.Name, (float)propertyInfo.GetValue(obj)); break; case NbtTagType.Double: tag[nbtTag.Name] = new NbtDouble(nbtTag.Name, (double)propertyInfo.GetValue(obj)); break; case NbtTagType.ByteArray: tag[nbtTag.Name] = new NbtByteArray(nbtTag.Name, (byte[])propertyInfo.GetValue(obj)); break; case NbtTagType.String: tag[nbtTag.Name] = new NbtString(nbtTag.Name, (string)propertyInfo.GetValue(obj) ?? ""); break; case NbtTagType.List: break; case NbtTagType.Compound: break; case NbtTagType.IntArray: tag[nbtTag.Name] = new NbtIntArray(nbtTag.Name, (int[])propertyInfo.GetValue(obj)); break; } } return(tag); }
public void CopyConstructorTest() { NbtByte byteTag = new NbtByte("byteTag", 1); NbtByte byteTagClone = (NbtByte)byteTag.Clone(); Assert.AreNotSame(byteTag, byteTagClone); Assert.AreEqual(byteTag.Name, byteTagClone.Name); Assert.AreEqual(byteTag.Value, byteTagClone.Value); Assert.Throws<ArgumentNullException>(() => new NbtByte((NbtByte)null)); NbtByteArray byteArrTag = new NbtByteArray("byteArrTag", new byte[] { 1, 2, 3, 4 }); NbtByteArray byteArrTagClone = (NbtByteArray)byteArrTag.Clone(); Assert.AreNotSame(byteArrTag, byteArrTagClone); Assert.AreEqual(byteArrTag.Name, byteArrTagClone.Name); Assert.AreNotSame(byteArrTag.Value, byteArrTagClone.Value); CollectionAssert.AreEqual(byteArrTag.Value, byteArrTagClone.Value); Assert.Throws<ArgumentNullException>(() => new NbtByteArray((NbtByteArray)null)); NbtCompound compTag = new NbtCompound("compTag", new NbtTag[] { new NbtByte("innerTag", 1) }); NbtCompound compTagClone = (NbtCompound)compTag.Clone(); Assert.AreNotSame(compTag, compTagClone); Assert.AreEqual(compTag.Name, compTagClone.Name); Assert.AreNotSame(compTag["innerTag"], compTagClone["innerTag"]); Assert.AreEqual(compTag["innerTag"].Name, compTagClone["innerTag"].Name); Assert.AreEqual(compTag["innerTag"].ByteValue, compTagClone["innerTag"].ByteValue); Assert.Throws<ArgumentNullException>(() => new NbtCompound((NbtCompound)null)); NbtDouble doubleTag = new NbtDouble("doubleTag", 1); NbtDouble doubleTagClone = (NbtDouble)doubleTag.Clone(); Assert.AreNotSame(doubleTag, doubleTagClone); Assert.AreEqual(doubleTag.Name, doubleTagClone.Name); Assert.AreEqual(doubleTag.Value, doubleTagClone.Value); Assert.Throws<ArgumentNullException>(() => new NbtDouble((NbtDouble)null)); NbtFloat floatTag = new NbtFloat("floatTag", 1); NbtFloat floatTagClone = (NbtFloat)floatTag.Clone(); Assert.AreNotSame(floatTag, floatTagClone); Assert.AreEqual(floatTag.Name, floatTagClone.Name); Assert.AreEqual(floatTag.Value, floatTagClone.Value); Assert.Throws<ArgumentNullException>(() => new NbtFloat((NbtFloat)null)); NbtInt intTag = new NbtInt("intTag", 1); NbtInt intTagClone = (NbtInt)intTag.Clone(); Assert.AreNotSame(intTag, intTagClone); Assert.AreEqual(intTag.Name, intTagClone.Name); Assert.AreEqual(intTag.Value, intTagClone.Value); Assert.Throws<ArgumentNullException>(() => new NbtInt((NbtInt)null)); NbtIntArray intArrTag = new NbtIntArray("intArrTag", new[] { 1, 2, 3, 4 }); NbtIntArray intArrTagClone = (NbtIntArray)intArrTag.Clone(); Assert.AreNotSame(intArrTag, intArrTagClone); Assert.AreEqual(intArrTag.Name, intArrTagClone.Name); Assert.AreNotSame(intArrTag.Value, intArrTagClone.Value); CollectionAssert.AreEqual(intArrTag.Value, intArrTagClone.Value); Assert.Throws<ArgumentNullException>(() => new NbtIntArray((NbtIntArray)null)); NbtList listTag = new NbtList("listTag", new NbtTag[] { new NbtByte(1) }); NbtList listTagClone = (NbtList)listTag.Clone(); Assert.AreNotSame(listTag, listTagClone); Assert.AreEqual(listTag.Name, listTagClone.Name); Assert.AreNotSame(listTag[0], listTagClone[0]); Assert.AreEqual(listTag[0].ByteValue, listTagClone[0].ByteValue); Assert.Throws<ArgumentNullException>(() => new NbtList((NbtList)null)); NbtLong longTag = new NbtLong("longTag", 1); NbtLong longTagClone = (NbtLong)longTag.Clone(); Assert.AreNotSame(longTag, longTagClone); Assert.AreEqual(longTag.Name, longTagClone.Name); Assert.AreEqual(longTag.Value, longTagClone.Value); Assert.Throws<ArgumentNullException>(() => new NbtLong((NbtLong)null)); NbtShort shortTag = new NbtShort("shortTag", 1); NbtShort shortTagClone = (NbtShort)shortTag.Clone(); Assert.AreNotSame(shortTag, shortTagClone); Assert.AreEqual(shortTag.Name, shortTagClone.Name); Assert.AreEqual(shortTag.Value, shortTagClone.Value); Assert.Throws<ArgumentNullException>(() => new NbtShort((NbtShort)null)); NbtString stringTag = new NbtString("stringTag", "foo"); NbtString stringTagClone = (NbtString)stringTag.Clone(); Assert.AreNotSame(stringTag, stringTagClone); Assert.AreEqual(stringTag.Name, stringTagClone.Name); Assert.AreEqual(stringTag.Value, stringTagClone.Value); Assert.Throws<ArgumentNullException>(() => new NbtString((NbtString)null)); }
public void Write_DoubleTag() { // Arrange MemoryStream stream = new MemoryStream(); NbtWriter writer = new NbtWriter(stream); NbtDouble tag = new NbtDouble("asdf", 3.14159265358979311599796346854E0); byte[] expected = new byte[] { 0x06, 0x00, 0x04, 0x61, 0x73, 0x64, 0x66, 0x40, 0x09, 0x21, 0xFB, 0x54, 0x44, 0x2D, 0x18 }; // Act writer.Write(tag); byte[] result = stream.ToArray(); // Assert CollectionAssert.AreEqual(expected, result); }
internal void Write(NbtDouble tag, bool writeHeader) { Write(tag.Name, tag.Value, writeHeader); }
/// <summary> /// Writes out the specified tag. /// </summary> /// <param name="tag">The tag.</param> /// <exception cref="System.ArgumentNullException"><paramref name="tag"/> is <c>null</c>.</exception> /// <exception cref="System.ObjectDisposedException">The stream is closed.</exception> /// <exception cref="System.IO.IOException">An I/O error occured.</exception> public void Write(NbtDouble tag) { if (tag == null) throw new ArgumentNullException("tag", "tag is null."); Write(tag, true); }
static void RunOptions(Options opts) { Console.WriteLine($"PlayerMover {Assembly.GetExecutingAssembly().GetName().Version}\n"); // validate position try { var _ = opts.NewPos; } catch (Exception) { Console.WriteLine("Failed to parse position argument"); return; } // validate rotation try { var _ = opts.NewRot; } catch (Exception) { Console.WriteLine("Failed to parse rotation argument"); return; } // log settings Console.WriteLine($"Playerdata dir: {opts.DataDir}"); Console.WriteLine($"Moving players in world: {opts.FromWorldUUID}"); Console.WriteLine($"Moving players to world: DIM: {opts.Dimension} {opts.ToWorldName}/{opts.ToWorldUUID} at ({opts.NewPos}) / ({opts.NewRot})"); // confirm Console.WriteLine("\nPLEASE MAKE A BACKUP OF YOUR WORLD BEFORE RUNNING THIS SOFTWARE - PRESS Y TO CONTINUE, OR ANY OTHER KEY TO QUIT\n"); if (!new char[] { 'y', 'Y' }.Contains(Console.ReadKey(true).KeyChar)) { return; // force user to enter y } // load files string[] files = Directory.GetFiles(opts.DataDir, "*.dat"); if (opts.Verbose) { Console.WriteLine($"\nLoaded {files.Length} player files"); } // open stream to the output file for putting uuids using StreamWriter sw = new StreamWriter(File.OpenWrite("MovedPlayers.txt")); foreach (var fileName in files) { if (opts.Verbose) { Console.WriteLine($"\nLoading file: {fileName}"); } var myFile = new NbtFile(fileName); // read uuid bytes var worldLeast = myFile.RootTag.Get <NbtLong>("WorldUUIDLeast"); var worldMost = myFile.RootTag.Get <NbtLong>("WorldUUIDMost"); // verify uuid bytes if (worldLeast == null || worldMost == null) { throw new Exception($"NBT File {fileName} does not contain a valid world UUID"); } var uuid = UUID.Read(worldMost.Value, worldLeast.Value); if (opts.Verbose) { Console.WriteLine($"Existing world UUID: {uuid}"); } // are we moving the player? if (uuid.Equals(opts.FromWorldUUID)) { //read player's uuid var uuidLeast = myFile.RootTag.Get <NbtLong>("UUIDLeast"); var uuidMost = myFile.RootTag.Get <NbtLong>("UUIDMost"); // verify uuid bytes if (worldLeast == null || worldMost == null) { throw new Exception($"NBT File {fileName} does not contain a valid player UUID"); } var playerUuid = UUID.Read(uuidMost.Value, uuidLeast.Value); Console.WriteLine($"Moving {playerUuid}"); // change world (var newWorldMost, var newWorldLeast) = UUID.GetLongs(opts.ToWorldUUID); worldMost.Value = newWorldMost; worldLeast.Value = newWorldLeast; // set spawn world var spawnWorld = myFile.RootTag.Get <NbtString>("SpawnWorld"); if (spawnWorld != null) { spawnWorld.Value = opts.ToWorldName; } // set position var pos = myFile.RootTag.Get <NbtList>("Pos"); pos[0] = new NbtDouble(opts.NewPos.X); pos[1] = new NbtDouble(opts.NewPos.Y); pos[2] = new NbtDouble(opts.NewPos.Z); // set rotation var rot = myFile.RootTag.Get <NbtList>("Rotation"); rot[0] = new NbtFloat(opts.NewRot.Yaw); rot[1] = new NbtFloat(opts.NewRot.Roll); // set velocity to 0 var motion = myFile.RootTag.Get <NbtList>("Motion"); for (int i = 0; i < motion.Count; i++) { motion[i] = new NbtDouble(0d); } // set fall distance to 0 myFile.RootTag.Get <NbtFloat>("FallDistance").Value = 0f; // set on ground to true myFile.RootTag.Get <NbtByte>("OnGround").Value = 0x01; // write back to file with existing compression myFile.SaveToFile(fileName, myFile.FileCompression); if (opts.Verbose) { Console.WriteLine("Done updating NBT"); } sw.WriteLine(playerUuid.ToString()); } else { if (opts.Verbose) { Console.WriteLine("Move not required"); } } } Console.WriteLine($"\nLog of moved players' UUIDs stored in {Path.Combine(Environment.CurrentDirectory, "MovedPlayers.txt")}"); }