-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIceyStimmy.cs
165 lines (133 loc) · 5.75 KB
/
IceyStimmy.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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Cysharp.Threading.Tasks;
using IceyStimmy.Helpers;
using IceyStimmy.Models;
using JetBrains.Annotations;
using OpenMod.Unturned.Plugins;
using OpenMod.API.Plugins;
using OpenMod.Core.Helpers;
using SDG.Unturned;
using Steamworks;
[assembly: PluginMetadata("IceyStimmy",
DisplayName = "IceyStimmy",
Author = "Johnanater",
Website = "https://johnanater.com")]
namespace IceyStimmy
{
[UsedImplicitly]
public class IceyStimmy : OpenModUnturnedPlugin
{
private readonly IUtils m_Utils;
private readonly IConfiguration m_Configuration;
private readonly ILogger<IceyStimmy> m_Logger;
private bool _pluginRunning;
private readonly List<CSteamID> _payPlayers = new();
public static Config Config;
public IceyStimmy(
IUtils utils,
IConfiguration configuration,
ILogger<IceyStimmy> logger,
IServiceProvider serviceProvider) : base(serviceProvider)
{
m_Utils = utils;
m_Configuration = configuration;
m_Logger = logger;
}
protected override async UniTask OnLoadAsync()
{
Config = m_Configuration.Get<Config>();
_pluginRunning = true;
await UniTask.SwitchToMainThread();
Provider.onEnemyConnected += OnEnemyConnected;
Provider.onEnemyDisconnected += OnEnemyDisconnected;
PlayerLife.onPlayerDied += OnPlayerDied;
DamageTool.damageAnimalRequested += DamageAnimalRequested;
DamageTool.damageZombieRequested += OnDamageZombieRequested;
m_Logger.LogInformation($"Successfully loaded {GetType().Name} by Johnanater, version {Version}");
}
protected override async UniTask OnUnloadAsync()
{
_pluginRunning = false;
await UniTask.SwitchToMainThread();
Provider.onEnemyConnected -= OnEnemyConnected;
Provider.onEnemyDisconnected -= OnEnemyDisconnected;
PlayerLife.onPlayerDied -= OnPlayerDied;
DamageTool.damageAnimalRequested -= DamageAnimalRequested;
DamageTool.damageZombieRequested -= OnDamageZombieRequested;
m_Logger.LogInformation($"Successfully unloaded {GetType().Name} by Johnanater, version {Version}");
}
private async Task StartPlayTimer(SteamPlayer steamPlayer)
{
while (_pluginRunning && _payPlayers.Contains(steamPlayer.playerID.steamID))
{
await Task.Delay(Config.TimeMinutes * 1000 * 60);
if (!_pluginRunning || !_payPlayers.Contains(steamPlayer.playerID.steamID))
return;
m_Utils.RewardPlayer(steamPlayer.player, RewardType.PlayTime);
}
}
private void OnEnemyConnected(SteamPlayer steamPlayer)
{
var playerId = steamPlayer.playerID;
_payPlayers.Add(playerId.steamID);
AsyncHelper.Schedule($"IceyStimmy StartPlayTimer for {playerId.playerName} ({playerId.steamID.ToString()})", () => StartPlayTimer(steamPlayer));
}
private void OnEnemyDisconnected(SteamPlayer steamPlayer)
{
_payPlayers.RemoveAll(s => s.Equals(steamPlayer.playerID.steamID));
}
private void OnPlayerDied(PlayerLife playerLife, EDeathCause cause, ELimb limb, CSteamID instigator)
{
if (instigator == CSteamID.Nil)
return;
var killer = PlayerTool.getPlayer(instigator);
if (killer == null)
return;
// if the victim is the killer, no reward
if (!killer.channel.owner.playerID.steamID.ToString().Equals(playerLife.player.channel.owner.playerID.steamID.ToString()))
m_Utils.RewardPlayer(killer, RewardType.PlayerKill);
if (Config.PunishOnDeath)
m_Utils.PunishPlayer(playerLife.player);
}
private void DamageAnimalRequested(ref DamageAnimalParameters parameters, ref bool shouldAllow)
{
try
{
if (parameters.instigator is not Player killer)
return;
var animal = parameters.animal;
var health = ushort.Parse(ReflectionUtils.GetInstanceField(animal, "health").ToString());
var newHealth = health - parameters.damage * parameters.times;
if (newHealth >= 0 )
return;
m_Utils.RewardPlayer(killer, RewardType.AnimalKill);
}
catch (Exception ex)
{
m_Logger.LogError(ex, "Error on DamageAnimalRequested!");
}
}
private void OnDamageZombieRequested(ref DamageZombieParameters parameters, ref bool shouldAllow)
{
try
{
if (parameters.instigator is not Player player)
return;
var zombie = parameters.zombie;
var health = float.Parse(ReflectionUtils.GetInstanceField(zombie, "health").ToString());
var newHealth = health - parameters.damage * parameters.times;
if (newHealth >= 0 )
return;
m_Utils.RewardPlayer(player, RewardType.ZombieKill);
}
catch (Exception ex)
{
m_Logger.LogError(ex, "Error on OnDamageZombieRequested!");
}
}
}
}