Advertisement
Spectrewiz

Problem invisible to a debugger (harumph!)

Mar 9th, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Drawing;
  5. using Terraria;
  6. using Hooks;
  7. using TShockAPI;
  8. using TShockAPI.DB;
  9. using System.ComponentModel;
  10. using System.IO;
  11.  
  12. namespace TicketPlugin
  13. {
  14.     [APIVersion(1, 11)]
  15.     public class TicketPlugin : TerrariaPlugin
  16.     {
  17.         public static List<Player> Players = new List<Player>();
  18.         public override string Name
  19.         {
  20.             get { return "TicketSystem"; }
  21.         }
  22.  
  23.         public override string Author
  24.         {
  25.             get { return "Spectrewiz"; }
  26.         }
  27.  
  28.         public override string Description
  29.         {
  30.             get { return "This plugin allows users in game to file tickets (complaints) that admins and moderators can access."; }
  31.         }
  32.  
  33.         public override Version Version
  34.         {
  35.             get { return new Version(0, 9, 9); }
  36.         }
  37.  
  38.         public override void Initialize()
  39.         {
  40.             GameHooks.Update += OnUpdate;
  41.             GameHooks.Initialize += OnInitialize;
  42.             NetHooks.GreetPlayer += OnGreetPlayer;
  43.             ServerHooks.Leave += OnLeave;
  44.             ServerHooks.Chat += OnChat;
  45.         }
  46.  
  47.         protected override void Dispose(bool disposing)
  48.         {
  49.             if (disposing)
  50.             {
  51.                 GameHooks.Update -= OnUpdate;
  52.                 GameHooks.Initialize -= OnInitialize;
  53.                 NetHooks.GreetPlayer -= OnGreetPlayer;
  54.                 ServerHooks.Leave -= OnLeave;
  55.                 ServerHooks.Chat -= OnChat;
  56.             }
  57.             base.Dispose(disposing);
  58.         }
  59.  
  60.         public TicketPlugin(Main game)
  61.             : base(game)
  62.         {
  63.             Order = 10;
  64.         }
  65.  
  66.         public void OnInitialize()
  67.         {
  68.             bool tic = false;
  69.  
  70.             foreach (Group group in TShock.Groups.groups)
  71.             {
  72.                 if (group.Name != "superadmin")
  73.                 {
  74.                     if (group.HasPermission("TicketList"))
  75.                         tic = true;
  76.                 }
  77.             }
  78.  
  79.             List<string> permlist = new List<string>();
  80.             if (!tic)
  81.             {
  82.                 permlist.Add("TicketList");
  83.                 permlist.Add("TicketClear");
  84.             }
  85.  
  86.             TShock.Groups.AddPermissions("trustedadmin", permlist);
  87.  
  88.             Commands.ChatCommands.Add(new Command(Hlpme, "hlpme", "ticket"));
  89.             Commands.ChatCommands.Add(new Command("TicketList", TicketList, "ticketlist", "ticlist"));
  90.             Commands.ChatCommands.Add(new Command("TicketClear", TicketClear, "ticketclear", "ticketsclear", "ticclear", "ticsclear"));
  91.             Commands.ChatCommands.Add(new Command("TicketClear", TicBan, "ticketban", "ticban"));
  92.         }
  93.  
  94.         public void OnUpdate()
  95.         {
  96.         }
  97.  
  98.         public void OnGreetPlayer(int who, HandledEventArgs e)
  99.         {
  100.             lock (Players)
  101.                 Players.Add(new Player(who));
  102.             string name = TShock.Players[who].Name;
  103.             string line;
  104.             var ListedPlayer = Player.GetPlayerByName(name);
  105.             int count = NumberOfTickets(name);
  106.             if (!TShock.Players[who].Group.HasPermission("TicketList"))
  107.             {
  108.                 TShock.Players[who].SendMessage("To write a Complaint, use /hlpme ''<Message>''", Color.DarkCyan);
  109.             }
  110.             else if (TShock.Players[who].Group.HasPermission("TicketList"))
  111.             {
  112.                 TShock.Players[who].SendMessage("There are " + count + " tickets submitted, use /ticketlist to view them.", Color.Cyan);
  113.             }
  114.             if (File.Exists(@"tshock\bannedfromtics.txt"))
  115.             {
  116.                 using (StreamReader reader = new StreamReader(@"tshock\bannedfromtics.txt"))
  117.                 {
  118.                     while ((line = reader.ReadLine()) != null)
  119.                     {
  120.                         if (String.Compare(line, name) == 0)
  121.                         {
  122.                             ListedPlayer.SetTicState(Player.CanSubmitTickets.no);
  123.                         }
  124.                     }
  125.                 }
  126.             }
  127.         }
  128.  
  129.         public void OnLeave(int ply)
  130.         {
  131.             lock (Players)
  132.             {
  133.                 for (int i = 0; i < Players.Count; i++)
  134.                 {
  135.                     if (Players[i].Index == ply)
  136.                     {
  137.                         Players.RemoveAt(i);
  138.                         break; //Found the player, break.
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.  
  144.         public void OnChat(messageBuffer msg, int ply, string text, HandledEventArgs e)
  145.         {
  146.         }
  147.  
  148.         public static int NumberOfTickets(string name)
  149.         {
  150.             if (name != null && File.Exists("Tickets.txt"))
  151.             {
  152.                 int count = 0;
  153.                 StreamReader sr = new StreamReader("Tickets.txt", true);
  154.                 while (sr.Peek() >= 0)
  155.                 {
  156.                     sr.ReadLine();
  157.                     count++;
  158.                 }
  159.                 sr.Close();
  160.                 return count;
  161.             }
  162.             return 0;
  163.         }
  164.  
  165.         public static void Hlpme(CommandArgs args)
  166.         {
  167.             var FindMe = Player.GetPlayerByName(args.Player.Name);
  168.             if (FindMe.GetTicState() == Player.CanSubmitTickets.no)
  169.             {
  170.                 args.Player.SendMessage("You cannot send tickets because you have had that privilege revoked.", Color.Red);
  171.             }
  172.             else if ((FindMe.GetTicState() == Player.CanSubmitTickets.yes) && ((args.Parameters.Count == 1) && (args.Parameters[0].ToLower() == "help")))
  173.             {
  174.                 args.Player.SendMessage("To file a complaint about a bug or just a general issue that you have, do /hlpme <message>", Color.Cyan);
  175.             }
  176.             else if ((FindMe.GetTicState() == Player.CanSubmitTickets.yes) && (args.Parameters.Count < 1))
  177.             {
  178.                 args.Player.SendMessage("You must enter a message!", Color.Red);
  179.             }
  180.             else if ((FindMe.GetTicState() == Player.CanSubmitTickets.yes) && ((args.Parameters.Count >= 1) || (args.Parameters.Count == 1 && args.Parameters[0].ToLower() != "help")))
  181.             {
  182.                 try
  183.                 {
  184.                     string text = "";
  185.                     foreach (string word in args.Parameters)
  186.                     {
  187.                         text += word + " ";
  188.                     }
  189.                     string username = args.Player.Name;
  190.                     args.Player.SendMessage("Your Ticket has been sent!", Color.DarkCyan);
  191.                     StreamWriter tw = new StreamWriter("Tickets.txt", true);
  192.                     tw.WriteLine(string.Format("{0} - {1}: {2}", DateTime.Now, username, text));
  193.                     tw.Close();
  194.                     foreach (Player player in TicketPlugin.Players)
  195.                     {
  196.                         if (player.TSPlayer.Group.HasPermission("TicketList"))
  197.                         {
  198.                             player.TSPlayer.SendMessage(string.Format("{0} just submitted a ticket: {1}", args.Player.Name, text), Color.Cyan);
  199.                         }
  200.                     }
  201.                 }
  202.                 catch (Exception e)
  203.                 {
  204.                     args.Player.SendMessage("Your ticket could not be sent, contact an administrator.", Color.Red);
  205.                     Console.ForegroundColor = ConsoleColor.Red;
  206.                     Console.WriteLine(e.Message);
  207.                     Console.ResetColor();
  208.                 }
  209.             }
  210.         }
  211.  
  212.         public static int linenumber = 1;
  213.         public static void TicketList(CommandArgs args)
  214.         {
  215.             try
  216.             {
  217.                 StreamReader sr = new StreamReader("Tickets.txt");
  218.                 while (sr.Peek() >= 0)
  219.                 {
  220.                     args.Player.SendMessage(linenumber + ". " + sr.ReadLine(), Color.Cyan);
  221.                     linenumber++;
  222.                 }
  223.                 sr.Close();
  224.                 linenumber = 1;
  225.             }
  226.             catch (Exception e)
  227.             {
  228.                 // Let the console know what went wrong, and tell the player that the file could not be read.
  229.                 args.Player.SendMessage("The file could not be read, or it doesnt exist.", Color.Red);
  230.                 Console.ForegroundColor = ConsoleColor.Red;
  231.                 Console.WriteLine(e.Message);
  232.                 Console.ResetColor();
  233.             }
  234.         }
  235.  
  236.         public static void TicketClear(CommandArgs args)
  237.         {
  238.             if (args.Parameters.Count < 1)
  239.             {
  240.                 args.Player.SendMessage("Syntax: /ticketclear <all/id> <id>", Color.DarkCyan);
  241.                 args.Player.SendMessage("/ticketclear all: Removes all the tickets", Color.DarkCyan);
  242.                 args.Player.SendMessage("/ticketclear id <id>: Removes one ticket ID based on the IDs listed with /ticketlist", Color.DarkCyan);
  243.                 args.Player.SendMessage("Note: /ticketclear can be shortened to /ticclear", Color.DarkCyan);
  244.             }
  245.             else
  246.             {
  247.                 switch (args.Parameters[0].ToLower())
  248.                 {
  249.                     case "all":
  250.                         try
  251.                         {
  252.                             File.Delete("Tickets.txt");
  253.                             args.Player.SendMessage("All of the Tickets were cleared!", Color.DarkCyan);
  254.                             Console.ForegroundColor = ConsoleColor.Red;
  255.                             Console.WriteLine(string.Format("{0} has cleared all of the tickets.", args.Player.Name));
  256.                             Console.ResetColor();
  257.                         }
  258.                         catch (Exception e)
  259.                         {
  260.                             // Let the console know what went wrong, and tell the player that there was an error.
  261.                             args.Player.SendMessage("All the tickets are already cleared!", Color.Red);
  262.                             Console.ForegroundColor = ConsoleColor.Red;
  263.                             Console.WriteLine(e.Message);
  264.                             Console.ResetColor();
  265.                         }
  266.                         break;
  267.                     case "id":
  268.                         if (args.Parameters.Count > 1)
  269.                         {
  270.                             try
  271.                             {
  272.                                 int lineToDelete = (Convert.ToInt32(args.Parameters[1]) - 1);
  273.                                 var file = new List<string>(System.IO.File.ReadAllLines("Tickets.txt"));
  274.                                 file.RemoveAt(lineToDelete);
  275.                                 File.WriteAllLines("Tickets.txt", file.ToArray());
  276.                                 args.Player.SendMessage(string.Format("Ticket ID {0} was cleared!", args.Parameters[1]), Color.DarkCyan);
  277.                                 Console.ForegroundColor = ConsoleColor.Red;
  278.                                 Console.WriteLine(string.Format("{0} has cleared ticket ID: {1}", args.Player.Name, args.Parameters[1]));
  279.                                 Console.ResetColor();
  280.                             }
  281.                             catch (Exception e)
  282.                             {
  283.                                 args.Player.SendMessage("Not a valid ID.", Color.Red);
  284.                                 Console.ForegroundColor = ConsoleColor.Red;
  285.                                 Console.WriteLine(e.Message);
  286.                                 Console.ResetColor();
  287.                             }
  288.                         }
  289.                         else
  290.                         {
  291.                             args.Player.SendMessage("You have to state a ticket ID! Syntax: /ticclear id <ticID>", Color.Red);
  292.                         }
  293.                         break;
  294.                     case "help":
  295.                         args.Player.SendMessage("Syntax: /ticketclear <all/id> <id>", Color.DarkCyan);
  296.                         args.Player.SendMessage("/ticketclear all: Removes all the tickets", Color.DarkCyan);
  297.                         args.Player.SendMessage("/ticketclear id <id>: Removes one ticket ID based on the IDs listed with /ticketlist", Color.DarkCyan);
  298.                         args.Player.SendMessage("Note: /ticketclear can be shortened to /ticclear", Color.DarkCyan);
  299.                         break;
  300.                     default:
  301.                         args.Player.SendMessage("Syntax: /ticketclear <all/id> <id>", Color.Red);
  302.                         break;
  303.                 }
  304.             }
  305.         }
  306.  
  307.     //THIS IS WHERE IT MESSES UP
  308.         public static void TicBan(CommandArgs args)
  309.         {
  310.             int numberOfPeopleBanned = 1;
  311.             var FindPlayer = TShock.Utils.FindPlayer(args.Parameters[1]);
  312.             var FoundPlayer = FindPlayer[1];
  313.             var ListedPlayer = Player.GetPlayerByName(args.Parameters[1]);
  314.             if (args.Parameters.Count < 1)
  315.             {
  316.                 args.Player.SendMessage("Syntax: /ticketban <ban/unban/list> <player name/id>", Color.DarkCyan);
  317.                 args.Player.SendMessage("/ticketban ban <player name>: stops the player from filing tickets", Color.DarkCyan);
  318.                 args.Player.SendMessage("/ticketban unban <player id>: unbans a player only based on their ID, not their name. Use /ticban list to find out banned IDs", Color.DarkCyan);
  319.                 args.Player.SendMessage("/ticketban list: lists players that are banned and their IDs", Color.DarkCyan);
  320.                 args.Player.SendMessage("Note: /ticketban can be shortened to /ticban", Color.DarkCyan);
  321.             }
  322.             else
  323.             {
  324.                 switch (args.Parameters[0].ToLower())
  325.                 {
  326.                     case "ban":
  327.                         try
  328.                         {
  329.                             if (FindPlayer.Count == 1)
  330.                             {
  331.                                 if (ListedPlayer.GetTicState() == Player.CanSubmitTickets.yes)
  332.                                 {
  333.                                     ListedPlayer.SetTicState(Player.CanSubmitTickets.no);
  334.                                     args.Player.SendMessage(string.Format("You have revoked the privileges of submitting tickets from {0}", FoundPlayer.Name), Color.Red);
  335.                                     using (StreamWriter writer = new StreamWriter(@"tshock\bannedfromtics.txt"))
  336.                                     {
  337.                                         writer.WriteLine(FoundPlayer.Name);
  338.                                     }
  339.                                 }
  340.                                 else
  341.                                 {
  342.                                     args.Player.SendMessage("This player already is banned from using tickets. If you want to give back the privilege, use /ticstop <playerID> (find banned IDs by doing /ticstop)", Color.Red);
  343.                                 }
  344.                             }
  345.                             else if (FindPlayer.Count > 1)
  346.                             {
  347.                                 args.Player.SendMessage(string.Format("There is more than 1 person with the name {0}", args.Parameters[1]), Color.Red);
  348.                             }
  349.                             else if (FindPlayer.Count < 1)
  350.                             {
  351.                                 args.Player.SendMessage(string.Format("There is nobody with the name {0}", args.Parameters[1]), Color.Red);
  352.                             }
  353.                         }
  354.                         catch (Exception e)
  355.                         {
  356.                             args.Player.SendMessage("Something went wrong, contact an administrator when you can.", Color.Red);
  357.                             Console.ForegroundColor = ConsoleColor.Red;
  358.                             Console.WriteLine(e.Message);
  359.                             Console.ResetColor();
  360.                         }
  361.                         break;
  362.                     case "unban":
  363.                         try
  364.                         {
  365.                             int PlayerToDelete = (Convert.ToInt32(args.Parameters[1]) - 1);
  366.                             var file = new List<string>(System.IO.File.ReadAllLines(@"tshock\bannedfromtics.txt"));
  367.                             file.RemoveAt(PlayerToDelete - 1);
  368.                             File.WriteAllLines(@"tshock\bannedfromtics.txt", file.ToArray());
  369.                             args.Player.SendMessage(string.Format("You have given back the privileges of submitting tickets to {0}, this will take affect when they next log in.", args.Parameters[1]), Color.Cyan);
  370.                             Console.ForegroundColor = ConsoleColor.Red;
  371.                             Console.WriteLine(string.Format("{0} has unbanned player ID: {1}", args.Player.Name, args.Parameters[1]));
  372.                             Console.ResetColor();
  373.                         }
  374.                         catch (Exception e)
  375.                         {
  376.                             args.Player.SendMessage(string.Format("Cannot find player ID {0}", args.Parameters[1]), Color.Red);
  377.                             Console.ForegroundColor = ConsoleColor.Red;
  378.                             Console.WriteLine(e.Message);
  379.                             Console.ResetColor();
  380.                         }
  381.                         break;
  382.                     case "list":
  383.                         try
  384.                         {
  385.                             StreamReader sr = new StreamReader(@"tshock\bannedfromtics.txt");
  386.                             while (sr.Peek() >= 0)
  387.                             {
  388.                                 args.Player.SendMessage(numberOfPeopleBanned + ". " + sr.ReadLine(), Color.Cyan);
  389.                                 numberOfPeopleBanned++;
  390.                             }
  391.                             sr.Close();
  392.                             numberOfPeopleBanned = 1;
  393.                         }
  394.                         catch (Exception e)
  395.                         {
  396.                             args.Player.SendMessage("Nobody is banned from using tickets.", Color.Red);
  397.                             Console.ForegroundColor = ConsoleColor.Red;
  398.                             Console.WriteLine(e.Message);
  399.                             Console.ResetColor();
  400.                         }
  401.                         break;
  402.                     case "help":
  403.                         args.Player.SendMessage("Syntax: /ticketban <ban/unban/list> <player name/id>", Color.DarkCyan);
  404.                         args.Player.SendMessage("/ticketban ban <player name>: stops the player from filing tickets", Color.DarkCyan);
  405.                         args.Player.SendMessage("/ticketban unban <player id>: unbans a player only based on their ID, not their name. Use /ticban list to find out banned IDs", Color.DarkCyan);
  406.                         args.Player.SendMessage("/ticketban list: lists players that are banned and their IDs", Color.DarkCyan);
  407.                         args.Player.SendMessage("Note: /ticketban can be shortened to /ticban", Color.DarkCyan);
  408.                         break;
  409.                     default:
  410.                         args.Player.SendMessage("Syntax: /ticketban <ban/unban/list> <player name/id>", Color.Red);
  411.                         break;
  412.                 }
  413.             }
  414.         }
  415.     }
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement