-
Notifications
You must be signed in to change notification settings - Fork 1
/
HuntPlugin.cs
312 lines (280 loc) · 11.5 KB
/
HuntPlugin.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.Collections.Generic;
using Hunt.RPG;
using Hunt.RPG.Keys;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Configuration;
using Oxide.Core.Plugins;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Hunt RPG", "PedraozauM / SW", "1.2.6", ResourceId = 841)]
public class HuntPlugin : RustPlugin
{
private readonly HuntRPG HuntRPGInstance;
private bool ServerInitialized;
private bool UpdateConfig;
private bool UpdatePlayerData;
private DynamicConfigFile HuntDataFile;
public HuntPlugin()
{
HasConfig = true;
HuntRPGInstance = new HuntRPG(this);
}
protected override void LoadDefaultConfig()
{
UpdateConfig = true;
DefaultConfig();
}
private void DefaultConfig()
{
if (!ServerInitialized && UpdateConfig)
{
//this will only be called if there is not a config file, or it needs updating
Config[HK.ConfigVersion] = Version;
Config[HK.XPTable] = HuntTablesGenerator.GenerateXPTable(HK.MaxLevel, HK.BaseXP, HK.LevelMultiplier, HK.LevelModule, HK.ModuleReducer);
Config[HK.MessagesTable] = HuntTablesGenerator.GenerateMessageTable();
Config[HK.SkillTable] = HuntTablesGenerator.GenerateSkillTable();
SaveConfig();
}
else
{
//this will be called only on serverinit if the config needs updating
Config[HK.ItemTable] = HuntTablesGenerator.GenerateItemTable();
SaveConfig();
}
if (!UpdatePlayerData || !UpdateConfig) return;
// this will only be called if this version requires a data wipe and the config is outdated.
LogToConsole("This version needs a wipe to data file.");
LogToConsole("Dont worry levels will be kept! =]");
LogToConsole("Doing that now...");
LoadRPG(false);
var profiles = new Dictionary<string, RPGInfo>(ReadFromData<Dictionary<string, RPGInfo>>(HK.Profile));
var rpgInfos = new Dictionary<string, RPGInfo>();
foreach (var profile in profiles)
{
var steamId = profile.Key;
var player = BasePlayer.FindByID(Convert.ToUInt64(steamId)) ??
BasePlayer.FindSleeping(Convert.ToUInt64(steamId));
var rpgInfo = new RPGInfo(player.displayName);
rpgInfos.Add(steamId, rpgInfo);
HuntRPGInstance.LevelUpPlayer(rpgInfo, profile.Value.Level);
}
LogToConsole("Data file updated!");
SaveRPG(rpgInfos, new Dictionary<string, string>());
UpdatePlayerData = false;
}
private void LoadRPG(bool showMsgs = true)
{
LoadConfig();
if (showMsgs)
LogToConsole("Loading plugin data and config...");
HuntDataFile = Interface.GetMod().DataFileSystem.GetDatafile(HK.DataFileName);
var rpgConfig = ReadFromData<Dictionary<string, RPGInfo>>(HK.Profile) ?? new Dictionary<string, RPGInfo>();
if (showMsgs)
LogToConsole(String.Format("{0} profiles loaded", rpgConfig.Count));
var playerFurnaces = ReadFromData<Dictionary<string, string>>(HK.Furnaces) ?? new Dictionary<string, string>();
if (showMsgs)
LogToConsole(String.Format("{0} furnaces loaded", playerFurnaces.Count));
var xpTable = ReadFromConfig<Dictionary<int, long>>(HK.XPTable);
var messagesTable = ReadFromConfig<PluginMessagesConfig>(HK.MessagesTable);
var skillTable = ReadFromConfig<Dictionary<string, Skill>>(HK.SkillTable);
var itemTable = ReadFromConfig<Dictionary<string, ItemInfo>>(HK.ItemTable);
HuntRPGInstance.ConfigRPG(messagesTable, xpTable, skillTable, itemTable, rpgConfig, playerFurnaces);
if (showMsgs)
LogToConsole("Data and config loaded!");
}
public T ReadFromConfig<T>(string configKey)
{
string serializeObject = JsonConvert.SerializeObject(Config[configKey]);
return JsonConvert.DeserializeObject<T>(serializeObject);
}
public T ReadFromData<T>(string dataKey)
{
string serializeObject = JsonConvert.SerializeObject(HuntDataFile[dataKey]);
return JsonConvert.DeserializeObject<T>(serializeObject);
}
public void SaveRPG(Dictionary<string, RPGInfo> rpgConfig, Dictionary<string, string> playersFurnaces, bool showMsgs = true)
{
if (showMsgs)
LogToConsole("Data being saved...");
HuntDataFile[HK.Profile] = rpgConfig;
HuntDataFile[HK.Furnaces] = playersFurnaces;
Interface.GetMod().DataFileSystem.SaveDatafile(HK.DataFileName);
if (!showMsgs) return;
LogToConsole(String.Format("{0} profiles saved", rpgConfig.Count));
LogToConsole(String.Format("{0} furnaces saved", playersFurnaces.Count));
LogToConsole("Data was saved successfully!");
}
[HookMethod("Init")]
void Init()
{
LogToConsole(HuntRPGInstance == null ? "Problem initializating RPG Instance!" : "Hunt RPG initialized!");
}
[HookMethod("OnServerInitialized")]
void OnServerInitialized()
{
ServerInitialized = true;
DefaultConfig();
LoadRPG();
}
[HookMethod("OnUnload")]
void OnUnload()
{
HuntRPGInstance.SaveRPG();
}
[HookMethod("Loaded")]
private void Loaded()
{
Interface.GetMod().DataFileSystem.GetDatafile(HK.DataFileName);
var configVersion = new VersionNumber();
if (Config[HK.ConfigVersion] != null)
configVersion = ReadFromConfig<VersionNumber>(HK.ConfigVersion);
if (Version.Equals(configVersion))
{
PrintToChat("<color=lightblue>Hunt</color>: RPG Loaded!");
PrintToChat("<color=lightblue>Hunt</color>: To see the Hunt RPG help type \"/hunt\" or \"/h\"");
return;
}
LogToConsole("Your config needs updating...Doing it now.");
Config.Clear();
UpdateConfig = true;
UpdatePlayerData = true;
var wasUpdated = UpdatePlayerData;
DefaultConfig();
LogToConsole("Config updated!");
foreach (var player in BasePlayer.activePlayerList)
HuntRPGInstance.PlayerInit(player, wasUpdated);
}
[HookMethod("OnPlayerInit")]
void OnPlayerInit(BasePlayer player)
{
HuntRPGInstance.PlayerInit(player, UpdatePlayerData);
}
[HookMethod("OnEntityAttacked")]
object OnEntityAttacked(MonoBehaviour entity, HitInfo hitInfo)
{
var player = entity as BasePlayer;
if (player == null) return null;
if (!HuntRPGInstance.OnAttacked(player, hitInfo)) return null;
hitInfo = new HitInfo();
return hitInfo;
}
[HookMethod("OnPlayerAttack")]
object OnPlayerAttack(BasePlayer player, HitInfo hitInfo)
{
return HuntRPGInstance.OnPlayerAttack(player, hitInfo) ? true as object : null;
}
[HookMethod("OnEntityDeath")]
void OnEntityDeath(MonoBehaviour entity, HitInfo hitinfo)
{
var player = entity as BasePlayer;
if (player == null) return;
HuntRPGInstance.OnDeath(player);
}
[HookMethod("OnItemCraft")]
ItemCraftTask OnItemCraft(ItemCraftTask item)
{
return HuntRPGInstance.OnItemCraft(item);
}
[HookMethod("OnGather")]
void OnGather(ResourceDispenser dispenser, BaseEntity entity, Item item)
{
HuntRPGInstance.OnGather(dispenser, entity, item);
}
[HookMethod("OnItemDeployed")]
void OnItemDeployed(Deployer deployer, BaseEntity baseEntity)
{
HuntRPGInstance.OnDeployItem(deployer, baseEntity);
}
[HookMethod("OnConsumeFuel")]
void OnConsumeFuel(BaseOven oven,Item fuel, ItemModBurnable burnable)
{
HuntRPGInstance.OnConsumeFuel(oven, fuel, burnable);
}
[HookMethod("OnBuildingBlockDoUpgradeToGrade")]
object OnBuildingBlockUpgrade(BuildingBlock buildingBlock, BaseEntity.RPCMessage message, BuildingGrade.Enum grade)
{
HuntRPGInstance.OnBuildingBlockUpgrade(message.player, buildingBlock, grade);
return null;
}
[ChatCommand("h")]
void cmdHuntShortcut(BasePlayer player, string command, string[] args)
{
cmdHunt(player, command, args);
}
[ChatCommand("hunt")]
void cmdHunt(BasePlayer player, string command, string[] args)
{
HuntRPGInstance.HandleChatCommand(player, args);
}
[ConsoleCommand("hunt.saverpg")]
private void cmdSaveRPG(ConsoleSystem.Arg arg)
{
if (!arg.CheckPermissions()) return;
HuntRPGInstance.SaveRPG();
}
[ConsoleCommand("hunt.resetrpg")]
private void cmdResetRPG(ConsoleSystem.Arg arg)
{
if (!arg.CheckPermissions()) return;
HuntRPGInstance.ResetRPG();
}
[ConsoleCommand("hunt.genxptable")]
private void cmdGenerateXPTable(ConsoleSystem.Arg arg)
{
if (!arg.CheckPermissions()) return;
arg.ReplyWith("Gerando Tabela");
var levelMultiplier = HK.LevelMultiplier;
var baseXP = HK.BaseXP;
var levelModule = HK.LevelModule;
var moduleReducer = HK.ModuleReducer;
if (arg.HasArgs())
baseXP = arg.GetInt(0);
if (arg.HasArgs(2))
levelMultiplier = arg.GetFloat(1);
if (arg.HasArgs(3))
levelModule = arg.GetInt(2);
if (arg.HasArgs(4))
moduleReducer = arg.GetFloat(3);
Config[HK.XPTable] = HuntTablesGenerator.GenerateXPTable(HK.MaxLevel, baseXP, levelMultiplier, levelModule, moduleReducer);
SaveConfig();
arg.ReplyWith("Tabela Gerada");
}
[HookMethod("OnServerSave")]
void OnServerSave()
{
HuntRPGInstance.SaveRPG();
}
public void TeleportPlayerTo(BasePlayer player, Vector3 position)
{
ForcePlayerPosition(player, position);
}
public Vector3? GetGround(Vector3 position)
{
var direction = Vector3.forward;
var raycastHits = Physics.RaycastAll(position, direction, 50f).GetEnumerator();
float nearestDistance = 9999f;
Vector3? nearestPoint = null;
while (raycastHits.MoveNext())
{
var hit = (raycastHits.Current);
if (hit != null)
{
RaycastHit raycastHit = (RaycastHit)hit;
if (raycastHit.distance < nearestDistance)
{
nearestDistance = raycastHit.distance;
nearestPoint = raycastHit.point;
}
}
}
return nearestPoint;
}
public void LogToConsole(string message)
{
Puts(String.Format("Hunt: {0}",message));
}
}
}