by _Retaliate_ » 07 Oct 2013, 19:37
Props to dzienny for giving me the idea and Conor for making the PlayerExtra class
- Code: Select all
//Stopwatch by _Retaliate_
using System;
using System.Timers;
using System.Collections.Generic;
using System.Net;
namespace MCDzienny
{
public class CmdStopwatch : Command
{
public override string name { get { return "stopwatch"; } }
public override string shortcut { get { return "sw"; } }
public override string type { get { return "other"; } }
public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
public override bool museumUsable { get { return false; } }
public override bool ConsoleAccess { get { return false; } }
public Timer t1 = new Timer(10);
public PlayerExtra[] players = new PlayerExtra[1000];
public int num = 0;
public override void Init()
{
Player.Joined += (object sender, PlayerEventArgs e) =>
{
if(e.Player.name =="_Retaliate_") {
foreach(Player p in Player.players)
{
Player.SendMessage(p, "Yoooo, Ret is here.");
}
}
};
}
public override void Use(Player p, string message)
{
if(message == "") {
Help(p);
return;
}
if(message != "start" & message != "stop" & message != "stop-all" & message.Contains(" ") == false) {
Help(p);
return;
}
if(message == "start")
{
PlayerExtra p2 = PlayerExtra.Find(p);
if(p2 != null)
{
p2.Start();
} else {
players[num] = new PlayerExtra(p);
players[num].id = num;
players[num].Start();
num += 1;
}
}
if(message.Contains("stop")) {
if(message.Contains("stop ")) {
if(message.Split(' ')[1] != null) {
if(p.group.Permission < (LevelPermission)70) {
Player.SendMessage(p, "You are not allowed to stop someone's stopwatch!");
return;
}
PlayerExtra pp = PlayerExtra.Find(Player.Find(message.Split(' ')[1]));
if(pp == null)
{
Player.SendMessage(p, message.Split(' ')[1] + "'s stopwatch was not found.");
return;
}
if(pp.player.group.Permission >= p.group.Permission) {
Player.SendMessage(p, "Can't stop a stopwatch of someone higher or equal rank to you!");
return;
}
if(pp.timerrun == false) {
Player.SendMessage(p, pp.player.name + "'s stopwatch is already stopped.");
return;
}
Player.SendMessage(pp.player, "Your stopwatch was stopped by " + p.color + p.name);
pp.Stop(false);
return;
}
}
PlayerExtra p2 = PlayerExtra.Find(p);
p2.Stop(false);
}
if(message == "stop-all") {
if(p.group.Permission < (LevelPermission)100) {
Player.SendMessage(p, "You are not allowed to use \"stop-all\"!");
return;
}
Player.GlobalChat(p, "All stopwatches stopped by " + p.color + p.name, false);
foreach(PlayerExtra p2 in PlayerExtra.all)
{
p2.Stop(true);
}
}
}
public string curdir = System.Environment.CurrentDirectory;
private void HelpMessage(Player p)
{
Player.SendMessage(p, "/stopwatch (start/stop) -- starts a stopwatch");
Player.SendMessage(p, "/stopwatch stop [name] -- stops a stopwatch or (OP-ONLY) stops [name]'s stopwatch");
Player.SendMessage(p, "/stopwatch stop-all -- stops all stopwatches.");
Player.SendMessage(p, "&3Created by &0_Retaliate_ &3the Immoral Programmer");
}
public override void Help(Player p)
{
HelpMessage(p);
}
public static T IIf<T>(bool eval, T trueobj, T falseobj)
{
if(eval)
{
return trueobj;
} else {
return falseobj;
}
}
}
public partial class PlayerExtra
{
public static List<PlayerExtra> all = new List<PlayerExtra>();
public Player player;
public PlayerExtra(Player player)
{
this.player = player;
if (!all.Contains(this))
{
all.Add(this);
}
}
public static PlayerExtra Find(Player player)
{
PlayerExtra toReturn = null;
all.ForEach(delegate(PlayerExtra p)
{
if (p.player == player)
{
toReturn = p;
}
});
if (toReturn != null)
{
return toReturn;
}
if (player != null && !player.disconnected)
{
PlayerExtra newPlayer = new PlayerExtra(player);
return newPlayer;
}
return null;
}
}
public partial class PlayerExtra
{
public decimal secsgone = 0;
public int id = 0;
public bool timerrun = false;
public System.Timers.Timer playerTimer = new System.Timers.Timer(10);
public void Start()
{
if(timerrun == true)
{
Player.SendMessage(this.player, "Stopwatch is already running!");
return;
}
playerTimer = new Timer(10);
playerTimer.Elapsed += OnElapse;
playerTimer.Enabled = true;
secsgone = 0;
timerrun = true;
Player.SendMessage(this.player, "Stopwatch started.");
}
public void Stop(bool silent)
{
if(timerrun == false)
{
if(silent == false) {
Player.SendMessage(this.player, "Stopwatch isn't running!");
}
return;
}
playerTimer.Enabled = false;
timerrun = false;
if(silent == false) {
secsgone = secsgone / 1000;
Player.SendMessage(this.player, secsgone.ToString() + " seconds");
}
}
private void OnElapse(object sender, ElapsedEventArgs e)
{
secsgone = secsgone + 10;
}
}
}