Note that there are some explanatory texts on larger screens.

plurals
  1. POC# (Craft.Net.Client) ServerList Exception
    primarykey
    data
    text
    <p>I'm using Craft.Net.Client library but I've got an error when trying to use the ServerList.SaveTo(string file) method : Unable to read beyond the end of the stream (EndOfStreamException)</p> <p>ServerList.cs : </p> <pre><code>using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using fNbt; namespace Craft.Net.Client { /// &lt;summary&gt; /// Provides functionality for interacting with /// the saved vanilla server list. /// &lt;/summary&gt; public class ServerList { public static string ServersDat { get { return Path.Combine(DotMinecraft.GetDotMinecraftPath(), "servers.dat"); } } public ServerList() { Servers = new List&lt;Server&gt;(); } public List&lt;Server&gt; Servers { get; set; } public void Save() { SaveTo(ServersDat); } public void SaveTo(string file) { var nbt = new NbtFile(file); // ERROR : Unable to read beyond the end of the stream (EndOfStreamException) nbt.RootTag = new NbtCompound(""); var list = new NbtList("servers", NbtTagType.Compound); foreach (var server in Servers) { var compound = new NbtCompound(); compound.Add(new NbtString("name", server.Name)); compound.Add(new NbtString("ip", server.Ip)); compound.Add(new NbtByte("hideAddress", (byte)(server.HideAddress ? 1 : 0))); compound.Add(new NbtByte("acceptTextures", (byte)(server.AcceptTextures ? 1 : 0))); list.Add(compound); } nbt.RootTag.Add(list); nbt.SaveToFile(file, NbtCompression.None); } public static ServerList Load() { return LoadFrom(ServersDat); } public static ServerList LoadFrom(string file) { var list = new ServerList(); var nbt = new NbtFile(file); foreach (NbtCompound server in nbt.RootTag["servers"] as NbtList) { var entry = new Server(); if (server.Contains("name")) entry.Name = server["name"].StringValue; if (server.Contains("ip")) entry.Ip = server["ip"].StringValue; if (server.Contains("hideAddress")) entry.HideAddress = server["hideAddress"].ByteValue == 1; if (server.Contains("acceptTextures")) entry.AcceptTextures = server["acceptTextures"].ByteValue == 1; list.Servers.Add(entry); } return list; } public class Server { public string Name { get; set; } public string Ip { get; set; } public bool HideAddress { get; set; } public bool AcceptTextures { get; set; } public override string ToString() { return Name; } } } </code></pre> <p>}</p> <p>And where I call the method : </p> <pre><code>ServerList.Server server = new ServerList.Server(); server.Name = "x"; server.Ip = "x"; server.HideAddress = true; server.AcceptTextures = true; ServerList list = new ServerList(); list.Servers.Add(server); if (!File.Exists(RuntimeInfo.getMinecraftDir() + @"\servers.dat")) { File.Create(RuntimeInfo.getMinecraftDir() + @"\servers.dat"); } list.SaveTo(RuntimeInfo.getMinecraftDir() + @"\servers.dat"); </code></pre> <p>One thing I've noticed is that I only got the error when servers.dat is empty but not if it has already servers saved.</p> <p>Can anyone help me?</p> <p>Thanks in advance, </p> <p>EDIT : Thanks to steveg89, the solution below solves the problem (updating SaveTo method) : </p> <pre><code> public void SaveTo(string file) { NbtFile nbt; if (File.Exists(RuntimeInfo.getMinecraftDir() + @"\servers.dat")) { nbt = new NbtFile(); nbt.SaveToFile(RuntimeInfo.getMinecraftDir() + @"\servers.dat", NbtCompression.None); } else nbt = new NbtFile(); nbt.RootTag = new NbtCompound(""); var list = new NbtList("servers", NbtTagType.Compound); foreach (var server in Servers) { var compound = new NbtCompound(); compound.Add(new NbtString("name", server.Name)); compound.Add(new NbtString("ip", server.Ip)); compound.Add(new NbtByte("hideAddress", (byte)(server.HideAddress ? 1 : 0))); compound.Add(new NbtByte("acceptTextures", (byte)(server.AcceptTextures ? 1 : 0))); list.Add(compound); } nbt.RootTag.Add(list); nbt.SaveToFile(file, NbtCompression.None); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload