blob: 09edcf3c91611f6b437da807c8e7dbc4abfcb5c7 (
plain)
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
|
package de.hysky.skyblocker.utils.config;
import de.hysky.skyblocker.utils.Utils;
import dev.isxander.yacl3.api.Option;
import dev.isxander.yacl3.api.utils.Dimension;
import dev.isxander.yacl3.gui.AbstractWidget;
import dev.isxander.yacl3.gui.YACLScreen;
import dev.isxander.yacl3.gui.controllers.string.IStringController;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public record DurationController(Option<Integer> option) implements IStringController<Integer> {
private static final Pattern secondsPattern = Pattern.compile("(^|\\s)(\\d+)s(\\s|$)");
private static final Pattern minutesPattern = Pattern.compile("(^|\\s)(\\d+)m(\\s|$)");
private static final Pattern hoursPattern = Pattern.compile("(^|\\s)(\\d+)h(\\s|$)");
@Override
public String getString() {
return Utils.getDurationText(option.pendingValue()).getString();
}
@Override
public void setFromString(String value) {
Matcher hoursMatcher = hoursPattern.matcher(value);
Matcher minutesMatcher = minutesPattern.matcher(value);
Matcher secondsMatcher = secondsPattern.matcher(value);
int result = 0;
if (hoursMatcher.find()) {
result += Integer.parseInt(hoursMatcher.group(2)) * 3600;
}
if (minutesMatcher.find()) {
result += Integer.parseInt(minutesMatcher.group(2)) * 60;
}
if (secondsMatcher.find()) {
result += Integer.parseInt(secondsMatcher.group(2));
}
option.requestSet(result);
}
@Override
public boolean isInputValid(String s) {
Matcher hoursMatcher = hoursPattern.matcher(s);
Matcher minutesMatcher = minutesPattern.matcher(s);
Matcher secondsMatcher = secondsPattern.matcher(s);
int hoursCount = 0;
while (hoursMatcher.find()) hoursCount++;
int minutesCount = 0;
while (minutesMatcher.find()) minutesCount++;
int secondsCount = 0;
while (secondsMatcher.find()) secondsCount++;
if (hoursCount == 0 && minutesCount == 0 && secondsCount == 0) return false;
if (hoursCount > 1 || minutesCount > 1 || secondsCount > 1) return false;
s = s.replaceAll(hoursPattern.pattern(), "");
s = s.replaceAll(minutesPattern.pattern(), "");
s = s.replaceAll(secondsPattern.pattern(), "");
return s.isBlank();
}
@Override
public AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) {
return new DurationControllerWidget(this, screen, widgetDimension);
}
}
|