diff --git a/MCGalaxy/CorePlugin/ConnectHandler.cs b/MCGalaxy/CorePlugin/ConnectHandler.cs index 40845bf84..0ff259283 100644 --- a/MCGalaxy/CorePlugin/ConnectHandler.cs +++ b/MCGalaxy/CorePlugin/ConnectHandler.cs @@ -61,7 +61,8 @@ namespace MCGalaxy.Core { static void LoadWaypoints(Player p) { try { - p.Waypoints.Load(p); + p.Waypoints.Filename = "extra/Waypoints/" + p.name + ".save"; + p.Waypoints.Load(); } catch (IOException ex) { Player.Message(p, "Error loading waypoints."); Logger.LogError(ex); diff --git a/MCGalaxy/Player/Player.Fields.cs b/MCGalaxy/Player/Player.Fields.cs index 5b28b37b9..b122e04a9 100644 --- a/MCGalaxy/Player/Player.Fields.cs +++ b/MCGalaxy/Player/Player.Fields.cs @@ -229,7 +229,7 @@ namespace MCGalaxy { public List spyChatRooms = new List(); public DateTime lastchatroomglobal; - public WarpList Waypoints = new WarpList(true); + public WarpList Waypoints = new WarpList(); public Random random = new Random(); public LevelPermission Rank { get { return group.Permission; } } diff --git a/MCGalaxy/Player/Warp.cs b/MCGalaxy/Player/Warp.cs index 8c141a4ca..2b3fdef77 100644 --- a/MCGalaxy/Player/Warp.cs +++ b/MCGalaxy/Player/Warp.cs @@ -31,14 +31,9 @@ namespace MCGalaxy { } public sealed class WarpList { - public static WarpList Global = new WarpList(false); - + public static WarpList Global = new WarpList(); public List Items = new List(); - bool playerWarp; - - public WarpList(bool playerWarp) { - this.playerWarp = playerWarp; - } + public string Filename; /// Finds the warp whose name caselessly equals the given name. public Warp Find(string name) { @@ -47,6 +42,37 @@ namespace MCGalaxy { } return null; } + + /// Returns whether a warp with the given name exists. + public bool Exists(string name) { return Find(name) != null; } + + /// Creates a new warp with the given name, located at the + /// player's current position, orientation, and level. + public void Create(string name, Player p) { + Warp warp = new Warp(); + InitWarp(warp, name, p); + Items.Add(warp); + Save(); + } + + void InitWarp(Warp warp, string name, Player p) { + warp.Pos = p.Pos; warp.Name = name; + warp.Yaw = p.Rot.RotY; warp.Pitch = p.Rot.HeadX; + warp.Level = p.level.name; + } + + /// Moves the given warp to the target + /// player's position, orientation, and map. + public void Update(Warp warp, Player p) { + InitWarp(warp, warp.Name, p); + Save(); + } + + /// Removes the given warp. + public void Remove(Warp warp, Player p) { + Items.Remove(warp); + Save(); + } /// Attempts to move the given player to the given warp. public void Goto(Warp warp, Player p) { @@ -61,73 +87,41 @@ namespace MCGalaxy { Player.Message(p, "Unable to send you to the warp as the map it is on is not loaded."); } } - - /// Creates a new warp with the given name, located at the - /// player's current position, orientation, and level. - public void Create(string name, Player p) { - Warp warp = new Warp(); - InitWarp(warp, name, p); - Items.Add(warp); - Save(p); - } - - void InitWarp(Warp warp, string name, Player p) { - warp.Pos = p.Pos; warp.Name = name; - warp.Yaw = p.Rot.RotY; warp.Pitch = p.Rot.HeadX; - warp.Level = p.level.name; - } - - /// Moves the given warp to the target - /// player's position, orientation, and map. - public void Update(Warp warp, Player p) { - InitWarp(warp, warp.Name, p); - Save(p); - } - - /// Removes the given warp. - public void Remove(Warp warp, Player p) { - Items.Remove(warp); - Save(p); - } - - /// Returns whether a warp with the given name exists. - public bool Exists(string name) { return Find(name) != null; } - public void Load(Player p) { - string file = playerWarp ? "extra/Waypoints/" + p.name + ".save" : "extra/warps.save"; - if (!File.Exists(file)) return; - - using (StreamReader r = new StreamReader(file)) { + /// Loads the list of warps from the file located at Filename. + public void Load() { + if (!File.Exists(Filename)) return; + using (StreamReader r = new StreamReader(Filename)) { string line; while ((line = r.ReadLine()) != null) { line = line.Trim(); if (line.StartsWith("#") || !line.Contains(":")) continue; string[] parts = line.Split(':'); - Warp wp = new Warp(); + Warp warp = new Warp(); try { - wp.Name = parts[0]; - wp.Level = parts[1]; - wp.Pos.X = int.Parse(parts[2]); - wp.Pos.Y = int.Parse(parts[3]); - wp.Pos.Z = int.Parse(parts[4]); - wp.Yaw = byte.Parse(parts[5]); - wp.Pitch = byte.Parse(parts[6]); - Items.Add(wp); + warp.Name = parts[0]; + warp.Level = parts[1]; + warp.Pos.X = int.Parse(parts[2]); + warp.Pos.Y = int.Parse(parts[3]); + warp.Pos.Z = int.Parse(parts[4]); + warp.Yaw = byte.Parse(parts[5]); + warp.Pitch = byte.Parse(parts[6]); + Items.Add(warp); } catch { - Logger.Log(LogType.Warning, "Failed loading a warp from " + file); + Logger.Log(LogType.Warning, "Failed loading a warp from " + Filename); } } } } - public void Save(Player p) { - string file = playerWarp ? "extra/Waypoints/" + p.name + ".save" : "extra/warps.save"; - using (StreamWriter w = new StreamWriter(file)) { + /// Saves this list of warps to Filename. + public void Save() { + using (StreamWriter w = new StreamWriter(Filename)) { foreach (Warp warp in Items) { w.WriteLine(warp.Name + ":" + warp.Level + ":" + warp.Pos.X + ":" + - warp.Pos.Y + ":" + warp.Pos.Z + ":" + warp.Yaw + ":" + warp.Pitch); + warp.Pos.Y + ":" + warp.Pos.Z + ":" + warp.Yaw + ":" + warp.Pitch); } } } diff --git a/MCGalaxy/Server/Server.cs b/MCGalaxy/Server/Server.cs index 3d5c93562..e973ffefa 100644 --- a/MCGalaxy/Server/Server.cs +++ b/MCGalaxy/Server/Server.cs @@ -206,7 +206,8 @@ namespace MCGalaxy { BlockDefinition.UpdateGlobalBlockProps(); Awards.Load(); Economy.Load(); - WarpList.Global.Load(null); + WarpList.Global.Filename = "extra/warps.save"; + WarpList.Global.Load(); CommandExtraPerms.Load(); ProfanityFilter.Init(); Team.LoadList();