2011-08-08 15:00:04 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
2011-08-08 15:00:04 +00:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.*;
|
|
|
|
|
2011-08-08 15:46:12 +00:00
|
|
|
|
2011-08-08 15:00:04 +00:00
|
|
|
/**
|
|
|
|
* This utility class is used for converting between the ingame
|
|
|
|
* time in ticks to ingame time as a friendly string.
|
|
|
|
* Note that the time is INGAME.
|
|
|
|
*
|
|
|
|
* http://www.minecraftwiki.net/wiki/Day/night_cycle
|
|
|
|
*
|
|
|
|
* @author Olof Larsson
|
|
|
|
*/
|
2011-08-08 15:49:32 +00:00
|
|
|
public final class DescParseTickFormat
|
2011-08-08 15:46:12 +00:00
|
|
|
{
|
|
|
|
// ============================================
|
|
|
|
// First some information vars. TODO: Should this be in a config file?
|
|
|
|
// --------------------------------------------
|
|
|
|
public static final Map<String, Integer> nameToTicks = new LinkedHashMap<String, Integer>();
|
2011-08-08 15:00:04 +00:00
|
|
|
public static final Set<String> resetAliases = new HashSet<String>();
|
2011-08-08 15:46:12 +00:00
|
|
|
public static final int ticksAtMidnight = 18000;
|
|
|
|
public static final int ticksPerDay = 24000;
|
|
|
|
public static final int ticksPerHour = 1000;
|
|
|
|
public static final double ticksPerMinute = 1000d / 60d;
|
|
|
|
public static final double ticksPerSecond = 1000d / 60d / 60d;
|
|
|
|
private static final SimpleDateFormat SDFTwentyFour = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
|
|
|
|
private static final SimpleDateFormat SDFTwelve = new SimpleDateFormat("h:mmaa", Locale.ENGLISH);
|
|
|
|
|
|
|
|
static
|
|
|
|
{
|
2011-08-10 04:41:03 +00:00
|
|
|
SDFTwentyFour.setTimeZone(TimeZone.getTimeZone("GMT"));
|
|
|
|
SDFTwelve.setTimeZone(TimeZone.getTimeZone("GMT"));
|
2011-08-08 15:46:12 +00:00
|
|
|
|
2011-08-10 03:54:03 +00:00
|
|
|
nameToTicks.put("sunrise", 23000);
|
|
|
|
nameToTicks.put("dawn", 23000);
|
2011-08-08 15:46:12 +00:00
|
|
|
|
|
|
|
nameToTicks.put("daystart", 0);
|
|
|
|
nameToTicks.put("day", 0);
|
|
|
|
|
2011-08-10 03:54:03 +00:00
|
|
|
nameToTicks.put("morning", 1000);
|
2011-08-08 15:46:12 +00:00
|
|
|
|
|
|
|
nameToTicks.put("midday", 6000);
|
|
|
|
nameToTicks.put("noon", 6000);
|
|
|
|
|
|
|
|
nameToTicks.put("afternoon", 9000);
|
|
|
|
|
|
|
|
nameToTicks.put("sunset", 12000);
|
|
|
|
nameToTicks.put("dusk", 12000);
|
|
|
|
nameToTicks.put("sundown", 12000);
|
|
|
|
nameToTicks.put("nightfall", 12000);
|
|
|
|
|
|
|
|
nameToTicks.put("nightstart", 14000);
|
|
|
|
nameToTicks.put("night", 14000);
|
|
|
|
|
|
|
|
nameToTicks.put("midnight", 18000);
|
|
|
|
|
2011-08-08 15:00:04 +00:00
|
|
|
resetAliases.add("reset");
|
|
|
|
resetAliases.add("normal");
|
|
|
|
resetAliases.add("default");
|
2011-08-08 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 15:49:32 +00:00
|
|
|
private DescParseTickFormat()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-08-08 15:46:12 +00:00
|
|
|
// ============================================
|
|
|
|
// PARSE. From describing String to int
|
|
|
|
// --------------------------------------------
|
|
|
|
public static long parse(String desc) throws NumberFormatException
|
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
// Only look at alphanumeric and lowercase and : for 24:00
|
2011-11-21 01:55:26 +00:00
|
|
|
desc = desc.toLowerCase(Locale.ENGLISH).replaceAll("[^A-Za-z0-9:]", "");
|
2011-08-08 15:46:12 +00:00
|
|
|
|
|
|
|
// Detect ticks format
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return parseTicks(desc);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect 24-hour format
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return parse24(desc);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect 12-hour format
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return parse12(desc);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect aliases
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return parseAlias(desc);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Well we failed to understand...
|
|
|
|
throw new NumberFormatException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long parseTicks(String desc) throws NumberFormatException
|
|
|
|
{
|
|
|
|
if (!desc.matches("^[0-9]+ti?c?k?s?$"))
|
|
|
|
{
|
|
|
|
throw new NumberFormatException();
|
|
|
|
}
|
|
|
|
|
|
|
|
desc = desc.replaceAll("[^0-9]", "");
|
|
|
|
|
|
|
|
return Long.parseLong(desc) % 24000;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long parse24(String desc) throws NumberFormatException
|
|
|
|
{
|
|
|
|
if (!desc.matches("^[0-9]{2}[^0-9]?[0-9]{2}$"))
|
|
|
|
{
|
|
|
|
throw new NumberFormatException();
|
|
|
|
}
|
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
desc = desc.toLowerCase(Locale.ENGLISH).replaceAll("[^0-9]", "");
|
2011-08-08 15:46:12 +00:00
|
|
|
|
|
|
|
if (desc.length() != 4)
|
|
|
|
{
|
|
|
|
throw new NumberFormatException();
|
|
|
|
}
|
|
|
|
|
2011-08-08 15:49:32 +00:00
|
|
|
final int hours = Integer.parseInt(desc.substring(0, 2));
|
|
|
|
final int minutes = Integer.parseInt(desc.substring(2, 4));
|
2011-08-08 15:46:12 +00:00
|
|
|
|
|
|
|
return hoursMinutesToTicks(hours, minutes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long parse12(String desc) throws NumberFormatException
|
|
|
|
{
|
|
|
|
if (!desc.matches("^[0-9]{1,2}([^0-9]?[0-9]{2})?(pm|am)$"))
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
2011-08-08 15:46:12 +00:00
|
|
|
throw new NumberFormatException();
|
|
|
|
}
|
|
|
|
|
|
|
|
int hours = 0;
|
2011-08-08 15:00:04 +00:00
|
|
|
int minutes = 0;
|
2011-08-08 15:46:12 +00:00
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
desc = desc.toLowerCase(Locale.ENGLISH).replaceAll("[^0-9]", "");
|
2011-08-08 15:46:12 +00:00
|
|
|
|
2011-08-08 15:00:04 +00:00
|
|
|
if (desc.length() > 4)
|
|
|
|
{
|
|
|
|
throw new NumberFormatException();
|
|
|
|
}
|
2011-08-08 15:46:12 +00:00
|
|
|
|
2011-08-08 15:00:04 +00:00
|
|
|
if (desc.length() == 4)
|
|
|
|
{
|
2011-08-08 15:46:12 +00:00
|
|
|
hours += Integer.parseInt(desc.substring(0, 2));
|
2011-08-08 15:00:04 +00:00
|
|
|
minutes += Integer.parseInt(desc.substring(2, 4));
|
|
|
|
}
|
|
|
|
else if (desc.length() == 3)
|
|
|
|
{
|
2011-08-08 15:46:12 +00:00
|
|
|
hours += Integer.parseInt(desc.substring(0, 1));
|
2011-08-08 15:00:04 +00:00
|
|
|
minutes += Integer.parseInt(desc.substring(1, 3));
|
|
|
|
}
|
|
|
|
else if (desc.length() == 2)
|
|
|
|
{
|
2011-08-08 15:46:12 +00:00
|
|
|
hours += Integer.parseInt(desc.substring(0, 2));
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
|
|
|
else if (desc.length() == 1)
|
|
|
|
{
|
2011-08-08 15:46:12 +00:00
|
|
|
hours += Integer.parseInt(desc.substring(0, 1));
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new NumberFormatException();
|
|
|
|
}
|
2011-11-21 01:55:26 +00:00
|
|
|
|
2011-08-23 11:20:35 +00:00
|
|
|
if (desc.endsWith("pm") && hours != 12)
|
|
|
|
{
|
|
|
|
hours += 12;
|
|
|
|
}
|
2011-11-21 01:55:26 +00:00
|
|
|
|
2011-08-23 11:20:35 +00:00
|
|
|
if (desc.endsWith("am") && hours == 12)
|
|
|
|
{
|
|
|
|
hours -= 12;
|
|
|
|
}
|
2011-08-08 15:00:04 +00:00
|
|
|
|
2011-08-08 15:46:12 +00:00
|
|
|
return hoursMinutesToTicks(hours, minutes);
|
|
|
|
}
|
|
|
|
|
2011-08-08 15:49:32 +00:00
|
|
|
public static long hoursMinutesToTicks(final int hours, final int minutes)
|
2011-08-08 15:46:12 +00:00
|
|
|
{
|
|
|
|
long ret = ticksAtMidnight;
|
2011-08-10 04:41:03 +00:00
|
|
|
ret += (hours) * ticksPerHour;
|
2011-08-08 15:46:12 +00:00
|
|
|
|
|
|
|
ret += (minutes / 60.0) * ticksPerHour;
|
|
|
|
|
|
|
|
ret %= ticksPerDay;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-08 15:49:32 +00:00
|
|
|
public static long parseAlias(final String desc) throws NumberFormatException
|
2011-08-08 15:46:12 +00:00
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
final Integer ret = nameToTicks.get(desc);
|
2011-08-08 15:46:12 +00:00
|
|
|
if (ret == null)
|
|
|
|
{
|
|
|
|
throw new NumberFormatException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-08 15:49:32 +00:00
|
|
|
public static boolean meansReset(final String desc)
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
|
|
|
return resetAliases.contains(desc);
|
|
|
|
}
|
2011-08-08 15:46:12 +00:00
|
|
|
|
|
|
|
// ============================================
|
|
|
|
// FORMAT. From int to describing String
|
|
|
|
// --------------------------------------------
|
2011-08-08 15:49:32 +00:00
|
|
|
public static String format(final long ticks)
|
2011-08-08 15:46:12 +00:00
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
return _("timeFormat", format24(ticks), format12(ticks), formatTicks(ticks));
|
2011-08-08 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 15:49:32 +00:00
|
|
|
public static String formatTicks(final long ticks)
|
2011-08-08 15:46:12 +00:00
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
return (ticks % ticksPerDay) + "ticks";
|
2011-08-08 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 15:49:32 +00:00
|
|
|
public static String format24(final long ticks)
|
2011-08-08 15:46:12 +00:00
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
synchronized (SDFTwentyFour)
|
|
|
|
{
|
|
|
|
return formatDateFormat(ticks, SDFTwentyFour);
|
|
|
|
}
|
2011-08-08 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 15:49:32 +00:00
|
|
|
public static String format12(final long ticks)
|
2011-08-08 15:46:12 +00:00
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
synchronized (SDFTwelve)
|
|
|
|
{
|
|
|
|
return formatDateFormat(ticks, SDFTwelve);
|
|
|
|
}
|
2011-08-08 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 15:49:32 +00:00
|
|
|
public static String formatDateFormat(final long ticks, final SimpleDateFormat format)
|
2011-08-08 15:46:12 +00:00
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
final Date date = ticksToDate(ticks);
|
2011-08-08 15:46:12 +00:00
|
|
|
return format.format(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Date ticksToDate(long ticks)
|
|
|
|
{
|
|
|
|
// Assume the server time starts at 0. It would start on a day.
|
|
|
|
// But we will simulate that the server started with 0 at midnight.
|
|
|
|
ticks = ticks - ticksAtMidnight + ticksPerDay;
|
|
|
|
|
|
|
|
// How many ingame days have passed since the server start?
|
2011-08-08 15:51:16 +00:00
|
|
|
final long days = ticks / ticksPerDay;
|
2011-08-08 15:46:12 +00:00
|
|
|
ticks = ticks - days * ticksPerDay;
|
|
|
|
|
|
|
|
// How many hours on the last day?
|
2011-08-08 15:49:32 +00:00
|
|
|
final long hours = ticks / ticksPerHour;
|
2011-08-08 15:46:12 +00:00
|
|
|
ticks = ticks - hours * ticksPerHour;
|
|
|
|
|
|
|
|
// How many minutes on the last day?
|
2011-08-08 15:49:32 +00:00
|
|
|
final long minutes = (long)Math.floor(ticks / ticksPerMinute);
|
|
|
|
final double dticks = ticks - minutes * ticksPerMinute;
|
2011-08-08 15:46:12 +00:00
|
|
|
|
|
|
|
// How many seconds on the last day?
|
2011-08-08 15:49:32 +00:00
|
|
|
final long seconds = (long)Math.floor(dticks / ticksPerSecond);
|
2011-08-08 15:46:12 +00:00
|
|
|
|
|
|
|
// Now we create an english GMT calendar (We wan't no daylight savings)
|
2011-08-08 15:49:32 +00:00
|
|
|
final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);
|
2011-08-08 15:46:12 +00:00
|
|
|
cal.setLenient(true);
|
|
|
|
|
|
|
|
// And we set the time to 0! And append the time that passed!
|
|
|
|
cal.set(0, Calendar.JANUARY, 1, 0, 0, 0);
|
|
|
|
cal.add(Calendar.DAY_OF_YEAR, (int)days);
|
|
|
|
cal.add(Calendar.HOUR_OF_DAY, (int)hours);
|
|
|
|
cal.add(Calendar.MINUTE, (int)minutes);
|
|
|
|
cal.add(Calendar.SECOND, (int)seconds + 1); // To solve rounding errors.
|
|
|
|
|
|
|
|
return cal.getTime();
|
|
|
|
}
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|