From a5729b4ba0189e3878608b5af44a7eb97a01e70d Mon Sep 17 00:00:00 2001 From: HappyPikachu Date: Thu, 29 Mar 2018 03:25:35 -0400 Subject: [PATCH] NEW date/time Planner section, part 4, #93 --- .../quests/prompts/DateTimePrompt.java | 98 ++++++++---- .../quests/prompts/PlannerPrompt.java | 150 ++++++------------ .../java/me/blackvein/quests/util/CK.java | 2 +- src/main/resources/strings.yml | 4 +- 4 files changed, 125 insertions(+), 129 deletions(-) diff --git a/src/main/java/me/blackvein/quests/prompts/DateTimePrompt.java b/src/main/java/me/blackvein/quests/prompts/DateTimePrompt.java index fdc68afc1..d8c8e6d7c 100644 --- a/src/main/java/me/blackvein/quests/prompts/DateTimePrompt.java +++ b/src/main/java/me/blackvein/quests/prompts/DateTimePrompt.java @@ -7,6 +7,7 @@ import java.util.Locale; import java.util.TimeZone; import me.blackvein.quests.Quests; +import me.blackvein.quests.util.CK; import me.blackvein.quests.util.Lang; import org.bukkit.ChatColor; @@ -19,11 +20,13 @@ public class DateTimePrompt extends FixedSetPrompt { Quests quests; final Prompt oldPrompt; + String source = ""; - public DateTimePrompt(Quests plugin, Prompt old) { - super("0", "1", "2", "3", "4", "5", "6", "7", "8"); + public DateTimePrompt(Quests plugin, Prompt old, String origin) { + super("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); quests = plugin; oldPrompt = old; + source = origin; } @Override @@ -68,7 +71,7 @@ public class DateTimePrompt extends FixedSetPrompt { String[] sep = String.valueOf(hour).replace("-", "").split("\\."); DecimalFormat zoneFormat = new DecimalFormat("00"); dateData += ChatColor.LIGHT_PURPLE + "UTC" + (hour < 0 ? "-":"+") + zoneFormat.format(Integer.valueOf(sep[0])) + ":" + zoneFormat.format(Integer.valueOf(sep[1])) - + ChatColor.RED + " (" + cal.getTimeZone().getDisplayName(loc) + ")"; + + ChatColor.GREEN + " (" + cal.getTimeZone().getDisplayName(loc) + ")"; if (dateData != null) { menu += dateData + "\n"; } @@ -100,7 +103,7 @@ public class DateTimePrompt extends FixedSetPrompt { } else if (input.equalsIgnoreCase("6")) { return new SecondPrompt(); } else if (input.equalsIgnoreCase("7")) { - return new ZonePrompt(); + return new OffsetPrompt(); } else if (input.equalsIgnoreCase("8")) { cc.setSessionData("tempDay", null); cc.setSessionData("tempMonth", null); @@ -119,7 +122,11 @@ public class DateTimePrompt extends FixedSetPrompt { String zone = (String) cc.getSessionData("tempZone"); String date = day + ":" + month + ":" + year + ":" + hour + ":" + minute + ":" + second + ":" + zone; - cc.setSessionData("tempDate", date); + if (source.equals("start")) { + cc.setSessionData(CK.PLN_START_DATE, date); + } else if (source.equals("end")) { + cc.setSessionData(CK.PLN_END_DATE, date); + } } try { return oldPrompt; @@ -147,14 +154,14 @@ public class DateTimePrompt extends FixedSetPrompt { return new DayPrompt(); } else { cc.setSessionData("tempDay", Integer.parseInt(input)); - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } catch (NumberFormatException e) { cc.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput")); return new DayPrompt(); } } else { - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } } @@ -177,14 +184,14 @@ public class DateTimePrompt extends FixedSetPrompt { return new MonthPrompt(); } else { cc.setSessionData("tempMonth", Integer.parseInt(input) - 1); - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } catch (NumberFormatException e) { cc.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput")); return new MonthPrompt(); } } else { - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } } @@ -207,14 +214,14 @@ public class DateTimePrompt extends FixedSetPrompt { return new YearPrompt(); } else { cc.setSessionData("tempYear", Integer.parseInt(input)); - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } catch (NumberFormatException e) { cc.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput")); return new YearPrompt(); } } else { - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } } @@ -237,14 +244,14 @@ public class DateTimePrompt extends FixedSetPrompt { return new HourPrompt(); } else { cc.setSessionData("tempHour", Integer.parseInt(input)); - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } catch (NumberFormatException e) { cc.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput")); return new HourPrompt(); } } else { - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } } @@ -267,14 +274,14 @@ public class DateTimePrompt extends FixedSetPrompt { return new MinutePrompt(); } else { cc.setSessionData("tempMinute", Integer.parseInt(input)); - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } catch (NumberFormatException e) { cc.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput")); return new MinutePrompt(); } } else { - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } } @@ -297,23 +304,23 @@ public class DateTimePrompt extends FixedSetPrompt { return new SecondPrompt(); } else { cc.setSessionData("tempSecond", Integer.parseInt(input)); - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } catch (NumberFormatException e) { cc.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput")); return new SecondPrompt(); } } else { - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } } - private class ZonePrompt extends StringPrompt { + private class OffsetPrompt extends StringPrompt { @Override public String getPromptText(ConversationContext cc) { - return ChatColor.YELLOW + Lang.get("dateCreateEnterZone"); + return ChatColor.YELLOW + Lang.get("dateCreateEnterOffset"); } @Override @@ -324,27 +331,60 @@ public class DateTimePrompt extends FixedSetPrompt { if (amt < -12.0 || amt > 14.0) { cc.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("invalidRange") .replace("", "-12:00").replace("", "14:00")); - return new ZonePrompt(); + return new OffsetPrompt(); } else { String[] t = TimeZone.getAvailableIDs((int) Math.round(amt * 60.0 * 60.0 * 1000.0)); - //TODO - let user choose - for (String output : t) { - System.out.println("zone id= " + output); - } - if (t.length > 0) { + if (t.length > 1) { + return new ZonePrompt(t); + } else if (t.length > 0) { cc.setSessionData("tempZone", t[0]); - } else { + } else { cc.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput")); quests.getLogger().severe("Unable to get time zone for converted offset " + input); } - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); } } catch (NumberFormatException e) { cc.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput")); - return new ZonePrompt(); + return new OffsetPrompt(); } } else { - return new DateTimePrompt(quests, oldPrompt); + return new DateTimePrompt(quests, oldPrompt, source); + } + } + } + + private class ZonePrompt extends StringPrompt { + + String[] zones; + + public ZonePrompt(String[] timezones) { + zones = timezones; + } + + @Override + public String getPromptText(ConversationContext cc) { + String text = ChatColor.LIGHT_PURPLE + Lang.get("timeZoneTitle") + "\n"; + for (String z : zones) { + text += ChatColor.GREEN + z + ", "; + } + text = text.substring(0, text.length() - 2); + return text + "\n" + ChatColor.YELLOW + Lang.get("dateCreateEnterZone"); + } + + @Override + public Prompt acceptInput(ConversationContext cc, String input) { + if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) { + for (String z : zones) { + if (z.toLowerCase().startsWith(input.toLowerCase())) { + cc.setSessionData("tempZone", z); + return new DateTimePrompt(quests, oldPrompt, source); + } + } + cc.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput")); + return new ZonePrompt(zones); + } else { + return new DateTimePrompt(quests, oldPrompt, source); } } } diff --git a/src/main/java/me/blackvein/quests/prompts/PlannerPrompt.java b/src/main/java/me/blackvein/quests/prompts/PlannerPrompt.java index 4dc6425f1..7916287f9 100644 --- a/src/main/java/me/blackvein/quests/prompts/PlannerPrompt.java +++ b/src/main/java/me/blackvein/quests/prompts/PlannerPrompt.java @@ -12,6 +12,12 @@ package me.blackvein.quests.prompts; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Locale; +import java.util.TimeZone; + import me.blackvein.quests.QuestFactory; import me.blackvein.quests.Quests; import me.blackvein.quests.util.CK; @@ -41,24 +47,32 @@ public class PlannerPrompt extends FixedSetPrompt { lang = lang.replaceAll("", ChatColor.BLUE + (String) context.getSessionData(CK.Q_NAME)); text = ChatColor.AQUA + lang + "\n"; if (context.getSessionData(CK.PLN_START_DATE) == null) { - text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnStart") + " " + ChatColor.GRAY + "(" + Lang.get("noneSet") + ")\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnStart") + " " + + ChatColor.GRAY + "(" + Lang.get("noneSet") + ")\n"; } else { - text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnStart") + " (" + getDate((String) context.getSessionData(CK.PLN_START_DATE)) + ")\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "1" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnStart") + "\n"; + text += " - " + getDate((String) context.getSessionData(CK.PLN_START_DATE)) + "\n"; } if (context.getSessionData(CK.PLN_END_DATE) == null) { - text += ChatColor.BLUE + "" + ChatColor.BOLD + "2" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnEnd") + " " + ChatColor.GRAY + "(" + Lang.get("noneSet") + ")\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "2" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnEnd") + " " + + ChatColor.GRAY + "(" + Lang.get("noneSet") + ")\n"; } else { - text += ChatColor.BLUE + "" + ChatColor.BOLD + "2" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnEnd") + " (" + getDate((String) context.getSessionData(CK.PLN_END_DATE)) + ")\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "2" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnEnd") + "\n"; + text += " - " + getDate((String) context.getSessionData(CK.PLN_END_DATE)) + "\n"; } if (context.getSessionData(CK.PLN_REPEAT_CYCLE) == null) { - text += ChatColor.BLUE + "" + ChatColor.BOLD + "3" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnRepeat") + " " + ChatColor.GRAY + "(" + Lang.get("noneSet") + ")\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "3" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnRepeat") + " " + + ChatColor.GRAY + "(" + Lang.get("noneSet") + ")\n"; } else { - text += ChatColor.BLUE + "" + ChatColor.BOLD + "3" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnRepeat") + " (" + Quests.getTime((Long) context.getSessionData(CK.PLN_REPEAT_CYCLE)) + ")\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "3" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnRepeat") + " (" + + Quests.getTime((Long) context.getSessionData(CK.PLN_REPEAT_CYCLE)) + ChatColor.RESET + ChatColor.YELLOW + ")\n"; } if (context.getSessionData(CK.PLN_COOLDOWN) == null) { - text += ChatColor.BLUE + "" + ChatColor.BOLD + "4" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnCooldown") + " " + ChatColor.GRAY + "(" + Lang.get("noneSet") + ")\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "4" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnCooldown") + " " + + ChatColor.GRAY + "(" + Lang.get("noneSet") + ")\n"; } else { - text += ChatColor.BLUE + "" + ChatColor.BOLD + "4" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnCooldown") + " (" + Quests.getTime((Long) context.getSessionData(CK.PLN_COOLDOWN)) + ")\n"; + text += ChatColor.BLUE + "" + ChatColor.BOLD + "4" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("plnCooldown") + " (" + + Quests.getTime((Long) context.getSessionData(CK.PLN_COOLDOWN)) + ChatColor.RESET + ChatColor.YELLOW + ")\n"; } text += ChatColor.GREEN + "" + ChatColor.BOLD + "5" + ChatColor.RESET + ChatColor.YELLOW + " - " + Lang.get("done"); return text; @@ -67,11 +81,9 @@ public class PlannerPrompt extends FixedSetPrompt { @Override protected Prompt acceptValidatedInput(ConversationContext context, String input) { if (input.equalsIgnoreCase("1")) { - //return new StartPrompt(); - return new DateTimePrompt(quests, PlannerPrompt.this); + return new DateTimePrompt(quests, PlannerPrompt.this, "start"); } else if (input.equalsIgnoreCase("2")) { - //return new EndPrompt(); - return new DateTimePrompt(quests, PlannerPrompt.this); + return new DateTimePrompt(quests, PlannerPrompt.this, "end"); } else if (input.equalsIgnoreCase("3")) { return new RepeatPrompt(); } else if (input.equalsIgnoreCase("4")) { @@ -81,75 +93,7 @@ public class PlannerPrompt extends FixedSetPrompt { } return null; } - - /*private class StartPrompt extends StringPrompt { - @Override - public String getPromptText(ConversationContext context) { - return ChatColor.YELLOW + Lang.get("plnStartPrompt"); - } - - @Override - public Prompt acceptInput(ConversationContext context, String input) { - if (input.equalsIgnoreCase(Lang.get("cmdCancel"))) { - return new PlannerPrompt(quests, factory); - } - if (input.equalsIgnoreCase(Lang.get("cmdClear"))) { - context.setSessionData(CK.PLN_START_DATE, null); - } - long delay; - try { - int i = Integer.parseInt(input); - delay = i * 1000; - } catch (NumberFormatException e) { - context.getForWhom().sendRawMessage(ChatColor.ITALIC + "" + ChatColor.RED + input + ChatColor.RESET + ChatColor.RED + " " + Lang.get("stageEditorInvalidNumber")); - return new StartPrompt(); - } - if (delay < -1) { - context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("questEditorPositiveAmount")); - } else if (delay == 0) { - context.setSessionData(CK.PLN_START_DATE, null); - } else if (delay != -1) { - context.setSessionData(CK.PLN_START_DATE, delay); - } - return new PlannerPrompt(quests, factory); - } - } - - private class EndPrompt extends StringPrompt { - - @Override - public String getPromptText(ConversationContext context) { - return ChatColor.YELLOW + Lang.get("plnEndPrompt"); - } - - @Override - public Prompt acceptInput(ConversationContext context, String input) { - if (input.equalsIgnoreCase(Lang.get("cmdCancel"))) { - return new PlannerPrompt(quests, factory); - } - if (input.equalsIgnoreCase(Lang.get("cmdClear"))) { - context.setSessionData(CK.PLN_END_DATE, null); - } - long delay; - try { - int i = Integer.parseInt(input); - delay = i * 1000; - } catch (NumberFormatException e) { - context.getForWhom().sendRawMessage(ChatColor.ITALIC + "" + ChatColor.RED + input + ChatColor.RESET + ChatColor.RED + " " + Lang.get("stageEditorInvalidNumber")); - return new EndPrompt(); - } - if (delay < -1) { - context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("questEditorPositiveAmount")); - } else if (delay == 0) { - context.setSessionData(CK.PLN_END_DATE, null); - } else if (delay != -1) { - context.setSessionData(CK.PLN_END_DATE, delay); - } - return new PlannerPrompt(quests, factory); - } - }*/ - private class RepeatPrompt extends StringPrompt { @Override @@ -170,7 +114,8 @@ public class PlannerPrompt extends FixedSetPrompt { int i = Integer.parseInt(input); delay = i * 1000; } catch (NumberFormatException e) { - context.getForWhom().sendRawMessage(ChatColor.ITALIC + "" + ChatColor.RED + input + ChatColor.RESET + ChatColor.RED + " " + Lang.get("stageEditorInvalidNumber")); + context.getForWhom().sendRawMessage(ChatColor.ITALIC + "" + ChatColor.RED + input + ChatColor.RESET + ChatColor.RED + " " + + Lang.get("stageEditorInvalidNumber")); return new RepeatPrompt(); } if (delay < -1) { @@ -204,7 +149,8 @@ public class PlannerPrompt extends FixedSetPrompt { int i = Integer.parseInt(input); delay = i * 1000; } catch (NumberFormatException e) { - context.getForWhom().sendRawMessage(ChatColor.ITALIC + "" + ChatColor.RED + input + ChatColor.RESET + ChatColor.RED + " " + Lang.get("stageEditorInvalidNumber")); + context.getForWhom().sendRawMessage(ChatColor.ITALIC + "" + ChatColor.RED + input + ChatColor.RESET + ChatColor.RED + " " + + Lang.get("stageEditorInvalidNumber")); return new CooldownPrompt(); } if (delay < -1) { @@ -219,22 +165,30 @@ public class PlannerPrompt extends FixedSetPrompt { } private String getDate(String formattedDate) { + Calendar cal = Calendar.getInstance(); + SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); + SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss"); String[] date = formattedDate.split(":"); - String day = date[0]; - String month = date[1]; - String year = date[2]; - String hour = date[3]; - String minute = date[4]; - String second = date[5]; - String zone = date[6]; - String output = day + "-" + month + "-" + year + " " - + hour + ":" + minute + ":" + second + " UTC"; - if (Integer.parseInt(date[6]) < 0) { - output += "-"; - } else { - output += "+"; - } - output += zone; + int day = Integer.valueOf(date[0]); + int month = Integer.valueOf(date[1]); + int year = Integer.valueOf(date[2]); + int hour = Integer.valueOf(date[3]); + int minute = Integer.valueOf(date[4]); + int second = Integer.valueOf(date[5]); + + cal.set(year, month, day, hour, minute, second); + String output = ChatColor.DARK_AQUA + dateFormat.format(cal.getTime()); + output += ChatColor.AQUA + " " + timeFormat.format(cal.getTime()); + + TimeZone tz = TimeZone.getTimeZone(date[6]); + cal.setTimeZone(tz); + String[] iso = quests.lang.iso.split("-"); + Locale loc = new Locale(iso[0], iso[1]); + Double zhour = (double) (cal.getTimeZone().getRawOffset() / 60 / 60 / 1000); + String[] sep = String.valueOf(zhour).replace("-", "").split("\\."); + DecimalFormat zoneFormat = new DecimalFormat("00"); + output += ChatColor.LIGHT_PURPLE + " UTC" + (zhour < 0 ? "-":"+") + zoneFormat.format(Integer.valueOf(sep[0])) + ":" + + zoneFormat.format(Integer.valueOf(sep[1])) + ChatColor.GREEN + " (" + cal.getTimeZone().getDisplayName(loc) + ")"; return output; } -} +} \ No newline at end of file diff --git a/src/main/java/me/blackvein/quests/util/CK.java b/src/main/java/me/blackvein/quests/util/CK.java index 747b62223..e7ffc42e6 100644 --- a/src/main/java/me/blackvein/quests/util/CK.java +++ b/src/main/java/me/blackvein/quests/util/CK.java @@ -121,7 +121,7 @@ public class CK { public static final String REW_CUSTOM_DATA = "customRewData"; public static final String REW_CUSTOM_DATA_DESCRIPTIONS = "customRewDataDesc"; public static final String REW_CUSTOM_DATA_TEMP = "customRewDataTemp"; - //Planner + // Planner public static final String PLN_START_DATE = "startDatePln"; public static final String PLN_END_DATE = "endDatePln"; public static final String PLN_REPEAT_CYCLE = "repeatCyclePln"; diff --git a/src/main/resources/strings.yml b/src/main/resources/strings.yml index 7ad69b4a4..1c4c0f35a 100644 --- a/src/main/resources/strings.yml +++ b/src/main/resources/strings.yml @@ -580,7 +580,8 @@ dateCreateEnterYear: "Enter a year (max. 9999), " dateCreateEnterHour: "Enter an hour (max. 23), " dateCreateEnterMinute: "Enter a minute (max. 59), " dateCreateEnterSecond: "Enter a second (max. 59), " -dateCreateEnterZone: "Enter a UTC time offset (max. 14), " +dateCreateEnterOffset: "Enter a UTC time offset (max. 14), " +dateCreateEnterZone: "Enter a UTC time zone, " dateCreateNoYearAmount: "You must set a year first!" questTitle: "-- --" questObjectivesTitle: "---()---" @@ -626,6 +627,7 @@ completedQuestsTitle: "- Completed Quests -" topQuestersTitle: "- Top Questers -" createItemTitle: "- Create Item -" dateTimeTitle: "- Date & Time -" +timeZoneTitle: "- Time Zones -" enchantmentsTitle: "- Enchantments -" questGUITitle: "- GUI Item Display -" questRegionTitle: "- Quest Region -"