-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathRustPlugin.cs
More file actions
257 lines (231 loc) · 10 KB
/
RustPlugin.cs
File metadata and controls
257 lines (231 loc) · 10 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using System;
using System.Reflection;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Libraries;
using UnityEngine;
namespace Oxide.Plugins
{
public abstract class RustPlugin : CSharpPlugin
{
protected Command cmd = Interface.Oxide.GetLibrary<Command>();
protected Game.Rust.Libraries.Rust rust = Interface.Oxide.GetLibrary<Game.Rust.Libraries.Rust>();
protected Game.Rust.Libraries.Item Item = Interface.Oxide.GetLibrary<Game.Rust.Libraries.Item>();
protected Player Player = Interface.Oxide.GetLibrary<Player>();
protected Server Server = Interface.Oxide.GetLibrary<Server>();
public override void HandleAddedToManager(PluginManager manager)
{
foreach (FieldInfo field in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
{
object[] attributes = field.GetCustomAttributes(typeof(OnlinePlayersAttribute), true);
if (attributes.Length > 0)
{
PluginFieldInfo pluginField = new PluginFieldInfo(this, field);
if (pluginField.GenericArguments.Length != 2 || pluginField.GenericArguments[0] != typeof(BasePlayer))
{
Puts($"The {field.Name} field is not a Hash with a BasePlayer key! (online players will not be tracked)");
continue;
}
if (!pluginField.LookupMethod("Add", pluginField.GenericArguments))
{
Puts($"The {field.Name} field does not support adding BasePlayer keys! (online players will not be tracked)");
continue;
}
if (!pluginField.LookupMethod("Remove", typeof(BasePlayer)))
{
Puts($"The {field.Name} field does not support removing BasePlayer keys! (online players will not be tracked)");
continue;
}
if (pluginField.GenericArguments[1].GetField("Player") == null)
{
Puts($"The {pluginField.GenericArguments[1].Name} class does not have a public Player field! (online players will not be tracked)");
continue;
}
if (!pluginField.HasValidConstructor(typeof(BasePlayer)))
{
Puts($"The {field.Name} field is using a class which contains no valid constructor (online players will not be tracked)");
continue;
}
onlinePlayerFields.Add(pluginField);
}
}
foreach (MethodInfo method in GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance))
{
object[] attributes = method.GetCustomAttributes(typeof(ConsoleCommandAttribute), true);
if (attributes.Length > 0)
{
if (attributes[0] is ConsoleCommandAttribute attribute)
{
cmd.AddConsoleCommand(attribute.Command, this, method.Name);
}
continue;
}
attributes = method.GetCustomAttributes(typeof(ChatCommandAttribute), true);
if (attributes.Length > 0)
{
if (attributes[0] is ChatCommandAttribute attribute)
{
cmd.AddChatCommand(attribute.Command, this, method.Name);
}
}
}
if (onlinePlayerFields.Count > 0)
{
foreach (BasePlayer player in BasePlayer.activePlayerList)
{
AddOnlinePlayer(player);
}
}
base.HandleAddedToManager(manager);
}
[HookMethod("OnPlayerConnected")]
private void base_OnPlayerConnected(BasePlayer player) => AddOnlinePlayer(player);
[HookMethod("OnPlayerDisconnected")]
private void base_OnPlayerDisconnected(BasePlayer player, string reason)
{
// Delay removing player until OnPlayerDisconnected has fired in plugin
NextTick(() =>
{
foreach (PluginFieldInfo pluginField in onlinePlayerFields)
{
pluginField.Call("Remove", player);
}
});
}
private void AddOnlinePlayer(BasePlayer player)
{
foreach (PluginFieldInfo pluginField in onlinePlayerFields)
{
Type type = pluginField.GenericArguments[1];
object onlinePlayer = type.GetConstructor(new[] { typeof(BasePlayer) }) == null ? Activator.CreateInstance(type) : Activator.CreateInstance(type, (object)player);
type.GetField("Player").SetValue(onlinePlayer, player);
pluginField.Call("Add", player, onlinePlayer);
}
}
/// <summary>
/// Print a message to the players console log
/// </summary>
/// <param name="player"></param>
/// <param name="format"></param>
/// <param name="args"></param>
protected void PrintToConsole(BasePlayer player, string format, params object[] args)
{
if (player?.net != null)
{
player.SendConsoleCommand("echo " + (args.Length > 0 ? string.Format(format, args) : format));
}
}
/// <summary>
/// Print a message to every players console log
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
protected void PrintToConsole(string format, params object[] args)
{
if (BasePlayer.activePlayerList.Count >= 1)
{
ConsoleNetwork.BroadcastToAllClients("echo " + (args.Length > 0 ? string.Format(format, args) : format));
}
}
/// <summary>
/// Print a message to the players chat log
/// </summary>
/// <param name="player"></param>
/// <param name="format"></param>
/// <param name="args"></param>
protected void PrintToChat(BasePlayer player, string format, params object[] args)
{
if (player?.net != null)
{
player.SendConsoleCommand("chat.add", 2, 0, args.Length > 0 ? string.Format(format, args) : format);
}
}
/// <summary>
/// Print a message to every players chat log
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
protected void PrintToChat(string format, params object[] args)
{
if (BasePlayer.activePlayerList.Count >= 1)
{
ConsoleNetwork.BroadcastToAllClients("chat.add", 2, 0, args.Length > 0 ? string.Format(format, args) : format);
}
}
/// <summary>
/// Send a reply message in response to a console command
/// </summary>
/// <param name="arg"></param>
/// <param name="format"></param>
/// <param name="args"></param>
protected void SendReply(ConsoleSystem.Arg arg, string format, params object[] args)
{
BasePlayer player = arg.Connection?.player as BasePlayer;
string message = args.Length > 0 ? string.Format(format, args) : format;
if (player?.net != null)
{
player.SendConsoleCommand("echo " + message);
return;
}
Puts(message);
}
/// <summary>
/// Send a reply message in response to a chat command
/// </summary>
/// <param name="player"></param>
/// <param name="format"></param>
/// <param name="args"></param>
protected void SendReply(BasePlayer player, string format, params object[] args) => PrintToChat(player, format, args);
/// <summary>
/// Send a warning message in response to a console command
/// </summary>
/// <param name="arg"></param>
/// <param name="format"></param>
/// <param name="args"></param>
protected void SendWarning(ConsoleSystem.Arg arg, string format, params object[] args)
{
BasePlayer player = arg.Connection?.player as BasePlayer;
string message = args.Length > 0 ? string.Format(format, args) : format;
if (player?.net != null)
{
player.SendConsoleCommand("echo " + message);
return;
}
Debug.LogWarning(message);
}
/// <summary>
/// Send an error message in response to a console command
/// </summary>
/// <param name="arg"></param>
/// <param name="format"></param>
/// <param name="args"></param>
protected void SendError(ConsoleSystem.Arg arg, string format, params object[] args)
{
BasePlayer player = arg.Connection?.player as BasePlayer;
string message = args.Length > 0 ? string.Format(format, args) : format;
if (player?.net != null)
{
player.SendConsoleCommand("echo " + message);
return;
}
Debug.LogError(message);
}
/// <summary>
/// Forces the player to a specific position
/// </summary>
/// <param name="player"></param>
/// <param name="destination"></param>
protected void ForcePlayerPosition(BasePlayer player, Vector3 destination)
{
player.MovePosition(destination);
if (!player.IsSpectating() || Vector3.Distance(player.transform.position, destination) > 25.0)
{
player.ClientRPC(RpcTarget.Player("ForcePositionTo", player), destination);
}
else
{
player.SendNetworkUpdate(BasePlayer.NetworkQueue.UpdateDistance);
}
}
}
}