Advertisement
Spectrewiz

"Commands.cs" for my Ticket Plugin (TShock) - VERY PRIMITIVE

Feb 6th, 2012
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.77 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 override string Name
  18.         {
  19.             get { return "TicketSystem"; }
  20.         }
  21.  
  22.         public override string Author
  23.         {
  24.             get { return "Spectrewiz"; }
  25.         }
  26.  
  27.         public override string Description
  28.         {
  29.             get { return "This plugin allows users in game to file tickets that admins and moderators can access."; }
  30.         }
  31.  
  32.         public override Version Version
  33.         {
  34.             get { return Assembly.GetExecutingAssembly().GetName().Version; }
  35.         }
  36.  
  37.         public override void Initialize()
  38.         {
  39.             GameHooks.Update += OnUpdate;
  40.             GameHooks.Initialize += OnInitialize;
  41.             NetHooks.GreetPlayer += OnGreetPlayer;
  42.             ServerHooks.Leave += OnLeave;
  43.             ServerHooks.Chat += OnChat;
  44.         }
  45.  
  46.         protected override void Dispose(bool disposing)
  47.         {
  48.             if (disposing)
  49.             {
  50.                 GameHooks.Update -= OnUpdate;
  51.                 GameHooks.Initialize -= OnInitialize;
  52.                 NetHooks.GreetPlayer -= OnGreetPlayer;
  53.                 ServerHooks.Leave -= OnLeave;
  54.                 ServerHooks.Chat -= OnChat;
  55.             }
  56.             base.Dispose(disposing);
  57.         }
  58.  
  59.         public TicketPlugin(Main game)
  60.             : base(game)
  61.         {
  62.         }
  63.  
  64.         public void OnInitialize()
  65.         {
  66.             List<string> permlist = new List<string>();
  67.             TShock.Groups.AddPermissions("trustedadmin", permlist);
  68.  
  69.             Commands.ChatCommands.Add(new Command(Hlpme, "hlpme", "Hlpme"));
  70.             Commands.ChatCommands.Add(new Command("TicketList", TicketList, "TicketList", "ticketlist", "Ticketlist", "ticketList"));
  71.             Commands.ChatCommands.Add(new Command("TicketClear", TicketClear, "TicketClear", "Ticketclear", "ticketclear", "ticketClear", "TicketsClear", "Ticketsclear", "ticketsclear", "ticketsClear"));
  72.         }
  73.  
  74.         public void OnUpdate()
  75.         {
  76.         }
  77.  
  78.         public void OnGreetPlayer(int who, HandledEventArgs e)
  79.         {
  80.             TShock.Players[who].SendMessage("To write a Ticket, use /hlpme <Message>", Color.DarkCyan);
  81.         }
  82.  
  83.         public void OnLeave(int ply)
  84.         {
  85.         }
  86.  
  87.         public void OnChat(messageBuffer msg, int ply, string text, HandledEventArgs e)
  88.         {
  89.         }
  90.  
  91.         public static void Hlpme(CommandArgs args)
  92.         {
  93.             if (args.Parameters.Count < 1)
  94.             {
  95.                 args.Player.SendMessage("You must enter a message!", Color.Red);
  96.             }
  97.             else if (args.Parameters.Count > 1)
  98.             {
  99.                 args.Player.SendMessage("You must put quotes around a multiple worded ticket. Like /hlpme ''word1 word2''", Color.Red);
  100.             }
  101.             else
  102.             {
  103.                 string username = args.Player.Name;
  104.                 string ticket = args.Parameters[0];
  105.                 args.Player.SendMessage("Your Ticket has been sent!", Color.DarkCyan);
  106.                 StreamWriter tw = new StreamWriter("Tickets.txt", true);
  107.                 tw.WriteLine(string.Format("{0} - {1}: {2}", DateTime.Now, username, ticket));
  108.                 tw.Close();
  109.             }
  110.         }
  111.  
  112.         public static void TicketList(CommandArgs args)
  113.         {
  114.             try
  115.             {
  116.                 StreamReader sw = new StreamReader("Tickets.txt", true);
  117.                 while (sw.Peek() >= 0)
  118.                 {
  119.                     args.Player.SendMessage(sw.ReadLine());
  120.                 }
  121.                 sw.Close();
  122.             }
  123.             catch (Exception e)
  124.             {
  125.                 // Let the console know what went wrong, and tell the player that the file could not be read.
  126.                 args.Player.SendMessage("The file could not be read, or it doesnt exist.");
  127.                 Console.WriteLine(e.Message);
  128.             }
  129.         }
  130.  
  131.         public static void TicketClear(CommandArgs args)
  132.         {
  133.             if (args.Parameters.Count < 1)
  134.             {
  135.                 args.Player.SendMessage("You must either choose all, or select a date to clear.", Color.Red);
  136.             }
  137.             else if (args.Parameters.Count > 1)
  138.             {
  139.                 args.Player.SendMessage("You can't input more than one option!", Color.Red);
  140.             }
  141.             else if (args.Parameters.Count == 1)
  142.             {
  143.                 switch (args.Parameters[0])
  144.                 {
  145.                     case "all":
  146.                         try
  147.                         {
  148.                             File.Delete("Tickets.txt");
  149.                             args.Player.SendMessage("All of the Tickets were cleared!", Color.DarkCyan);
  150.                             Console.WriteLine("{0} has cleared all of the tickets.", args.Player.Name);
  151.                         }
  152.                         catch (Exception e)
  153.                         {
  154.                             // Let the console know what went wrong, and tell the player that there was an error.
  155.                             args.Player.SendMessage("Something went wrong when you tried to clear the tickets, contact an administrator when you can.", Color.Red);
  156.                             Console.WriteLine(e.Message);
  157.                         }
  158.                         break;
  159.                     case "mm/dd/yyyy": //I need to figure this out soon
  160.                         /*
  161.             * COMING SOON *
  162.             */
  163.                         break;
  164.                 }
  165.             }
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement