55
66import java .time .LocalDate ;
77import java .time .format .DateTimeFormatter ;
8+ import java .time .format .DateTimeFormatterBuilder ;
89import java .time .format .DateTimeParseException ;
9-
10+ import java . time . temporal . ChronoField ;
1011
1112public class MessageManager {
1213 private final ConfigUtils configUtils ;
1314 private final Plugin plugin ;
1415
15- // Messages
16+ // Повідомлення
1617 public Component BIRTHDAY_BOY_PREFIX ;
1718 public Component USER_NO_ENTER_DATA ;
1819 public Component BIRTHDAY_SET_SUCCESS ;
@@ -26,18 +27,15 @@ public class MessageManager {
2627 public Component BIRTHDAY_ONLY_PLAYERS ;
2728 public Component BIRTHDAY_ALREADY_SET ;
2829 public Component BIRTHDAY_LUCKPERMS_MESSAGE ;
29- //LuckPerm
30+ // LuckPerms
3031 public boolean LUCK_PERM_ENABLED ;
3132 public String LUCK_PERM_GROUP ;
3233 public String LUCK_PERM_TIME ;
3334
34-
35- // Date format
35+ // Формат дати
3636 private String dateFormat ;
3737 private DateTimeFormatter dateFormatter ;
3838
39- // Discord
40-
4139 public MessageManager (Plugin plugin ) {
4240 this .plugin = plugin ;
4341 this .configUtils = new ConfigUtils (plugin );
@@ -57,14 +55,13 @@ private void loadMessages() {
5755 BIRTHDAY_UNKNOWN_COMMAND = logComponentLoad ("Messages.birthday-unknown-command" );
5856 BIRTHDAY_ONLY_PLAYERS = logComponentLoad ("Messages.birthday-only-players" );
5957 BIRTHDAY_ALREADY_SET = logComponentLoad ("Messages.birthday-already-set" );
60-
6158 BIRTHDAY_LUCKPERMS_MESSAGE = logComponentLoad ("Messages.birthday-luckperm-message" );
6259
6360 // Зчитування формату дати з конфігурації
6461 dateFormat = configUtils .getString ("Format-Data" , "yyyy-MM-dd" );
6562 updateDateFormatter ();
6663
67- //luckperms
64+ // luckperms
6865 LUCK_PERM_ENABLED = configUtils .getBoolean ("birthday-luckPerm.enable" , false );
6966 LUCK_PERM_GROUP = configUtils .getString ("birthday-luckPerm.group" , "" );
7067 LUCK_PERM_TIME = configUtils .getString ("birthday-luckPerm.time" , "1d" );
@@ -82,7 +79,12 @@ private Component logComponentLoad(String path) {
8279
8380 private void updateDateFormatter () {
8481 try {
85- dateFormatter = DateTimeFormatter .ofPattern (dateFormat );
82+ DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder ().appendPattern (dateFormat );
83+ // Якщо у шаблоні не задано рік, встановлюємо значення за замовчуванням 2000
84+ if (!dateFormat .toLowerCase ().contains ("y" )) {
85+ builder .parseDefaulting (ChronoField .YEAR , 2000 );
86+ }
87+ dateFormatter = builder .toFormatter ();
8688 plugin .getLogger ().info ("Date format updated to: " + dateFormat );
8789 } catch (IllegalArgumentException e ) {
8890 plugin .getLogger ().warning ("Invalid date format in config: " + dateFormat + ". Defaulting to yyyy-MM-dd." );
@@ -97,46 +99,38 @@ public void reloadMessages() {
9799 plugin .getLogger ().info ("Messages reloaded successfully." );
98100 }
99101
100-
101102 public LocalDate parseDate (String date ) {
103+ // Нормалізація дати: видалення зайвих пробілів та заміна роздільників на дефіс
102104 String normalizedDate = normalizeDate (date );
103-
104105 try {
105- // Перевіряємо, чи введена повна дата у форматі yyyy-MM-dd або yyyy.MM.dd
106- if (normalizedDate .matches ("\\ d{4}[-.]\\ d{2}[-.]\\ d{2}" )) {
107- // Якщо формат yyyy.MM.dd, замінюємо "." на "-"
108- normalizedDate = normalizedDate .replace ("." , "-" );
109- DateTimeFormatter fullDateFormatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd" );
110- return LocalDate .parse (normalizedDate , fullDateFormatter );
111- }
112-
113- // Якщо формат MM-dd або MM.dd, додаємо рік 2000
114- if (normalizedDate .matches ("\\ d{2}[-.]\\ d{2}" )) {
115- normalizedDate = "2000-" + normalizedDate .replace ("." , "-" );
116- DateTimeFormatter partialDateFormatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd" );
117- return LocalDate .parse (normalizedDate , partialDateFormatter );
118- }
119-
120- // Якщо формат не підтримується
121- throw new DateTimeParseException ("Unsupported date format" , date , 0 );
106+ return LocalDate .parse (normalizedDate , dateFormatter );
122107 } catch (DateTimeParseException e ) {
123- plugin .getLogger ().warning ("Unable to parse date: " + date + " with formats: yyyy-MM-dd, yyyy.MM.dd, MM-dd, or MM.dd." );
124- throw new IllegalArgumentException ("Дата повинна бути у форматі yyyy-MM-dd, yyyy.MM.dd, MM-dd або MM.dd." );
108+ plugin .getLogger ().warning ("Unable to parse date: " + date + " with format: " + dateFormat );
109+ throw new IllegalArgumentException ("Supported date format is: " + dateFormat );
125110 }
126111 }
127112
128-
113+ /**
114+ * Нормалізація дати:
115+ * - Видаляємо зайві пробіли.
116+ * - Визначаємо очікуваний роздільник за конфігурацією (наприклад, '-' або '.').
117+ * - Замінюємо усі послідовності нецифрових символів на очікуваний роздільник.
118+ */
129119 private String normalizeDate (String date ) {
130- // Видалення зайвих пробілів
131120 date = date .trim ();
132-
133- // Заміна будь-яких роздільників на дефіс
134- return date .replaceAll ("[./,;\\ s-]" , "-" );
121+ char expectedDelimiter = '-' ;
122+ // Шукаємо перший символ, що не є буквою чи цифрою у dateFormat як роздільник
123+ for (int i = 0 ; i < dateFormat .length (); i ++) {
124+ char ch = dateFormat .charAt (i );
125+ if (!Character .isLetterOrDigit (ch )) {
126+ expectedDelimiter = ch ;
127+ break ;
128+ }
129+ }
130+ // Замінюємо будь-які послідовності нецифрових символів на очікуваний роздільник
131+ return date .replaceAll ("\\ D+" , String .valueOf (expectedDelimiter ));
135132 }
136133
137-
138-
139-
140134 public String formatDate (LocalDate date ) {
141135 return date .format (dateFormatter );
142136 }
@@ -145,4 +139,3 @@ public String getDateFormat() {
145139 return dateFormat ;
146140 }
147141}
148-
0 commit comments