PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/MCHmkCore/Commands/Moderation/CmdMute.cs

https://bitbucket.org/LegoBricker/mchmok
C# | 320 lines | 130 code | 20 blank | 170 comment | 22 complexity | 49d962a1a7c4a3cbabc14580ffb9bb32 MD5 | raw file
  1. /*
  2. Copyright 2016 Jjp137
  3. This file have been changed from the original source code by MCForge.
  4. Dual-licensed under the Educational Community License, Version 2.0 and
  5. the GNU General Public License, Version 3 (the "Licenses"); you may
  6. not use this file except in compliance with the Licenses. You may
  7. obtain a copy of the Licenses at
  8. http://www.opensource.org/licenses/ecl2.php
  9. http://www.gnu.org/licenses/gpl-3.0.html
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the Licenses are distributed on an "AS IS"
  12. BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  13. or implied. See the Licenses for the specific language governing
  14. permissions and limitations under the Licenses.
  15. */
  16. /*
  17. Copyright © 2011-2014 MCForge-Redux
  18. Dual-licensed under the Educational Community License, Version 2.0 and
  19. the GNU General Public License, Version 3 (the "Licenses"); you may
  20. not use this file except in compliance with the Licenses. You may
  21. obtain a copy of the Licenses at
  22. http://www.opensource.org/licenses/ecl2.php
  23. http://www.gnu.org/licenses/gpl-3.0.html
  24. Unless required by applicable law or agreed to in writing,
  25. software distributed under the Licenses are distributed on an "AS IS"
  26. BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  27. or implied. See the Licenses for the specific language governing
  28. permissions and limitations under the Licenses.
  29. */
  30. using System;
  31. using System.Collections.ObjectModel;
  32. using System.Diagnostics;
  33. using System.IO;
  34. using System.Threading;
  35. namespace MCHmk.Commands {
  36. /// <summary>
  37. /// The code for the /mute command, which mutes another player.
  38. /// </summary>
  39. public class CmdMute : Command {
  40. private ReadOnlyCollection<string> _keywords = Array.AsReadOnly<string>(
  41. new string[] {"voice", "chat", "player"});
  42. public override string Name {
  43. get {
  44. return "mute";
  45. }
  46. }
  47. public override string Shortcut {
  48. get {
  49. return String.Empty;
  50. }
  51. }
  52. public override string Type {
  53. get {
  54. return "mod";
  55. }
  56. }
  57. public override ReadOnlyCollection<string> Keywords {
  58. get {
  59. return _keywords;
  60. }
  61. }
  62. public override bool MuseumUsable {
  63. get {
  64. return true;
  65. }
  66. }
  67. public override int DefaultRank {
  68. get {
  69. return DefaultRankValue.Operator;
  70. }
  71. }
  72. public CmdMute(Server s) : base(s) { }
  73. /// <summary>
  74. /// The code that runs when /mute is called.
  75. /// </summary>
  76. /// <param name="p"> The player that used the command. </param>
  77. /// <param name="args"> Any parameters that came after the command. </param>
  78. public override void Use(Player p, string args) {
  79. // An array to keep the split message.
  80. string[] splitmessage = args.Split(' ');
  81. // If your message is too long or too short, return the help
  82. if (args == String.Empty || splitmessage.Length > 2) {
  83. Help(p);
  84. return;
  85. }
  86. // Finds the player.
  87. Player who = _s.players.Find(splitmessage[0]);
  88. // If the player was not found online, check offline
  89. if (who == null) {
  90. String mutee = splitmessage[0].ToLower();
  91. Uuid uuid = Uuid.FindUuid(_s.database, mutee);
  92. if (!uuid.IsValid) {
  93. p.SendMessage("Invalid player! Either the player does not exist, or they are not muted.");
  94. }
  95. else if (_s.muted.Contains(uuid)) {
  96. _s.muted.Remove(uuid);
  97. _s.muted.Save("muted.txt");
  98. _s.logger.Log("SAVED: ranks/muted.txt");
  99. _s.GlobalMessage(mutee + " &f(offline) " + _s.props.DefaultColor +
  100. "is now unmuted!");
  101. }
  102. return;
  103. }
  104. // This is the time the person will be muted for.
  105. int time;
  106. // If you did not pass a time, it will mute indefinitely.
  107. if (splitmessage.Length == 1) {
  108. time = 0;
  109. }
  110. // Here it tries to get a time from the message.
  111. else {
  112. try {
  113. time = Convert.ToInt32(splitmessage[1]);
  114. }
  115. catch (Exception) { // TODO: find exact exception to catch
  116. p.SendMessage("Invalid time given!");
  117. Help(p);
  118. //_s.logger.ErrorLog(e);
  119. return;
  120. }
  121. }
  122. // Makes sure you can't mute someone of equal or higher rank.
  123. if (!p.IsConsole) {
  124. if (who.rank.Permission >= p.rank.Permission && who != p) {
  125. p.SendMessage("Cannot mute somebody of an equal or higher rank!");
  126. return;
  127. }
  128. }
  129. // Checks to ensure you use a positive time.
  130. if (time < 0) {
  131. p.SendMessage("Cannot use negative times!");
  132. return;
  133. }
  134. // If the person is muted, unmute them regardless of time.
  135. if (who.muted) {
  136. who.muted = false;
  137. _s.GlobalMessage(who.color + who.name + _s.props.DefaultColor + " has been &bun-muted!");
  138. _s.muted.Remove (who.uuid);
  139. _s.muted.Save("muted.txt");
  140. _s.logger.Log("SAVED: ranks/muted.txt");
  141. return;
  142. }
  143. // Mutes indefinitely.
  144. if (time == 0) {
  145. who.muted = true;
  146. _s.GlobalMessage (who.color + who.name + _s.props.DefaultColor + " has been &8muted!");
  147. _s.muted.Add (who.uuid, who.name);
  148. _s.muted.Save("muted.txt");
  149. _s.logger.Log("SAVED: ranks/muted.txt");
  150. return;
  151. }
  152. /*
  153. // Mutes yourself indefinitely
  154. if (who == p && time == 0) {
  155. if (p.muted) {
  156. p.muted = false;
  157. //p.SendMessage("You &bun-muted" + _s.props.DefaultColor + " yourself!");
  158. _s.GlobalMessage(p.color + p.name + _s.props.DefaultColor + " has been &bun-muted!");
  159. _s.muted.Remove (p.uuid);
  160. _s.muted.Save("muted.txt");
  161. _s.logger.Log("SAVED: ranks/muted.txt");
  162. string path = "ranks/muted.txt";
  163. string name = who.name.ToLower();
  164. foreach (string line in File.ReadAllLines(path)) {
  165. string[] finder = line.Split (':');
  166. if (finder.Length == 2 && finder[0] == name) {
  167. _s.muted.Remove(p.uuid);
  168. _s.muted.Save("muted.txt");
  169. _s.logger.Log("SAVED: ranks/muted.txt");
  170. }
  171. }
  172. return;
  173. }
  174. else {
  175. p.muted = true;
  176. //p.SendMessage("You &8muted " + _s.props.DefaultColor + "yourself!");
  177. _s.GlobalMessage (p.color + p.name + _s.props.DefaultColor + " has been &8muted!");
  178. _s.muted.Add (p.uuid, p.name);
  179. _s.muted.Save("muted.txt");
  180. _s.logger.Log("SAVED: ranks/muted.txt");
  181. return;
  182. }
  183. }
  184. // If you didn't enter a time, the person will be muted indefinitely.
  185. if (time == 0) {
  186. if (who.muted) {
  187. who.muted = false;
  188. _s.GlobalChat(who, who.color + who.name + _s.props.DefaultColor + " has been &bun-muted", false);
  189. _s.muted.Remove(who.uuid);
  190. _s.muted.Save("muted.txt");
  191. _s.logger.Log("SAVED: ranks/muted.txt");
  192. string path = "ranks/muted.txt";
  193. string name = who.name.ToLower();
  194. foreach (string line in File.ReadAllLines(path)) {
  195. string[] finder = line.Split (':');
  196. if (finder.Length == 2 && finder[0] == name) {
  197. _s.muted.Remove(who.uuid);
  198. _s.muted.Save("muted.txt");
  199. _s.logger.Log("SAVED: ranks/muted.txt");
  200. }
  201. }
  202. }
  203. else {
  204. who.muted = true;
  205. _s.GlobalChat(who, who.color + who.name + _s.props.DefaultColor + " has been &8muted", false);
  206. _s.muted.Add(who.uuid, who.name);
  207. _s.muted.Save("muted.txt");
  208. _s.logger.Log("SAVED: ranks/muted.txt");
  209. }
  210. return;
  211. }
  212. // If you entered a negative time, shame on you.
  213. if (time < 0) {
  214. p.SendMessage("Cannot use negative times!");
  215. return;
  216. }
  217. // Finally, if you entered a good time, the person is muted for that time.
  218. else {
  219. if (who.muted) {
  220. p.SendMessage(who.name + " is already muted! Please wait until they are unmuted.");
  221. return;
  222. }
  223. if (p.IsConsole) {
  224. Thread t = new Thread(delegate() {
  225. ConsoleMute (time, who);
  226. });
  227. t.Start();
  228. return;
  229. }
  230. else {
  231. Stopwatch timer = new Stopwatch();
  232. timer.Start();
  233. who.muted = true;
  234. string mutedtime = who.name + ":" + time.ToString ();
  235. _s.GlobalMessage(who.color + who.name + _s.props.DefaultColor + " was &8muted" + _s.props.DefaultColor + " for " + time.ToString() + " seconds.");
  236. //Player.SendMessage (who, "You were &8muted" + _s.props.DefaultColor + " for " + time.ToString() + " seconds");
  237. _s.muted.Add (who.uuid, mutedtime);
  238. _s.muted.Save ("muted.txt");
  239. _s.logger.Log("SAVED: ranks/muted.txt");
  240. while (timer.ElapsedMilliseconds < time*1000) { }
  241. if (_s.players.Contains(who) && _s.muted.Contains(who.uuid)) {
  242. _s.muted.Remove(who.uuid);
  243. _s.muted.Save ("muted.txt");
  244. _s.logger.Log("SAVED: ranks/muted.txt");
  245. who.muted = false;
  246. _s.GlobalMessage(who.color + who.name + _s.props.DefaultColor + " was &bun-muted.");
  247. timer.Reset();
  248. return;
  249. }
  250. }
  251. }*/
  252. }
  253. /// <summary>
  254. /// Method that mutes a player for a certain amount of time if
  255. /// /mute is used from the console.
  256. /// </summary>
  257. /// <param name="data"> The time the player is muted for. </param>
  258. /// <param name="who"> The player to be muted.</param>
  259. public void ConsoleMute(int data, Player who) {
  260. Stopwatch timer = new Stopwatch();
  261. timer.Start();
  262. who.muted = true;
  263. string mutedtime = who.name + ":" + data.ToString ();
  264. _s.GlobalMessage(who.color + who.name + _s.props.DefaultColor + " was &8muted" + _s.props.DefaultColor + " for " + data.ToString() + " seconds.");
  265. //Player.SendMessage (who, "You were muted for " + data.ToString() + " seconds!");
  266. _s.muted.Add (who.uuid, mutedtime);
  267. _s.muted.Save ("muted.txt");
  268. _s.logger.Log("SAVED: ranks/muted.txt");
  269. while (timer.ElapsedMilliseconds < data*1000) { }
  270. if (_s.players.Contains(who) && _s.muted.Contains(who.uuid)) {
  271. _s.muted.Remove(who.uuid);
  272. _s.muted.Save ("muted.txt");
  273. _s.logger.Log("SAVED: ranks/muted.txt");
  274. who.muted = false;
  275. _s.GlobalMessage(who.color + who.name + _s.props.DefaultColor + " was &bun-muted.");
  276. timer.Reset();
  277. return;
  278. }
  279. }
  280. /// <summary>
  281. /// Called when /help is used on /mute.
  282. /// </summary>
  283. /// <param name="p"> The player that used the /help command. </param>
  284. public override void Help(Player p) {
  285. p.SendMessage("/mute <player?> [seconds?] - Mutes or unmutes the specified player.");
  286. p.SendMessage("If a number of seconds is provided, mutes the specified player for that" +
  287. " amount of time.");
  288. }
  289. }
  290. }