forked from bippity/AdvancedWarpplates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cs
157 lines (143 loc) · 5.11 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Reflection;
using System.Timers;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;
namespace AdvancedWarpplates
{
[ApiVersion(2, 1)]
public class WarpplatePlugin : TerrariaPlugin
{
public Player[] Players = new Player[256];
public WarpplateManager Manager;
public Timer QuickUpdate = new Timer(1000);
public Commands Commands;
private readonly DateTime LastCheck = DateTime.UtcNow;
public override string Name
{
get { return "AdvancedWarpplates"; }
}
public override string Author
{
get { return "DarkunderdoG; Maintained by popstarfreas, Zaicon, bippity, mpql"; }
}
public override string Description
{
get { return "AdvancedWarpplates"; }
}
public override Version Version
{
get { return Assembly.GetExecutingAssembly().GetName().Version; }
}
public WarpplatePlugin(Main game)
: base(game)
{
Order = 1;
}
/// <summary>
/// Registers all necessary hooks to set up the plugin
/// </summary>
public override void Initialize()
{
Manager = new WarpplateManager(TShock.DB);
ServerApi.Hooks.GamePostInitialize.Register(this, OnPostInit);
ServerApi.Hooks.GameInitialize.Register(this, OnInitialize);
ServerApi.Hooks.NetGreetPlayer.Register(this, OnGreetPlayer);
ServerApi.Hooks.ServerLeave.Register(this, OnLeave);
}
/// <summary>
/// Disposes all hooks registered
/// </summary>
/// <param name="disposing">Whether or not the plugin is being disposed</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
ServerApi.Hooks.GamePostInitialize.Deregister(this, OnPostInit);
ServerApi.Hooks.GameInitialize.Deregister(this, OnInitialize);
ServerApi.Hooks.NetGreetPlayer.Deregister(this, OnGreetPlayer);
ServerApi.Hooks.ServerLeave.Deregister(this, OnLeave);
}
base.Dispose(disposing);
}
/// <summary>
/// Loads all the warpplates for the world when it has loaded
/// </summary>
/// <param name="args"></param>
private async void OnPostInit(EventArgs args)
{
await Manager.ReloadAllWarpplates();
QuickUpdate.Elapsed += OnQuickUpdate;
QuickUpdate.Enabled = true;
}
/// <summary>
/// Initializes all of the commands for this plugin
/// </summary>
/// <param name="args">The initialize event arguments</param>
public void OnInitialize(EventArgs args)
{
Commands = new Commands(Manager, Players);
TShockAPI.Commands.ChatCommands.Add(new Command("warpplate.set", Commands.MainCommand, "warpplate", "wp")
{
HelpText = "Manage warpplates."
});
TShockAPI.Commands.ChatCommands.Add(new Command("warpplate.use", Commands.WarpplateAllow, "wpa")
{
HelpText = "Enable/Disable activating warpplates for yourself."
});
}
/// <summary>
/// Creates a new player object when a player joins
/// </summary>
/// <param name="args">The greet player arguments</param>
public void OnGreetPlayer(GreetPlayerEventArgs args)
{
lock (Players)
{
Players[args.Who] = new Player(args.Who, Manager);
}
}
/// <summary>
/// Nulls out the index of the players list when a player leaves
/// </summary>
/// <param name="args">The leave event arguments</param>
private void OnLeave(LeaveEventArgs args)
{
lock (Players)
{
Players[args.Who] = null;
}
}
/// <summary>
/// Runs the checks for every player if they have enabled warpplate use and have permission
/// </summary>
/// <param name="sender">The timer object that invoked this method</param>
/// <param name="args">The elapsed event args</param>
private void OnQuickUpdate(object sender, ElapsedEventArgs args)
{
lock (Players)
{
for (int i = 0; i < Players.Length; i++)
{
Player player = Players[i];
if (player == null || player.TSPlayer == null)
{
continue;
}
try
{
if (player.TSPlayer.Group.HasPermission("warpplate.use") && player.CanUseWarpplates)
{
player.Update();
}
}
catch (Exception ex)
{
TShock.Log.Error(ex.ToString());
}
}
}
}
}
}