Skip to content

Commit 4e8342b

Browse files
authored
Merge pull request #4 from Lotus-Craft/dev
bug: fix data formated and fix discord message
2 parents 5845d3f + 16eb156 commit 4e8342b

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ repositories {
2929
dependencies {
3030
compileOnly "io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT"
3131
implementation 'org.bstats:bstats-bukkit:3.0.0'
32-
implementation 'com.mojang:authlib:1.5.21'
3332
compileOnly 'net.luckperms:api:5.4'
3433
compileOnly 'me.clip:placeholderapi:2.11.6'
3534
// JDBI Core

src/main/java/org/referix/birthDayReload/BirthDayReload.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public final class BirthDayReload extends JavaPlugin {
4545
private NamespacedKey textureKey;
4646
private DiscordHttp discordHttp;
4747

48+
private DiscordConfig discordSettings;
49+
4850
private File configFile;
4951

5052
@Override
@@ -98,7 +100,7 @@ public void onEnable() {
98100

99101
try {
100102
log("Try to start Discord Bot...");
101-
DiscordConfig discordSettings = new DiscordConfig(getConfig());
103+
discordSettings = new DiscordConfig(getConfig());
102104
if (discordSettings.isEnabled()) {
103105
this.discordHttp = new DiscordHttp(discordSettings);
104106
// discordManager.getMessageService().sendMessage("Плагин BirthDayReload успешно запущен!");
@@ -134,7 +136,7 @@ public void onEnable() {
134136

135137
// Регистрация PlaceholderAPI
136138
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
137-
new BirthdayPlaceholder().register();
139+
new BirthdayPlaceholder(messageManager).register();
138140
log("BirthdayPlaceholder successfully registered with PlaceholderAPI.");
139141
} else {
140142
logWarning("PlaceholderAPI not found. BirthdayPlaceholder not registered.");
@@ -158,7 +160,9 @@ public void onEnable() {
158160

159161
@Override
160162
public void onDisable() {
161-
discordHttp.close();
163+
if (discordSettings.isEnabled()){
164+
discordHttp.close();
165+
}
162166
}
163167

164168

src/main/java/org/referix/birthDayReload/papi/BirthdayPlaceholder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
import org.jetbrains.annotations.Nullable;
1010
import org.referix.birthDayReload.playerdata.PlayerData;
1111
import org.referix.birthDayReload.playerdata.PlayerManager;
12+
import org.referix.birthDayReload.utils.configmannagers.MessageManager;
1213

1314
import java.time.LocalDate;
1415
import java.time.format.DateTimeFormatter;
1516

1617
public class BirthdayPlaceholder extends PlaceholderExpansion {
1718

18-
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
19+
private final DateTimeFormatter DATE_FORMAT;
20+
21+
public BirthdayPlaceholder(MessageManager messageManager) {
22+
DATE_FORMAT = DateTimeFormatter.ofPattern(messageManager.getDateFormat());
23+
}
1924

2025
@Override
2126
public @NotNull String getIdentifier() {

src/main/java/org/referix/birthDayReload/utils/configmannagers/MessageManager.java

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
import java.time.LocalDate;
77
import java.time.format.DateTimeFormatter;
8+
import java.time.format.DateTimeFormatterBuilder;
89
import java.time.format.DateTimeParseException;
9-
10+
import java.time.temporal.ChronoField;
1011

1112
public 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

Comments
 (0)