Skip to content

Commit 250f255

Browse files
committed
Add duration-based constructor to SkullCacheHandler for flexible delay
1 parent 25f425a commit 250f255

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

SimpleAPI/src/main/java/com/bencodez/simpleapi/skull/SkullCacheHandler.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import org.bukkit.Material;
1717
import org.bukkit.inventory.ItemStack;
1818

19+
import com.bencodez.simpleapi.time.ParsedDuration;
20+
1921
import lombok.Getter;
2022
import lombok.Setter;
2123

@@ -51,15 +53,33 @@ public abstract class SkullCacheHandler {
5153

5254
private final AtomicInteger rateLimitHitCount = new AtomicInteger(0);
5355

54-
public SkullCacheHandler() {
55-
this.currentDelayMs = clamp(skullDelayTime, minDelayMs, maxDelayMs);
56-
}
57-
56+
/**
57+
* Legacy constructor (milliseconds).
58+
*/
5859
public SkullCacheHandler(int skullDelayTime) {
5960
this.skullDelayTime = skullDelayTime;
6061
this.currentDelayMs = clamp(skullDelayTime, minDelayMs, maxDelayMs);
6162
}
6263

64+
/**
65+
* Duration-based constructor (ParsedDuration).
66+
*
67+
* @param skullDelayTime duration string (e.g. 4s, 4000ms, 1m)
68+
*/
69+
public SkullCacheHandler(String skullDelayTime) {
70+
this.skullDelayTime = clamp(parseMs(skullDelayTime, 4000), minDelayMs, maxDelayMs);
71+
this.currentDelayMs = this.skullDelayTime;
72+
}
73+
74+
private static int parseMs(String duration, int fallback) {
75+
try {
76+
long ms = ParsedDuration.parse(duration).getMillis();
77+
return ms > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) ms;
78+
} catch (Exception e) {
79+
return fallback;
80+
}
81+
}
82+
6383
public void addToCache(UUID uuid, String name) {
6484
if (uuid == null || name == null) {
6585
return;

0 commit comments

Comments
 (0)