Skip to content

Commit

Permalink
Add duration parsing code to CommandContext
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Apr 29, 2022
1 parent 5d55c1b commit 8f5d637
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main/java/net/citizensnpcs/api/command/CommandContext.java
Expand Up @@ -176,6 +176,19 @@ public Set<Character> getFlags() {
return flags;
}

public int getFlagTicks(String ch) throws NumberFormatException {
return parseTicks(valueFlags.get(ch));
}

public int getFlagTicks(String ch, int def) throws NumberFormatException {
final String value = valueFlags.get(ch);
if (value == null) {
return def;
}

return parseTicks(value);
}

public int getInteger(int index) throws NumberFormatException {
return Integer.parseInt(args[index + 1]);
}
Expand Down Expand Up @@ -250,6 +263,10 @@ public String getString(int index, String def) {
return index + 1 < args.length ? args[index + 1] : def;
}

public int getTicks(int index) throws NumberFormatException {
return parseTicks(args[index + 1]);
}

public Map<String, String> getValueFlags() {
return valueFlags;
}
Expand Down Expand Up @@ -283,6 +300,28 @@ public boolean matches(String command) {
return args[0].equalsIgnoreCase(command);
}

public int parseTicks(String dur) {
dur = dur.trim();
char last = Character.toLowerCase(dur.charAt(dur.length() - 1));
if (Character.isDigit(last)) {
return Integer.parseInt(dur);
}
int factor = 1;
if (last == 's') {
factor = 20;
}
if (last == 'm') {
factor = 20 * 60;
}
if (last == 'h') {
factor = 20 * 60 * 60;
}
if (last == 'd') {
factor = 20 * 60 * 60 * 24;
}
return (int) Math.ceil(Double.parseDouble(dur.substring(0, dur.length() - 1)) * factor);
}

public static Location parseLocation(Location currentLocation, String flag) throws CommandException {
boolean denizen = flag.startsWith("l@");
String[] parts = Iterables.toArray(LOCATION_SPLITTER.split(flag.replaceFirst("l@", "")), String.class);
Expand Down

0 comments on commit 8f5d637

Please sign in to comment.