forked from sharkbound/MessageAnnouncer
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMessageAnnouncerPlugin.cs
68 lines (56 loc) · 2.11 KB
/
MessageAnnouncerPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using Rocket.Core.Plugins;
using System.Globalization;
using fr34kyn01535.MessageAnnouncer.Config;
using Rocket.API.Commands;
using Rocket.API.DependencyInjection;
using Rocket.API.Drawing;
using Rocket.API.Scheduler;
using Rocket.API.User;
using Rocket.Core.Logging;
using Rocket.Core.Scheduler;
namespace fr34kyn01535.MessageAnnouncer
{
public class MessageAnnouncerPlugin : Plugin<MessageAnnouncerConfiguration>
{
private readonly ITaskScheduler _scheduler;
public MessageAnnouncerPlugin(IDependencyContainer container, ITaskScheduler scheduler) : base("MessageAnnouncer", container)
{
_scheduler = scheduler;
}
private int _lastindex;
protected override void OnLoad(bool isFromReload)
{
base.OnLoad(isFromReload);
Logger.LogInformation("Loaded.");
_scheduler.SchedulePeriodically(this, PrintMessage, "Message announcer", new TimeSpan(0, 0, 0, ConfigurationInstance.Interval));
if (!isFromReload)
return;
var provider = (TextCommandProvider)Container.Resolve<ICommandProvider>("text_commands");
provider.Rebuild();
}
private void PrintMessage()
{
var userManager = Container.Resolve<IUserManager>();
if (_lastindex > (ConfigurationInstance.Messages.Length - 1))
_lastindex = 0;
Message message = ConfigurationInstance.Messages[_lastindex];
userManager.Broadcast(null, message.Text, GetColorFromName(message.Color, Color.Green));
_lastindex++;
}
private Color GetColorFromName(string colorString, Color defaultColor)
{
var color = defaultColor;
try
{
color = Color.FromName(colorString);
if (color != default(Color))
return color;
int argb = Int32.Parse(colorString.Replace("#", ""), NumberStyles.HexNumber);
color = Color.FromArgb(argb);
}
catch { }
return color;
}
}
}