diff --git a/Commands/Moderation/CmdJail.cs b/Commands/Moderation/CmdJail.cs index 9a12ff71e..41d26dd13 100644 --- a/Commands/Moderation/CmdJail.cs +++ b/Commands/Moderation/CmdJail.cs @@ -48,17 +48,18 @@ namespace MCGalaxy.Commands Entities.GlobalSpawn(who, (ushort)who.level.jailx, (ushort)who.level.jaily, (ushort)who.level.jailz, who.level.jailrotx, who.level.jailroty, true); - Server.Jailed.DeleteContains(who.name.ToLower()); - Server.Jailed.Append(who.name.ToLower() + " " + who.level.name); + Server.jailed.Remove(who.name); + Server.jailed.Add(who.name, who.level.name); Player.SendChatFrom(who, who.ColoredName + " %Swas &8jailed", false); Player.AddNote(who.name, p, "J"); } else { - Server.Jailed.DeleteContains(who.name.ToLower()); + Server.jailed.Remove(who.name); who.jailed = false; Command.all.Find("spawn").Use(who, ""); Player.Message(p, "You freed " + who.name + " from jail"); Player.SendChatFrom(who, who.ColoredName + " %Swas &afreed %Sfrom jail", false); } + Server.jailed.Save(); } public override void Help(Player p) { diff --git a/Commands/other/CmdXJail.cs b/Commands/other/CmdXJail.cs index 5300ffaf1..ff40b3b34 100644 --- a/Commands/other/CmdXJail.cs +++ b/Commands/other/CmdXJail.cs @@ -17,8 +17,8 @@ */ using System.IO; namespace MCGalaxy.Commands { - public sealed class CmdXJail : Command { + public override string name { get { return "xjail"; } } public override string shortcut { get { return "xj"; } } public override string type { get { return CommandTypes.Other; } } @@ -26,65 +26,49 @@ namespace MCGalaxy.Commands { public override bool museumUsable { get { return true; } } public override void Use(Player p, string message) { - string dir = "extra/jail/"; - string jailMapFile = dir + "xjail.map.xjail"; - if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } - if (!File.Exists(jailMapFile)) { - using (StreamWriter SW = new StreamWriter(jailMapFile)) - SW.WriteLine(Server.mainLevel.name); + string xjailMap = Server.xjailLevel; + if (xjailMap == "(main)") xjailMap = Server.mainLevel.name; + if (message == "") { Help(p); return; } + + Command jail = Command.all.Find("jail"); + if (message == "set") { + if (!p.level.IsMuseum) { + jail.Use(p, "set"); + Server.xjailLevel = p.level.name; + SrvProperties.Save(); + Player.Message(p, "The xjail map was set from '" + xjailMap + "' to '" + p.level.name + "'"); + } else { + Player.Message(p, "You are in a museum!"); + } + return; } - if (message == "") { Help(p); return; } - using (StreamReader SR = new StreamReader(jailMapFile)) { - string xjailMap = SR.ReadLine(); - SR.Close(); - Command jail = Command.all.Find("jail"); - if (message == "set") { - if (!p.level.IsMuseum) { - jail.Use(p, "create"); - using (StreamWriter SW = new StreamWriter(jailMapFile)) - SW.WriteLine(p.level.name); - Player.Message(p, "The xjail map was set from '" + xjailMap + "' to '" + p.level.name + "'"); - } else { - Player.Message(p, "You are in a museum!"); - } - return; - } - - Player pl = PlayerInfo.FindOrShowMatches(p, message); - if (pl == null) return; + Player pl = PlayerInfo.FindOrShowMatches(p, message); + if (pl == null) return; - Command move = Command.all.Find("move"); - Command spawn = Command.all.Find("spawn"); - Command freeze = Command.all.Find("freeze"); - Command mute = Command.all.Find("mute"); - string playerFile = dir + pl.name + "_temp.xjail"; + Command spawn = Command.all.Find("spawn"); + Command freeze = Command.all.Find("freeze"); + Command mute = Command.all.Find("mute"); + + string data = Server.jailed.Find(pl.name); + if (data == null) { + if (!pl.muted) mute.Use(p, message); + if (!pl.frozen) freeze.Use(p, message); - if (!File.Exists(playerFile)) { - using (StreamWriter writeFile = new StreamWriter(playerFile)) - writeFile.WriteLine(pl.level.name); - if (!pl.muted) { mute.Use(p, message); } - if (!pl.frozen) { freeze.Use(p, message); } - move.Use(p, message + " " + xjailMap); - - pl.BlockUntilLoad(10); - if (!pl.jailed) { jail.Use(p, message); } - Player.GlobalMessage(pl.ColoredName + " %Swas XJailed!"); - } else { - using (StreamReader readFile = new StreamReader(playerFile)) { - string playerMap = readFile.ReadLine(); - readFile.Close(); - File.Delete(playerFile); - move.Use(p, message + " " + playerMap); - - pl.BlockUntilLoad(10); - mute.Use(p, message); - jail.Use(p, message); - freeze.Use(p, message); - spawn.Use(pl, ""); - Player.GlobalMessage(pl.ColoredName + " %Swas released from XJail!"); - } - } + PlayerActions.ChangeMap(pl, xjailMap); + pl.BlockUntilLoad(10); + jail.Use(p, message); + Player.GlobalMessage(pl.ColoredName + " %Swas XJailed!"); + } else { + if (pl.muted) mute.Use(p, message); + if (pl.frozen) freeze.Use(p, message); + + PlayerActions.ChangeMap(pl, Server.mainLevel.name); + pl.BlockUntilLoad(10); + + jail.Use(p, message); + spawn.Use(pl, ""); + Player.GlobalMessage(pl.ColoredName + " %Swas released from XJail!"); } } @@ -92,6 +76,6 @@ namespace MCGalaxy.Commands { Player.Message(p, "/xjail - Mutes , freezes and sends to the XJail map (shortcut = /xj)"); Player.Message(p, "If is already jailed, will be spawned, unfrozen and unmuted"); Player.Message(p, "/xjail set - Sets the map to be used for xjail to your current map and sets jail to current location"); - } + } } } diff --git a/MCGalaxy_.csproj b/MCGalaxy_.csproj index fcd7437d8..dbbbed54c 100644 --- a/MCGalaxy_.csproj +++ b/MCGalaxy_.csproj @@ -521,6 +521,7 @@ + diff --git a/Player/Player.Handlers.cs b/Player/Player.Handlers.cs index 6d192ec4b..d812fef52 100644 --- a/Player/Player.Handlers.cs +++ b/Player/Player.Handlers.cs @@ -663,30 +663,18 @@ namespace MCGalaxy { } void CheckLoginJailed() { - //very very sloppy, yes I know.. but works for the time - try { - if (!File.Exists("ranks/jailed.txt")) { - File.Create("ranks/jailed.txt").Close(); return; - } - - using (StreamReader reader = new StreamReader("ranks/jailed.txt")) { - string line; - while ((line = reader.ReadLine()) != null) { - string[] parts = line.Split(); - if (!parts[0].CaselessEq(name)) continue; - reader.Dispose(); - - try { - PlayerActions.ChangeMap(this, parts[1]); - Command.all.Find("jail").Use(null, parts[0]); - } catch (Exception ex) { - Kick("Error occured"); - Server.ErrorLog(ex); - } - return; - } - } - } catch { + string line = Server.jailed.Find(name); + if (line == null) return; + int space = line.IndexOf(' '); + if (space == -1) return; + string level = line.Substring(space + 1); + + try { + PlayerActions.ChangeMap(this, level); + Command.all.Find("jail").Use(null, name); + } catch (Exception ex) { + Kick("Error occured"); + Server.ErrorLog(ex); } } diff --git a/Player/PlayerExtList.cs b/Player/PlayerExtList.cs new file mode 100644 index 000000000..bedaf8257 --- /dev/null +++ b/Player/PlayerExtList.cs @@ -0,0 +1,106 @@ +/* + Copyright 2015 MCGalaxy + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace MCGalaxy { + public sealed class PlayerExtList { + + string path; + List players = new List(); + List lines = new List(); + readonly object locker = new object(); + + public void Add(string p, string data) { + lock (locker) { + players.Add(p.ToLower()); + lines.Add(p.ToLower() + " " + data.ToLower()); + } + } + + public bool Remove(string p) { + lock (locker) { + int idx = players.IndexOf(p.ToLower()); + if (idx == -1) return false; + + players.RemoveAt(idx); + lines.RemoveAt(idx); + return true; + } + } + + public string Find(string p) { + lock (locker) { + int idx = players.IndexOf(p.ToLower()); + if (idx == -1) return null; + return lines[idx]; + } + } + + public int Count { get { + lock (locker) + return players.Count; + } } + + public void Save() { Save(path, true); } + + public void Save(string path, bool console) { + using (StreamWriter w = File.CreateText(path)) { + lock (locker) { + foreach (string line in lines) + w.WriteLine(line); + } + } + if (console) + Server.s.Log("SAVED: " + path); + } + + public static PlayerExtList Load(string path) { + PlayerExtList list = new PlayerExtList(); + list.path = path; + + if (!File.Exists(path)) { + File.Create(path).Close(); + Server.s.Log("CREATED NEW: " + path); + return list; + } + + using (StreamReader r = new StreamReader(path, Encoding.UTF8)) { + string line = null; + while ((line = r.ReadLine()) != null) { + // Need to convert uppercase to lowercase, in case user added in entries. + bool anyUpper = false; + for (int i = 0; i < line.Length; i++) { + char c = line[i]; + anyUpper |= (c >= 'A' && c <= 'Z'); + } + + if (anyUpper) line = line.ToLower(); + list.lines.Add(line); + + int space = line.IndexOf(' '); + string name = space >= 0 ? line.Substring(0, space) : line; + list.players.Add(line); + } + } + return list; + } + } +} \ No newline at end of file diff --git a/Player/PlayerList.cs b/Player/PlayerList.cs index d7c006f0c..da442ffa7 100644 --- a/Player/PlayerList.cs +++ b/Player/PlayerList.cs @@ -15,15 +15,14 @@ or implied. See the Licenses for the specific language governing permissions and limitations under the Licenses. */ -using System; -using System.IO; -using System.Collections.Generic; -using System.Text; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; namespace MCGalaxy { - - public sealed class PlayerList { - + public sealed class PlayerList { + string file; List players = new List(); readonly object locker = new object(); diff --git a/Server/Server.Tasks.cs b/Server/Server.Tasks.cs index 258d72309..28d8b1331 100644 --- a/Server/Server.Tasks.cs +++ b/Server/Server.Tasks.cs @@ -71,8 +71,7 @@ namespace MCGalaxy { muted = PlayerList.Load("muted.txt"); frozen = PlayerList.Load("frozen.txt"); hidden = PlayerList.Load("hidden.txt"); - //jailed = PlayerList.Load("jailed.txt"); - if (!File.Exists("ranks/jailed.txt")) { File.Create("ranks/jailed.txt").Close(); Server.s.Log("CREATED NEW: ranks/jailed.txt"); } + jailed = PlayerExtList.Load("ranks/jailed.txt"); foreach (Group grp in Group.GroupList) grp.playerList = PlayerList.Load(grp.fileName); diff --git a/Server/Server.cs b/Server/Server.cs index 26c0a3f3c..751f358ae 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -68,7 +68,6 @@ namespace MCGalaxy public static PlayersFile AutoloadMaps = new PlayersFile("text/autoload.txt"); public static PlayersFile RankInfo = new PlayersFile("text/rankinfo.txt"); - public static PlayersFile Jailed = new PlayersFile("ranks/jailed.txt"); public static PlayersFile TempRanks = new PlayersFile("text/tempranks.txt"); public static PlayersFile Notes = new PlayersFile("text/notes.txt"); public static PlayersFile Skins = new PlayersFile("extra/skins.txt"); @@ -101,9 +100,9 @@ namespace MCGalaxy public static bool ServerSetupFinished = false; public static Auto_CTF ctf = null; public static PlayerList bannedIP, whiteList, ircControllers, muted, ignored, frozen, hidden; + public static PlayerExtList jailed; - public static readonly List Devs = new List(); - public static readonly List Mods = new List(); + public static readonly List Devs = new List(), Mods = new List(); internal static readonly List opstats = new List(new [] { "ban", "tempban", "kick", "warn", "mute", "freeze", "undo", "kickban", "demote", "promote" }); public static List Opstats { get { return new List(opstats); } } @@ -118,7 +117,6 @@ namespace MCGalaxy public static List levels; //reviewlist intitialize public static List reviewlist = new List(); - public static List ircafkset = new List(); public static List messages = new List(); @@ -209,6 +207,8 @@ namespace MCGalaxy [ConfigString("main-name", "General", null, "main", false, "._+")] public static string level = "main"; + [ConfigString("xjail-map-name", "Other", null, "(main)", false, "()._+")] + public static string xjailLevel = "(main)"; public static string errlog = "error.log"; [ConfigBool("report-back", "Error handling", null, true)]