From 8f5d63722fe241c7b00fc77d0e829a3658575d33 Mon Sep 17 00:00:00 2001 From: fullwall Date: Sat, 30 Apr 2022 02:39:00 +0800 Subject: [PATCH] Add duration parsing code to CommandContext --- .../api/command/CommandContext.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/main/java/net/citizensnpcs/api/command/CommandContext.java b/src/main/java/net/citizensnpcs/api/command/CommandContext.java index f8ca3c1a..2814fa1e 100644 --- a/src/main/java/net/citizensnpcs/api/command/CommandContext.java +++ b/src/main/java/net/citizensnpcs/api/command/CommandContext.java @@ -176,6 +176,19 @@ public Set 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]); } @@ -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 getValueFlags() { return valueFlags; } @@ -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);