From 2603e7510b265d9ee812c4cc3689558b01daeae3 Mon Sep 17 00:00:00 2001 From: Zontreck Date: Fri, 16 Feb 2024 14:53:39 -0700 Subject: [PATCH] Adds some new API calls: Time functions --- .../ariaslib/terminal/ConsolePrompt.java | 20 --- .../zontreck/ariaslib/terminal/Terminal.java | 3 +- .../dev/zontreck/ariaslib/util/Lists.java | 71 ++++++++++ .../dev/zontreck/ariaslib/util/Progress.java | 1 - .../zontreck/ariaslib/util/TimeNotation.java | 134 ++++++++++++++++++ .../dev/zontreck/ariaslib/util/TimeUtil.java | 76 ++++++++++ 6 files changed, 282 insertions(+), 23 deletions(-) delete mode 100644 src/main/java/dev/zontreck/ariaslib/terminal/ConsolePrompt.java create mode 100644 src/main/java/dev/zontreck/ariaslib/util/TimeNotation.java create mode 100644 src/main/java/dev/zontreck/ariaslib/util/TimeUtil.java diff --git a/src/main/java/dev/zontreck/ariaslib/terminal/ConsolePrompt.java b/src/main/java/dev/zontreck/ariaslib/terminal/ConsolePrompt.java deleted file mode 100644 index 9a86027..0000000 --- a/src/main/java/dev/zontreck/ariaslib/terminal/ConsolePrompt.java +++ /dev/null @@ -1,20 +0,0 @@ -package dev.zontreck.ariaslib.terminal; - -import java.io.Console; - -@Deprecated -public class ConsolePrompt extends Task { - public static final Console console = System.console(); - - public ConsolePrompt() { - super("ConsolePrompt", true); - } - - - @Override - public void run() { - // Print a prompt - - } - -} diff --git a/src/main/java/dev/zontreck/ariaslib/terminal/Terminal.java b/src/main/java/dev/zontreck/ariaslib/terminal/Terminal.java index f0bbe65..eed0500 100644 --- a/src/main/java/dev/zontreck/ariaslib/terminal/Terminal.java +++ b/src/main/java/dev/zontreck/ariaslib/terminal/Terminal.java @@ -1,6 +1,5 @@ package dev.zontreck.ariaslib.terminal; -import dev.zontreck.ariaslib.util.DelayedExecutorService; import dev.zontreck.ariaslib.util.EnvironmentUtils; import java.util.concurrent.atomic.AtomicBoolean; @@ -20,7 +19,7 @@ public class Terminal { if ( EnvironmentUtils.isRunningInsideDocker ( ) ) return 0; running.set ( true ); - DelayedExecutorService.getInstance ( ).schedule ( new ConsolePrompt ( ) , 1 ); + //DelayedExecutorService.getInstance ( ).schedule ( new ConsolePrompt ( ) , 1 ); return ID.getAndIncrement ( ); diff --git a/src/main/java/dev/zontreck/ariaslib/util/Lists.java b/src/main/java/dev/zontreck/ariaslib/util/Lists.java index 23f9239..e243545 100644 --- a/src/main/java/dev/zontreck/ariaslib/util/Lists.java +++ b/src/main/java/dev/zontreck/ariaslib/util/Lists.java @@ -6,6 +6,12 @@ import java.util.List; public class Lists { + /** + * Programatically constructs a list + * @param values The list of values + * @return The new list + * @param An arbitrary type parameter + */ public static List of(T... values) { List arr = new ArrayList<>(); @@ -17,6 +23,71 @@ public class Lists return arr; } + + /** + * Splits a string into a list + * @param input The string to split + * @param delimiters The list of delimiters + * @return A non-strided list + */ + public static List split(String input, String... delimiters) { + List result = new ArrayList<>(); + StringBuilder regex = new StringBuilder("("); + + // Constructing the regular expression pattern with provided delimiters + for (String delimiter : delimiters) { + regex.append(delimiter).append("|"); + } + regex.deleteCharAt(regex.length() - 1); // Remove the extra '|' character + regex.append(")"); + + String[] tokens = input.split(regex.toString()); + + // Add non-empty tokens to the result list + for (String token : tokens) { + if (!token.isEmpty()) { + result.add(token); + } + } + + return result; + } + + /** + * Split a string, and keep the delimiters + * @param input The string to be parsed and split + * @param delimiters A list of delimiters + * @return A strided list containing the parsed options, and the delimiters + */ + public static List splitWithDelim(String input, String... delimiters) { + List result = new ArrayList<>(); + StringBuilder regex = new StringBuilder("("); + + // Constructing the regular expression pattern with provided delimiters + for (String delimiter : delimiters) { + regex.append(delimiter).append("|"); + } + regex.deleteCharAt(regex.length() - 1); // Remove the extra '|' character + regex.append(")"); + + // Splitting the input string using the regex pattern + String[] tokens = input.split(regex.toString()); + + // Adding tokens and delimiters to the result list in a strided manner + for (int i = 0; i < tokens.length; i++) { + if (!tokens[i].isEmpty()) { + result.add(tokens[i]); + } + // Adding delimiter if it exists and it's not the last token + if (i < tokens.length - 1) { + result.add(input.substring(input.indexOf(tokens[i]) + tokens[i].length(), input.indexOf(tokens[i + 1]))); + } + } + + return result; + } + + private Lists(){ } diff --git a/src/main/java/dev/zontreck/ariaslib/util/Progress.java b/src/main/java/dev/zontreck/ariaslib/util/Progress.java index c2fc89e..a0d6670 100644 --- a/src/main/java/dev/zontreck/ariaslib/util/Progress.java +++ b/src/main/java/dev/zontreck/ariaslib/util/Progress.java @@ -2,7 +2,6 @@ package dev.zontreck.ariaslib.util; import java.util.concurrent.atomic.AtomicInteger; -@Deprecated public class Progress { private int maximum; diff --git a/src/main/java/dev/zontreck/ariaslib/util/TimeNotation.java b/src/main/java/dev/zontreck/ariaslib/util/TimeNotation.java new file mode 100644 index 0000000..c2eefd7 --- /dev/null +++ b/src/main/java/dev/zontreck/ariaslib/util/TimeNotation.java @@ -0,0 +1,134 @@ +package dev.zontreck.ariaslib.util; + +import java.util.List; + +/** + * Contains useful structures and functions for dealing with, and manipulation of, time. + */ +public class TimeNotation +{ + public int Years; + public int Months; + public int Weeks; + public int Days; + public int Hours; + public int Minutes; + public int Seconds; + + public TimeNotation(int years, int months, int weeks, int days, int hours, int minutes, int seconds) + { + Years=years; + Months=months; + Weeks=weeks; + Days=days; + Hours=hours; + Minutes=minutes; + Seconds = seconds; + } + + private TimeNotation(){} + + + @Override + public String toString() { + return + Pluralize(Years, "year") + ", " + + Pluralize(Months, "month") + ", " + + Pluralize(Weeks, "week") + ", " + + Pluralize(Days, "day") + ", " + + Pluralize(Hours, "hour") + ", " + + Pluralize(Minutes, "minute") + ", " + + Pluralize(Seconds, "second"); + } + + /** + * Create a plural version for a number + * @param num The number to prefix + * @param str The singular form of the string + * @return Combined string, num + str in plural form if necessary + */ + private String Pluralize(int num, String str) + { + return num + " " + (num > 1 ? str+"s" : str); + } + + /** + * Encodes time notation! + * @return A program readable string that can be decoded back to a time notation + */ + public String toNotation() + { + return Years + "Y" + Months + "M" + Weeks + "W" + Days + "d" + Hours + "h" + Minutes + "m" + Seconds + "s"; + } + + /** + * Parses a time notation string + * @param notation Serialized time notation + * @return The deserialized time notation object + */ + public static TimeNotation fromNotation(String notation) + { + TimeNotation notationX = new TimeNotation(); + String[] delims = new String[]{"Y", "M", "W", "d", "h", "m", "s"}; + List opts = Lists.split(notation, delims); + + + int index = 0; + for(String dlim : delims) + { + if(notation.contains(dlim)) + { + switch (dlim) + { + case "Y": + { + notationX.Years = Integer.parseInt(opts.get(index)); + + break; + } + case "M": + { + notationX.Months = Integer.parseInt(opts.get(index)); + + break; + } + case "W": + { + notationX.Weeks = Integer.parseInt(opts.get(index)); + + break; + } + case "d": + { + notationX.Days = Integer.parseInt(opts.get(index)); + + break; + } + case "h": + { + notationX.Hours = Integer.parseInt(opts.get(index)); + + break; + } + case "m": + { + notationX.Minutes = Integer.parseInt(opts.get(index)); + + break; + } + case "s": + { + notationX.Seconds = Integer.parseInt(opts.get(index)); + + break; + } + } + + index++; + } + } + + return notationX; + + } +} diff --git a/src/main/java/dev/zontreck/ariaslib/util/TimeUtil.java b/src/main/java/dev/zontreck/ariaslib/util/TimeUtil.java new file mode 100644 index 0000000..33991c8 --- /dev/null +++ b/src/main/java/dev/zontreck/ariaslib/util/TimeUtil.java @@ -0,0 +1,76 @@ +package dev.zontreck.ariaslib.util; + +/** + * This class is a helper with some minecraft specific functions + */ +public class TimeUtil +{ + /** + * Converts seconds to ticks. (seconds*ticks) + * @param seconds Number of seconds + * @param ticks Number of ticks in a single second + * @return Number of ticks + */ + public static int secondsToTicks(int seconds, int ticks) + { + return seconds*ticks; + } + + /** + * Converts the number of ticks to seconds + * @param ticks The number of ticks to convert + * @param ticksInASecond The number of ticks in a single second + * @return Number of seconds + */ + public static int ticksToSeconds(int ticks, int ticksInASecond) + { + return ticks / ticksInASecond; + } + + /** + * Encodes a LibAC Time Notation + * @param seconds Number of seconds to convert + * @return Time Notation + */ + public static TimeNotation secondsToTimeNotation(int seconds) + { + int years = seconds / YEAR; + if(years > 0) seconds -= YEAR * years; + + int month = seconds / MONTH; + if(month > 0) seconds -= MONTH * month; + + int week = seconds / WEEK; + if(week > 0) seconds -= WEEK * week; + + int day = seconds / DAY; + if(day > 0) seconds -= DAY * day; + + int hour = seconds / HOUR; + if(hour > 0) seconds -= HOUR * hour; + + int minute = seconds / MINUTE; + if(minute > 0) seconds -= MINUTE * minute; + + return new TimeNotation(years, month, week, day, hour, minute, seconds); + } + + + public static final int SECOND; + public static final int MINUTE; + public static final int HOUR; + public static final int DAY; + public static final int WEEK; + public static final int MONTH; + public static final int YEAR; + + static { + SECOND = 1; + MINUTE = SECOND * 60; + HOUR = MINUTE * 60; + DAY = HOUR*24; + WEEK = DAY*7; + MONTH = WEEK*4; + YEAR = DAY*365; + } +}