From ef1cebaee750a2d87541a05e3d1456019e0ba33f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 21 Jun 2016 23:17:42 +1000 Subject: [PATCH] Make /vip actually work and get rid of the pointlessly duplicated code. --- Commands/Moderation/CmdVIP.cs | 115 +++++++++++++++++----------------- Commands/World/CmdCopyLVL.cs | 2 +- MCGalaxy_.csproj | 1 - Player/Group/Group.cs | 2 +- Player/Player.Handlers.cs | 2 +- Player/PlayerList.cs | 37 +++++------ Player/VIP.cs | 79 ----------------------- Server/Server.Tasks.cs | 3 +- Server/Server.cs | 2 +- 9 files changed, 79 insertions(+), 164 deletions(-) delete mode 100644 Player/VIP.cs diff --git a/Commands/Moderation/CmdVIP.cs b/Commands/Moderation/CmdVIP.cs index 181fa0203..43557b52f 100644 --- a/Commands/Moderation/CmdVIP.cs +++ b/Commands/Moderation/CmdVIP.cs @@ -1,81 +1,78 @@ /* - Copyright 2011 MCForge - - 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. + Copyright 2011 MCForge + + 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.Collections.Generic; -using System.Text; -namespace MCGalaxy.Commands -{ - public sealed class CmdVIP : Command - { + +namespace MCGalaxy.Commands { + public sealed class CmdVIP : Command { public override string name { get { return "vip"; } } public override string shortcut { get { return ""; } } - public override string type { get { return CommandTypes.Moderation; } } + public override string type { get { return CommandTypes.Moderation; } } public override bool museumUsable { get { return true; } } public override LevelPermission defaultRank { get { return LevelPermission.Admin; } } public CmdVIP() { } - public override void Use(Player p, string message) - { + public override void Use(Player p, string message) { if (message == "") { Help(p); return; } - string[] split = message.Split(' '); - if (split[0] == "add") - { - if (split.Length < 2) { Help(p); return; } - Player pl = PlayerInfo.Find(split[1]); - if (pl != null) split[1] = pl.name; - if (VIP.Find(split[1])) Player.Message(p, (pl == null ? "" : pl.color) + split[1] + " is already a VIP!"); - else - { - VIP.Add(split[1]); - Player.Message(p, (pl == null ? "" : pl.color) + split[1] + " is now a VIP."); + string[] args = message.Split(' '); + + if (args[0] == "add") { + if (args.Length < 2) { Help(p); return; } + Player pl = PlayerInfo.Find(args[1]); + if (pl != null) args[1] = pl.name; + + if (Server.vip.Contains(args[1])) { + Player.Message(p, Name(pl, args[1]) + " %Sis already a VIP."); + } else { + Server.vip.Add(args[1]); + Server.vip.Save(false); + Player.Message(p, Name(pl, args[1]) + " %Sis now a VIP."); if (pl != null) Player.Message(pl, "You are now a VIP!"); } - } - else if (split[0] == "remove") - { - if (split.Length < 2) { Help(p); return; } - Player pl = PlayerInfo.Find(split[1]); - if (pl != null) split[1] = pl.name; - if (!VIP.Find(split[1])) Player.Message(p, (pl == null ? "" : pl.color) + split[1] + " is not a VIP!"); - else - { - VIP.Remove(split[1]); - Player.Message(p, (pl == null ? "" : pl.color) + split[1] + " is no longer a VIP."); + } else if (args[0] == "remove") { + if (args.Length < 2) { Help(p); return; } + Player pl = PlayerInfo.Find(args[1]); + if (pl != null) args[1] = pl.name; + + if (Server.vip.Contains(args[1])) { + Player.Message(p, Name(pl, args[1]) + " %Sis not a VIP."); + } else { + Server.vip.Remove(args[1]); + Server.vip.Save(false); + Player.Message(p, Name(pl, args[1]) + " %Sis no longer a VIP."); if (pl != null) Player.Message(pl, "You are no longer a VIP!"); } - } - else if (split[0] == "list") - { - List list = VIP.GetAll(); - if (list.Count < 1) Player.Message(p, "There are no VIPs."); - else { - StringBuilder sb = new StringBuilder(); - foreach (string name in list) - sb.Append(name).Append(", "); - + } else if (args[0] == "list") { + List list = Server.vip.All(); + if (list.Count == 0) { + Player.Message(p, "There are no VIPs."); + } else { string count = list.Count > 1 ? "is 1" : "are " + list.Count; Player.Message(p, "There " + count + " VIPs:"); - Player.Message(p, sb.Remove(sb.Length - 2, 2).ToString()); + Player.Message(p, list.Concatenate(", ")); } + } else { + Help(p); } - else Help(p); } - public override void Help(Player p) - { + + static string Name(Player pl, string name) { return pl == null ? name : pl.ColoredName; } + + public override void Help(Player p) { Player.Message(p, "VIPs are players who can join regardless of the player limit."); Player.Message(p, "/vip add - Add a VIP."); Player.Message(p, "/vip remove - Remove a VIP."); diff --git a/Commands/World/CmdCopyLVL.cs b/Commands/World/CmdCopyLVL.cs index 54d66bc9a..b6383b080 100644 --- a/Commands/World/CmdCopyLVL.cs +++ b/Commands/World/CmdCopyLVL.cs @@ -36,7 +36,7 @@ namespace MCGalaxy.Commands.World { } string src = args[0], dst = args[1]; - if (!p.group.CanExecute("newlvl")) { + if (p != null && !p.group.CanExecute("newlvl")) { Player.Message(p, "You cannot use /copylvl as you cannot use /newlvl."); return; } src = LevelInfo.FindMapMatches(p, src); diff --git a/MCGalaxy_.csproj b/MCGalaxy_.csproj index 5d4ee42c2..09eefb6c2 100644 --- a/MCGalaxy_.csproj +++ b/MCGalaxy_.csproj @@ -576,7 +576,6 @@ - diff --git a/Player/Group/Group.cs b/Player/Group/Group.cs index d61d3585a..e18735597 100644 --- a/Player/Group/Group.cs +++ b/Player/Group/Group.cs @@ -88,7 +88,7 @@ namespace MCGalaxy fileName = file; OverseerMaps = maps; this.prefix = prefix; - playerList = name != "nobody" ? PlayerList.Load(fileName, this) : new PlayerList(); + playerList = name != "nobody" ? PlayerList.Load(fileName) : new PlayerList(); if (OnGroupLoaded != null) OnGroupLoaded(this); OnGroupLoadedEvent.Call(this); diff --git a/Player/Player.Handlers.cs b/Player/Player.Handlers.cs index 913ee25d3..b6b864dae 100644 --- a/Player/Player.Handlers.cs +++ b/Player/Player.Handlers.cs @@ -393,7 +393,7 @@ namespace MCGalaxy { } bool CheckPlayersCount(Group foundGrp) { - if (VIP.Find(this)) return true; + if (Server.vip.Contains(name)) return true; Player[] online = PlayerInfo.Online.Items; if (online.Length >= Server.players && !IPInPrivateRange(ip)) { Leave("Server full!", true); return false; } diff --git a/Player/PlayerList.cs b/Player/PlayerList.cs index 23dca39f5..229e43dc5 100644 --- a/Player/PlayerList.cs +++ b/Player/PlayerList.cs @@ -21,14 +21,13 @@ using System.IO; using System.Text; namespace MCGalaxy { - public sealed class PlayerList { - - string file; + public sealed class PlayerList { + string path; List players = new List(); readonly object locker = new object(); public PlayerList() { } - public PlayerList(string file) { this.file = file; } + public PlayerList(string path) { this.path = path; } public void Add(string p) { lock (locker) @@ -62,32 +61,30 @@ namespace MCGalaxy { } - public void Save() { Save(file, true); } - public void Save(bool console) { Save(file, console); } - - public void Save(string file, bool console) { - using (StreamWriter w = new StreamWriter("ranks/" + file)) { + public void Save() { Save(true); } + public void Save(bool console) { + using (StreamWriter w = new StreamWriter(path)) { lock (locker) { foreach (string p in players) w.WriteLine(p); } } - if (console) - Server.s.Log("SAVED: " + file, true); + if (console) Server.s.Log("SAVED: " + path, true); } - public static PlayerList Load(string path) { - if (!Directory.Exists("ranks")) - Directory.CreateDirectory("ranks"); - PlayerList list = new PlayerList(path); - path = "ranks/" + path; - if (!File.Exists(path)) { - File.Create(path).Close(); - Server.s.Log("CREATED NEW: " + list.file); + public static PlayerList Load(string file) { + if (!Directory.Exists("ranks")) Directory.CreateDirectory("ranks"); + PlayerList list = new PlayerList(file); + if (file.IndexOf('/') == -1) file = "ranks/" + file; + list.path = file; + + if (!File.Exists(list.path)) { + File.Create(list.path).Close(); + Server.s.Log("CREATED NEW: " + list.path); return list; } - using (StreamReader r = new StreamReader(path, Encoding.UTF8)) { + using (StreamReader r = new StreamReader(list.path, Encoding.UTF8)) { string line = null; while ((line = r.ReadLine()) != null) { // Need to convert uppercase to lowercase, in case user added in entries. diff --git a/Player/VIP.cs b/Player/VIP.cs deleted file mode 100644 index 2162851a6..000000000 --- a/Player/VIP.cs +++ /dev/null @@ -1,79 +0,0 @@ -/* - Copyright 2011 MCForge - - 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.Collections.Generic; -using System.IO; -namespace MCGalaxy -{ - public static class VIP - { - public static readonly string file = "text/vips.txt"; //this file is never even created anywhere.. - - public static void Add(string name) - { - if (!File.Exists(file)) return; - name = name.Trim().ToLower(); - List list = new List(File.ReadAllLines(file)); - list.Add(name); - File.WriteAllLines(file, list.ToArray()); - } - /// - /// Adds a Player to VIP's - /// - /// the Player to add - public static void Add(Player p) - { - Add(p.name); - } - - public static void Remove(string name) - { - if (!File.Exists(file)) return; - name = name.Trim().ToLower(); - List list = new List(); - foreach (string line in File.ReadAllLines(file)) - if (line.Trim().ToLower() != name) - list.Add(line); - File.WriteAllLines(file, list.ToArray()); - } - public static void Remove(Player p) - { - Remove(p.name); - } - - public static bool Find(string name) - { - if (!File.Exists(file)) return false; - name = name.Trim().ToLower(); - foreach (string line in File.ReadAllLines(file)) - if (line.Trim().ToLower() == name) - return true; - return false; - } - public static bool Find(Player p) - { - return Find(p.name); - } - - public static List GetAll() - { - if (File.Exists(file)) - return new List(File.ReadAllLines(file)); - return new List(); - } - } -} \ No newline at end of file diff --git a/Server/Server.Tasks.cs b/Server/Server.Tasks.cs index ff34e3e6d..01d463335 100644 --- a/Server/Server.Tasks.cs +++ b/Server/Server.Tasks.cs @@ -78,7 +78,8 @@ namespace MCGalaxy { ircControllers = PlayerList.Load("IRC_Controllers.txt"); muted = PlayerList.Load("muted.txt"); frozen = PlayerList.Load("frozen.txt"); - hidden = PlayerList.Load("hidden.txt"); + hidden = PlayerList.Load("hidden.txt"); + vip = PlayerList.Load("text/vip.txt"); jailed = PlayerExtList.Load("ranks/jailed.txt"); models = PlayerExtList.Load("extra/models.txt"); skins = PlayerExtList.Load("extra/skins.txt"); diff --git a/Server/Server.cs b/Server/Server.cs index da7b46bcb..3d99497e3 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -99,7 +99,7 @@ namespace MCGalaxy public static bool UseCTF = false; public static bool ServerSetupFinished = false; public static Auto_CTF ctf = null; - public static PlayerList bannedIP, whiteList, ircControllers, muted, ignored, frozen, hidden, agreed; + public static PlayerList bannedIP, whiteList, ircControllers, muted, ignored, frozen, hidden, agreed, vip; public static PlayerExtList jailed, models, skins; public static readonly List Devs = new List(), Mods = new List();