Adds some new API calls: Time functions

This commit is contained in:
Zontreck 2024-02-16 14:53:39 -07:00
parent 261cc7673f
commit 2603e7510b
6 changed files with 282 additions and 23 deletions

View file

@ -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
}
}

View file

@ -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 ( );

View file

@ -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 <T> An arbitrary type parameter
*/
public static <T> List<T> of(T... values)
{
List<T> 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<String> split(String input, String... delimiters) {
List<String> 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<String> splitWithDelim(String input, String... delimiters) {
List<String> 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(){
}

View file

@ -2,7 +2,6 @@ package dev.zontreck.ariaslib.util;
import java.util.concurrent.atomic.AtomicInteger;
@Deprecated
public class Progress
{
private int maximum;

View file

@ -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<String> 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;
}
}

View file

@ -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;
}
}