generated from srnyx/plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AnnoyingCooldown.java
89 lines (78 loc) · 2.64 KB
/
AnnoyingCooldown.java
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
package xyz.srnyx.annoyingapi;
import org.jetbrains.annotations.NotNull;
import xyz.srnyx.annoyingapi.utility.AnnoyingUtility;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* This class is used to create and manage cooldowns for anything with a {@link UUID}
*/
public class AnnoyingCooldown {
@NotNull private final AnnoyingPlugin plugin;
@NotNull private final UUID uuid;
/**
* The {@link CooldownType} that was used to create this cooldown
*/
@NotNull public final CooldownType type;
/**
* Creates and starts a cooldown of the specified {@link CooldownType} for the specified {@link UUID}
*
* @param plugin the plugin that is creating the cooldown
* @param uuid the UUID of the thing that is being cooled down
* @param type {@link #type}
*/
public AnnoyingCooldown(@NotNull AnnoyingPlugin plugin, @NotNull UUID uuid, @NotNull CooldownType type) {
this.plugin = plugin;
this.uuid = uuid;
this.type = type;
}
/**
* Starts the cooldown
*/
public void start() {
plugin.cooldowns.computeIfAbsent(uuid, k -> new HashMap<>()).put(type, System.currentTimeMillis() + type.getDuration());
}
/**
* Stops the cooldown
*/
public void stop() {
final Map<CooldownType, Long> cooldowns = plugin.cooldowns.get(uuid);
if (cooldowns != null) cooldowns.remove(type);
}
/**
* A {@code long} of the time remaining on the cooldown
*
* @return amount of time left in the cooldown (in milliseconds)
*
* @see AnnoyingUtility#formatMillis(long, String, boolean)
*/
public long getRemaining() {
final Map<CooldownType, Long> map = plugin.cooldowns.get(uuid);
if (map == null) return 0;
final Long time = map.get(type);
if (time == null) return 0;
return time - System.currentTimeMillis();
}
/**
* A {@code boolean} of whether the cooldown is active. If the cooldown is not active, it will be {@link #stop() stopped}
*
* @return whether the cooldown is active
*/
public boolean isOnCooldown() {
final boolean onCooldown = getRemaining() > 0;
if (!onCooldown) stop();
return onCooldown;
}
/**
* Implement this interface to create your own cooldown types (enums are recommended)
*/
public interface CooldownType {
/**
* Returns the cooldowns duration in milliseconds
*
* @return the duration of the cooldown in milliseconds
*/
@SuppressWarnings("SameReturnValue")
long getDuration();
}
}