-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompostStacks.cs
177 lines (156 loc) · 6.45 KB
/
CompostStacks.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
//████████╗ █████╗ ██████╗███╗ ███╗ █████╗ ███╗ ██╗
//╚══██╔══╝██╔══██╗██╔════╝████╗ ████║██╔══██╗████╗ ██║
// ██║ ███████║██║ ██╔████╔██║███████║██╔██╗ ██║
// ██║ ██╔══██║██║ ██║╚██╔╝██║██╔══██║██║╚██╗██║
// ██║ ██║ ██║╚██████╗██║ ╚═╝ ██║██║ ██║██║ ╚████║
// ╚═╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Oxide.Plugins;
using Oxide.Core.Libraries.Covalence;
using Oxide.Core;
using System.IO;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security;
using Newtonsoft.Json;
using System;
namespace Oxide.Plugins
{
[Info("Compost Stacks", "Tacman", "2.1.1")]
[Description("Toggle the CompostEntireStack boolean on load and for new Composter entities, which will compost entire stacks of all compostable items.")]
public class CompostStacks : RustPlugin
{
private bool CompostEntireStack = true;
private const string permissionName = "compoststacks.use";
private Dictionary<ulong, bool> composterData = new Dictionary<ulong, bool>();
private const string dataFileName = "CompostStacksData";
private void OnServerInitialized()
{
permission.RegisterPermission(permissionName, this);
UpdateComposters();
}
private void OnEntitySpawned(BaseNetworkable entity)
{
if (entity is Composter composter)
{
ulong ownerID = composter.OwnerID;
IPlayer ownerPlayer = covalence.Players.FindPlayerById(ownerID.ToString());
if (ownerPlayer != null)
{
bool hasPermission = HasPermission(ownerPlayer);
composter.CompostEntireStack = hasPermission ? CompostEntireStack : false;
composterData[ownerID] = composter.CompostEntireStack;
}
else
{
if (config.debug)
{
Puts($"Owner player not found for OwnerID: {ownerID}");
}
}
}
}
private void UpdateComposters()
{
if (composterData != null && composterData.Count > 0)
{
foreach (var entry in composterData)
{
IPlayer ownerPlayer = covalence.Players.FindPlayerById(entry.Key.ToString());
if (ownerPlayer != null)
{
foreach (Composter composter in BaseNetworkable.serverEntities.Where(x => x is Composter && ((Composter)x).OwnerID == entry.Key))
{
composter.CompostEntireStack = entry.Value;
}
}
}
}
else
{
foreach (Composter composter in BaseNetworkable.serverEntities.Where(x => x is Composter))
{
ulong ownerID = composter.OwnerID;
IPlayer ownerPlayer = covalence.Players.FindPlayerById(ownerID.ToString());
if (ownerPlayer != null)
{
bool hasPermission = HasPermission(ownerPlayer);
composter.CompostEntireStack = hasPermission ? CompostEntireStack : false;
composterData[ownerID] = composter.CompostEntireStack;
}
}
}
}
private void OnUserPermissionGranted(string id, string permission)
{
if (permission == permissionName)
{
Puts($"Permission {permissionName} granted to user {id}");
UpdateComposters();
}
}
private void OnUserPermissionRevoked(string id, string permission)
{
if (permission == permissionName)
{
Puts($"Permission {permissionName} revoked for user {id}");
UpdateComposters();
}
}
private void OnGroupPermissionGranted(string id, string permission)
{
if (permission == permissionName)
{
Puts($"Permission {permissionName} granted to group {id}");
UpdateComposters();
}
}
private void OnGroupPermissionRevoked(string id, string permission)
{
if (permission == permissionName)
{
Puts($"Permission {permissionName} revoked for group {id}");
UpdateComposters();
}
}
private bool HasPermission(IPlayer player)
{
return player.HasPermission(permissionName);
}
#region Config
static Configuration config;
public class Configuration
{
[JsonProperty(PropertyName = "Debug")]
public bool debug;
public static Configuration DefaultConfig()
{
return new Configuration
{
debug = false
};
}
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
config = Config.ReadObject<Configuration>();
if (config == null) LoadDefaultConfig();
SaveConfig();
}
catch (Exception ex)
{
Debug.LogException(ex);
PrintWarning("Creating new configuration file.");
LoadDefaultConfig();
}
}
protected override void LoadDefaultConfig() => config = Configuration.DefaultConfig();
protected override void SaveConfig() => Config.WriteObject(config);
#endregion
}
}