Make /vip actually work and get rid of the pointlessly duplicated code.

This commit is contained in:
UnknownShadow200 2016-06-21 23:17:42 +10:00
parent f35dcbd827
commit ef1cebaee7
9 changed files with 79 additions and 164 deletions

View File

@ -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<string> 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<string> 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 <name> - Add a VIP.");
Player.Message(p, "/vip remove <name> - Remove a VIP.");

View File

@ -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);

View File

@ -576,7 +576,6 @@
<Compile Include="Server\Server.Tasks.cs" />
<Compile Include="Server\Server.Threads.cs" />
<Compile Include="Server\Updater.cs" />
<Compile Include="Player\VIP.cs" />
<Compile Include="API\WebServer.cs" />
<Compile Include="util\FastList.cs" />
<Compile Include="util\App.cs" />

View File

@ -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);

View File

@ -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; }

View File

@ -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<string> players = new List<string>();
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.

View File

@ -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<string> list = new List<string>(File.ReadAllLines(file));
list.Add(name);
File.WriteAllLines(file, list.ToArray());
}
/// <summary>
/// Adds a Player to VIP's
/// </summary>
/// <param name="p">the Player to add</param>
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<string> list = new List<string>();
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<string> GetAll()
{
if (File.Exists(file))
return new List<string>(File.ReadAllLines(file));
return new List<string>();
}
}
}

View File

@ -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");

View File

@ -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<string> Devs = new List<string>(), Mods = new List<string>();