-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFireworkLimiter.cs
140 lines (128 loc) · 5.69 KB
/
FireworkLimiter.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
using System.Collections.Generic;
using Oxide.Core;
using UnityEngine;
using Rust;
namespace Oxide.Plugins
{
[Info("Firework Limit", "Tacman", "1.0.0")]
class FireworkLimiter : RustPlugin
{
private Dictionary<ulong, int> placedFireworks = new Dictionary<ulong, int>();
private int maxFireworks;
void OnServerInitialized()
{
LoadConfigValues();
DetectExistingFireworks();
Debug.Log("FireworkLimiter: Plugin Initialized.");
}
private void DetectExistingFireworks()
{
// Iterate through all entities in the game world
foreach (var entity in BaseNetworkable.serverEntities)
{
if (entity == null) continue;
// Check if the entity is a firework
if (entity.ShortPrefabName.Contains("volcano") || entity.ShortPrefabName.Contains("mortar") || entity.ShortPrefabName.Contains("roman"))
{
var baseEntity = entity as BaseEntity;
if (baseEntity != null)
{
var ownerID = baseEntity.OwnerID;
if (!placedFireworks.ContainsKey(ownerID))
{
placedFireworks[ownerID] = 1;
}
else
{
placedFireworks[ownerID]++;
}
// Debug.Log($"FireworkLimiter: Detected existing firework with owner ID {ownerID}.");
}
}
}
}
protected override void LoadDefaultConfig()
{
// Set default values only if the config doesn't exist
if (Config.Get("MaxFireworks") == null)
{
Config.Set("MaxFireworks", 5); // Default value as integer
Config.Save();
Debug.Log("FireworkLimiter: Default config loaded and saved.");
}
}
private void LoadConfigValues()
{
// Load config values
maxFireworks = Config.Get<int>("MaxFireworks");
Debug.Log("FireworkLimiter: Config values loaded.");
}
object CanBuild(Planner planner, Construction prefab, Construction.Target target)
{
if (prefab.fullName.Contains("volcano") || prefab.fullName.Contains("mortar") || prefab.fullName.Contains("roman"))
{
BasePlayer player = planner.GetOwnerPlayer();
if (player != null && !player.IsAdmin)
{
var ownerID = player.userID;
int currentFireworks = placedFireworks.ContainsKey(ownerID) ? placedFireworks[ownerID] : 0;
if (currentFireworks >= maxFireworks)
{
player.ChatMessage($"You have reached the maximum limit of fireworks. {currentFireworks}/{maxFireworks}");
return false;
}
else
{
return null; // Allow building since player hasn't reached the limit
}
}
}
return null; // Allow building if not a firework or if the player is an admin
}
void OnEntityBuilt(Planner planner, GameObject gameObject)
{
var entity = gameObject.ToBaseEntity();
string shortPrefabName = entity.ShortPrefabName.ToLower(); // Convert to lowercase for case-insensitive comparison
if (shortPrefabName.Contains("volcano") || shortPrefabName.Contains("mortar") || shortPrefabName.Contains("roman"))
{
BasePlayer player = planner.GetOwnerPlayer();
if (player != null && !player.IsAdmin)
{
var ownerID = player.userID;
int currentFireworks = placedFireworks.ContainsKey(ownerID) ? placedFireworks[ownerID] : 0;
if (currentFireworks >= maxFireworks)
{
player.ChatMessage($"You have reached the maximum limit of fireworks. {currentFireworks}/{maxFireworks}");
}
else
{
if (!placedFireworks.ContainsKey(ownerID))
{
placedFireworks[ownerID] = 1;
}
else
{
placedFireworks[ownerID]++;
}
// Display a message to the player
currentFireworks = placedFireworks[ownerID];
player.ChatMessage($"You have placed {currentFireworks} out of {maxFireworks} fireworks.");
//Debug.Log($"FireworkLimiter: Player {player.userID} placed a firework entity ({shortPrefabName}).");
}
}
}
}
void OnEntityKill(BaseEntity entity)
{
if (entity.ShortPrefabName.Contains("volcano") || entity.ShortPrefabName.Contains("mortar") || entity.ShortPrefabName.Contains("roman"))
{
var ownerID = entity.OwnerID;
if (ownerID != 0 && placedFireworks.ContainsKey(ownerID))
{
placedFireworks[ownerID]--;
// Debug.Log($"FireworkLimiter: Firework destroyed with owner ID {ownerID}.");
}
}
}
}
}