|
1 | 1 | package com.eternalcode.formatter.mention; |
2 | 2 |
|
3 | | -import net.luckperms.api.LuckPerms; |
4 | | -import net.luckperms.api.LuckPermsProvider; |
5 | | -import net.luckperms.api.model.user.User; |
6 | | -import net.luckperms.api.node.types.MetaNode; |
7 | | -import org.bukkit.Server; |
8 | | - |
| 3 | +import java.util.Optional; |
9 | 4 | import java.util.UUID; |
10 | 5 | import java.util.logging.Logger; |
11 | 6 |
|
12 | 7 | public class MentionPlayerSettings { |
13 | 8 |
|
14 | | - private static final String MENTION_SOUND_META_KEY = "chatformatter-mention-sound"; |
15 | | - |
16 | 9 | private final Logger logger; |
17 | 10 | private final MentionConfig config; |
18 | | - private final Server server; |
19 | | - private LuckPerms luckPerms; |
| 11 | + private final Optional<LuckPermsHook> luckPermsHook; |
20 | 12 |
|
21 | | - public MentionPlayerSettings(Server server, Logger logger, MentionConfig config) { |
22 | | - this.server = server; |
| 13 | + public MentionPlayerSettings(Logger logger, MentionConfig config, Optional<LuckPermsHook> luckPermsHook) { |
23 | 14 | this.logger = logger; |
24 | 15 | this.config = config; |
25 | | - this.initializeLuckPerms(); |
26 | | - } |
27 | | - |
28 | | - private void initializeLuckPerms() { |
29 | | - if (!this.server.getPluginManager().isPluginEnabled("LuckPerms")) { |
30 | | - this.logger.warning("LuckPerms is not installed! Mention sound toggle feature will not work."); |
31 | | - return; |
32 | | - } |
33 | | - |
34 | | - try { |
35 | | - this.luckPerms = LuckPermsProvider.get(); |
36 | | - } catch (IllegalStateException e) { |
37 | | - this.logger.warning("Failed to initialize LuckPerms API: " + e.getMessage()); |
38 | | - } |
| 16 | + this.luckPermsHook = luckPermsHook; |
39 | 17 | } |
40 | 18 |
|
41 | 19 | public boolean isMentionSoundEnabled(UUID uuid) { |
42 | | - if (this.luckPerms == null) { |
43 | | - return this.config.enabled; |
44 | | - } |
45 | | - |
46 | | - User user = this.luckPerms.getUserManager().getUser(uuid); |
47 | | - if (user == null) { |
48 | | - return this.config.enabled; |
49 | | - } |
50 | | - |
51 | | - String metaValue = user.getCachedData().getMetaData().getMetaValue(MENTION_SOUND_META_KEY); |
52 | | - if (metaValue == null) { |
53 | | - return this.config.enabled; |
54 | | - } |
55 | | - |
56 | | - return Boolean.parseBoolean(metaValue); |
| 20 | + return this.luckPermsHook |
| 21 | + .flatMap(hook -> hook.getMentionSoundEnabled(uuid)) |
| 22 | + .orElse(this.config.enabled); |
57 | 23 | } |
58 | 24 |
|
59 | 25 | public void setMentionSoundEnabled(UUID uuid, boolean enabled) { |
60 | | - if (this.luckPerms == null) { |
| 26 | + if (this.luckPermsHook.isEmpty()) { |
61 | 27 | this.logger.warning("Cannot set mention sound preference - LuckPerms is not available!"); |
62 | 28 | return; |
63 | 29 | } |
64 | 30 |
|
65 | | - this.luckPerms.getUserManager().modifyUser(uuid, user -> { |
66 | | - user.data().clear(node -> |
67 | | - node instanceof MetaNode metaNode && metaNode.getMetaKey().equals(MENTION_SOUND_META_KEY) |
68 | | - ); |
69 | | - |
70 | | - MetaNode metaNode = MetaNode.builder(MENTION_SOUND_META_KEY, Boolean.toString(enabled)).build(); |
71 | | - user.data().add(metaNode); |
72 | | - }); |
| 31 | + this.luckPermsHook.get().setMentionSoundEnabled(uuid, enabled); |
73 | 32 | } |
74 | 33 |
|
75 | 34 | public boolean toggleMentionSound(UUID uuid) { |
|
0 commit comments