Skip to content

Commit f364302

Browse files
committed
add LuckPerm depend
1 parent 541c928 commit f364302

File tree

7 files changed

+206
-12
lines changed

7 files changed

+206
-12
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ repositories {
2323
maven {
2424
url = 'https://repo.extendedclip.com/content/repositories/placeholderapi/'
2525
}
26+
maven { url 'https://repo.luckperms.net/' }
2627
}
2728

2829
dependencies {
2930
compileOnly "io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT"
3031
implementation 'com.mojang:authlib:1.5.21'
31-
32+
compileOnly 'net.luckperms:api:5.4'
3233
compileOnly 'me.clip:placeholderapi:2.11.6'
3334
// JDBI Core
3435
implementation 'org.jdbi:jdbi3-core:3.38.0'

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.referix.birthDayReload;
22

3+
import net.luckperms.api.LuckPerms;
4+
import net.luckperms.api.LuckPermsProvider;
35
import org.bukkit.Bukkit;
46
import org.bukkit.NamespacedKey;
57
import org.bukkit.plugin.java.JavaPlugin;
@@ -16,6 +18,7 @@
1618
import org.referix.birthDayReload.utils.ItemManagerConfig;
1719
import org.referix.birthDayReload.utils.LoggerUtils;
1820
import org.referix.birthDayReload.utils.MessageManager;
21+
import org.referix.birthDayReload.utils.luckperm.LuckPerm;
1922

2023
import static org.referix.birthDayReload.utils.LoggerUtils.log;
2124
import static org.referix.birthDayReload.utils.LoggerUtils.logWarning;
@@ -29,6 +32,8 @@ public final class BirthDayReload extends JavaPlugin {
2932

3033
private ItemManagerConfig itemConfig;
3134

35+
private LuckPerm luckPermUtils;
36+
3237
private NamespacedKey textureKey;
3338

3439
@Override
@@ -62,10 +67,17 @@ public void onEnable() {
6267
return;
6368
}
6469

70+
// Проверяем, доступен ли LuckPerms API
6571
try {
66-
log("Try to start Discord Bot...");
67-
MessageManager messageManager = new MessageManager(this);
72+
LuckPerms luckPerms = LuckPermsProvider.get();
73+
getLogger().info("LuckPerms detected. Enabling related features.");
74+
this.luckPermUtils = new LuckPerm(luckPerms,messageManager);
75+
} catch (Exception e) {
76+
this.luckPermUtils = null;
77+
}
6878

79+
try {
80+
log("Try to start Discord Bot...");
6981
DiscordSettings discordSettings = new DiscordSettings(messageManager);
7082
discordManager = new DiscordManager(discordSettings);
7183
if (discordSettings.getIsEnabled()) {
@@ -114,7 +126,7 @@ public void onEnable() {
114126
try {
115127
log("Registering listeners...");
116128
getServer().getPluginManager().registerEvents(new InventoryClickHandler(), this);
117-
getServer().getPluginManager().registerEvents(new MainListener(textureKey), this);
129+
getServer().getPluginManager().registerEvents(new MainListener(textureKey,luckPermUtils,messageManager), this);
118130
log("Listeners registered successfully.");
119131
} catch (Exception e) {
120132
log("Error registering listeners: " + e.getMessage());
@@ -133,6 +145,10 @@ public void onDisable() {
133145
}
134146
}
135147

148+
public LuckPerm getLuckPermUtils() {
149+
return this.luckPermUtils;
150+
}
151+
136152
public NamespacedKey getTextureKey() {
137153
return textureKey;
138154
}

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.referix.birthDayReload;
22

33

4-
import net.kyori.adventure.text.Component;
54
import org.bukkit.*;
65
import org.bukkit.block.Block;
76
import org.bukkit.block.Skull;
@@ -21,6 +20,8 @@
2120
import org.referix.birthDayReload.inventory.InventoryManager;
2221
import org.referix.birthDayReload.playerdata.PlayerData;
2322
import org.referix.birthDayReload.playerdata.PlayerManager;
23+
import org.referix.birthDayReload.utils.MessageManager;
24+
import org.referix.birthDayReload.utils.luckperm.LuckPerm;
2425

2526
import java.time.LocalDate;
2627

@@ -29,9 +30,13 @@
2930
public class MainListener implements Listener {
3031

3132
private final NamespacedKey textureKey;
33+
private final LuckPerm luckPerm;
34+
private final MessageManager messageManager;
3235

33-
public MainListener(NamespacedKey textureKey) {
36+
public MainListener(NamespacedKey textureKey, LuckPerm luckPerm, MessageManager messageManager) {
3437
this.textureKey = textureKey;
38+
this.luckPerm = luckPerm;
39+
this.messageManager = messageManager;
3540
}
3641

3742
@EventHandler
@@ -57,11 +62,8 @@ public void onPlayerJoin(PlayerJoinEvent event) {
5762
if (!data.isWished()) {
5863
player.sendMessage("§6§lHappy Birthday, " + player.getName() + "! 🎉");
5964
player.sendMessage("§aMay your day be filled with joy and celebration!");
60-
65+
// Perms Luck
6166
log("Birthday prefix and wish set for: " + player.getName());
62-
} else {
63-
player.sendMessage("§eWelcome back and Happy Birthday once again! 🎂");
64-
manager.savePlayerData(player); // Зберігаємо дані
6567
}
6668
data.setPrefix(BirthDayReload.getInstance().getMessageManager().BIRTHDAY_BOY_PREFIX); // Встановлюємо префікс
6769
} else {
@@ -116,7 +118,11 @@ public void onBlockPlace(BlockPlaceEvent event) {
116118
@EventHandler
117119
public void onBlockBreak(BlockBreakEvent event) {
118120
Block block = event.getBlock();
119-
121+
if (luckPerm != null) {
122+
Player player = event.getPlayer();
123+
luckPerm.applyLuckPermGroup(player);
124+
player.sendMessage(messageManager.BIRTHDAY_LUCKPERMS_MESSAGE);
125+
}
120126
if (block.getType() == Material.PLAYER_HEAD || block.getType() == Material.PLAYER_WALL_HEAD) {
121127
Skull skull = (Skull) block.getState();
122128
PersistentDataContainer data = skull.getPersistentDataContainer();

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.referix.birthDayReload.utils;
22

33
import net.kyori.adventure.text.Component;
4-
import net.kyori.adventure.text.minimessage.MiniMessage;
54
import org.bukkit.plugin.Plugin;
65

76
import java.time.LocalDate;
@@ -25,6 +24,12 @@ public class MessageManager {
2524
public Component BIRTHDAY_UNKNOWN_COMMAND;
2625
public Component BIRTHDAY_ONLY_PLAYERS;
2726
public Component BIRTHDAY_ALREADY_SET;
27+
public Component BIRTHDAY_LUCKPERMS_MESSAGE;
28+
//LuckPerm
29+
public boolean LUCK_PERM_ENABLED;
30+
public String LUCK_PERM_GROUP;
31+
public String LUCK_PERM_TIME;
32+
2833

2934
// Date format
3035
private String dateFormat;
@@ -55,10 +60,17 @@ private void loadMessages() {
5560
BIRTHDAY_ONLY_PLAYERS = logComponentLoad("Messages.birthday-only-players");
5661
BIRTHDAY_ALREADY_SET = logComponentLoad("Messages.birthday-already-set");
5762

63+
BIRTHDAY_LUCKPERMS_MESSAGE = logComponentLoad("Messages.birthday-luckperm-message");
64+
5865
// Зчитування формату дати з конфігурації
5966
dateFormat = configUtils.getString("Format-Data", "yyyy-MM-dd");
6067
updateDateFormatter();
6168

69+
//luckperms
70+
LUCK_PERM_ENABLED = configUtils.getBoolean("birthday-luckPerm.enable", false);
71+
LUCK_PERM_GROUP = configUtils.getString("birthday-luckPerm.group", "");
72+
LUCK_PERM_TIME = configUtils.getString("birthday-luckPerm.time", "1d");
73+
6274
DISCORD_ENABLED = configUtils.getBoolean("Discord.enabled", false);
6375
DISCORD_TOKEN = configUtils.getString("Discord.token", "");
6476
DISCORD_CHANNEL_ID = configUtils.getString("Discord.channel-id", "");
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package org.referix.birthDayReload.utils.luckperm;
2+
3+
import net.luckperms.api.LuckPerms;
4+
import net.luckperms.api.model.user.User;
5+
import net.luckperms.api.node.types.InheritanceNode;
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.entity.Player;
8+
import org.bukkit.plugin.Plugin;
9+
import org.referix.birthDayReload.BirthDayReload;
10+
import org.referix.birthDayReload.utils.MessageManager;
11+
12+
import java.time.Duration;
13+
import java.util.UUID;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
16+
17+
public class LuckPerm {
18+
19+
private final LuckPerms luckPerms;
20+
private final Plugin plugin = BirthDayReload.getInstance();
21+
private final MessageManager messageManager;
22+
23+
public LuckPerm(LuckPerms luckPerms, MessageManager messageManager) {
24+
this.luckPerms = luckPerms;
25+
this.messageManager = messageManager;
26+
}
27+
28+
/**
29+
* Публичный метод: добавляет игрока в группу, указанную в конфиге (LUCK_PERM_GROUP),
30+
* с истечением срока, если (LUCK_PERM_TIME) задан. Поддерживаются d/h/m/s/w/mo/y.
31+
*/
32+
public void applyLuckPermGroup(Player player) {
33+
if (luckPerms == null) {
34+
plugin.getLogger().severe("[LuckPerms] LuckPerms API no connected");
35+
return;
36+
}
37+
if (!messageManager.LUCK_PERM_ENABLED) {
38+
return;
39+
}
40+
41+
String group = messageManager.LUCK_PERM_GROUP;
42+
String time = messageManager.LUCK_PERM_TIME;
43+
44+
if (group == null || group.isEmpty()) {
45+
return;
46+
}
47+
48+
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
49+
addGroupWithConfigTime(player, group, time);
50+
});
51+
}
52+
53+
/**
54+
* Приватный метод, который действительно вносит игрока в группу
55+
* с использованием Duration (если распарсился) или навсегда (если парсинг = null).
56+
*/
57+
private void addGroupWithConfigTime(Player player, String groupName, String timeString) {
58+
User user = getUser(player.getUniqueId());
59+
if (user == null) {
60+
return;
61+
}
62+
63+
try {
64+
Duration duration = parseDuration(timeString);
65+
66+
InheritanceNode node;
67+
if (duration == null || duration.isZero() || duration.isNegative()) {
68+
node = InheritanceNode.builder(groupName).build();
69+
} else {
70+
node = InheritanceNode.builder(groupName)
71+
.expiry(duration)
72+
.build();
73+
}
74+
75+
if (!user.data().add(node).wasSuccessful()) {
76+
return;
77+
}
78+
79+
luckPerms.getUserManager().saveUser(user).join();
80+
} catch (Exception e) {
81+
return;
82+
}
83+
}
84+
85+
/**
86+
* Парсим строку в Duration.
87+
* Поддерживаем форматы: d/h/m/s/w/mo/y (дни, часы, минуты, секунды, недели, месяцы, годы).
88+
* Можно комбинировать: "1d2h30m", "2w1d", "3mo" и т.д.
89+
* Возвращаем null, если не нашли ни одного валидного фрагмента.
90+
*/
91+
private Duration parseDuration(String input) {
92+
if (input == null || input.isEmpty()) {
93+
return null;
94+
}
95+
96+
Pattern pattern = Pattern.compile("(\\d+)(d|h|m|s|w|mo|y)");
97+
Matcher matcher = pattern.matcher(input);
98+
99+
Duration total = Duration.ZERO;
100+
boolean foundAny = false;
101+
102+
while (matcher.find()) {
103+
foundAny = true;
104+
int value = Integer.parseInt(matcher.group(1));
105+
String unit = matcher.group(2);
106+
107+
switch (unit) {
108+
case "d":
109+
total = total.plusDays(value);
110+
break;
111+
case "h":
112+
total = total.plusHours(value);
113+
break;
114+
case "m":
115+
total = total.plusMinutes(value);
116+
break;
117+
case "s":
118+
total = total.plusSeconds(value);
119+
break;
120+
case "w":
121+
total = total.plusDays(value * 7L);
122+
break;
123+
case "mo":
124+
total = total.plusDays(value * 30L);
125+
break;
126+
case "y":
127+
total = total.plusDays(value * 365L);
128+
break;
129+
default:
130+
return null;
131+
}
132+
}
133+
134+
if (!foundAny) {
135+
return null;
136+
}
137+
return total;
138+
}
139+
140+
/**
141+
* Приватный метод: Загружаем LuckPerms-пользователя по UUID
142+
*/
143+
private User getUser(UUID uuid) {
144+
try {
145+
return luckPerms.getUserManager().loadUser(uuid).join();
146+
} catch (Exception e) {
147+
return null;
148+
}
149+
}
150+
}

src/main/resources/config.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Format-Data: "MM-dd"
77

88
currentDate: "Europe/Moscow" # the timezone the plugin will focus on
99

10+
birthday-luckPerm:
11+
enable: false # to use need the setup LuckPerm
12+
group: "birthday-vip" # group luck perm
13+
time: 7d # time (d - day, h - hour, w - week , mo, y)
14+
1015
Discord:
1116
enabled: false
1217
token: ""
@@ -24,4 +29,6 @@ Messages:
2429
birthday-unknown-command: "<red>Unknown subcommand. Use <yellow>/birthday help</yellow> for more information.</red>"
2530
birthday-only-players: "<red>Only players can set their own birthday.</red>"
2631
birthday-already-set: "<red>Your birthday is already set to: <bold>%date%</bold>. It cannot be changed.</red>"
32+
birthday-luckperm-message: "<green>Congratulations! <bold>You have received a donation gift: <gold>VIP group</gold>!</bold></green> <yellow>Enjoy your special perks!</yellow>"
33+
2734

src/main/resources/plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ version: '1.0-SNAPSHOT'
33
main: org.referix.birthDayReload.BirthDayReload
44
api-version: '1.20'
55
depend: [PlaceholderAPI]
6+
softdepend:
7+
- LuckPerms
68
commands:
79
birthday:
810
description: Manage birthdays.

0 commit comments

Comments
 (0)