-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathDynamicCooldownManager.java
More file actions
274 lines (234 loc) · 8.96 KB
/
DynamicCooldownManager.java
File metadata and controls
274 lines (234 loc) · 8.96 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package fr.openmc.api.cooldown;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.DatabaseTable;
import com.j256.ormlite.table.TableUtils;
import fr.openmc.core.OMCPlugin;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* Main class for managing cooldowns
*/
public class DynamicCooldownManager {
/**
* Represents a single cooldown with duration and last use time
*/
@DatabaseTable(tableName = "cooldowns")
public static class Cooldown {
@DatabaseField(generatedId = true)
private int id;
@DatabaseField(uniqueCombo = true, canBeNull = false)
private UUID uniqueId;
@DatabaseField(uniqueCombo = true, canBeNull = false)
private String group;
@DatabaseField(canBeNull = false)
private long duration;
@DatabaseField(canBeNull = false)
private long lastUse;
private BukkitTask scheduledTask;
Cooldown() {
// required for ORMLite
}
/**
* @param duration Cooldown duration in ms
*/
public Cooldown(UUID cooldownUUID, String group, long duration, long lastUse) {
this.duration = duration;
this.lastUse = lastUse;
this.uniqueId = cooldownUUID;
this.group = group;
Bukkit.getScheduler().runTask(OMCPlugin.getInstance(), () ->
Bukkit.getPluginManager().callEvent(new CooldownStartEvent(this.uniqueId, this.group))
);
long delayTicks = getRemaining() / 50; //ticks
this.scheduledTask = Bukkit.getScheduler().runTaskLater(OMCPlugin.getInstance(), () -> {
Bukkit.getScheduler().runTask(OMCPlugin.getInstance(), () ->
Bukkit.getPluginManager().callEvent(new CooldownEndEvent(this.uniqueId, this.group))
);
DynamicCooldownManager.clear(this.uniqueId, this.group, false);
}, delayTicks);
}
public void cancelTask() {
if (scheduledTask != null) scheduledTask.cancel();
}
/**
* @return true if cooldown has expired
*/
public boolean isReady() {
return System.currentTimeMillis() - lastUse > duration;
}
/**
* @return remaining time in milliseconds
*/
public long getRemaining() {
return Math.max(0, duration - (System.currentTimeMillis() - lastUse));
}
}
public static void init() {
loadCooldowns();
}
// Map structure: UUID -> (Group -> Cooldown)
private static final Map<UUID, Map<String, Cooldown>> cooldowns = new HashMap<>();
private static Dao<Cooldown, String> cooldownDao;
public static void initDB(ConnectionSource connectionSource) throws SQLException {
TableUtils.createTableIfNotExists(connectionSource, Cooldown.class);
cooldownDao = DaoManager.createDao(connectionSource, Cooldown.class);
}
public static void loadCooldowns() {
try {
List<Cooldown> dbCooldowns = cooldownDao.queryForAll();
for (Cooldown cooldown : dbCooldowns) {
if (cooldown.isReady()) {
Bukkit.getPluginManager().callEvent(new CooldownEndEvent(cooldown.uniqueId, cooldown.group));
cooldownDao.delete(cooldown);
continue;
}
cooldowns.computeIfAbsent(cooldown.uniqueId, k -> new HashMap<>())
.put(cooldown.group, new Cooldown(cooldown.uniqueId, cooldown.group, cooldown.duration, cooldown.lastUse));
}
} catch (SQLException e) {
throw new RuntimeException("Erreur lors du chargement des cooldowns depuis la base de données", e);
}
}
public static void saveCooldowns() {
OMCPlugin.getInstance().getSLF4JLogger().info("Saving cooldowns...");
cooldowns.forEach((uuid, groupCooldowns) -> {
groupCooldowns.forEach((group, cooldown) -> {
if (!cooldown.isReady()) {
try {
cooldownDao.createOrUpdate(cooldown);
} catch (SQLException e) {
throw new RuntimeException(e);
}
} else {
try {
cooldownDao.delete(cooldown);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
});
OMCPlugin.getInstance().getSLF4JLogger().info("Cooldowns saved successfully.");
}
/**
* @param uuid Entity UUID to check
* @return Map of cooldowns for the entity, or null if no cooldowns
*/
public static Map<String, Cooldown> getCooldowns(UUID uuid) {
return cooldowns.get(uuid);
}
/**
* @param uuid Entity UUID to check
* @param group Cooldown group
* @return true if an entity can perform action
*/
public static boolean isReady(UUID uuid, String group) {
var userCooldowns = cooldowns.get(uuid);
if (userCooldowns == null)
return true;
Cooldown cooldown = userCooldowns.get(group);
return cooldown == null || cooldown.isReady();
}
/**
* Puts entity on cooldown
*
* @param uuid Entity UUID
* @param group Cooldown group
* @param duration Cooldown duration in ms
*/
public static void use(UUID uuid, String group, long duration) {
cooldowns.computeIfAbsent(uuid, k -> new HashMap<>())
.put(group, new Cooldown(uuid, group, duration, System.currentTimeMillis()));
}
/**
* Get remaining cooldown time
*
* @param uuid Entity UUID
* @param group Cooldown group
* @return remaining time in milliseconds, 0 if no cooldown
*/
public static long getRemaining(UUID uuid, String group) {
var userCooldowns = cooldowns.get(uuid);
if (userCooldowns == null) return 0;
Cooldown cooldown = userCooldowns.get(group);
return cooldown == null ? 0 : cooldown.getRemaining();
}
/**
* Réduit la durée restante d'un cooldown en cours.
*
* @param uuid UUID de l'entité
* @param group Nom du groupe de cooldown
* @param reductionMillis Réduction en millisecondes
*/
public static void reduceCooldown(Player player, UUID uuid, String group, long reductionMillis) {
var userCooldowns = cooldowns.get(uuid);
if (userCooldowns == null) {
return;
}
Cooldown cooldown = userCooldowns.get(group);
if (cooldown == null) {
return;
}
if (cooldown.isReady()) {
return;
}
long remaining = cooldown.getRemaining();
long newRemaining = Math.max(0, remaining - reductionMillis);
cooldown.cancelTask();
if (newRemaining == 0) {
userCooldowns.remove(group);
Bukkit.getPluginManager().callEvent(new CooldownEndEvent(uuid, group));
if (userCooldowns.isEmpty()) cooldowns.remove(uuid);
player.closeInventory();
return;
}
long newLastUse = System.currentTimeMillis() - (cooldown.duration - newRemaining);
Cooldown newCooldown = new Cooldown(uuid, group, cooldown.duration, newLastUse);
userCooldowns.put(group, newCooldown);
}
/**
* Removes all expired cooldowns
*/
public static void cleanup() {
cooldowns.entrySet().removeIf(entry -> {
entry.getValue().entrySet().removeIf(groupEntry -> groupEntry.getValue().isReady());
return entry.getValue().isEmpty();
});
}
/**
* Removes all cooldowns for group
*
* @param group Cooldown group
*/
public static void clear(String group) {
cooldowns.forEach((uuid, userCooldowns) -> {
Cooldown removed = userCooldowns.remove(group);
if (removed != null) removed.cancelTask();
});
cooldowns.entrySet().removeIf(entry -> entry.getValue().isEmpty()); // A test
}
/**
* Removes a specific cooldown group for an entity
*
* @param uuid Entity UUID
* @param group Cooldown group
*/
public static void clear(UUID uuid, String group, boolean callEvent) {
var userCooldowns = cooldowns.get(uuid);
if (userCooldowns != null) {
if (callEvent) Bukkit.getPluginManager().callEvent(new CooldownEndEvent(uuid, group));
Cooldown removed = userCooldowns.remove(group);
if (removed != null) removed.cancelTask();
if (userCooldowns.isEmpty()) cooldowns.remove(uuid);
}
}
}